|
|
@@ -1,9 +1,7 @@
|
|
|
define([
|
|
|
- 'angular',
|
|
|
- 'underscore',
|
|
|
- 'jquery'
|
|
|
+ 'angular'
|
|
|
],
|
|
|
-function (angular, _, $) {
|
|
|
+function (angular) {
|
|
|
'use strict';
|
|
|
|
|
|
var module = angular.module('kibana.services');
|
|
|
@@ -21,7 +19,7 @@ function (angular, _, $) {
|
|
|
'keyCode': false
|
|
|
};
|
|
|
// Store all keyboard combination shortcuts
|
|
|
- keyboardManagerService.keyboardEvent = {}
|
|
|
+ keyboardManagerService.keyboardEvent = {};
|
|
|
// Add a new keyboard combination shortcut
|
|
|
keyboardManagerService.bind = function (label, callback, opt) {
|
|
|
var fct, elt, code, k;
|
|
|
@@ -29,7 +27,10 @@ function (angular, _, $) {
|
|
|
opt = angular.extend({}, defaultOpt, opt);
|
|
|
label = label.toLowerCase();
|
|
|
elt = opt.target;
|
|
|
- if(typeof opt.target == 'string') elt = document.getElementById(opt.target);
|
|
|
+
|
|
|
+ if(typeof opt.target === 'string') {
|
|
|
+ elt = document.getElementById(opt.target);
|
|
|
+ }
|
|
|
|
|
|
fct = function (e) {
|
|
|
e = e || $window.event;
|
|
|
@@ -37,19 +38,37 @@ function (angular, _, $) {
|
|
|
// Disable event handler when focus input and textarea
|
|
|
if (opt['inputDisabled']) {
|
|
|
var elt;
|
|
|
- if (e.target) elt = e.target;
|
|
|
- else if (e.srcElement) elt = e.srcElement;
|
|
|
- if (elt.nodeType == 3) elt = elt.parentNode;
|
|
|
- if (elt.tagName == 'INPUT' || elt.tagName == 'TEXTAREA') return;
|
|
|
+ if (e.target) {
|
|
|
+ elt = e.target;
|
|
|
+ }
|
|
|
+ else if (e.srcElement) {
|
|
|
+ elt = e.srcElement;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (elt.nodeType === 3) {
|
|
|
+ elt = elt.parentNode;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (elt.tagName === 'INPUT' || elt.tagName === 'TEXTAREA') {
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Find out which key is pressed
|
|
|
- if (e.keyCode) code = e.keyCode;
|
|
|
- else if (e.which) code = e.which;
|
|
|
+ if (e.keyCode) {
|
|
|
+ code = e.keyCode;
|
|
|
+ }
|
|
|
+ else if (e.which) {
|
|
|
+ code = e.which;
|
|
|
+ }
|
|
|
var character = String.fromCharCode(code).toLowerCase();
|
|
|
|
|
|
- if (code == 188) character = ","; // If the user presses , when the type is onkeydown
|
|
|
- if (code == 190) character = "."; // If the user presses , when the type is onkeydown
|
|
|
+ if (code === 188) {
|
|
|
+ character = ","; // If the user presses , when the type is onkeydown
|
|
|
+ }
|
|
|
+ if (code === 190) {
|
|
|
+ character = "."; // If the user presses , when the type is onkeydown
|
|
|
+ }
|
|
|
|
|
|
var keys = label.split("+");
|
|
|
// Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
|
|
|
@@ -152,39 +171,47 @@ function (angular, _, $) {
|
|
|
// Foreach keys in label (split on +)
|
|
|
for(var i=0, l=keys.length; k=keys[i],i<l; i++) {
|
|
|
switch (k) {
|
|
|
- case 'ctrl':
|
|
|
- case 'control':
|
|
|
- kp++;
|
|
|
- modifiers.ctrl.wanted = true;
|
|
|
- break;
|
|
|
- case 'shift':
|
|
|
- case 'alt':
|
|
|
- case 'meta':
|
|
|
- kp++;
|
|
|
- modifiers[k].wanted = true;
|
|
|
- break;
|
|
|
+ case 'ctrl':
|
|
|
+ case 'control':
|
|
|
+ kp++;
|
|
|
+ modifiers.ctrl.wanted = true;
|
|
|
+ break;
|
|
|
+ case 'shift':
|
|
|
+ case 'alt':
|
|
|
+ case 'meta':
|
|
|
+ kp++;
|
|
|
+ modifiers[k].wanted = true;
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
if (k.length > 1) { // If it is a special key
|
|
|
- if(special_keys[k] == code) kp++;
|
|
|
+ if(special_keys[k] === code) {
|
|
|
+ kp++;
|
|
|
+ }
|
|
|
} else if (opt['keyCode']) { // If a specific key is set into the config
|
|
|
- if (opt['keyCode'] == code) kp++;
|
|
|
+ if (opt['keyCode'] === code) {
|
|
|
+ kp++;
|
|
|
+ }
|
|
|
} else { // The special keys did not match
|
|
|
- if(character == k) kp++;
|
|
|
+ if(character === k) {
|
|
|
+ kp++;
|
|
|
+ }
|
|
|
else {
|
|
|
if(shift_nums[character] && e.shiftKey) { // Stupid Shift key bug created by using lowercase
|
|
|
character = shift_nums[character];
|
|
|
- if(character == k) kp++;
|
|
|
+ if(character === k) {
|
|
|
+ kp++;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(kp == keys.length &&
|
|
|
- modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
|
|
|
- modifiers.shift.pressed == modifiers.shift.wanted &&
|
|
|
- modifiers.alt.pressed == modifiers.alt.wanted &&
|
|
|
- modifiers.meta.pressed == modifiers.meta.wanted) {
|
|
|
+ if(kp === keys.length &&
|
|
|
+ modifiers.ctrl.pressed === modifiers.ctrl.wanted &&
|
|
|
+ modifiers.shift.pressed === modifiers.shift.wanted &&
|
|
|
+ modifiers.alt.pressed === modifiers.alt.wanted &&
|
|
|
+ modifiers.meta.pressed === modifiers.meta.wanted) {
|
|
|
$timeout(function() {
|
|
|
callback(e);
|
|
|
}, 1);
|
|
|
@@ -211,22 +238,40 @@ function (angular, _, $) {
|
|
|
'event': opt['type']
|
|
|
};
|
|
|
//Attach the function with the event
|
|
|
- if(elt.addEventListener) elt.addEventListener(opt['type'], fct, false);
|
|
|
- else if(elt.attachEvent) elt.attachEvent('on' + opt['type'], fct);
|
|
|
- else elt['on' + opt['type']] = fct;
|
|
|
+ if(elt.addEventListener) {
|
|
|
+ elt.addEventListener(opt['type'], fct, false);
|
|
|
+ }
|
|
|
+ else if(elt.attachEvent) {
|
|
|
+ elt.attachEvent('on' + opt['type'], fct);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ elt['on' + opt['type']] = fct;
|
|
|
+ }
|
|
|
};
|
|
|
// Remove the shortcut - just specify the shortcut and I will remove the binding
|
|
|
keyboardManagerService.unbind = function (label) {
|
|
|
label = label.toLowerCase();
|
|
|
+
|
|
|
var binding = keyboardManagerService.keyboardEvent[label];
|
|
|
- delete(keyboardManagerService.keyboardEvent[label])
|
|
|
- if(!binding) return;
|
|
|
+ delete(keyboardManagerService.keyboardEvent[label]);
|
|
|
+
|
|
|
+ if(!binding) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
var type = binding['event'],
|
|
|
elt = binding['target'],
|
|
|
callback = binding['callback'];
|
|
|
- if(elt.detachEvent) elt.detachEvent('on' + type, callback);
|
|
|
- else if(elt.removeEventListener) elt.removeEventListener(type, callback, false);
|
|
|
- else elt['on'+type] = false;
|
|
|
+
|
|
|
+ if(elt.detachEvent) {
|
|
|
+ elt.detachEvent('on' + type, callback);
|
|
|
+ }
|
|
|
+ else if(elt.removeEventListener) {
|
|
|
+ elt.removeEventListener(type, callback, false);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ elt['on'+type] = false;
|
|
|
+ }
|
|
|
};
|
|
|
//
|
|
|
return keyboardManagerService;
|