Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: ui/webui/resources/cr_elements/cr_action_menu/cr_action_menu.js

Issue 2801453002: MD Settings: mouse movements should focus cr-action-menu items (Closed)
Patch Set: Merge branch 'iron-test-helpers' into hover-menu Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 Polymer({ 5 Polymer({
6 is: 'cr-action-menu', 6 is: 'cr-action-menu',
7 extends: 'dialog', 7 extends: 'dialog',
8 8
9 /** 9 /**
10 * List of all options in this action menu. 10 * List of all options in this action menu.
(...skipping 14 matching lines...) Expand all
25 * @private {?Function} 25 * @private {?Function}
26 */ 26 */
27 boundClose_: null, 27 boundClose_: null,
28 28
29 hostAttributes: { 29 hostAttributes: {
30 tabindex: 0, 30 tabindex: 0,
31 }, 31 },
32 32
33 listeners: { 33 listeners: {
34 'keydown': 'onKeyDown_', 34 'keydown': 'onKeyDown_',
35 'mousemove': 'onMouseMove_',
35 'tap': 'onTap_', 36 'tap': 'onTap_',
36 }, 37 },
37 38
38 /** override */ 39 /** override */
39 attached: function() { 40 attached: function() {
40 this.options_ = this.querySelectorAll('.dropdown-item'); 41 this.options_ = this.querySelectorAll('.dropdown-item');
41 }, 42 },
42 43
43 /** override */ 44 /** override */
44 detached: function() { 45 detached: function() {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 return; 78 return;
78 79
79 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : -1); 80 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : -1);
80 if (nextOption) 81 if (nextOption)
81 nextOption.focus(); 82 nextOption.focus();
82 83
83 e.preventDefault(); 84 e.preventDefault();
84 }, 85 },
85 86
86 /** 87 /**
88 * @param {!MouseEvent} e
89 * @private
90 */
91 onMouseMove_: function(e) {
92 var target = e.target;
Dan Beam 2017/04/05 03:29:21 i'm still a little confused. I don't think this i
scottchen 2017/04/05 20:56:31 Acknowledged the concern, I've modified the code t
93
94 if (target.classList.contains('dropdown-item') &&
Dan Beam 2017/04/04 23:41:13 does this move if you move the mouse inside of a <
scottchen 2017/04/04 23:48:45 yes, it focuses the checkbox (ripple shown around
Dan Beam 2017/04/04 23:49:59 don't you actually want it to focus the item, not
scottchen 2017/04/05 00:08:19 The top-most item *is* the <paper-checkbox>, which
Dan Beam 2017/04/05 03:29:21 ok
95 target != document.activeElement) {
96 target.focus();
97 } else {
98 this.focus(); // Blur option focus but keep up/down button working.
Dan Beam 2017/04/05 03:29:21 why is this needed? keydown should be fine as lon
scottchen 2017/04/05 20:56:31 It's more about blurring the option focus. The com
scottchen 2017/04/05 20:58:10 Also (noting for other readers) we discussed offli
99 }
100 },
101
102 /**
87 * @param {number} step -1 for getting previous option (up), 1 for getting 103 * @param {number} step -1 for getting previous option (up), 1 for getting
88 * next option (down). 104 * next option (down).
89 * @return {?Element} The next focusable option, taking into account 105 * @return {?Element} The next focusable option, taking into account
90 * disabled/hidden attributes, or null if no focusable option exists. 106 * disabled/hidden attributes, or null if no focusable option exists.
91 * @private 107 * @private
92 */ 108 */
93 getNextOption_: function(step) { 109 getNextOption_: function(step) {
94 // Using a counter to ensure no infinite loop occurs if all elements are 110 // Using a counter to ensure no infinite loop occurs if all elements are
95 // hidden/disabled. 111 // hidden/disabled.
96 var counter = 0; 112 var counter = 0;
97 var nextOption = null; 113 var nextOption = null;
98 var numOptions = this.options_.length; 114 var numOptions = this.options_.length;
99 var focusedIndex = 115 var focusedIndex =
100 Array.prototype.indexOf.call(this.options_, this.root.activeElement); 116 Array.prototype.indexOf.call(this.options_, this.root.activeElement);
101 117
118 // Avoid off-by-one when nothing is focused and up is pressed.
119 if (focusedIndex === -1 && step === -1)
120 focusedIndex = 0;
121
102 do { 122 do {
103 focusedIndex = (numOptions + focusedIndex + step) % numOptions; 123 focusedIndex = (numOptions + focusedIndex + step) % numOptions;
104 nextOption = this.options_[focusedIndex]; 124 nextOption = this.options_[focusedIndex];
105 if (nextOption.disabled || nextOption.hidden) 125 if (nextOption.disabled || nextOption.hidden)
106 nextOption = null; 126 nextOption = null;
107 counter++; 127 counter++;
108 } while (!nextOption && counter < numOptions); 128 } while (!nextOption && counter < numOptions);
109 129
110 return nextOption; 130 return nextOption;
111 }, 131 },
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 // Attempt to show the menu starting from the top of the rectangle and 171 // Attempt to show the menu starting from the top of the rectangle and
152 // extending downwards. If that does not fit within the window, fallback to 172 // extending downwards. If that does not fit within the window, fallback to
153 // starting from the bottom and extending upwards. 173 // starting from the bottom and extending upwards.
154 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top : 174 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top :
155 rect.bottom - 175 rect.bottom -
156 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0); 176 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0);
157 177
158 this.style.top = top + 'px'; 178 this.style.top = top + 'px';
159 }, 179 },
160 }); 180 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698