forked from ccampbell/mousetrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mousetrap.min.js
1 lines (1 loc) · 5.1 KB
/
mousetrap.min.js
1
!function(e,r,t){if(e){for(var a,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:"'"},c={"~":"`","!":"1","@":"2","#":"3",$:"4","%":"5","^":"6","&":"7","*":"8","(":"9",")":"0",_:"-","+":"=",":":";",'"':"'","<":",",">":".","?":"/","|":"\\"},s={option:"alt",command:"meta",return:"enter",escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},i=1;i<20;++i)o[111+i]="f"+i;for(i=0;i<=9;++i)o[i+96]=i.toString();w.prototype.destroy=function(){var e=this;e.reset(),l(e.target,"keypress",e._handleKeyEvent),l(e.target,"keydown",e._handleKeyEvent),l(e.target,"keyup",e._handleKeyEvent),e.target=t,e._handleKeyEvent=t},w.prototype.bind=function(e,t,n){return e=e instanceof Array?e:[e],this._bindMultiple.call(this,e,t,n),this},w.prototype.unbind=function(e,t){return this.bind.call(this,e,function(){},t)},w.prototype.trigger=function(e,t){return this._directMap[e+":"+t]&&this._directMap[e+":"+t]({},e),this},w.prototype.reset=function(){return this._callbacks={},this._directMap={},this},w.prototype.stopCallback=function(e,t){return!(-1<(" "+t.className+" ").indexOf(" mousetrap "))&&(!function e(t,n){return null!==t&&t!==r&&(t===n||e(t.parentNode,n))}(t,this.target)&&("INPUT"==t.tagName||"SELECT"==t.tagName||"TEXTAREA"==t.tagName||t.isContentEditable))},w.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)},w.addKeycodes=function(e){for(var t in e)e.hasOwnProperty(t)&&(o[t]=e[t]);a=null},w.init=function(){var t=w(r);for(var e in t)"_"!==e.charAt(0)&&(w[e]=function(e){return function(){return t[e].apply(t,arguments)}}(e))},w.init(),e.Mousetrap=w,"undefined"!=typeof module&&module.exports&&(module.exports=w),"function"==typeof define&&define.amd&&define(function(){return w})}function k(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent("on"+t,n)}function l(e,t,n){e.removeEventListener?e.removeEventListener(t,n,!1):e.detachEvent("on"+t,n)}function g(e){if("keypress"==e.type){var t=String.fromCharCode(e.which);return e.shiftKey||(t=t.toLowerCase()),t}return o[e.which]?o[e.which]:n[e.which]?n[e.which]:String.fromCharCode(e.which).toLowerCase()}function b(e){return"shift"==e||"ctrl"==e||"alt"==e||"meta"==e}function u(e,t,n){return n||(n=function(){if(!a)for(var e in a={},o)95<e&&e<112||o.hasOwnProperty(e)&&(a[o[e]]=e);return a}()[e]?"keydown":"keypress"),"keypress"==n&&t.length&&(n="keydown"),n}function _(e,t){var n,r,a,o,i=[];for(n="+"===(o=e)?["+"]:(o=o.replace(/\+{2}/g,"+plus")).split("+"),a=0;a<n.length;++a)r=n[a],s[r]&&(r=s[r]),t&&"keypress"!=t&&c[r]&&(r=c[r],i.push("shift")),b(r)&&i.push(r);return{key:r,modifiers:i,action:t=u(r,i,t)}}function w(e){var d=this;if(e=e||r,!(d instanceof w))return new w(e);d.target=e,d._callbacks={},d._directMap={};var s,y={},l=!1,u=!1,p=!1;function f(e){e=e||{};var t,n=!1;for(t in y)e[t]?n=!0:y[t]=0;n||(p=!1)}function h(e,t,n,r,a,o){var i,c,s,l,u=[],p=n.type;if(!d._callbacks[e])return[];for("keyup"==p&&b(e)&&(t=[e]),i=0;i<d._callbacks[e].length;++i)if(c=d._callbacks[e][i],(r||!c.seq||y[c.seq]==c.level)&&p==c.action&&("keypress"==p&&!n.metaKey&&!n.ctrlKey||(s=t,l=c.modifiers,s.sort().join(",")===l.sort().join(",")))){var f=!r&&c.combo==a,h=r&&c.seq==r&&c.level==o;(f||h)&&d._callbacks[e].splice(i,1),u.push(c)}return u}function v(e,t,n,r){var a,o;d.stopCallback(t,t.target||t.srcElement,n,r)||!1===e(t,n)&&((o=t).preventDefault?o.preventDefault():o.returnValue=!1,(a=t).stopPropagation?a.stopPropagation():a.cancelBubble=!0)}function c(t,e,n,r){function a(e){return function(){p=e,++y[t],clearTimeout(s),s=setTimeout(f,1e3)}}function o(e){v(n,e,t),"keyup"!==r&&(l=g(e)),setTimeout(f,10)}for(var i=y[t]=0;i<e.length;++i){var c=i+1===e.length?o:a(r||_(e[i+1]).action);m(e[i],c,r,t,i)}}function m(e,t,n,r,a){d._directMap[e+":"+n]=t;var o,i=(e=e.replace(/\s+/g," ")).split(" ");1<i.length?c(e,i,t,n):(o=_(e,n),d._callbacks[o.key]=d._callbacks[o.key]||[],h(o.key,o.modifiers,{type:o.action},r,e,a),d._callbacks[o.key][r?"unshift":"push"]({callback:t,modifiers:o.modifiers,action:o.action,seq:r,level:a,combo:e}))}d._handleKey=function(e,t,n){var r,a=h(e,t,n),o={},i=0,c=!1;for(r=0;r<a.length;++r)a[r].seq&&(i=Math.max(i,a[r].level));for(r=0;r<a.length;++r)if(a[r].seq){if(a[r].level!=i)continue;c=!0,o[a[r].seq]=1,v(a[r].callback,n,a[r].combo,a[r].seq)}else c||v(a[r].callback,n,a[r].combo);var s="keypress"==n.type&&u;n.type!=p||b(e)||s||f(o),u=c&&"keydown"==n.type},d._bindMultiple=function(e,t,n){for(var r=0;r<e.length;++r)m(e[r],t,n)},d._handleKeyEvent=function(e){"number"!=typeof e.which&&(e.which=e.keyCode);var t,n,r=g(e);r&&("keyup"!=e.type||l!==r?d.handleKey(r,(n=[],(t=e).shiftKey&&n.push("shift"),t.altKey&&n.push("alt"),t.ctrlKey&&n.push("ctrl"),t.metaKey&&n.push("meta"),n),e):l=!1)},k(e,"keypress",d._handleKeyEvent),k(e,"keydown",d._handleKeyEvent),k(e,"keyup",d._handleKeyEvent)}}("undefined"!=typeof window?window:null,"undefined"!=typeof window?document:null);