2013-05-11 22:51:32 +02:00
|
|
|
"use strict";
|
2013-05-12 01:39:32 +02:00
|
|
|
define(['app', 'Config', 'Commands/CommandController', 'Shared/Messenger'], function () {
|
2013-05-11 22:51:32 +02:00
|
|
|
|
|
|
|
NzbDrone.Shared.Toolbar.ButtonView = Backbone.Marionette.ItemView.extend({
|
|
|
|
template : 'Shared/Toolbar/ButtonTemplate',
|
|
|
|
className: 'btn',
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click': 'onClick'
|
|
|
|
},
|
|
|
|
|
2013-05-12 01:39:32 +02:00
|
|
|
ui: {
|
|
|
|
icon: '.x-icon'
|
|
|
|
},
|
|
|
|
|
2013-05-11 22:51:32 +02:00
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.storageKey = this.model.get('menuKey') + ':' + this.model.get('key');
|
2013-05-12 01:39:32 +02:00
|
|
|
this.idle = true;
|
2013-05-11 22:51:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onRender: function () {
|
|
|
|
if (this.model.get('active')) {
|
|
|
|
this.$el.addClass('active');
|
|
|
|
this.invokeCallback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onClick: function () {
|
2013-05-12 01:39:32 +02:00
|
|
|
if (this.idle) {
|
|
|
|
this.invokeCallback();
|
|
|
|
this.invokeRoute();
|
|
|
|
this.invokeCommand();
|
|
|
|
}
|
2013-05-11 22:51:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
invokeCommand: function () {
|
|
|
|
var command = this.model.get('command');
|
|
|
|
if (command) {
|
2013-05-12 01:39:32 +02:00
|
|
|
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')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-05-29 07:03:43 +02:00
|
|
|
commandPromise.fail(function (options) {
|
|
|
|
if (options.readyState === 0 || options.status === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-12 01:39:32 +02:00
|
|
|
if (self.model.get('errorMessage')) {
|
|
|
|
NzbDrone.Shared.Messenger.show({
|
|
|
|
message: self.model.get('errorMessage'),
|
|
|
|
type : 'error'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
commandPromise.always(function () {
|
2013-05-29 07:03:43 +02:00
|
|
|
if (!self.isClosed) {
|
|
|
|
self.$el.removeClass('disabled');
|
|
|
|
self.ui.icon.removeClass('icon-spinner icon-spin');
|
|
|
|
self.idle = true;
|
|
|
|
}
|
2013-05-12 01:39:32 +02:00
|
|
|
});
|
2013-05-11 22:51:32 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
invokeRoute: function () {
|
|
|
|
var route = this.model.get('route');
|
|
|
|
if (route) {
|
|
|
|
NzbDrone.Router.navigate(route, {trigger: true});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
invokeCallback: function () {
|
|
|
|
|
|
|
|
if (!this.model.ownerContext) {
|
|
|
|
throw 'ownerContext must be set.';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var callback = this.model.get('callback');
|
|
|
|
if (callback) {
|
|
|
|
callback.call(this.model.ownerContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|