Changeset View
Changeset View
Standalone View
Standalone View
webroot/rsrc/js/core/KeyboardShortcutManager.js
| Show All 10 Lines | |||||
| JX.install('KeyboardShortcutManager', { | JX.install('KeyboardShortcutManager', { | ||||
| construct : function() { | construct : function() { | ||||
| this._shortcuts = []; | this._shortcuts = []; | ||||
| JX.Stratcom.listen('keypress', null, JX.bind(this, this._onkeypress)); | JX.Stratcom.listen('keypress', null, JX.bind(this, this._onkeypress)); | ||||
| JX.Stratcom.listen('keydown', null, JX.bind(this, this._onkeydown)); | JX.Stratcom.listen('keydown', null, JX.bind(this, this._onkeydown)); | ||||
| JX.Stratcom.listen('keyup', null, JX.bind(this, this._onkeyup)); | JX.Stratcom.listen('keyup', null, JX.bind(this, this._onkeyup)); | ||||
| var onelement = JX.bind(this, this._onelement); | |||||
| JX.Stratcom.listen('click', 'has-key-command', onelement); | |||||
| }, | }, | ||||
| statics : { | statics : { | ||||
| _instance : null, | _instance : null, | ||||
| /** | /** | ||||
| * Some keys don't invoke keypress events in some browsers. We handle these | * Some keys don't invoke keypress events in some browsers. We handle these | ||||
| * on keydown instead of keypress. | * on keydown instead of keypress. | ||||
| ▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | _onkeyhit : function(e) { | ||||
| if (JX.DOM.isType(target, ignore)) { | if (JX.DOM.isType(target, ignore)) { | ||||
| // Never activate keyboard shortcuts if the user has some other control | // Never activate keyboard shortcuts if the user has some other control | ||||
| // focused. | // focused. | ||||
| return; | return; | ||||
| } | } | ||||
| var key = this._getKey(e); | var key = this._getKey(e); | ||||
| var handled = this._handleKey(key); | |||||
| if (handled) { | |||||
| e.kill(); | |||||
| } | |||||
| }, | |||||
| _handleKey: function(key) { | |||||
| var shortcuts = this._shortcuts; | var shortcuts = this._shortcuts; | ||||
| for (var ii = 0; ii < shortcuts.length; ii++) { | for (var ii = 0; ii < shortcuts.length; ii++) { | ||||
| var keys = shortcuts[ii].getKeys(); | var keys = shortcuts[ii].getKeys(); | ||||
| for (var jj = 0; jj < keys.length; jj++) { | for (var jj = 0; jj < keys.length; jj++) { | ||||
| if (keys[jj] == key) { | if (keys[jj] == key) { | ||||
| shortcuts[ii].getHandler()(this); | shortcuts[ii].getHandler()(this); | ||||
| e.kill(); // Consume the event | return true; | ||||
| return; | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| return false; | |||||
| }, | }, | ||||
| _onkeydown : function(e) { | _onkeydown : function(e) { | ||||
| this._handleTooltipKeyEvent(e, true); | this._handleTooltipKeyEvent(e, true); | ||||
| if (this._getKey(e) in JX.KeyboardShortcutManager._downkeys) { | if (this._getKey(e) in JX.KeyboardShortcutManager._downkeys) { | ||||
| this._onkeyhit(e); | this._onkeyhit(e); | ||||
| } | } | ||||
| }, | }, | ||||
| _onkeyup : function(e) { | _onkeyup : function(e) { | ||||
| this._handleTooltipKeyEvent(e, false); | this._handleTooltipKeyEvent(e, false); | ||||
| }, | }, | ||||
| _getKey : function(e) { | _getKey : function(e) { | ||||
| return e.getSpecialKey() || String.fromCharCode(e.getRawEvent().charCode); | return e.getSpecialKey() || String.fromCharCode(e.getRawEvent().charCode); | ||||
| }, | }, | ||||
| _onelement: function(e) { | |||||
| var data = e.getNodeData('has-key-command'); | |||||
| this._handleKey(data.keyCommand); | |||||
| e.kill(); | |||||
| }, | |||||
| _handleTooltipKeyEvent : function(e, is_keydown) { | _handleTooltipKeyEvent : function(e, is_keydown) { | ||||
| if (e.getRawEvent().keyCode != 18) { | if (e.getRawEvent().keyCode != 18) { | ||||
| // If this isn't the alt/option key, don't do anything. | // If this isn't the alt/option key, don't do anything. | ||||
| return; | return; | ||||
| } | } | ||||
| // Fire all the shortcut handlers. | // Fire all the shortcut handlers. | ||||
| var shortcuts = this._shortcuts; | var shortcuts = this._shortcuts; | ||||
| for (var ii = 0; ii < shortcuts.length; ii++) { | for (var ii = 0; ii < shortcuts.length; ii++) { | ||||
| var handler = shortcuts[ii].getTooltipHandler(); | var handler = shortcuts[ii].getTooltipHandler(); | ||||
| handler && handler(this, is_keydown); | handler && handler(this, is_keydown); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| }); | }); | ||||