1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

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');
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);
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;

View File

@ -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);
};
module.exports = singleton();

View File

@ -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);
var CollectionView = Marionette.CollectionView.extend({
itemView : CommandMessengerItemView
});
module.exports = new CollectionView({
collection : commandCollection
});

View File

@ -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);
}
});

View File

@ -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';
}
});