Created
July 13, 2012 01:12
-
-
Save djalmaaraujo/3102134 to your computer and use it in GitHub Desktop.
Konami Code for YUI3
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
YUI.add('konami', function (Y) { | |
Y.Event.define('konami', { | |
_konamiKeys: [38,38,40,40,37,39,37,39,66,65], | |
on: function (node, sub, notifier) { | |
var instance = this; | |
sub._firstIndexLenght = 0; | |
sub._index = 0; | |
sub._handle = Y.on('keydown', Y.rbind(instance._handleKeyDown, instance, sub, notifier)); | |
}, | |
_handleKeyDown: function(e, sub, notifier) { | |
var instance = this, | |
index = sub._index, | |
konamiKeys = this._konamiKeys; | |
if (sub._firstIndexLenght === 0) { | |
while (konamiKeys[0] === konamiKeys[++sub._firstIndexLenght]); | |
} | |
if (e.keyCode === konamiKeys[index]) { | |
if (++index === konamiKeys.length) { | |
notifier.fire(e); | |
index = 0; | |
} | |
} | |
else { | |
if ((index === sub._firstIndexLenght) && (e.keyCode === konamiKeys[0])) { | |
return; | |
} | |
index = 0; | |
} | |
sub._index = index; | |
}, | |
detach: function (node, sub, notifier) { | |
sub._handle.detach(); | |
} | |
}); | |
}, '1.0', { requires: ['event-synthetic'] }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment