Skip to content

Commit

Permalink
gallery-2009.10.26-18.14.28 lsmith gallery-event-konami
Browse files Browse the repository at this point in the history
  • Loading branch information
YUI Builder committed Oct 26, 2009
1 parent ffc84d5 commit 66715e2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/gallery-event-konami/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
builddir=../../../builder/componentbuild
component=gallery-event-konami
component.jsfiles=konami.js
component.requires=node-base,event-custom
6 changes: 6 additions & 0 deletions src/gallery-event-konami/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="YUI" default="local">
<property file="build.properties" />
<import file="${builddir}/3.x/bootstrap.xml"
description="Default Build Properties and Targets" />
</project>
67 changes: 67 additions & 0 deletions src/gallery-event-konami/js/konami.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Based on the Konami code (http://en.wikipedia.org/wiki/Konami_Code).
* Subscribers to this event should do something special. The event will be
* fired only once for each subscriber. With great power comes great
* responsibility, after all.
*
* @module event-konami
*/

/**
* Provides a subscribable event named &quot;konami&quot;.
*
* @event konami
* @for YUI
* @param type {String} 'konami'
* @param fn {Function} the callback function
* @param id {String|Node|etc} the element to bind (typically document)
* @param o {Object} optional context object
* @param args 0..n additional arguments that should be provided
* to the listener.
* @return {Event.Handle} the detach handle
*/
var progress = {},
handlers = {},
keys = [38,38,40,40,37,39,37,39,66,65],
eventDef;

eventDef = {
on: function (type, fn, el) {
var args = Y.Array(arguments,0,true),
ename;

el = args[2] = Y.get(el);

if (el) {
ename = ('konami_' + Y.stamp(el)).replace(/,/g,'_');

if (!Y.getEvent(ename)) {
progress[ename] = 0;
handlers[ename] = {};

handlers[ename].dom = el.on('keydown', function (e) {
if (e.keyCode === keys[progress[ename]]) {
if (++progress[ename] === keys.length) {
Y.fire(ename,e);
handlers[ename].dom.detach();
handlers[ename].proxy.detach();
delete handlers[ename];
}
} else {
progress[ename] = 0;
}
});
}

args[0] = ename;
args.splice(2,1);

handlers[ename].proxy = Y.on.apply(Y,args);
}
}
};

Y.Env.evt.plugins.konami = eventDef;
if (Y.Node) {
Y.Node.DOM_EVENTS.konami = eventDef;
}

0 comments on commit 66715e2

Please sign in to comment.