123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943 |
-
- Namespace.register("U.K");
- U.K._REVERSE_MAP;
- U.K._MAP = {
- 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',
- 45: 'ins',
- 46: 'del',
- 91: 'meta',
- 93: 'meta',
- 224: 'meta'
- };
- U.K._KEYCODE_MAP = {
- 106: '*',
- 107: '+',
- 109: '-',
- 110: '.',
- 111: '/',
- 186: ';',
- 187: '=',
- 188: ',',
- 189: '-',
- 190: '.',
- 191: '/',
- 192: '`',
- 219: '[',
- 220: '\\',
- 221: ']',
- 222: '\''
- };
- U.K._SHIFT_MAP = {
- '~': '`',
- '!': '1',
- '@': '2',
- '#': '3',
- '$': '4',
- '%': '5',
- '^': '6',
- '&': '7',
- '*': '8',
- '(': '9',
- ')': '0',
- '_': '-',
- '+': '=',
- ':': ';',
- '\"': '\'',
- '<': ',',
- '>': '.',
- '?': '/',
- '|': '\\'
- };
- U.K._SPECIAL_ALIASES = {
- 'option': 'alt',
- 'command': 'meta',
- 'return': 'enter',
- 'escape': 'esc',
- 'plus': '+',
- 'mod': /Mac|iPod|iPhone|iPad/.test(navigator.platform) ? 'meta' : 'ctrl'
- };
- U.K.start = function () {
-
- for (var i = 1; i < 20; ++i) {
- U.K._MAP[111 + i] = 'f' + i;
- }
-
- for (i = 0; i <= 9; ++i) {
- U.K._MAP[i + 96] = i.toString();
- }
- }
- U.K._characterFromEvent = function (e) {
-
- if (e.type == 'keypress') {
- var character = String.fromCharCode(e.which);
-
-
-
-
-
-
-
-
-
- if (!e.shiftKey) {
- character = character.toLowerCase();
- }
- return character;
- }
-
- if (U.K._MAP[e.which]) {
- return U.K._MAP[e.which];
- }
- if (U.K._KEYCODE_MAP[e.which]) {
- return U.K._KEYCODE_MAP[e.which];
- }
-
-
-
-
- return String.fromCharCode(e.which).toLowerCase();
- }
- U.K._modifiersMatch = function (modifiers1, modifiers2) {
- return modifiers1.sort().join(',') === modifiers2.sort().join(',');
- }
- U.K._eventModifiers = function (e) {
- var modifiers = [];
- if (e.shiftKey) {
- modifiers.push('shift');
- }
- if (e.altKey) {
- modifiers.push('alt');
- }
- if (e.ctrlKey) {
- modifiers.push('ctrl');
- }
- if (e.metaKey) {
- modifiers.push('meta');
- }
- return modifiers;
- }
- U.K._isModifier = function (key) {
- return key == 'shift' || key == 'ctrl' || key == 'alt' || key == 'meta';
- }
- U.K._getReverseMap = function () {
- if (!_REVERSE_MAP) {
- _REVERSE_MAP = {};
- for (var key in U.K._MAP) {
-
-
- if (key > 95 && key < 112) {
- continue;
- }
- if (U.K._MAP.hasOwnProperty(key)) {
- _REVERSE_MAP[U.K._MAP[key]] = key;
- }
- }
- }
- return _REVERSE_MAP;
- }
- U.K._pickBestAction = function (key, modifiers, action) {
-
-
- if (!action) {
- action = U.K._getReverseMap()[key] ? 'keydown' : 'keypress';
- }
-
-
- if (action == 'keypress' && modifiers.length) {
- action = 'keydown';
- }
- return action;
- }
- U.K._keysFromString = function (combination) {
- if (combination === '+') {
- return ['+'];
- }
- combination = combination.replace(/\+{2}/g, '+plus');
- return combination.split('+');
- }
- U.K._getKeyInfo = function (combination, action) {
- var keys;
- var key;
- var i;
- var modifiers = [];
-
-
- keys = U.K._keysFromString(combination);
- for (i = 0; i < keys.length; ++i) {
- key = keys[i];
-
- if (U.K._SPECIAL_ALIASES[key]) {
- key = U.K._SPECIAL_ALIASES[key];
- }
-
-
-
- if (action && action != 'keypress' && U.K._SHIFT_MAP[key]) {
- key = U.K._SHIFT_MAP[key];
- modifiers.push('shift');
- }
-
- if (U.K._isModifier(key)) {
- modifiers.push(key);
- }
- }
-
-
- action = U.K._pickBestAction(key, modifiers, action);
- return {
- key: key,
- modifiers: modifiers,
- action: action
- };
- }
- U.K._belongsTo = function (element, ancestor) {
- if (element === null || element === document) {
- return false;
- }
- if (element === ancestor) {
- return true;
- }
- return U.K._getReverseMap(element.parentNode, ancestor);
- }
- U.K.Mousetrap = function (targetElement) {
- var self = this;
- targetElement = targetElement || document;
- if (!(self instanceof U.K.Mousetrap)) {
- return new U.K.Mousetrap(targetElement);
- }
-
- self.target = targetElement;
-
- self._callbacks = {};
-
- self._directMap = {};
-
- var _sequenceLevels = {};
-
- var _resetTimer;
-
- var _ignoreNextKeyup = false;
-
- var _ignoreNextKeypress = false;
-
- var _nextExpectedAction = false;
-
- function _resetSequences(doNotReset) {
- doNotReset = doNotReset || {};
- var activeSequences = false,
- key;
- for (key in _sequenceLevels) {
- if (doNotReset[key]) {
- activeSequences = true;
- continue;
- }
- _sequenceLevels[key] = 0;
- }
- if (!activeSequences) {
- _nextExpectedAction = false;
- }
- }
-
- function _getMatches(character, modifiers, e, sequenceName, combination, level) {
- var i;
- var callback;
- var matches = [];
- var action = e.type;
-
- if (!self._callbacks[character]) {
- return [];
- }
-
- if (action == 'keyup' && U.K._isModifier(character)) {
- modifiers = [character];
- }
-
-
- for (i = 0; i < self._callbacks[character].length; ++i) {
- callback = self._callbacks[character][i];
-
-
- if (!sequenceName && callback.seq && _sequenceLevels[callback.seq] != callback.level) {
- continue;
- }
-
-
- if (action != callback.action) {
- continue;
- }
-
-
-
-
-
-
-
- if ((action == 'keypress' && !e.metaKey && !e.ctrlKey) || U.K._modifiersMatch(modifiers, callback.modifiers)) {
-
-
-
-
-
- var deleteCombo = !sequenceName && callback.combo == combination;
- var deleteSequence = sequenceName && callback.seq == sequenceName && callback.level == level;
- if (deleteCombo || deleteSequence) {
- self._callbacks[character].splice(i, 1);
- }
- matches.push(callback);
- }
- }
- return matches;
- }
-
- function _fireCallback(callback, e, combo, sequence) {
-
- if (self.stopCallback(e, e.target || e.srcElement, combo, sequence)) {
- return;
- }
- if (callback(e, combo) === false) {
- U.M.StopDefault(e);
- U.M.StopBubble(e);
- }
- }
-
- self._handleKey = function (character, modifiers, e) {
- var callbacks = _getMatches(character, modifiers, e);
- var i;
- var doNotReset = {};
- var maxLevel = 0;
- var processedSequenceCallback = false;
-
- for (i = 0; i < callbacks.length; ++i) {
- if (callbacks[i].seq) {
- maxLevel = Math.max(maxLevel, callbacks[i].level);
- }
- }
-
- for (i = 0; i < callbacks.length; ++i) {
-
-
-
-
-
- if (callbacks[i].seq) {
-
-
-
-
-
-
-
-
- if (callbacks[i].level != maxLevel) {
- continue;
- }
- processedSequenceCallback = true;
-
- doNotReset[callbacks[i].seq] = 1;
- _fireCallback(callbacks[i].callback, e, callbacks[i].combo, callbacks[i].seq);
- continue;
- }
-
-
- if (!processedSequenceCallback) {
- _fireCallback(callbacks[i].callback, e, callbacks[i].combo);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var ignoreThisKeypress = e.type == 'keypress' && _ignoreNextKeypress;
- if (e.type == _nextExpectedAction && !U.K._isModifier(character) && !ignoreThisKeypress) {
- _resetSequences(doNotReset);
- }
- _ignoreNextKeypress = processedSequenceCallback && e.type == 'keydown';
- };
-
- function _handleKeyEvent(e) {
-
- if (typeof e.which !== 'number') {
- e.which = e.keyCode;
- }
- var character = U.K._characterFromEvent(e);
-
- if (!character) {
- return;
- }
-
- if (e.type == 'keyup' && _ignoreNextKeyup === character) {
- _ignoreNextKeyup = false;
- return;
- }
- self.handleKey(character, U.K._eventModifiers(e), e);
- }
-
- function _resetSequenceTimer() {
- clearTimeout(_resetTimer);
- _resetTimer = setTimeout(_resetSequences, 1000);
- }
-
- function _bindSequence(combo, keys, callback, action) {
-
-
- _sequenceLevels[combo] = 0;
-
- function _increaseSequence(nextAction) {
- return function () {
- _nextExpectedAction = nextAction;
- ++_sequenceLevels[combo];
- _resetSequenceTimer();
- };
- }
-
- function _callbackAndReset(e) {
- _fireCallback(callback, e, combo);
-
-
-
- if (action !== 'keyup') {
- _ignoreNextKeyup = U.K._characterFromEvent(e);
- }
-
-
- setTimeout(_resetSequences, 10);
- }
-
-
-
-
-
-
-
-
-
- for (var i = 0; i < keys.length; ++i) {
- var isFinal = i + 1 === keys.length;
- var wrappedCallback = isFinal ? _callbackAndReset : _increaseSequence(action || U.K._getKeyInfo(keys[i + 1]).action);
- _bindSingle(keys[i], wrappedCallback, action, combo, i);
- }
- }
-
- function _bindSingle(combination, callback, action, sequenceName, level) {
-
- self._directMap[combination + ':' + action] = callback;
-
- combination = combination.replace(/\s+/g, ' ');
- var sequence = combination.split(' ');
- var info;
-
-
- if (sequence.length > 1) {
- _bindSequence(combination, sequence, callback, action);
- return;
- }
- info = U.K._getKeyInfo(combination, action);
-
-
- self._callbacks[info.key] = self._callbacks[info.key] || [];
-
- _getMatches(info.key, info.modifiers, { type: info.action }, sequenceName, combination, level);
-
-
-
-
-
-
- self._callbacks[info.key][sequenceName ? 'unshift' : 'push']({
- callback: callback,
- modifiers: info.modifiers,
- action: info.action,
- seq: sequenceName,
- level: level,
- combo: combination
- });
- }
-
- self._bindMultiple = function (combinations, callback, action) {
- for (var i = 0; i < combinations.length; ++i) {
- _bindSingle(combinations[i], callback, action);
- }
- };
-
- U.M.AddEvent(targetElement, 'keypress', _handleKeyEvent);
- U.M.AddEvent(targetElement, 'keydown', _handleKeyEvent);
- U.M.AddEvent(targetElement, 'keyup', _handleKeyEvent);
- }
- U.K.Mousetrap = function () { };
- U.K.Mousetrap.prototype.bind = function (keys, callback, action) {
- var self = this;
- keys = keys instanceof Array ? keys : [keys];
- self._bindMultiple.call(self, keys, callback, action);
- return self;
- };
- U.K.Mousetrap.prototype.unbind = function (keys, action) {
- var self = this;
- return self.bind.call(self, keys, function () { }, action);
- };
- U.K.Mousetrap.prototype.trigger = function (keys, action) {
- var self = this;
- if (self._directMap[keys + ':' + action]) {
- self._directMap[keys + ':' + action]({}, keys);
- }
- return self;
- };
- U.K.Mousetrap.prototype.reset = function () {
- var self = this;
- self._callbacks = {};
- self._directMap = {};
- return self;
- };
- U.K.Mousetrap.prototype.stopCallback = function (e, element) {
- var self = this;
-
- if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
- return false;
- }
- if (U.K._getReverseMap(element, self.target)) {
- return false;
- }
-
- return element.tagName == 'INPUT' || element.tagName == 'SELECT' || element.tagName == 'TEXTAREA' || element.isContentEditable;
- };
- U.K.Mousetrap.prototype.handleKey = function () {
- var self = this;
- return self._handleKey.apply(self, arguments);
- };
- U.K.Mousetrap.addKeycodes = function (object) {
- for (var key in object) {
- if (object.hasOwnProperty(key)) {
- U.K._MAP[key] = object[key];
- }
- }
- _REVERSE_MAP = null;
- };
- U.K.Mousetrap.init = function () {
- var documentMousetrap = Mousetrap(document);
- for (var method in documentMousetrap) {
- if (method.charAt(0) !== '_') {
- Mousetrap[method] = (function (method) {
- return function () {
- return documentMousetrap[method].apply(documentMousetrap, arguments);
- };
- } (method));
- }
- }
- };
|