-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkonamiz.js
78 lines (73 loc) · 1.85 KB
/
konamiz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var Konamiz = (function(){
function addEvent(obj, eventName, callback){
if (obj.addEventListener){
obj.addEventListener(eventName, callback, false);
} else if (obj.attachEvent){
obj.attachEvent("on"+eventName, function(e){ return callback.call(obj, e); });
}
}
function getCustomSuit(suit){
var _suit = '';
for (var i = 0; i < suit.length; i++) {
var _keyCode = suit[i].toUpperCase();
if(typeof SPECIAL_KEYCODES[_keyCode] !== 'undefined'){
_suit += SPECIAL_KEYCODES[_keyCode];
}
else{
_suit += _keyCode.charCodeAt(0);
}
}
return _suit;
}
var SPECIAL_KEYCODES = {
'SPACE' : 32,
'ENTER' : 13,
'UP' : 38,
'DOWN' : 40,
'LEFT' : 37,
'RIGHT' : 39
};
var konamizSuit = '3838404037393739666513';
return function(suit){
var myOnStart = function(){};
var myOnStop = function(){};
var mySuit = (suit instanceof Array && suit.length > 0) ? getCustomSuit(suit) : konamizSuit;
var myCurrentSuit = '';
(function(that){
addEvent(document, 'keydown', function(e){
myCurrentSuit += e.keyCode;
if(myCurrentSuit === mySuit){
myCurrentSuit = '';
if(!that.isStarted){
that.start();
}
else{
that.stop();
}
}
else if(mySuit.indexOf(myCurrentSuit) === -1){
myCurrentSuit = e.keyCode + '';
}
});
})(this);
this.isStarted = false;
this.onStart = function(fn){
myOnStart = fn;
return this;
};
this.onStop = function(fn){
myOnStop = fn;
return this;
};
this.start = function(){
myOnStart.call(this);
this.isStarted = true;
return this;
};
this.stop = function(){
myOnStop.call(this);
this.isStarted = false;
return this;
};
}
})();