Created
August 12, 2014 18:26
-
-
Save tommycoppers/fd0146a7086281305a93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function easterEgg ($, namespace) { | |
function EasterEgg (options) { | |
var defaults = { | |
timeout : 2000, // Time you have to enter a new key before the keysPressed array gets flushed | |
logKeys : false, // Console log the keys that you are pressing. Helps to determine combos. | |
combos : [] // [[keys], callback] - Add the combos from the start here. OR, add them with a returned function after the fact | |
}; | |
var settings = $.extend({}, defaults, options); | |
var $document = $(document), | |
easterTimeout, // Placeholder for easterEgg timeout | |
eventNamespace, // Unique namespace so that keypressing can be unbound | |
keysPressed = [], // Dynamically populates with key codes as they are actively pressed | |
combos = settings.combos; // [[keys], callback] | |
// Private Functions | |
//////////////////// | |
function addPressedKey (e) { | |
/** | |
* Populates keysPressed array with the event's key code and start a timeout, | |
* Then call matchKeysPressed. | |
* | |
*/ | |
var keycode = e.keyCode; | |
keysPressed.push(keycode); | |
clearTimeout(easterTimeout); | |
easterTimeout = setTimeout(removePressedKey, settings.timeout); | |
matchKeysPressed(); | |
// Just log the keys pressed. | |
if (settings.logKeys) { | |
console.log('======================='); | |
console.log('key pressed: ' + keycode); | |
console.log('current keys: ' + keysPressed); | |
} | |
} | |
function removePressedKey () { | |
/** | |
* Reset the keysPressed Array | |
*/ | |
keysPressed = []; | |
} | |
function matchKeysPressed () { | |
/** | |
* If key combinations have been added to the combos array, loop through the combinations. | |
* Within each combination, check the keysPressed array for the keys needed to be active for the callback to execute. | |
* If a key isn't found in keysPressed array, turn the doCallback switch to false and do not perform the callback. | |
* If all the keys exist, do the callback | |
*/ | |
if (combos.length) { | |
for (var i in combos) { | |
var thisCombo = combos[i]; | |
var thisKeys = thisCombo[0]; | |
var thisCallback = thisCombo[1]; | |
var doCallback = true; | |
if (thisKeys.length) { | |
for (var i in thisKeys) { | |
var key = thisKeys[i]; | |
if (keysPressed[i] !== key) { | |
doCallback = false; | |
break; | |
} | |
} | |
} | |
if (doCallback) { | |
thisCallback(); | |
} | |
} | |
} | |
} | |
function initialize () { | |
/** | |
* Create a random eventNamespace ID that will be unique to this instance of key events. | |
* Unique namespaces allow the document to be unbound from key presses relative to this instance | |
* Bind events to document with unique namespace | |
*/ | |
eventNamespace = '.keycombo_' + Math.ceil(Math.random()*100); | |
$document | |
.on('keydown' + eventNamespace, function (e) { | |
addPressedKey(e); | |
}); | |
} | |
// Private Functions | |
//////////////////// | |
function getCombos () { | |
/** | |
* As a developer, if you need to retrieve a list of active key combinations, call this function. | |
* Returns - combos array. | |
*/ | |
return combos; | |
} | |
function addCondition (keys, callback) { | |
/** | |
* Populates the combos array with an array containing [keys, callback] | |
* note: keys needs to be an array. | |
*/ | |
combos.push([keys, callback]); | |
} | |
function unbindKeypress () { | |
/** | |
* Unbinds keydown and keyup from document | |
*/ | |
$document.off('keydown' + eventNamespace).off('keyup' + eventNamespace); | |
} | |
// Initialize | |
///////////// | |
initialize(); | |
return { | |
combos : getCombos, | |
add : addCondition, | |
unbind : unbindKeypress | |
}; | |
} | |
namespace.easter = EasterEgg; | |
})(window.jQuery, window.project || {}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment