panels: Restructure to actually make sense.

The original implementation of panels.js was just for notifications,
and ended up running a bunch of notifications-specific code, including
registration click handlers and some localstorage-related
notifications logic, every time a panel was supposed to be opened.

This refactoring makes the panels library make sense -- we now
initialize all click handlers in the initialize() method, and do the
notifications check in a single, coherent place scoped to notifications.
This commit is contained in:
Tim Abbott
2020-03-26 12:49:28 -07:00
parent 162396cfe3
commit 900aea88a4

View File

@@ -17,24 +17,16 @@ const get_step = function ($process) {
return $process.find("[data-step]").filter(":visible").data("step"); return $process.find("[data-step]").filter(":visible").data("step");
}; };
exports.initialize = function () { function should_show_notifications(ls) {
// if email has not been set up and the user is the admin, display a warning // if the user said to never show banner on this computer again, it will
// to tell them to set up an email server. // be stored as `true` so we want to negate that.
if (page_params.insecure_desktop_app) { if (localstorage.supported()) {
exports.open($("[data-process='insecure-desktop-app']")); if (ls.get("dontAskForNotifications") === true) {
} else if (page_params.warn_no_email === true && page_params.is_admin) { return false;
exports.open($("[data-process='email-server']")); }
} else {
exports.open($("[data-process='notifications']"));
} }
};
exports.open = function ($process) { return (
const ls = localstorage();
$("[data-process]").hide();
let should_show_notifications =
// notifications *basically* don't work on any mobile platforms, so don't // notifications *basically* don't work on any mobile platforms, so don't
// event show the banners. This prevents trying to access things that // event show the banners. This prevents trying to access things that
// don't exist like `Notification.permission`. // don't exist like `Notification.permission`.
@@ -43,26 +35,22 @@ exports.open = function ($process) {
!notifications.granted_desktop_notifications_permission() && !notifications.granted_desktop_notifications_permission() &&
// if permission is allowed to be requested (e.g. not in "denied" state). // if permission is allowed to be requested (e.g. not in "denied" state).
notifications.permission_state() !== "denied" notifications.permission_state() !== "denied"
; );
}
if (localstorage.supported()) { exports.initialize = function () {
// if the user said to never show banner on this computer again, it will const ls = localstorage();
// be stored as `true` so we want to negate that. if (page_params.insecure_desktop_app) {
should_show_notifications = should_show_notifications && !ls.get("dontAskForNotifications"); exports.open($("[data-process='insecure-desktop-app']"));
} } else if (page_params.warn_no_email === true && page_params.is_admin) {
// if email has not been set up and the user is the admin,
if (should_show_notifications) { // display a warning to tell them to set up an email server.
$process.show(); exports.open($("[data-process='email-server']"));
resize_app(); } else if (should_show_notifications(ls)) {
} exports.open($("[data-process='notifications']"));
// if it is not the notifications prompt, show the error if it has been
// initialized here.
if ($process.is(":not([data-process='notifications'])")) {
$process.show();
resize_app();
} }
// Configure click handlers.
$(".request-desktop-notifications").on("click", function (e) { $(".request-desktop-notifications").on("click", function (e) {
e.preventDefault(); e.preventDefault();
$(this).closest(".alert").hide(); $(this).closest(".alert").hide();
@@ -78,6 +66,7 @@ exports.open = function ($process) {
$("#panels").on("click", ".alert .close, .alert .exit", function (e) { $("#panels").on("click", ".alert .close, .alert .exit", function (e) {
e.stopPropagation(); e.stopPropagation();
const $process = $(e.target).closest("[data-process]");
if (get_step($process) === 1 && $process.data("process") === "notifications") { if (get_step($process) === 1 && $process.data("process") === "notifications") {
show_step($process, 2); show_step($process, 2);
} else { } else {
@@ -87,4 +76,10 @@ exports.open = function ($process) {
}); });
}; };
exports.open = function ($process) {
$("[data-process]").hide();
$process.show();
resize_app();
};
window.panels = exports; window.panels = exports;