From 2ab240ebf2a454a5a45ab354ae49de0fcaf0aa4f Mon Sep 17 00:00:00 2001 From: Mert Kosova Date: Sun, 5 May 2019 20:41:39 +0300 Subject: [PATCH] Added export and destroy function --- egg.js | 20 ++++++++++++++++---- egg.min.js | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/egg.js b/egg.js index 6795b81..e4215cb 100644 --- a/egg.js +++ b/egg.js @@ -36,7 +36,7 @@ function Egg(/* keySequence, fn, metadata */) { // attempt to call passed function bound to Egg object instance Egg.prototype.__execute = function(fn) { return typeof fn === 'function' && fn.call(this); -} +}; // converts literal character values to keyCodes Egg.prototype.__toCharCodes = function(keys) { @@ -67,20 +67,20 @@ Egg.prototype.__toCharCodes = function(keys) { }); return characterKeyCodes.join(','); -} +}; // Keycode lookup: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes Egg.prototype.AddCode = function(keys, fn, metadata) { this.eggs.push({keys: this.__toCharCodes(keys), fn: fn, metadata: metadata}); return this; -} +}; Egg.prototype.AddHook = function(fn) { this.hooks.push(fn); return this; -} +}; Egg.prototype.handleEvent = function(e) { var keyCode = e.which; @@ -142,9 +142,21 @@ Egg.prototype.Listen = function() { return this; }; +Egg.prototype.Destroy = function() { + if ( document.removeEventListener !== void 0 ) { + document.removeEventListener( "keydown", this, false ); + document.removeEventListener( "keyup", this, false ); + } + + return this; +}; + Egg.prototype.listen = Egg.prototype.Listen; Egg.prototype.addCode = Egg.prototype.AddCode; Egg.prototype.addHook = Egg.prototype.AddHook; +Egg.prototype.destroy = Egg.prototype.Destroy; + +module.exports = Egg; // EGGSAMPLE // var egg = new Egg(); diff --git a/egg.min.js b/egg.min.js index 6272d9a..e82aaf9 100644 --- a/egg.min.js +++ b/egg.min.js @@ -1 +1 @@ -function Egg(){this.eggs=[],this.hooks=[],this.kps=[],this.activeEgg="",this.ignoredKeys=[16],arguments.length&&this.AddCode.apply(this,arguments)}Egg.prototype.__execute=function(a){return"function"==typeof a&&a.call(this)},Egg.prototype.__toCharCodes=function(a){var b={slash:191,up:38,down:40,left:37,right:39,enter:13,space:32,ctrl:17,alt:18,tab:9,esc:27},c=Object.keys(b);"string"==typeof a&&(a=a.split(",").map(function(a){return a.trim()}));var d=a.map(function(a){return a===parseInt(a,10)?a:c.indexOf(a)>-1?b[a]:a.charCodeAt(0)});return d.join(",")},Egg.prototype.AddCode=function(a,b,c){return this.eggs.push({keys:this.__toCharCodes(a),fn:b,metadata:c}),this},Egg.prototype.AddHook=function(a){return this.hooks.push(a),this},Egg.prototype.handleEvent=function(a){var b=a.which,c=b>=65&&90>=b;if(!("keydown"!==a.type||a.metaKey||a.ctrlKey||a.altKey||a.shiftKey)){var d=a.target.tagName;if(("HTML"===d||"BODY"===d)&&c)return void a.preventDefault()}"keyup"===a.type&&this.eggs.length>0&&(c&&(a.shiftKey||(b+=32)),-1===this.ignoredKeys.indexOf(b)&&this.kps.push(b),this.eggs.forEach(function(a,b){var c=this.kps.toString().indexOf(a.keys)>=0;c&&(this.kps=[],this.activeEgg=a,this.__execute(a.fn,this),this.hooks.forEach(this.__execute,this),this.activeEgg="")},this))},Egg.prototype.Listen=function(){return void 0!==document.addEventListener&&(document.addEventListener("keydown",this,!1),document.addEventListener("keyup",this,!1)),this},Egg.prototype.listen=Egg.prototype.Listen,Egg.prototype.addCode=Egg.prototype.AddCode,Egg.prototype.addHook=Egg.prototype.AddHook; +function Egg(){this.eggs=[],this.hooks=[],this.kps=[],this.activeEgg="",this.ignoredKeys=[16],arguments.length&&this.AddCode.apply(this,arguments)}Egg.prototype.__execute=function(a){return"function"==typeof a&&a.call(this)},Egg.prototype.__toCharCodes=function(a){var b={slash:191,up:38,down:40,left:37,right:39,enter:13,space:32,ctrl:17,alt:18,tab:9,esc:27},c=Object.keys(b);return"string"==typeof a&&(a=a.split(",").map(function(a){return a.trim()})),a.map(function(a){return a===parseInt(a,10)?a:c.indexOf(a)>-1?b[a]:a.charCodeAt(0)}).join(",")},Egg.prototype.AddCode=function(a,b,c){return this.eggs.push({keys:this.__toCharCodes(a),fn:b,metadata:c}),this},Egg.prototype.AddHook=function(a){return this.hooks.push(a),this},Egg.prototype.handleEvent=function(a){var b=a.which,c=b>=65&&b<=90;if(!("keydown"!==a.type||a.metaKey||a.ctrlKey||a.altKey||a.shiftKey)){var d=a.target.tagName;if(("HTML"===d||"BODY"===d)&&c)return void a.preventDefault()}"keyup"===a.type&&this.eggs.length>0&&(c&&(a.shiftKey||(b+=32)),-1===this.ignoredKeys.indexOf(b)&&this.kps.push(b),this.eggs.forEach(function(a,b){this.kps.toString().indexOf(a.keys)>=0&&(this.kps=[],this.activeEgg=a,this.__execute(a.fn,this),this.hooks.forEach(this.__execute,this),this.activeEgg="")},this))},Egg.prototype.Listen=function(){return void 0!==document.addEventListener&&(document.addEventListener("keydown",this,!1),document.addEventListener("keyup",this,!1)),this},Egg.prototype.Destroy=function(){return void 0!==document.removeEventListener&&(document.removeEventListener("keydown",this,!1),document.removeEventListener("keyup",this,!1)),this},Egg.prototype.listen=Egg.prototype.Listen,Egg.prototype.addCode=Egg.prototype.AddCode,Egg.prototype.addHook=Egg.prototype.AddHook,Egg.prototype.destroy=Egg.prototype.Destroy,module.exports=Egg; \ No newline at end of file