@qooxdoo/framework
Version:
The JS Framework for Coders
178 lines (153 loc) • 4.79 kB
JavaScript
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2014 1&1 Internet AG, Germany, http://www.1und1.de
License:
MIT: https://opensource.org/licenses/MIT
See the LICENSE file in the project's top-level directory for details.
Authors:
* Martin Wittemann (martinwittemann)
* Mustafa Sak (msak)
************************************************************************ */
/**
* Commands can be used to globally define keyboard shortcuts. They could
* also be used to assign an execution of a command sequence to multiple
* widgets. It is possible to use the same Command in a MenuButton and
* ToolBarButton for example.
*
* NOTE: Instances of this class must be disposed of after use
*
*/
qx.Class.define("qx.ui.command.Command", {
extend: qx.core.Object,
/**
* @param shortcut {String} Shortcuts can be composed of optional modifier
* keys Control, Alt, Shift, Meta and a non modifier key.
* If no non modifier key is specified, the second parameter is evaluated.
* The key must be separated by a <code>+</code> or <code>-</code> character.
* Examples: Alt+F1, Control+C, Control+Alt+Delete
*/
construct(shortcut) {
super();
this._shortcut = new qx.bom.Shortcut(shortcut);
this._shortcut.addListener("execute", this.execute, this);
if (shortcut !== undefined) {
this.setShortcut(shortcut);
}
},
events: {
/**
* Fired when the command is executed. Sets the "data" property of the
* event to the object that issued the command.
*/
execute: "qx.event.type.Data"
},
properties: {
/** Whether the command should be activated. If 'false' execute event
* wouldn't fire. This property will be used by command groups when
* activating/deactivating all commands of the group.*/
active: {
init: true,
check: "Boolean",
event: "changeActive",
apply: "_applyActive"
},
/** Whether the command should be respected/enabled. If 'false' execute event
* wouldn't fire. If value of property {@link qx.ui.command.Command#active}
* is 'false', enabled value can be set but has no effect until
* {@link qx.ui.command.Command#active} will be set to 'true'.*/
enabled: {
init: true,
check: "Boolean",
event: "changeEnabled",
apply: "_applyEnabled"
},
/** The command shortcut as a string */
shortcut: {
check: "String",
apply: "_applyShortcut",
nullable: true
},
/** The label, which will be set in all connected widgets (if available) */
label: {
check: "String",
nullable: true,
event: "changeLabel"
},
/** The icon, which will be set in all connected widgets (if available) */
icon: {
check: "String",
nullable: true,
event: "changeIcon"
},
/**
* The tooltip text, which will be set in all connected
* widgets (if available)
*/
toolTipText: {
check: "String",
nullable: true,
event: "changeToolTipText"
},
/** The value of the connected widgets */
value: {
nullable: true,
event: "changeValue"
},
/** The menu, which will be set in all connected widgets (if available) */
menu: {
check: "qx.ui.menu.Menu",
nullable: true,
event: "changeMenu"
}
},
members: {
_shortcut: null,
// property apply
_applyActive(value) {
if (value === false) {
this._shortcut.setEnabled(false);
} else {
// synchronize value with current "enabled" value of this command
this._shortcut.setEnabled(this.getEnabled());
}
},
// property apply
_applyEnabled(value) {
if (this.getActive()) {
this._shortcut.setEnabled(value);
}
},
// property apply
_applyShortcut(value) {
this._shortcut.setShortcut(value);
},
/**
* Fire the "execute" event on this command. If property
* <code>active</code> and <code>enabled</code> set to
* <code>true</code>.
* @param target {Object?} Object which issued the execute event
*/
execute(target) {
if (this.getActive() && this.getEnabled()) {
this.fireDataEvent("execute", target);
}
},
/**
* Returns the used shortcut as string using the currently selected locale.
*
* @return {String} shortcut
*/
toString() {
if (this._shortcut) {
return this._shortcut.toString();
}
return super.toString();
}
},
destruct() {
this._shortcut.removeListener("execute", this.execute, this);
this._disposeObjects("_shortcut");
}
});