From 3d82b870c005fb1eff4ccef2594c4215641a5220 Mon Sep 17 00:00:00 2001 From: Oeslei Kuhn Date: Mon, 29 Jul 2019 12:37:33 -0300 Subject: [PATCH] Add method addTarget to allow binding mousetrap to additional elements #235 --- mousetrap.js | 28 ++++++++++++++++++++-------- mousetrap.min.js | 12 +----------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/mousetrap.js b/mousetrap.js index 66257606..82007dd2 100644 --- a/mousetrap.js +++ b/mousetrap.js @@ -420,16 +420,16 @@ }; } - function _belongsTo(element, ancestor) { + function _belongsTo(element, ancestors) { if (element === null || element === document) { return false; } - if (element === ancestor) { + if (ancestors.indexOf(element) !== -1) { return true; } - return _belongsTo(element.parentNode, ancestor); + return _belongsTo(element.parentNode, ancestors); } function Mousetrap(targetElement) { @@ -446,7 +446,7 @@ * * @type {Element} */ - self.target = targetElement; + self.targets = []; /** * a list of all the callbacks setup via Mousetrap.bind() @@ -885,10 +885,22 @@ } }; + /** + * add a target to listen for events + * + * @param {Object} targetElement - DOM node + * @returns void + */ + self.addTarget = function(targetElement) { + self.targets.push(targetElement); + _addEvent(targetElement, 'keypress', _handleKeyEvent); + _addEvent(targetElement, 'keydown', _handleKeyEvent); + _addEvent(targetElement, 'keyup', _handleKeyEvent); + return self; + }; + // start! - _addEvent(targetElement, 'keypress', _handleKeyEvent); - _addEvent(targetElement, 'keydown', _handleKeyEvent); - _addEvent(targetElement, 'keyup', _handleKeyEvent); + self.addTarget(targetElement); } /** @@ -978,7 +990,7 @@ return false; } - if (_belongsTo(element, self.target)) { + if (_belongsTo(element, self.targets)) { return false; } diff --git a/mousetrap.min.js b/mousetrap.min.js index 35660c4d..f1806ba2 100644 --- a/mousetrap.min.js +++ b/mousetrap.min.js @@ -1,11 +1 @@ -/* mousetrap v1.6.3 craig.is/killing/mice */ -(function(q,u,c){function v(a,b,g){a.addEventListener?a.addEventListener(b,g,!1):a.attachEvent("on"+b,g)}function z(a){if("keypress"==a.type){var b=String.fromCharCode(a.which);a.shiftKey||(b=b.toLowerCase());return b}return n[a.which]?n[a.which]:r[a.which]?r[a.which]:String.fromCharCode(a.which).toLowerCase()}function F(a){var b=[];a.shiftKey&&b.push("shift");a.altKey&&b.push("alt");a.ctrlKey&&b.push("ctrl");a.metaKey&&b.push("meta");return b}function w(a){return"shift"==a||"ctrl"==a||"alt"==a|| -"meta"==a}function A(a,b){var g,d=[];var e=a;"+"===e?e=["+"]:(e=e.replace(/\+{2}/g,"+plus"),e=e.split("+"));for(g=0;gc||n.hasOwnProperty(c)&&(p[n[c]]=c)}g=p[e]?"keydown":"keypress"}"keypress"==g&&d.length&&(g="keydown");return{key:m,modifiers:d,action:g}}function D(a,b){return null===a||a===u?!1:a===b?!0:D(a.parentNode,b)}function d(a){function b(a){a= -a||{};var b=!1,l;for(l in p)a[l]?b=!0:p[l]=0;b||(x=!1)}function g(a,b,t,f,g,d){var l,E=[],h=t.type;if(!k._callbacks[a])return[];"keyup"==h&&w(a)&&(b=[a]);for(l=0;l":".","?":"/","|":"\\"},B={option:"alt",command:"meta","return":"enter", -escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},p;for(c=1;20>c;++c)n[111+c]="f"+c;for(c=0;9>=c;++c)n[c+96]=c.toString();d.prototype.bind=function(a,b,c){a=a instanceof Array?a:[a];this._bindMultiple.call(this,a,b,c);return this};d.prototype.unbind=function(a,b){return this.bind.call(this,a,function(){},b)};d.prototype.trigger=function(a,b){if(this._directMap[a+":"+b])this._directMap[a+":"+b]({},a);return this};d.prototype.reset=function(){this._callbacks={}; -this._directMap={};return this};d.prototype.stopCallback=function(a,b){if(-1<(" "+b.className+" ").indexOf(" mousetrap ")||D(b,this.target))return!1;if("composedPath"in a&&"function"===typeof a.composedPath){var c=a.composedPath()[0];c!==a.target&&(b=c)}return"INPUT"==b.tagName||"SELECT"==b.tagName||"TEXTAREA"==b.tagName||b.isContentEditable};d.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)};d.addKeycodes=function(a){for(var b in a)a.hasOwnProperty(b)&&(n[b]=a[b]);p=null}; -d.init=function(){var a=d(u),b;for(b in a)"_"!==b.charAt(0)&&(d[b]=function(b){return function(){return a[b].apply(a,arguments)}}(b))};d.init();q.Mousetrap=d;"undefined"!==typeof module&&module.exports&&(module.exports=d);"function"===typeof define&&define.amd&&define(function(){return d})}})("undefined"!==typeof window?window:null,"undefined"!==typeof window?document:null); +!function(e,r){if(e){for(var i,o={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"},n={106:"*",107:"+",109:"-",110:".",111:"/",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'"},a={"~":"`","!":"1","@":"2","#":"3",$:"4","%":"5","^":"6","&":"7","*":"8","(":"9",")":"0",_:"-","+":"=",":":";",'"':"'","<":",",">":".","?":"/","|":"\\"},c={option:"alt",command:"meta",return:"enter",escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},t=1;t<20;++t)o[111+t]="f"+t;for(t=0;t<=9;++t)o[t+96]=t.toString();_.prototype.bind=function(e,t,n){return e=e instanceof Array?e:[e],this._bindMultiple.call(this,e,t,n),this},_.prototype.unbind=function(e,t){return this.bind.call(this,e,function(){},t)},_.prototype.trigger=function(e,t){return this._directMap[e+":"+t]&&this._directMap[e+":"+t]({},e),this},_.prototype.reset=function(){return this._callbacks={},this._directMap={},this},_.prototype.stopCallback=function(e,t){if(-1<(" "+t.className+" ").indexOf(" mousetrap "))return!1;if(function e(t,n){return null!==t&&t!==r&&(-1!==n.indexOf(t)||e(t.parentNode,n))}(t,this.targets))return!1;if("composedPath"in e&&"function"==typeof e.composedPath){var n=e.composedPath()[0];n!==e.target&&(t=n)}return"INPUT"==t.tagName||"SELECT"==t.tagName||"TEXTAREA"==t.tagName||t.isContentEditable},_.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)},_.addKeycodes=function(e){for(var t in e)e.hasOwnProperty(t)&&(o[t]=e[t]);i=null},_.init=function(){var t=_(r);for(var e in t)"_"!==e.charAt(0)&&(_[e]=function(e){return function(){return t[e].apply(t,arguments)}}(e))},_.init(),e.Mousetrap=_,"undefined"!=typeof module&&module.exports&&(module.exports=_),"function"==typeof define&&define.amd&&define(function(){return _})}function g(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent("on"+t,n)}function v(e){if("keypress"!=e.type)return o[e.which]?o[e.which]:n[e.which]?n[e.which]:String.fromCharCode(e.which).toLowerCase();var t=String.fromCharCode(e.which);return e.shiftKey||(t=t.toLowerCase()),t}function b(e){return"shift"==e||"ctrl"==e||"alt"==e||"meta"==e}function s(e,t,n){return"keypress"==(n=n||(function(){if(!i)for(var e in i={},o)95