From c8a48d5df360aa42920904b9b19019eec299953f Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 11 May 2013 16:39:32 -0700 Subject: [PATCH] added command support to toolbar. --- UI/Commands/CommandController.js | 11 +++++ UI/Series/Index/SeriesIndexLayout.js | 23 ++++++--- UI/Shared/Messenger.js | 27 +++++++++++ UI/Shared/Toolbar/Button/ButtonView.js | 44 +++++++++++++++-- UI/Shared/Toolbar/ButtonTemplate.html | 2 +- UI/app.js | 65 ++++++++++++++++---------- 6 files changed, 136 insertions(+), 36 deletions(-) create mode 100644 UI/Commands/CommandController.js create mode 100644 UI/Shared/Messenger.js diff --git a/UI/Commands/CommandController.js b/UI/Commands/CommandController.js new file mode 100644 index 000000000..0bd4763d3 --- /dev/null +++ b/UI/Commands/CommandController.js @@ -0,0 +1,11 @@ +"use strict"; +define(['app'], function () { + + NzbDrone.Commands.Execute = function (name) { + return $.ajax({ + type: 'POST', + url : NzbDrone.Constants.ApiRoot + '/command', + data: JSON.stringify({command: name}) + }); + }; +}); \ No newline at end of file diff --git a/UI/Series/Index/SeriesIndexLayout.js b/UI/Series/Index/SeriesIndexLayout.js index 4519e93e1..7f0c19700 100644 --- a/UI/Series/Index/SeriesIndexLayout.js +++ b/UI/Series/Index/SeriesIndexLayout.js @@ -147,14 +147,25 @@ define([ route: 'series/add' }, { - title : 'RSS Sync', - icon : 'icon-rss', - command: 'rsssync' + title : 'RSS Sync', + icon : 'icon-rss', + command : 'rsssync', + successMessage: 'RSS Sync Completed', + errorMessage : 'RSS Sync Failed!' }, { - title : 'Update Library', - icon : 'icon-refresh', - command: 'updatelibrary' + title : 'Update Library', + icon : 'icon-refresh', + command : 'updatelibrary', + successMessage: 'Library was updated!', + errorMessage : 'Library update failed!' + }, + { + title : 'Test Action', + icon : 'icon-asterisk', + command : 'test', + successMessage: 'Test Completed', + errorMessage : 'Test Failed!' } ] }; diff --git a/UI/Shared/Messenger.js b/UI/Shared/Messenger.js new file mode 100644 index 000000000..d53399f1e --- /dev/null +++ b/UI/Shared/Messenger.js @@ -0,0 +1,27 @@ +"use strict"; +define(['app'], function () { + NzbDrone.Shared.Messenger = { + show: function (options) { + + if (!options.type) { + options.type = 'info'; + } + + if (!options.hideAfter) { + switch (options.type) { + case 'info': + options.hideAfter = 5; + break; + case 'error': + options.hideAfter = 0; + } + } + + return window.Messenger().post({ + message : options.message, + type : options.type, + showCloseButton: true, + hideAfter : options.hideAfter + }); + }}; +}); diff --git a/UI/Shared/Toolbar/Button/ButtonView.js b/UI/Shared/Toolbar/Button/ButtonView.js index 0683d4709..a3fc6095a 100644 --- a/UI/Shared/Toolbar/Button/ButtonView.js +++ b/UI/Shared/Toolbar/Button/ButtonView.js @@ -1,5 +1,5 @@ "use strict"; -define(['app', 'Config'], function () { +define(['app', 'Config', 'Commands/CommandController', 'Shared/Messenger'], function () { NzbDrone.Shared.Toolbar.ButtonView = Backbone.Marionette.ItemView.extend({ template : 'Shared/Toolbar/ButtonTemplate', @@ -9,9 +9,14 @@ define(['app', 'Config'], function () { 'click': 'onClick' }, + ui: { + icon: '.x-icon' + }, + initialize: function () { this.storageKey = this.model.get('menuKey') + ':' + this.model.get('key'); + this.idle = true; }, onRender: function () { @@ -22,16 +27,45 @@ define(['app', 'Config'], function () { }, onClick: function () { - this.invokeRoute(); - this.invokeCallback(); - this.invokeCommand(); + if (this.idle) { + this.invokeCallback(); + this.invokeRoute(); + this.invokeCommand(); + } }, invokeCommand: function () { var command = this.model.get('command'); if (command) { - window.alert(command); + this.idle = false; + this.$el.addClass('disabled'); + this.ui.icon.addClass('icon-spinner icon-spin'); + + var self = this; + var commandPromise = NzbDrone.Commands.Execute(command); + commandPromise.done(function () { + if (self.model.get('successMessage')) { + NzbDrone.Shared.Messenger.show({ + message: self.model.get('successMessage') + }); + } + }); + + commandPromise.fail(function () { + if (self.model.get('errorMessage')) { + NzbDrone.Shared.Messenger.show({ + message: self.model.get('errorMessage'), + type : 'error' + }); + } + }); + + commandPromise.always(function () { + self.$el.removeClass('disabled'); + self.ui.icon.removeClass('icon-spinner icon-spin'); + self.idle = true; + }); } }, diff --git a/UI/Shared/Toolbar/ButtonTemplate.html b/UI/Shared/Toolbar/ButtonTemplate.html index d49aaa9d4..428c15470 100644 --- a/UI/Shared/Toolbar/ButtonTemplate.html +++ b/UI/Shared/Toolbar/ButtonTemplate.html @@ -1 +1 @@ - {{title}} + {{title}} diff --git a/UI/app.js b/UI/app.js index 66ae7a77c..20d55e1bc 100644 --- a/UI/app.js +++ b/UI/app.js @@ -30,32 +30,49 @@ define('app', function () { window.NzbDrone = new Backbone.Marionette.Application(); window.NzbDrone.Config = {}; - window.NzbDrone.Series = {}; - window.NzbDrone.Series.Index = {}; - window.NzbDrone.Series.Index.Table = {}; - window.NzbDrone.Series.Index.List = {}; - window.NzbDrone.Series.Index.Posters = {}; - window.NzbDrone.Series.Edit = {}; - window.NzbDrone.Series.Delete = {}; - window.NzbDrone.Series.Details = {}; - window.NzbDrone.AddSeries = {}; - window.NzbDrone.AddSeries.New = {}; - window.NzbDrone.AddSeries.Existing = {}; - window.NzbDrone.AddSeries.RootFolders = {}; + + window.NzbDrone.Series = { + Index : { + Table : {}, + List : {}, + Posters: {} + + }, + Edit : {}, + Delete : {}, + Details: {} + }; + + window.NzbDrone.AddSeries = { + New : {}, + Existing : {}, + RootFolders: {} + }; + + window.NzbDrone.Quality = {}; - window.NzbDrone.Shared = {}; - window.NzbDrone.Shared.Toolbar = {}; + + window.NzbDrone.Commands = {}; + + window.NzbDrone.Shared = { + Toolbar : {}, + Messenger: {} + }; window.NzbDrone.Calendar = {}; - window.NzbDrone.Settings = {}; - window.NzbDrone.Settings.Naming = {}; - window.NzbDrone.Settings.Quality = {}; - window.NzbDrone.Settings.Quality.Size = {}; - window.NzbDrone.Settings.Quality.Profile = {}; - window.NzbDrone.Settings.Indexers = {}; - window.NzbDrone.Settings.DownloadClient = {}; - window.NzbDrone.Settings.Notifications = {}; - window.NzbDrone.Settings.System = {}; - window.NzbDrone.Settings.Misc = {}; + + window.NzbDrone.Settings = { + Naming : {}, + Quality : { + Size : {}, + Profile: {} + }, + Indexers : {}, + DownloadClient: {}, + Notifications : {}, + System : {}, + Misc : {} + }; + window.NzbDrone.Missing = {}; window.NzbDrone.History = {};