UI Cleanup - Updated Commands subtree.

This commit is contained in:
Taloth Saldono 2015-02-13 22:04:55 +01:00
parent 7b5c0a952b
commit 7b7f199587
5 changed files with 133 additions and 96 deletions

View File

@ -2,17 +2,19 @@ var Backbone = require('backbone');
var CommandModel = require('./CommandModel'); var CommandModel = require('./CommandModel');
require('../Mixins/backbone.signalr.mixin'); require('../Mixins/backbone.signalr.mixin');
module.exports = (function(){ var CommandCollection = Backbone.Collection.extend({
var CommandCollection = Backbone.Collection.extend({
url : window.NzbDrone.ApiRoot + '/command', url : window.NzbDrone.ApiRoot + '/command',
model : CommandModel, model : CommandModel,
findCommand : function(command){
return this.find(function(model){ findCommand : function(command) {
return this.find(function(model) {
return model.isSameCommand(command); return model.isSameCommand(command);
}); });
} }
}); });
var collection = new CommandCollection().bindSignalR();
collection.fetch(); var collection = new CommandCollection().bindSignalR();
return collection;
}).call(this); collection.fetch();
module.exports = collection;

View File

@ -7,59 +7,78 @@ var moment = require('moment');
var Messenger = require('../Shared/Messenger'); var Messenger = require('../Shared/Messenger');
require('../jQuery/jquery.spin'); require('../jQuery/jquery.spin');
module.exports = (function(){ CommandMessengerCollectionView.render();
CommandMessengerCollectionView.render();
var singleton = function(){ var singleton = function() {
return { return {
_lastCommand : {}, _lastCommand : {},
Execute : function(name, properties){
var attr = _.extend({name : name.toLocaleLowerCase()}, properties); Execute : function(name, properties) {
var attr = _.extend({ name : name.toLocaleLowerCase() }, properties);
var commandModel = new CommandModel(attr); var commandModel = new CommandModel(attr);
if(this._lastCommand.command && this._lastCommand.command.isSameCommand(attr) && moment().add('seconds', -5).isBefore(this._lastCommand.time)) {
if (this._lastCommand.command && this._lastCommand.command.isSameCommand(attr) && moment().add('seconds', -5).isBefore(this._lastCommand.time)) {
Messenger.show({ Messenger.show({
message : 'Please wait at least 5 seconds before running this command again', message : 'Please wait at least 5 seconds before running this command again',
hideAfter : 5, hideAfter : 5,
type : 'error' type : 'error'
}); });
return this._lastCommand.promise; return this._lastCommand.promise;
} }
var promise = commandModel.save().success(function(){
var promise = commandModel.save().success(function() {
CommandCollection.add(commandModel); CommandCollection.add(commandModel);
}); });
this._lastCommand = { this._lastCommand = {
command : commandModel, command : commandModel,
promise : promise, promise : promise,
time : moment() time : moment()
}; };
return promise; return promise;
}, },
bindToCommand : function(options){
bindToCommand : function(options) {
var self = this; var self = this;
var existingCommand = CommandCollection.findCommand(options.command); var existingCommand = CommandCollection.findCommand(options.command);
if(existingCommand) {
if (existingCommand) {
this._bindToCommandModel.call(this, existingCommand, options); this._bindToCommandModel.call(this, existingCommand, options);
} }
CommandCollection.bind('add', function(model){
if(model.isSameCommand(options.command)) { CommandCollection.bind('add', function(model) {
if (model.isSameCommand(options.command)) {
self._bindToCommandModel.call(self, model, options); self._bindToCommandModel.call(self, model, options);
} }
}); });
CommandCollection.bind('sync', function(){
CommandCollection.bind('sync', function() {
var command = CommandCollection.findCommand(options.command); var command = CommandCollection.findCommand(options.command);
if(command) { if (command) {
self._bindToCommandModel.call(self, command, options); self._bindToCommandModel.call(self, command, options);
} }
}); });
}, },
_bindToCommandModel : function bindToCommand (model, options){
if(!model.isActive()) { _bindToCommandModel : function bindToCommand (model, options) {
if (!model.isActive()) {
options.element.stopSpin(); options.element.stopSpin();
return; return;
} }
model.bind('change:state', function(model){
if(!model.isActive()) { model.bind('change:state', function(model) {
if (!model.isActive()) {
options.element.stopSpin(); options.element.stopSpin();
if(model.isComplete()) {
if (model.isComplete()) {
vent.trigger(vent.Events.CommandComplete, { vent.trigger(vent.Events.CommandComplete, {
command : model, command : model,
model : options.model model : options.model
@ -67,9 +86,9 @@ module.exports = (function(){
} }
} }
}); });
options.element.startSpin(); options.element.startSpin();
} }
}; };
}; };
return singleton(); module.exports = singleton();
}).call(this);

View File

@ -2,7 +2,10 @@ var Marionette = require('marionette');
var commandCollection = require('./CommandCollection'); var commandCollection = require('./CommandCollection');
var CommandMessengerItemView = require('./CommandMessengerItemView'); var CommandMessengerItemView = require('./CommandMessengerItemView');
module.exports = (function(){ var CollectionView = Marionette.CollectionView.extend({
var CollectionView = Marionette.CollectionView.extend({itemView : CommandMessengerItemView}); itemView : CommandMessengerItemView
return new CollectionView({collection : commandCollection}); });
}).call(this);
module.exports = new CollectionView({
collection : commandCollection
});

View File

@ -2,13 +2,15 @@ var Marionette = require('marionette');
var Messenger = require('../Shared/Messenger'); var Messenger = require('../Shared/Messenger');
module.exports = Marionette.ItemView.extend({ module.exports = Marionette.ItemView.extend({
initialize : function(){ initialize : function() {
this.listenTo(this.model, 'change', this.render); 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; return;
} }
var message = { var message = {
type : 'info', type : 'info',
message : '[{0}] {1}'.format(this.model.get('name'), this.model.get('message')), 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.hideAfter = isManual ? 10 : 4;
message.type = 'error'; message.type = 'error';
break; break;
default: default :
message.hideAfter = 0; message.hideAfter = 0;
} }
if(this.messenger) {
if (this.messenger) {
this.messenger.update(message); this.messenger.update(message);
} }
else { else {
this.messenger = Messenger.show(message); this.messenger = Messenger.show(message);
} }
console.log(message.message); console.log(message.message);
} }
}); });

View File

@ -3,32 +3,40 @@ var Backbone = require('backbone');
module.exports = Backbone.Model.extend({ module.exports = Backbone.Model.extend({
url : window.NzbDrone.ApiRoot + '/command', url : window.NzbDrone.ApiRoot + '/command',
parse : function(response){
parse : function(response) {
response.name = response.name.toLocaleLowerCase(); response.name = response.name.toLocaleLowerCase();
return response; 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; return false;
} }
for (var key in command) { for (var key in command) {
if(key !== 'name') { if (key !== 'name') {
if(Array.isArray(command[key])) { if (Array.isArray(command[key])) {
if(_.difference(command[key], this.get(key)).length > 0) { if (_.difference(command[key], this.get(key)).length > 0) {
return false; return false;
} }
} }
else if(command[key] !== this.get(key)) {
else if (command[key] !== this.get(key)) {
return false; return false;
} }
} }
} }
return true; return true;
}, },
isActive : function(){
isActive : function() {
return this.get('state') !== 'completed' && this.get('state') !== 'failed'; return this.get('state') !== 'completed' && this.get('state') !== 'failed';
}, },
isComplete : function(){
isComplete : function() {
return this.get('state') === 'completed'; return this.get('state') === 'completed';
} }
}); });