sonarr-repo-only/UI/Shared/Toolbar/Button/ButtonView.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2013-06-22 06:24:24 +00:00
'use strict';
define(
[
'app',
'marionette',
'Shared/Actioneer',
'Shared/Messenger'
], function (App, Marionette, Actioneer, 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;
Actioneer.ExecuteCommand({
command : command,
button : this.$el,
element : this.ui.icon,
errorMessage : this.model.get('errorMessage'),
successMessage: this.model.get('successMessage'),
always : this._commandAlways,
context : this
});
}
},
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);
}
},
_commandAlways: function () {
if (!this.isClosed) {
this.idle = true;
}
}
});
2013-05-11 20:51:32 +00:00
});