From 01d37fc9ee99b9cb247685e2a00954ad41968ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 5 Mar 2018 17:25:15 +0100 Subject: [PATCH] PMGR: Reduce notification spam --- .../pluginmanager/static/js/pluginmanager.js | 421 +++++++----------- 1 file changed, 156 insertions(+), 265 deletions(-) diff --git a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js index 19842dcb6..b30ab5971 100644 --- a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js +++ b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js @@ -205,6 +205,16 @@ $(function() { self.hiddenNoticeNotifications = {}; self.noticeCount = ko.observable(0); + self.notification = undefined; + self.logContents = { + steps: [], + action: { + reload: false, + refresh: false, + reconnect: false + } + }; + self.noticeCountText = ko.pureComputed(function() { var count = self.noticeCount(); if (count == 0) { @@ -701,133 +711,138 @@ $(function() { return self.isCompatible(data) ? (self.installed(data) ? gettext("Reinstall") : gettext("Install")) : (data.disabled ? gettext("Disabled") : gettext("Incompatible")); }; - self._displayNotification = function(response, action, titleSuccess, textSuccess, textRestart, textReload, textReconnect, titleError, textError) { - var notification; + self._displayPluginManagementNotification = function(response, action, plugin) { + self.logContents.action.restart = self.logContents.action.restart || response.needs_restart; + self.logContents.action.refresh = self.logContents.action.refresh || response.needs_refresh; + self.logContents.action_reconnect = self.logContents.action.reconnect || response.needs_reconnect; + self.logContents.steps.push({action: action, plugin: plugin, result: response.result}); - var beforeClose = function(notification) { - self.notifications = _.without(self.notifications, notification); - }; + var title = gettext("Plugin management log"); + var text = "

"; + + var confirm = undefined; + var type = "success"; + if (self.logContents.action.restart) { + text += "

" + gettext("A restart is needed for the changes to take effect.") + "

"; + type = "warning"; + + if (self.restartCommandSpec) { + var restartClicked = false; + confirm = { + confirm: true, + buttons: [{ + text: gettext("Restart now"), + click: function (notice) { + if (restartClicked) return; + restartClicked = true; + showConfirmationDialog({ + message: gettext("This will restart your OctoPrint server.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage)."), + onproceed: function() { + OctoPrint.system.executeCommand("core", "restart") + .done(function() { + notice.remove(); + new PNotify({ + title: gettext("Restart in progress"), + text: gettext("The server is now being restarted in the background") + }) + }) + .fail(function() { + new PNotify({ + title: gettext("Something went wrong"), + text: gettext("Trying to restart the server produced an error, please check octoprint.log for details. You'll have to restart manually.") + }) + }); + }, + onclose: function() { + restartClicked = false; + } + }); + } + }] + } + } + } else if (self.logContents.action.refresh) { + text += "

" + gettext("A refresh is needed for the changes to take effect.") + "

"; + type = "warning"; + + var refreshClicked = false; + confirm = { + confirm: true, + buttons: [{ + text: gettext("Reload now"), + click: function () { + if (refreshClicked) return; + refreshClicked = true; + location.reload(true); + } + }] + } + } else if (self.logContents.action_reconnect) { + text += "

" + gettext("A reconnect to the printer is needed for the changes to take effect.") + "

"; + type = "warning"; + } + + var options = { + title: title, + text: text, + type: type + }; + + if (self.logNotification !== undefined) { + self.logNotification.remove(); + } + + if (confirm !== undefined) { + options.confirm = confirm; + options.hide = false; + self.logNotification = PNotify.singleButtonNotify(options); + } else { + self.logNotification = new PNotify(options); + } }; self._markWorking = function(title, line) { @@ -1068,14 +1083,26 @@ $(function() { }; self.onUserLoggedOut = function() { - self._closeAllNotifications(); + self._resetNotifications(); }; self.onEventConnectivityChanged = function(payload) { self.requestData({eval_notices: true}); }; + self._resetNotifications = function() { + self._closeAllNotifications(); + self.logContents.action.restart + = self.logContents.action.reload + = self.logContents.action.reconnect + = false; + self.logContents.steps = []; + }; + self._closeAllNotifications = function() { + if (self.logNotification) { + self.logNotification.remove(); + } if (self.notifications) { _.each(self.notifications, function(notification) { notification.remove(); @@ -1084,7 +1111,7 @@ $(function() { }; self.onServerDisconnect = function() { - self._closeAllNotifications(); + self._resetNotifications(); return true; }; @@ -1095,7 +1122,7 @@ $(function() { }; self.onDataUpdaterPluginMessage = function(plugin, data) { - if (plugin != "pluginmanager") { + if (plugin !== "pluginmanager") { return; } @@ -1109,157 +1136,21 @@ $(function() { var messageType = data.type; - if (messageType == "loglines" && self.working()) { + if (messageType === "loglines" && self.working()) { _.each(data.loglines, function(line) { self.loglines.push(self._preprocessLine(line)); }); self._scrollWorkingOutputToEnd(); - } else if (messageType == "result") { - var titleSuccess, textSuccess, textRestart, textReload, textReconnect, titleError, textError; + } else if (messageType === "result") { var action = data.action; - var name = "Unknown"; - if (action == "install") { - var unknown = false; - - if (data.hasOwnProperty("plugin")) { - if (data.plugin == "unknown") { - unknown = true; - } else { - name = data.plugin.name; - } - } - - if (unknown) { - titleSuccess = _.sprintf(gettext("Plugin installed")); - textSuccess = gettext("A plugin was installed successfully, however it was impossible to detect which one. Please Restart OctoPrint to make sure everything will be registered properly"); - textRestart = textSuccess; - textReload = textSuccess; - textReconnect = textSuccess; - } else if (data.plugin && data.plugin.blacklisted) { - if (data.was_reinstalled) { - titleSuccess = _.sprintf(gettext("Plugin \"%(name)s\" reinstalled"), {name: name}); - textSuccess = gettext("The plugin was reinstalled successfully, however it is blacklisted and therefore won't be loaded."); - } else { - titleSuccess = _.sprintf(gettext("Plugin \"%(name)s\" installed"), {name: name}); - textSuccess = gettext("The plugin was installed successfully, however it is blacklisted and therefore won't be loaded."); - } - textRestart = textSuccess; - textReload = textSuccess; - textReconnect = textSuccess; - } else if (data.was_reinstalled) { - titleSuccess = _.sprintf(gettext("Plugin \"%(name)s\" reinstalled"), {name: name}); - textSuccess = gettext("The plugin was reinstalled successfully"); - textRestart = gettext("The plugin was reinstalled successfully, however a restart of OctoPrint is needed for that to take effect."); - textReload = gettext("The plugin was reinstalled successfully, however a reload of the page is needed for that to take effect."); - textReconnect = gettext("The plugin was reinstalled successfully, however a reconnect to the printer is needed for that to take effect."); - } else { - titleSuccess = _.sprintf(gettext("Plugin \"%(name)s\" installed"), {name: name}); - textSuccess = gettext("The plugin was installed successfully"); - textRestart = gettext("The plugin was installed successfully, however a restart of OctoPrint is needed for that to take effect."); - textReload = gettext("The plugin was installed successfully, however a reload of the page is needed for that to take effect."); - textReconnect = gettext("The plugin was installed successfully, however a reconnect to the printer is needed for that to take effect."); - } - - titleError = gettext("Something went wrong"); - var source = "unknown"; - if (data.hasOwnProperty("source")) { - source = data.source; - } - var sourceType = "unknown"; - if (data.hasOwnProperty("source_type")) { - sourceType = data.source_type; - } - - if (data.hasOwnProperty("reason")) { - if (data.was_reinstalled) { - if (sourceType == "path") { - textError = _.sprintf(gettext("Reinstalling the plugin from file failed: %(reason)s"), {reason: data.reason}); - } else { - textError = _.sprintf(gettext("Reinstalling the plugin from \"%(source)s\" failed: %(reason)s"), {reason: data.reason, source: source}); - } - } else { - if (sourceType == "path") { - textError = _.sprintf(gettext("Installing the plugin from file failed: %(reason)s"), {reason: data.reason}); - } else { - textError = _.sprintf(gettext("Installing the plugin from \"%(source)s\" failed: %(reason)s"), {reason: data.reason, source: source}); - } - } - } else { - if (data.was_reinstalled) { - if (sourceType == "path") { - textError = gettext("Reinstalling the plugin from file failed, please see the log for details."); - } else { - textError = _.sprintf(gettext("Reinstalling the plugin from \"%(source)s\" failed, please see the log for details."), {source: source}); - } - } else { - if (sourceType == "path") { - textError = gettext("Installing the plugin from file failed, please see the log for details."); - } else { - textError = _.sprintf(gettext("Installing the plugin from \"%(source)s\" failed, please see the log for details."), {source: source}); - } - } - } - - } else if (action == "uninstall") { - if (data.hasOwnProperty("plugin")) { + if (data.hasOwnProperty("plugin")) { + if (data.plugin !== "unknown") { name = data.plugin.name; } - - titleSuccess = _.sprintf(gettext("Plugin \"%(name)s\" uninstalled"), {name: name}); - textSuccess = gettext("The plugin was uninstalled successfully"); - textRestart = gettext("The plugin was uninstalled successfully, however a restart of OctoPrint is needed for that to take effect."); - textReload = gettext("The plugin was uninstalled successfully, however a reload of the page is needed for that to take effect."); - textReconnect = gettext("The plugin was uninstalled successfully, however a reconnect to the printer is needed for that to take effect."); - - titleError = gettext("Something went wrong"); - if (data.hasOwnProperty("reason")) { - textError = _.sprintf(gettext("Uninstalling the plugin failed: %(reason)s"), {reason: data.reason}); - } else { - textError = gettext("Uninstalling the plugin failed, please see the log for details."); - } - - } else if (action == "enable") { - if (data.hasOwnProperty("plugin")) { - name = data.plugin.name; - } - - titleSuccess = _.sprintf(gettext("Plugin \"%(name)s\" enabled"), {name: name}); - textSuccess = gettext("The plugin was enabled successfully."); - textRestart = gettext("The plugin was enabled successfully, however a restart of OctoPrint is needed for that to take effect."); - textReload = gettext("The plugin was enabled successfully, however a reload of the page is needed for that to take effect."); - textReconnect = gettext("The plugin was enabled successfully, however a reconnect to the printer is needed for that to take effect."); - - titleError = gettext("Something went wrong"); - if (data.hasOwnProperty("reason")) { - textError = _.sprintf(gettext("Toggling the plugin failed: %(reason)s"), {reason: data.reason}); - } else { - textError = gettext("Toggling the plugin failed, please see the log for details."); - } - - } else if (action == "disable") { - if (data.hasOwnProperty("plugin")) { - name = data.plugin.name; - } - - titleSuccess = _.sprintf(gettext("Plugin \"%(name)s\" disabled"), {name: name}); - textSuccess = gettext("The plugin was disabled successfully."); - textRestart = gettext("The plugin was disabled successfully, however a restart of OctoPrint is needed for that to take effect."); - textReload = gettext("The plugin was disabled successfully, however a reload of the page is needed for that to take effect."); - textReconnect = gettext("The plugin was disabled successfully, however a reconnect to the printer is needed for that to take effect."); - - titleError = gettext("Something went wrong"); - if (data.hasOwnProperty("reason")) { - textError = _.sprintf(gettext("Toggling the plugin failed: %(reason)s"), {reason: data.reason}); - } else { - textError = gettext("Toggling the plugin failed, please see the log for details."); - } - - } else { - return; } - self._displayNotification(data, action, titleSuccess, textSuccess, textRestart, textReload, textReconnect, titleError, textError); + self._displayPluginManagementNotification(data, action, name); self.requestData(); } };