123
* Declarative keybindings on the window object.
125
* Prefix supported are:
130
* Followed by a lowercase letter. For example
132
* A-s is the 'Alt + s' keybinding.
134
* This maps to an object which has the following behavior.
136
* target: {String} CSS selector of one element
137
* focus: {Boolean} Focus the element.
138
* toggle: {Boolean} Toggle element visibility.
139
* callback: {Function} Taking (event, target).
140
* help: {String} Help text to display in popup.
148
target: '#charm-search-field',
150
help: 'Select the charm Search'
153
target: '#shortcut-help',
155
callback: function(evt, target) {
156
// This could be its own view.
157
if (target && !target.getHTML().length) {
159
Y.each(this.keybindings, function(v, k) {
161
bindings.push({key: k, help: v.help});
165
views.Templates.shortcuts({bindings: bindings}));
168
help: 'Display this help'
171
callback: function(evt) {
172
this.fire('navigateTo', {url: '/:gui:/'});
174
help: 'Navigate to the Environment overview.'
177
callback: function() {
178
// Explicitly hide anything we might care about.
179
Y.one('#shortcut-help').hide();
181
help: 'Cancel current action'
123
186
* Data driven behaviors
125
188
* Placeholder for real behaviors associated with DOM Node data-*
154
217
* @method activateHotkeys
156
219
activateHotkeys: function() {
157
Y.one(window).on('keydown', function(ev) {
160
data = { preventDefault: false };
163
} else if (ev.ctrlKey) {
165
} else if (ev.shiftKey) {
168
if (key.length === 0 &&
169
// If we have no modifier, check if this is a function or the ESC
170
// key. If it is not one of these keys, do nothing.
171
!(ev.keyCode >= 112 && ev.keyCode <= 123 || ev.keyCode === 27)) {
172
return; //nothing to do
174
keyStr = keyCodeToString(ev.keyCode);
179
Y.fire('window-' + key.join('-') + '-pressed', data);
180
if (data.preventDefault) {
222
enter: 13, esc: 27, backspace: 8,
223
tab: 9, pageup: 33, pagedown: 34};
225
Y.each(key_map, function(v, k) {
185
Y.detachAll('window-alt-E-pressed');
186
Y.on('window-alt-E-pressed', function(data) {
187
this.fire('navigateTo', {url: this.nsRouter.url({gui: '/'})});
188
data.preventDefault = true;
191
Y.detachAll('window-alt-S-pressed');
192
Y.on('window-alt-S-pressed', function(data) {
193
var field = Y.one('#charm-search-field');
197
data.preventDefault = true;
201
* Transform a numeric keyCode value to its string version. Example:
202
* 16 returns 'shift'.
204
* @param {number} keyCode The numeric value of a key.
205
* @return {string} The string version of the given keyCode.
206
* @method keyCodeToString
208
function keyCodeToString(keyCode) {
209
if (keyCode === 16) {
212
if (keyCode === 17) {
215
if (keyCode === 18) {
218
if (keyCode === 27) {
221
// Numbers or Letters
222
if (keyCode >= 48 && keyCode <= 57 || //Numbers
223
keyCode >= 65 && keyCode <= 90) { //Letters
224
return String.fromCharCode(keyCode);
227
if (keyCode >= 112 && keyCode <= 123) {
228
return 'F' + (keyCode - 111);
228
Y.one(window).on('keydown', function(evt) {
231
if (evt.ctrlKey) { symbolic.push('C');}
232
if (evt.altKey) { symbolic.push('A');}
233
if (evt.shiftKey) { symbolic.push('S');}
234
symbolic.push(code_map[evt.keyCode] ||
235
String.fromCharCode(evt.keyCode).toLowerCase());
236
var trigger = symbolic.join('-');
237
var spec = this.keybindings[trigger];
239
var target = Y.one(spec.target);
241
if (spec.toggle) { target.toggleView(); }
242
if (spec.focus) { target.focus(); }
244
if (spec.callback) { spec.callback.call(this, evt, target); }
245
// If we handled the event nothing else has to.
246
evt.stopPropagation();
247
evt.preventDefault();