Browse Source

Add the possibility to add Buttons with a command directly assigned as an object or function

pull/36/head
Artur Arseniev 10 years ago
parent
commit
c592c12b20
  1. 18
      src/panels/main.js
  2. 14
      src/panels/view/ButtonView.js

18
src/panels/main.js

@ -143,6 +143,22 @@ define(function(require) {
* attributes: { title: 'Some title'}, * attributes: { title: 'Some title'},
* active: false, * active: false,
* }); * });
* // It's also possible to pass the command as an object
* // with .run and .stop methods
* ...
* command: {
* run: function(editor) {
* ...
* },
* stop: function(editor) {
* ...
* }
* },
* // Or simply like a function which will be evaluated as a single .run command
* ...
* command: function(editor) {
* ...
* }
*/ */
addButton: function(panelId, button){ addButton: function(panelId, button){
var pn = this.getPanel(panelId); var pn = this.getPanel(panelId);
@ -191,4 +207,4 @@ define(function(require) {
}; };
}; };
}); });

14
src/panels/view/ButtonView.js

@ -209,8 +209,14 @@ function(Backbone, require) {
var command = null; var command = null;
var editor = this.em && this.em.get ? this.em.get('Editor') : null; var editor = this.em && this.em.get ? this.em.get('Editor') : null;
var commandName = this.model.get('command'); var commandName = this.model.get('command');
if(this.commands)
if (this.commands && typeof commandName === 'string') {
command = this.commands.get(commandName); command = this.commands.get(commandName);
} else if (commandName !== null && typeof commandName === 'object') {
command = commandName;
} else if (typeof commandName === 'function') {
command = {run: commandName};
}
if(this.model.get('active')){ if(this.model.get('active')){
@ -220,7 +226,7 @@ function(Backbone, require) {
if(this.parentM) if(this.parentM)
this.parentM.set('active', true, { silent: true }).trigger('checkActive'); this.parentM.set('active', true, { silent: true }).trigger('checkActive');
if(command){ if(command && command.run){
command.run(editor, this.model, this.model.get('options')); command.run(editor, this.model, this.model.get('options'));
editor.trigger('run:' + commandName); editor.trigger('run:' + commandName);
} }
@ -232,7 +238,7 @@ function(Backbone, require) {
if(this.parentM) if(this.parentM)
this.parentM.set('active', false, { silent: true }).trigger('checkActive'); this.parentM.set('active', false, { silent: true }).trigger('checkActive');
if(command){ if(command && command.stop){
command.stop(editor, this.model, this.model.get('options')); command.stop(editor, this.model, this.model.get('options'));
editor.trigger('stop:' + commandName); editor.trigger('stop:' + commandName);
} }
@ -315,4 +321,4 @@ function(Backbone, require) {
}, },
}); });
}); });

Loading…
Cancel
Save