OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview 'cr-profile-avatar-selector-grid' is an accessible control for | 6 * @fileoverview 'cr-profile-avatar-selector-grid' is an accessible control for |
7 * profile avatar icons that allows keyboard navigation with all arrow keys. | 7 * profile avatar icons that allows keyboard navigation with all arrow keys. |
8 */ | 8 */ |
9 | 9 |
10 Polymer({ | 10 Polymer({ |
11 is: 'cr-profile-avatar-selector-grid', | 11 is: 'cr-profile-avatar-selector-grid', |
12 | 12 |
13 behaviors: [ | 13 behaviors: [ |
14 Polymer.IronMenubarBehavior, | 14 Polymer.IronMenubarBehavior, |
15 ], | 15 ], |
16 | 16 |
17 properties: { | |
18 ignoreModifiedKeyEvents: { | |
19 type: Boolean, | |
20 value: false, | |
21 }, | |
22 }, | |
23 | |
24 /** | |
25 * Handler that is called when left arrow key is pressed. Overrides | |
26 * IronMenubarBehaviorImpl#_onLeftKey. | |
27 * @param {!CustomEvent} event | |
28 * @private | |
29 */ | |
30 _onLeftKey: function(event) { | |
31 if (this.ignoreModifiedKeyEvents && this.hasKeyModifiers_(event)) | |
Moe
2016/12/20 19:07:40
nit: please add a comment why this is being overri
Dan Beam
2016/12/20 19:31:15
Done.
| |
32 return; | |
33 Polymer.IronMenubarBehaviorImpl._onLeftKey.call(this, event); | |
34 }, | |
35 | |
36 /** | |
37 * Handler that is called when right arrow key is pressed. Overrides | |
38 * IronMenubarBehaviorImpl#_onRightKey. | |
39 * @param {!CustomEvent} event | |
40 * @private | |
41 */ | |
42 _onRightKey: function(event) { | |
43 if (this.ignoreModifiedKeyEvents && this.hasKeyModifiers_(event)) | |
44 return; | |
45 Polymer.IronMenubarBehaviorImpl._onRightKey.call(this, event); | |
46 }, | |
47 | |
17 /** | 48 /** |
18 * Handler that is called when the up arrow key is pressed. | 49 * Handler that is called when the up arrow key is pressed. |
19 * @param {CustomEvent} event A key combination event. | 50 * @param {CustomEvent} event A key combination event. |
20 * @private | 51 * @private |
21 */ | 52 */ |
22 _onUpKey: function(event) { | 53 _onUpKey: function(event) { |
23 this.moveFocusRow_(-1); | 54 this.moveFocusRow_(-1); |
24 event.detail.keyboardEvent.preventDefault(); | 55 event.detail.keyboardEvent.preventDefault(); |
25 }, | 56 }, |
26 | 57 |
(...skipping 10 matching lines...) Expand all Loading... | |
37 /** | 68 /** |
38 * Handler that is called when the esc key is pressed. | 69 * Handler that is called when the esc key is pressed. |
39 * @param {CustomEvent} event A key combination event. | 70 * @param {CustomEvent} event A key combination event. |
40 * @private | 71 * @private |
41 */ | 72 */ |
42 _onEscKey: function(event) { | 73 _onEscKey: function(event) { |
43 // Override the original behavior by doing nothing. | 74 // Override the original behavior by doing nothing. |
44 }, | 75 }, |
45 | 76 |
46 /** | 77 /** |
78 * @param {!CustomEvent} event | |
79 * @return {boolean} Whether the key event has modifier keys pressed. | |
80 */ | |
81 hasKeyModifiers_: function(event) { | |
82 return hasKeyModifiers(assertInstanceof(event.detail.keyboardEvent, Event)); | |
83 }, | |
84 | |
85 /** | |
47 * Focuses an item on the same column as the currently focused item and on a | 86 * Focuses an item on the same column as the currently focused item and on a |
48 * row below or above the focus row by the given offset. Focus wraps if | 87 * row below or above the focus row by the given offset. Focus wraps if |
49 * necessary. | 88 * necessary. |
50 * @param {number} offset | 89 * @param {number} offset |
51 * @private | 90 * @private |
52 */ | 91 */ |
53 moveFocusRow_: function(offset) { | 92 moveFocusRow_: function(offset) { |
54 var length = this.items.length; | 93 var length = this.items.length; |
55 var style = getComputedStyle(this); | 94 var style = getComputedStyle(this); |
56 var avatarSpacing = | 95 var avatarSpacing = |
57 parseInt(style.getPropertyValue('--avatar-spacing'), 10); | 96 parseInt(style.getPropertyValue('--avatar-spacing'), 10); |
58 var avatarSize = parseInt(style.getPropertyValue('--avatar-size'), 10); | 97 var avatarSize = parseInt(style.getPropertyValue('--avatar-size'), 10); |
59 var rowSize = Math.floor(this.clientWidth / (avatarSpacing + avatarSize)); | 98 var rowSize = Math.floor(this.clientWidth / (avatarSpacing + avatarSize)); |
60 var rows = Math.ceil(length / rowSize); | 99 var rows = Math.ceil(length / rowSize); |
61 var gridSize = rows * rowSize; | 100 var gridSize = rows * rowSize; |
62 var focusIndex = this.indexOf(this.focusedItem); | 101 var focusIndex = this.indexOf(this.focusedItem); |
63 for (var i = offset; Math.abs(i) <= rows; i += offset) { | 102 for (var i = offset; Math.abs(i) <= rows; i += offset) { |
64 var item = this.items[(focusIndex + i * rowSize + gridSize) % gridSize]; | 103 var item = this.items[(focusIndex + i * rowSize + gridSize) % gridSize]; |
65 if (!item) | 104 if (!item) |
66 continue; | 105 continue; |
67 this._setFocusedItem(item); | 106 this._setFocusedItem(item); |
68 | 107 |
69 assert(Polymer.dom(item).getOwnerRoot().activeElement == item); | 108 assert(Polymer.dom(item).getOwnerRoot().activeElement == item); |
70 return; | 109 return; |
71 } | 110 } |
72 } | 111 } |
73 }); | 112 }); |
OLD | NEW |