Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @param {{lastFocused: Object}} listItem | |
|
Dan Beam
2017/03/18 00:09:56
why {lastFocused: Object} instead of just an Eleme
scottchen
2017/03/18 01:05:47
I believe this will make sure that the element pas
| |
| 7 * @constructor | |
| 8 * @implements {cr.ui.FocusRow.Delegate} | |
| 9 */ | |
| 10 function FocusRowDelegate(listItem) { | |
| 11 this.listItem = listItem; | |
|
Dan Beam
2017/03/18 00:09:56
can this be @private?
scottchen
2017/03/18 01:05:47
Done.
| |
| 12 } | |
| 13 | |
| 14 FocusRowDelegate.prototype = { | |
| 15 /** | |
| 16 * @override | |
| 17 * @param {!cr.ui.FocusRow} row | |
| 18 * @param {!Event} e | |
| 19 */ | |
| 20 onFocus: function(row, e) { | |
| 21 this.listItem.lastFocused = e.path[0]; | |
| 22 }, | |
| 23 | |
| 24 /** | |
| 25 * @override | |
| 26 * @param {!cr.ui.FocusRow} row The row that detected a keydown. | |
| 27 * @param {!Event} e | |
| 28 * @return {boolean} Whether the event was handled. | |
| 29 */ | |
| 30 onKeydown: function(row, e) { | |
| 31 // Prevent iron-list from changing the focus on enter. | |
| 32 if (e.key == 'Enter') | |
| 33 e.stopPropagation(); | |
| 34 | |
| 35 return false; | |
| 36 }, | |
| 37 }; | |
| 38 | |
| 39 /** | |
| 40 * @param {!Element} root | |
| 41 * @param {cr.ui.FocusRow.Delegate} delegate | |
| 42 * @constructor | |
| 43 * @extends {cr.ui.FocusRow} | |
| 44 */ | |
| 45 function VirtualFocusRow(root, delegate) { | |
| 46 cr.ui.FocusRow.call(this, root, /* boundary */ null, delegate); | |
| 47 } | |
| 48 | |
| 49 VirtualFocusRow.prototype = { | |
| 50 __proto__: cr.ui.FocusRow.prototype | |
| 51 }; | |
| 52 | |
| 53 | |
| 54 /** | |
| 55 * Any element that is being used as an iron-list row item can extend this | |
| 56 * behavior, which encapsulates focus controls of mouse and keyboards. | |
| 57 * To use this behavior: | |
| 58 * - The parent element should pass a "last-focused" attribute double-bound | |
| 59 * to the row items, to track the last-focused element across rows. | |
| 60 * - There must be a container in the extending element with the | |
| 61 * [focus-row-container] attribute that contains all focusable controls. | |
| 62 * - On each of the focusable controls, there must be a [focus-row-control] | |
| 63 * attribute, and a [type=] attribute unique for each type of control. | |
| 64 * | |
| 65 * @polymerBehavior | |
| 66 */ | |
| 67 var FocusRowBehavior = { | |
| 68 properties: { | |
| 69 /** @private {VirtualFocusRow} */ | |
| 70 row_: Object, | |
| 71 | |
| 72 /** @private {boolean} */ | |
| 73 mouseFocused_: Boolean, | |
| 74 | |
| 75 /** @type {Element} */ | |
| 76 lastFocused: { | |
| 77 type: Object, | |
| 78 notify: true, | |
| 79 }, | |
| 80 | |
| 81 /** @type {number} */ | |
| 82 ironListTabIndex: { | |
| 83 type: Number, | |
| 84 observer: 'ironListTabIndexChanged_', | |
| 85 }, | |
| 86 }, | |
| 87 | |
| 88 /** @override */ | |
| 89 attached: function() { | |
| 90 this.classList.add('no-outline'); | |
| 91 | |
| 92 Polymer.RenderStatus.afterNextRender(this, function() { | |
| 93 var rowContainer = this.root.querySelector('[focus-row-container]'); | |
| 94 assert(!!rowContainer); | |
| 95 this.row_ = new VirtualFocusRow(rowContainer, new FocusRowDelegate(this)); | |
| 96 this.row_.makeActive(this.ironListTabIndex == 0); | |
|
Dan Beam
2017/03/18 00:09:56
nit: this.ironListTabIndexChanged_();
scottchen
2017/03/18 01:05:47
Done.
| |
| 97 this.addItems_(); | |
| 98 | |
| 99 // Adding listeners asynchronously to reduce blocking time, since this | |
| 100 // behavior will be used by items in potentially long lists. | |
| 101 this.listen(this, 'focus', 'onFocus_'); | |
| 102 this.listen(this, 'dom-change', 'onDomChange_'); | |
|
hcarmona
2017/03/17 23:55:51
I think you forgot to change it here
scottchen
2017/03/18 01:05:47
Done.
| |
| 103 this.listen(this, 'mousedown', 'onMouseDown_'); | |
| 104 this.listen(this, 'blur', 'onBlur_'); | |
| 105 }); | |
| 106 }, | |
| 107 | |
| 108 /** @override */ | |
| 109 detached: function() { | |
| 110 this.unlisten(this, 'focus', 'onFocus_'); | |
| 111 this.unlisten(this, 'dom-change', 'addItems_'); | |
| 112 this.unlisten(this, 'mousedown', 'onMouseDown_'); | |
| 113 this.unlisten(this, 'blur', 'onBlur_'); | |
| 114 if (this.row_) | |
| 115 this.row_.destroy(); | |
| 116 }, | |
| 117 | |
| 118 /** @private */ | |
| 119 addItems_: function() { | |
| 120 if (this.row_) { | |
| 121 this.row_.destroy(); | |
| 122 | |
| 123 var controls = this.root.querySelectorAll('[focus-row-control]'); | |
| 124 | |
| 125 for (var i = 0; i < controls.length; i++) { | |
| 126 this.row_.addItem( | |
| 127 controls[i].getAttribute('type'), | |
| 128 /** @type {HTMLElement} */ (controls[i])); | |
| 129 } | |
| 130 } | |
| 131 }, | |
| 132 | |
| 133 /** @private */ | |
| 134 onFocus_: function() { | |
| 135 if (this.mouseFocused_) { | |
| 136 this.mouseFocused_ = false; // Consume and reset flag. | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 if (this.lastFocused) | |
| 141 this.row_.getEquivalentElement(this.lastFocused).focus(); | |
| 142 else { | |
|
Dan Beam
2017/03/18 00:09:56
needs curlies for if
if (this.lastFocused) {
} e
scottchen
2017/03/18 01:05:47
Style guide says "Omit curly braces for single-lin
hcarmona
2017/03/20 18:32:50
+1 to dbeam's comment. I don't think the coding st
scottchen
2017/03/20 21:42:07
Acknowledged.
| |
| 143 var firstFocusable = assert(this.row_.getFirstFocusable()); | |
| 144 firstFocusable.focus(); | |
| 145 } | |
| 146 | |
| 147 this.tabIndex = -1; | |
| 148 }, | |
| 149 | |
| 150 /** @private */ | |
| 151 ironListTabIndexChanged_: function() { | |
| 152 if (this.row_) | |
| 153 this.row_.makeActive(this.ironListTabIndex == 0); | |
| 154 }, | |
| 155 | |
| 156 /** @private */ | |
| 157 onDomChange_: function() { | |
|
hcarmona
2017/03/17 23:55:51
You can also delete the function if you're not usi
scottchen
2017/03/18 01:05:47
Done.
| |
| 158 this.addItems_(); | |
| 159 }, | |
| 160 | |
| 161 /** @private */ | |
| 162 onMouseDown_: function() { | |
| 163 this.mouseFocused_ = true; // Set flag to not do any control-focusing. | |
| 164 }, | |
| 165 | |
| 166 /** @private */ | |
| 167 onBlur_: function() { | |
| 168 this.mouseFocused_ = false; // Reset flag since it's not active anymore. | |
| 169 } | |
| 170 }; | |
| OLD | NEW |