-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-keyboard.html
98 lines (70 loc) · 2.62 KB
/
basic-keyboard.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
91
92
93
94
95
96
97
98
<!--
Aspect which manages the keyboard focus and keydown handling for a component
collective.
This aspect ensures that its only the outermost aspect in a collective that is
listening for keyboard events.
@element basic-keyboard
-->
<link rel="import" href="../basic-aspect/basic-aspect.html">
<script>
window.Basic = window.Basic || {};
window.Basic.Keyboard = {
contribute: {
/*
* When the collective changes, stop listening for keyboard events on
* whichever aspect was previously the outermost aspect, and start listening
* to keyboard events on whichever aspect is now the new outermost aspect.
*/
// TODO: Do we need to start/stop listening when attached/detached, or is
// that handled automatically?
collectiveChanged: function() {
var outermost = this.collective.outermostAttached;
if (outermost === this._previousOutermostAspect) {
// Should already be listening to events on the outermost aspect.
return;
}
if (this._previousOutermostAspect) {
// Clean up the previous aspect that was handling the keyboard.
if (this._previousTabIndex) {
// Restore previous tab index.
this._previousOutermostAspect.setAttribute('tabIndex', this._previousTabIndex);
} else {
// Aspect didn't have a tab index before, so remove it.
this._previousOutermostAspect.removeAttribute('tabIndex');
}
// Stop listening to events the previous outermost aspect.
this._previousOutermostAspect.removeEventListener('keydown', this._keydownHandler);
}
if (outermost.getAttribute('tabIndex')) {
// Leave existing tab index in place.
this._previousTabIndex = null;
} else {
// Make new outermost aspect focusable.
this._previousTabIndex = outermost.getAttribute('tabIndex');
outermost.setAttribute('tabIndex', 0);
}
// Start listening to events on the new outermost aspect.
if (!this._keydownHandler) {
this._keydownHandler = this._keydown.bind(this);
}
outermost.addEventListener('keydown', this._keydownHandler);
this._previousOutermostAspect = outermost;
},
// Default keydown handler. This will typically be handled by other aspects
// in the collective.
keydown: Basic.Collective.defaultMethod
},
name: 'Keyboard',
_keydown: function(event) {
return this.collective.keydown(event);
},
_keydownHandler: null,
_previousOutermostAspect: null,
_previousTabIndex: null
};
Polymer({
aspects: [Basic.Keyboard],
behaviors: [Basic.Aspect],
is: 'basic-keyboard'
});
</script>