@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
299 lines (249 loc) • 7.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Shortcut = Shortcut;
exports.keycallback = keycallback;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
//$Id$
var isShortCutEnabled;
var showShortcut = {};
var shortcutdatas = {};
var additionalKey = {};
var _SpecialKeyMaps = {
8: 'backspace',
9: 'tab',
13: 'enter',
16: 'shift',
17: 'ctrl',
18: 'alt',
//20: 'capslock',
27: 'esc',
32: 'space',
//33: 'pageup',
//34: 'pagedown',
//35: 'end',
//36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
46: 'del',
91: 'meta',
93: 'meta',
224: 'meta',
43: 'plus',
// +
61: 'plus',
// = , in firefox
63: 'ques',
// ?
187: 'plus',
// =
191: 'slash' // /
};
function Shortcut(target) {
_addEvent(target, 'keypress', keycallback);
_addEvent(target, 'keydown', keycallback);
_addEvent(target, 'keyup', keycallback);
}
Shortcut.setState = function (isEnabled) {
isShortCutEnabled = isEnabled;
};
Shortcut.addAll = function (keysObj, props) {
showShortcut = props.showShortcut;
isShortCutEnabled && keysObj.map(function (keyOptions, index) {
Shortcut.add(keyOptions.keyCombo, keyOptions.keyCallback(props), keyOptions.keyAction, keyOptions.displayLabel || []);
return;
});
};
/**
* @param keycombo = 'z' or 'z+e' or 'shift+a' etc..
* @param callback = any function
* @param action = keypress or keydown or keyup
*/
Shortcut.bulkAdd = function (shortcutArray) {
showShortcut && shortcutArray.map(function (shortcutObj) {
Shortcut.add(shortcutObj.keyCombo, shortcutObj.keyCallback, shortcutObj.keyAction, shortcutObj.displayLabel || []);
});
};
Shortcut.add = function (keycombo, cpFunc, action, displayLabel) {
if (!keycombo || !isShortCutEnabled) {
return;
}
var tempjson = {};
if (cpFunc === undefined) {
cpFunc = function cpFunc(keycode, keyOptions, e) {
_defaultCallBack(keycode, keyOptions);
};
} else {
var oldfunc = cpFunc;
cpFunc = function cpFunc(keycode, keyOptions, ev) {
oldfunc(keycode, ev);
};
}
tempjson.key = keycombo.toLowerCase();
tempjson.displayLabel = displayLabel;
tempjson.keyOptions = cpFunc;
tempjson.action = action || 'keypress';
shortcutdatas[keycombo.toLowerCase()] = tempjson;
};
/*Shortcut.prototype.remove = function(){
};*/
Shortcut.removeAll = function () {
shortcutdatas = {};
};
Shortcut.show = function () {
return Object.assign({}, shortcutdatas);
};
Shortcut.remove = function (keyCombo) {
keyCombo = Array.isArray(keyCombo) ? keyCombo : [keyCombo];
keyCombo.map(function (key) {
delete shortcutdatas[key];
});
};
Shortcut.init = function (doc) {
var documentshortcut = Shortcut(doc);
for (var method in documentshortcut) {
if (method.charAt(0) !== '_') {
Shortcut[method] = function (method) {
return function () {
return documentshortcut[method].apply(documentshortcut, arguments);
};
}(method);
}
}
};
function _addEvent(object, type, callback) {
if (object.addEventListener) {
object.addEventListener(type, callback, false);
return;
}
object.attachEvent("on".concat(type), callback);
}
function keycallback(e) {
if (!isShortCutEnabled) {
return;
}
var keyaction = e.type;
var keycode = _convertToKeyCharacter(e);
if (e.metaKey) {
return;
}
_bindNeedData(e, keyaction);
if ('esc' !== keycode) {
if (_shortRestrictionPlaces(e)) {
//return if type any text in input tag or texta area
return;
}
}
keycode = _eventModifiers(e) + keycode;
var keycodedata = shortcutdatas[keycode];
if (keycodedata) {
e.preventDefault();
}
if (keycodedata && keyaction === keycodedata.action && keycodedata.keyOptions) {
keycodedata.keyOptions && keycodedata.keyOptions(keycode, keycodedata.keyOptions, e);
showShortcut({
type: 'SHORTCUT_SHOW',
displayLabel: keycodedata.displayLabel
});
}
}
function _eventModifiers(e) {
if (e.shiftKey === true && e.metaKey === true) {
return 'command+shift+';
}
if (e.shiftKey === true && e.ctrlKey === true) {
return 'ctrl+shift+';
}
if (e.altKey === true && e.ctrlKey === true) {
return 'alt+ctrl+';
}
if (e.ctrlKey === true) {
return 'ctrl+';
}
if (e.metaKey === true) {
return 'command+';
}
if (e.shiftKey === true) {
return 'shift+';
}
if (additionalKey.zpress === true) {
return 'z+';
} else if (additionalKey.apress === true) {
return 'a+';
} else if (additionalKey.cpress === true) {
return 'c+';
} else if (additionalKey.dpress === true) {
return 'd+';
} else if (additionalKey.fpress === true) {
return 'f+';
} else if (additionalKey.kpress === true) {
return 'k+';
} else if (additionalKey.ppress === true) {
return 'p+';
} else if (additionalKey.rpress === true) {
return 'r+';
} else if (additionalKey.tpress === true) {
return 't+';
} else if (additionalKey.epress === true) {
return 'e+';
} else if (additionalKey.lpress === true) {
return 'l+';
}
return '';
}
function _bindNeedData(e, keyaction) {
var keyName = String.fromCharCode(e.keyCode).toLowerCase();
if (['z', 'a', 'c', 'd', 'f', 'k', 'p', 'r', 't', 'l', 'e'].indexOf(keyName) !== -1) {
var extraName = "".concat(keyName, "press");
if (keyaction === 'keydown') {
additionalKey = !additionalKey[extraName] ? Object.assign({}, additionalKey, _defineProperty({}, extraName, true)) : additionalKey;
additionalKey[extraName] = true;
} else if (keyaction === 'keyup') {
additionalKey = !additionalKey[extraName] ? Object.assign({}, additionalKey, _defineProperty({}, extraName, false)) : additionalKey;
additionalKey[extraName] = false;
}
}
}
function _convertToKeyCharacter(e) {
var keyCode = e.keyCode || e.charCode;
var keyName = String.fromCharCode(keyCode).toLowerCase();
keyName = _SpecialKeyMaps.hasOwnProperty(keyCode) ? _SpecialKeyMaps[keyCode] : keyName;
return keyName;
}
function _defaultCallBack(keycombo, keyOptions) {
/*var keyElement = jQuery("[data-shortcut-key='" + keycombo + "']");
if (keyOptions.callbackflow !== undefined) {
keycombo = keyOptions.callbackflow.split(',');
var click = function(i, combo) {
setTimeout(function() {
jQuery("[data-shortcut-key='" + combo + "']").trigger('click');
}, i * 200);
};
for (var i = 0; i < keycombo.length; i++) {
var clickFunc = click.bind(this, i);
clickFunc(keycombo[i]);
highLightKeys(keycombo[i]);
}
return;
}
if (keyElement.length === 1) {
keyElement.trigger('click');
highLightKeys(keycombo);
return;
}*/
}
function _shortRestrictionPlaces(e) {
var _target = e.target;
var tagName = _target.tagName;
var keyCode = e.keyCode || e.charCode;
var isContentEditable = _target.contentEditable === 'true';
if (['TEXTAREA', 'INPUT', 'SELECT'].indexOf(tagName) !== -1 || //!SupportUI.CurrentUser.getKBStatus() ||
//restrict_keyUI() ||
keyCode >= 112 && keyCode <= 123 || isContentEditable) {
return true;
}
return false;
}