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

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

Issue 2803543003: WebUI: fix cr-action-menu keyboard off-by-one issue (Closed)
Patch Set: add test 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 */ 117 */
118 getNextOption_: function(step) { 118 getNextOption_: function(step) {
119 // Using a counter to ensure no infinite loop occurs if all elements are 119 // Using a counter to ensure no infinite loop occurs if all elements are
120 // hidden/disabled. 120 // hidden/disabled.
121 var counter = 0; 121 var counter = 0;
122 var nextOption = null; 122 var nextOption = null;
123 var numOptions = this.options_.length; 123 var numOptions = this.options_.length;
124 var focusedIndex = 124 var focusedIndex =
125 Array.prototype.indexOf.call(this.options_, this.root.activeElement); 125 Array.prototype.indexOf.call(this.options_, this.root.activeElement);
126 126
127 // Avoid off-by-one when nothing is focused and up is pressed.
dpapad 2017/04/05 21:49:13 Nit (optional): Perhaps just rephrase to // Handle
scottchen 2017/04/06 17:54:02 Done.
128 if (focusedIndex === -1 && step === -1)
129 focusedIndex = 0;
130
127 do { 131 do {
128 focusedIndex = (numOptions + focusedIndex + step) % numOptions; 132 focusedIndex = (numOptions + focusedIndex + step) % numOptions;
129 nextOption = this.options_[focusedIndex]; 133 nextOption = this.options_[focusedIndex];
130 if (nextOption.disabled || nextOption.hidden) 134 if (nextOption.disabled || nextOption.hidden)
131 nextOption = null; 135 nextOption = null;
132 counter++; 136 counter++;
133 } while (!nextOption && counter < numOptions); 137 } while (!nextOption && counter < numOptions);
134 138
135 return nextOption; 139 return nextOption;
136 }, 140 },
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // Attempt to show the menu starting from the top of the rectangle and 180 // Attempt to show the menu starting from the top of the rectangle and
177 // extending downwards. If that does not fit within the window, fallback to 181 // extending downwards. If that does not fit within the window, fallback to
178 // starting from the bottom and extending upwards. 182 // starting from the bottom and extending upwards.
179 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top : 183 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top :
180 rect.bottom - 184 rect.bottom -
181 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0); 185 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0);
182 186
183 this.style.top = top + 'px'; 187 this.style.top = top + 'px';
184 }, 188 },
185 }); 189 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698