From 7b7f1995872b986afc75e8a41f25bc64235fc24c Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Fri, 13 Feb 2015 22:04:55 +0100 Subject: [PATCH] UI Cleanup - Updated Commands subtree. --- src/UI/Commands/CommandCollection.js | 30 ++-- src/UI/Commands/CommandController.js | 145 ++++++++++-------- .../CommandMessengerCollectionView.js | 11 +- src/UI/Commands/CommandMessengerItemView.js | 15 +- src/UI/Commands/CommandModel.js | 28 ++-- 5 files changed, 133 insertions(+), 96 deletions(-) diff --git a/src/UI/Commands/CommandCollection.js b/src/UI/Commands/CommandCollection.js index fcf622739..b8eaae543 100644 --- a/src/UI/Commands/CommandCollection.js +++ b/src/UI/Commands/CommandCollection.js @@ -2,17 +2,19 @@ var Backbone = require('backbone'); var CommandModel = require('./CommandModel'); require('../Mixins/backbone.signalr.mixin'); -module.exports = (function(){ - var CommandCollection = Backbone.Collection.extend({ - url : window.NzbDrone.ApiRoot + '/command', - model : CommandModel, - findCommand : function(command){ - return this.find(function(model){ - return model.isSameCommand(command); - }); - } - }); - var collection = new CommandCollection().bindSignalR(); - collection.fetch(); - return collection; -}).call(this); \ No newline at end of file +var CommandCollection = Backbone.Collection.extend({ + url : window.NzbDrone.ApiRoot + '/command', + model : CommandModel, + + findCommand : function(command) { + return this.find(function(model) { + return model.isSameCommand(command); + }); + } +}); + +var collection = new CommandCollection().bindSignalR(); + +collection.fetch(); + +module.exports = collection; \ No newline at end of file diff --git a/src/UI/Commands/CommandController.js b/src/UI/Commands/CommandController.js index 00970af05..f2b8060b3 100644 --- a/src/UI/Commands/CommandController.js +++ b/src/UI/Commands/CommandController.js @@ -7,69 +7,88 @@ var moment = require('moment'); var Messenger = require('../Shared/Messenger'); require('../jQuery/jquery.spin'); -module.exports = (function(){ - CommandMessengerCollectionView.render(); - var singleton = function(){ - return { - _lastCommand : {}, - Execute : function(name, properties){ - var attr = _.extend({name : name.toLocaleLowerCase()}, properties); - var commandModel = new CommandModel(attr); - if(this._lastCommand.command && this._lastCommand.command.isSameCommand(attr) && moment().add('seconds', -5).isBefore(this._lastCommand.time)) { - Messenger.show({ - message : 'Please wait at least 5 seconds before running this command again', - hideAfter : 5, - type : 'error' - }); - return this._lastCommand.promise; - } - var promise = commandModel.save().success(function(){ - CommandCollection.add(commandModel); +CommandMessengerCollectionView.render(); + +var singleton = function() { + + return { + + _lastCommand : {}, + + Execute : function(name, properties) { + + var attr = _.extend({ name : name.toLocaleLowerCase() }, properties); + var commandModel = new CommandModel(attr); + + if (this._lastCommand.command && this._lastCommand.command.isSameCommand(attr) && moment().add('seconds', -5).isBefore(this._lastCommand.time)) { + + Messenger.show({ + message : 'Please wait at least 5 seconds before running this command again', + hideAfter : 5, + type : 'error' }); - this._lastCommand = { - command : commandModel, - promise : promise, - time : moment() - }; - return promise; - }, - bindToCommand : function(options){ - var self = this; - var existingCommand = CommandCollection.findCommand(options.command); - if(existingCommand) { - this._bindToCommandModel.call(this, existingCommand, options); - } - CommandCollection.bind('add', function(model){ - if(model.isSameCommand(options.command)) { - self._bindToCommandModel.call(self, model, options); - } - }); - CommandCollection.bind('sync', function(){ - var command = CommandCollection.findCommand(options.command); - if(command) { - self._bindToCommandModel.call(self, command, options); - } - }); - }, - _bindToCommandModel : function bindToCommand (model, options){ - if(!model.isActive()) { - options.element.stopSpin(); - return; - } - model.bind('change:state', function(model){ - if(!model.isActive()) { - options.element.stopSpin(); - if(model.isComplete()) { - vent.trigger(vent.Events.CommandComplete, { - command : model, - model : options.model - }); - } - } - }); - options.element.startSpin(); + + return this._lastCommand.promise; } - }; + + var promise = commandModel.save().success(function() { + CommandCollection.add(commandModel); + }); + + this._lastCommand = { + command : commandModel, + promise : promise, + time : moment() + }; + + return promise; + }, + + bindToCommand : function(options) { + + var self = this; + var existingCommand = CommandCollection.findCommand(options.command); + + if (existingCommand) { + this._bindToCommandModel.call(this, existingCommand, options); + } + + CommandCollection.bind('add', function(model) { + if (model.isSameCommand(options.command)) { + self._bindToCommandModel.call(self, model, options); + } + }); + + CommandCollection.bind('sync', function() { + var command = CommandCollection.findCommand(options.command); + if (command) { + self._bindToCommandModel.call(self, command, options); + } + }); + }, + + _bindToCommandModel : function bindToCommand (model, options) { + + if (!model.isActive()) { + options.element.stopSpin(); + return; + } + + model.bind('change:state', function(model) { + if (!model.isActive()) { + options.element.stopSpin(); + + if (model.isComplete()) { + vent.trigger(vent.Events.CommandComplete, { + command : model, + model : options.model + }); + } + } + }); + + options.element.startSpin(); + } }; - return singleton(); -}).call(this); \ No newline at end of file +}; +module.exports = singleton(); diff --git a/src/UI/Commands/CommandMessengerCollectionView.js b/src/UI/Commands/CommandMessengerCollectionView.js index 4993da747..007760087 100644 --- a/src/UI/Commands/CommandMessengerCollectionView.js +++ b/src/UI/Commands/CommandMessengerCollectionView.js @@ -2,7 +2,10 @@ var Marionette = require('marionette'); var commandCollection = require('./CommandCollection'); var CommandMessengerItemView = require('./CommandMessengerItemView'); -module.exports = (function(){ - var CollectionView = Marionette.CollectionView.extend({itemView : CommandMessengerItemView}); - return new CollectionView({collection : commandCollection}); -}).call(this); \ No newline at end of file +var CollectionView = Marionette.CollectionView.extend({ + itemView : CommandMessengerItemView +}); + +module.exports = new CollectionView({ + collection : commandCollection +}); diff --git a/src/UI/Commands/CommandMessengerItemView.js b/src/UI/Commands/CommandMessengerItemView.js index f3f6effde..c7f419f31 100644 --- a/src/UI/Commands/CommandMessengerItemView.js +++ b/src/UI/Commands/CommandMessengerItemView.js @@ -2,13 +2,15 @@ var Marionette = require('marionette'); var Messenger = require('../Shared/Messenger'); module.exports = Marionette.ItemView.extend({ - initialize : function(){ + initialize : function() { this.listenTo(this.model, 'change', this.render); }, - render : function(){ - if(!this.model.get('message') || !this.model.get('sendUpdatesToClient')) { + + render : function() { + if (!this.model.get('message') || !this.model.get('sendUpdatesToClient')) { return; } + var message = { type : 'info', message : '[{0}] {1}'.format(this.model.get('name'), this.model.get('message')), @@ -26,15 +28,18 @@ module.exports = Marionette.ItemView.extend({ message.hideAfter = isManual ? 10 : 4; message.type = 'error'; break; - default: + default : message.hideAfter = 0; } - if(this.messenger) { + + if (this.messenger) { this.messenger.update(message); } + else { this.messenger = Messenger.show(message); } + console.log(message.message); } }); \ No newline at end of file diff --git a/src/UI/Commands/CommandModel.js b/src/UI/Commands/CommandModel.js index 4cc49b702..3f8c92b53 100644 --- a/src/UI/Commands/CommandModel.js +++ b/src/UI/Commands/CommandModel.js @@ -2,33 +2,41 @@ var _ = require('underscore'); var Backbone = require('backbone'); module.exports = Backbone.Model.extend({ - url : window.NzbDrone.ApiRoot + '/command', - parse : function(response){ + url : window.NzbDrone.ApiRoot + '/command', + + parse : function(response) { response.name = response.name.toLocaleLowerCase(); return response; }, - isSameCommand : function(command){ - if(command.name.toLocaleLowerCase() !== this.get('name').toLocaleLowerCase()) { + + isSameCommand : function(command) { + + if (command.name.toLocaleLowerCase() !== this.get('name').toLocaleLowerCase()) { return false; } + for (var key in command) { - if(key !== 'name') { - if(Array.isArray(command[key])) { - if(_.difference(command[key], this.get(key)).length > 0) { + if (key !== 'name') { + if (Array.isArray(command[key])) { + if (_.difference(command[key], this.get(key)).length > 0) { return false; } } - else if(command[key] !== this.get(key)) { + + else if (command[key] !== this.get(key)) { return false; } } } + return true; }, - isActive : function(){ + + isActive : function() { return this.get('state') !== 'completed' && this.get('state') !== 'failed'; }, - isComplete : function(){ + + isComplete : function() { return this.get('state') === 'completed'; } }); \ No newline at end of file