This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
konami-code.html
90 lines (75 loc) · 2.1 KB
/
konami-code.html
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
79
80
81
82
83
84
85
86
87
88
89
90
<!--
@license
Copyright (c) 2016 Abdón Rodríguez Davila (@abdonrd). All rights reserved.
This code may only be used under the MIT style license found at https://abdonrd.github.io/LICENSE.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
<!--
Web Component (with Polymer) to listen the Konami Code (↑↑↓↓←→←→BA).
@demo demo/index.html
-->
<dom-module id="konami-code">
<script>
Polymer({
is: 'konami-code',
properties: {
_konamiCode: {
type: Array,
value: function() {
return [
'up',
'up',
'down',
'down',
'left',
'right',
'left',
'right',
'b',
'a'
];
}
},
_nextCodeIndex: {
type: Number,
value: 0
},
_keydownListener: {
type: Object
}
},
attached: function() {
this._keydownListener = this.__onBodyKeydown.bind(this);
document.body.addEventListener('keydown', this._keydownListener);
},
detached: function() {
document.body.removeEventListener('keydown', this._keydownListener);
},
__onBodyKeydown: function(event) {
if (this._isExpectedKey(event)) {
this._nextCodeIndex += 1;
} else {
this._nextCodeIndex = 0;
}
if (this._isCodeComplete()) {
this.fire('code-completed');
this._nextCodeIndex = 0;
}
},
_isExpectedKey: function(event) {
var expectedKey = this._konamiCode[this._nextCodeIndex];
// eslint-disable-next-line max-len
return Polymer.IronA11yKeysBehavior.keyboardEventMatchesKeys(event, expectedKey);
},
_isCodeComplete: function() {
return this._nextCodeIndex === this._konamiCode.length;
}
/**
* Fired when the code is completed.
*
* @event code-completed
*/
});
</script>
</dom-module>