Sonarr/UI/Shared/Toolbar/Button/ButtonView.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-06-22 06:24:24 +00:00
'use strict';
define(
[
'app',
'marionette',
'Commands/CommandController',
'Shared/Messenger'
], function (App, Marionette, CommandController, Messenger) {
return Marionette.ItemView.extend({
template : 'Shared/Toolbar/ButtonTemplate',
className: 'btn',
events: {
'click': 'onClick'
},
ui: {
icon: '.x-icon'
},
initialize: function () {
this.storageKey = this.model.get('menuKey') + ':' + this.model.get('key');
this.idle = true;
},
onRender: function () {
if (this.model.get('active')) {
this.$el.addClass('active');
this.invokeCallback();
}
2013-06-28 23:19:00 +00:00
if(!this.model.get('title')){
this.$el.addClass('btn-icon-only');
}
},
onClick: function () {
if (this.idle) {
this.invokeCallback();
this.invokeRoute();
this.invokeCommand();
}
},
invokeCommand: function () {
var command = this.model.get('command');
if (command) {
this.idle = false;
this.$el.addClass('disabled');
this.ui.icon.addClass('icon-spinner icon-spin');
var self = this;
var commandPromise = CommandController.Execute(command);
commandPromise.done(function () {
if (self.model.get('successMessage')) {
Messenger.show({
message: self.model.get('successMessage')
});
}
});
commandPromise.fail(function (options) {
if (options.readyState === 0 || options.status === 0) {
return;
}
if (self.model.get('errorMessage')) {
Messenger.show({
message: self.model.get('errorMessage'),
type : 'error'
});
}
});
commandPromise.always(function () {
if (!self.isClosed) {
self.$el.removeClass('disabled');
self.ui.icon.removeClass('icon-spinner icon-spin');
self.idle = true;
}
});
}
},
invokeRoute: function () {
var route = this.model.get('route');
if (route) {
require(
[
'Router'
], function () {
App.Router.navigate(route, {trigger: true});
2013-05-11 23:39:32 +00:00
});
}
},
2013-05-11 20:51:32 +00:00
invokeCallback: function () {
2013-05-11 20:51:32 +00:00
if (!this.model.ownerContext) {
throw 'ownerContext must be set.';
}
2013-05-11 20:51:32 +00:00
var callback = this.model.get('callback');
if (callback) {
callback.call(this.model.ownerContext);
}
2013-05-11 20:51:32 +00:00
}
});
2013-05-11 20:51:32 +00:00
});