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

88 lines
2.3 KiB
JavaScript
Raw Normal View History

2013-06-22 06:24:24 +00:00
'use strict';
define(
[
'app',
'marionette',
2013-09-12 00:42:15 +00:00
'Commands/CommandController'
], function (App, Marionette, CommandController) {
return Marionette.ItemView.extend({
template : 'Shared/Toolbar/ButtonTemplate',
className: 'btn',
events: {
'click': 'onClick'
},
initialize: function () {
this.storageKey = this.model.get('menuKey') + ':' + this.model.get('key');
},
onRender: function () {
if (this.model.get('active')) {
this.$el.addClass('active');
this.invokeCallback();
}
2013-06-28 23:19:00 +00:00
2013-09-11 06:33:47 +00:00
if (!this.model.get('title')) {
2013-06-28 23:19:00 +00:00
this.$el.addClass('btn-icon-only');
}
2013-09-11 06:33:47 +00:00
var command = this.model.get('command');
if (command) {
2013-09-12 00:42:15 +00:00
CommandController.bindToCommand({
2013-09-11 06:33:47 +00:00
command: {name: command},
element: this.$el
});
}
},
onClick: function () {
2013-09-11 06:33:47 +00:00
if (this.$el.hasClass('disabled')) {
return;
}
2013-09-11 06:33:47 +00:00
this.invokeCallback();
this.invokeRoute();
this.invokeCommand();
},
invokeCommand: function () {
var command = this.model.get('command');
if (command) {
2013-09-11 06:33:47 +00:00
CommandController.Execute(command);
}
},
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
});