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

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: added flag to avoid multiple mousemove listeners 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
« no previous file with comments | « ui/webui/resources/cr_elements/cr_action_menu/compiled_resources2.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
11 * @private {?NodeList<!Element>} 11 * @private {?NodeList<!Element>}
12 */ 12 */
13 options_: null, 13 options_: null,
14 14
15 /** 15 /**
16 * The element which the action menu will be anchored to. Also the element 16 * The element which the action menu will be anchored to. Also the element
17 * where focus will be returned after the menu is closed. 17 * where focus will be returned after the menu is closed.
18 * @private {?Element} 18 * @private {?Element}
19 */ 19 */
20 anchorElement_: null, 20 anchorElement_: null,
21 21
22 /** 22 /**
23 * Bound reference to an event listener function such that it can be removed 23 * Bound reference to an event listener function such that it can be removed
24 * on detach. 24 * on detach.
25 * @private {?Function} 25 * @private {?Function}
26 */ 26 */
27 boundClose_: null, 27 boundClose_: null,
28 28
29 /** @private {boolean} */
30 hasMousemoveListener_: false,
31
29 hostAttributes: { 32 hostAttributes: {
30 tabindex: 0, 33 tabindex: 0,
31 }, 34 },
32 35
33 listeners: { 36 listeners: {
34 'keydown': 'onKeyDown_', 37 'keydown': 'onKeyDown_',
38 'mouseover': 'onMouseover_',
35 'tap': 'onTap_', 39 'tap': 'onTap_',
36 }, 40 },
37 41
38 /** override */ 42 /** override */
39 attached: function() { 43 attached: function() {
40 this.options_ = this.querySelectorAll('.dropdown-item'); 44 this.options_ = this.querySelectorAll('.dropdown-item');
41 }, 45 },
42 46
43 /** override */ 47 /** override */
44 detached: function() { 48 detached: function() {
(...skipping 21 matching lines...) Expand all
66 * @param {!KeyboardEvent} e 70 * @param {!KeyboardEvent} e
67 * @private 71 * @private
68 */ 72 */
69 onKeyDown_: function(e) { 73 onKeyDown_: function(e) {
70 if (e.key == 'Tab' || e.key == 'Escape') { 74 if (e.key == 'Tab' || e.key == 'Escape') {
71 this.close(); 75 this.close();
72 e.preventDefault(); 76 e.preventDefault();
73 return; 77 return;
74 } 78 }
75 79
80 if (!this.hasMousemoveListener_) {
81 this.hasMousemoveListener_ = true;
82 listenOnce(this, 'mousemove', this.onMouseover_.bind(this));
Dan Beam 2017/04/06 03:03:03 this is fairly similar and easier to comprehend, I
scottchen 2017/04/06 17:41:36 Not intentional, updated.
83 }
84
76 if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') 85 if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp')
77 return; 86 return;
78 87
79 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : -1); 88 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : -1);
80 if (nextOption) 89 if (nextOption)
81 nextOption.focus(); 90 nextOption.focus();
82 91
83 e.preventDefault(); 92 e.preventDefault();
84 }, 93 },
85 94
86 /** 95 /**
96 * @param {!Event} e
97 * @private
98 */
99 onMouseover_: function(e) {
100 this.hasMousemoveListener_ = false;
101 // TODO(scottchen): Using "focus" to determine selected item might mess
102 // with screen readers in some edge cases.
103 var i = 0;
104 do {
105 var target = e.path[i++];
106 if (target.classList && target.classList.contains('dropdown-item')) {
107 target.focus();
108 return;
109 }
110 } while (this != target);
111
112 // The user moved the mouse off the options. Reset focus to the dialog.
113 this.focus();
114 },
115
116 /**
87 * @param {number} step -1 for getting previous option (up), 1 for getting 117 * @param {number} step -1 for getting previous option (up), 1 for getting
88 * next option (down). 118 * next option (down).
89 * @return {?Element} The next focusable option, taking into account 119 * @return {?Element} The next focusable option, taking into account
90 * disabled/hidden attributes, or null if no focusable option exists. 120 * disabled/hidden attributes, or null if no focusable option exists.
91 * @private 121 * @private
92 */ 122 */
93 getNextOption_: function(step) { 123 getNextOption_: function(step) {
94 // Using a counter to ensure no infinite loop occurs if all elements are 124 // Using a counter to ensure no infinite loop occurs if all elements are
95 // hidden/disabled. 125 // hidden/disabled.
96 var counter = 0; 126 var counter = 0;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 // Attempt to show the menu starting from the top of the rectangle and 181 // 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 182 // extending downwards. If that does not fit within the window, fallback to
153 // starting from the bottom and extending upwards. 183 // starting from the bottom and extending upwards.
154 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top : 184 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top :
155 rect.bottom - 185 rect.bottom -
156 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0); 186 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0);
157 187
158 this.style.top = top + 'px'; 188 this.style.top = top + 'px';
159 }, 189 },
160 }); 190 });
OLDNEW
« no previous file with comments | « ui/webui/resources/cr_elements/cr_action_menu/compiled_resources2.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698