Chromium Code Reviews| 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 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 Loading... | |
| 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 'mouseover': 'onMouseover_', | |
| 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 21 matching lines...) Expand all Loading... | |
| 66 * @param {!KeyboardEvent} e | 67 * @param {!KeyboardEvent} e |
| 67 * @private | 68 * @private |
| 68 */ | 69 */ |
| 69 onKeyDown_: function(e) { | 70 onKeyDown_: function(e) { |
| 70 if (e.key == 'Tab' || e.key == 'Escape') { | 71 if (e.key == 'Tab' || e.key == 'Escape') { |
| 71 this.close(); | 72 this.close(); |
| 72 e.preventDefault(); | 73 e.preventDefault(); |
| 73 return; | 74 return; |
| 74 } | 75 } |
| 75 | 76 |
| 77 listenOnce(this, 'mousemove', this.onMouseover_.bind(this)); | |
|
dpapad
2017/04/06 00:06:02
We are leaking mousemove listeners. When the user
Dan Beam
2017/04/06 00:08:35
sure, that makes sense
scottchen
2017/04/06 00:44:33
Done.
| |
| 78 | |
| 76 if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') | 79 if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') |
| 77 return; | 80 return; |
| 78 | 81 |
| 79 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : -1); | 82 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : -1); |
| 80 if (nextOption) | 83 if (nextOption) |
| 81 nextOption.focus(); | 84 nextOption.focus(); |
| 82 | 85 |
| 83 e.preventDefault(); | 86 e.preventDefault(); |
| 84 }, | 87 }, |
| 85 | 88 |
| 86 /** | 89 /** |
| 90 * @param {!MouseEvent} e | |
| 91 * @private | |
| 92 */ | |
| 93 onMouseover_: function(e) { | |
| 94 // TODO(scottchen): Using "focus" to determine selected item might mess | |
| 95 // with screen readers in some edge cases. | |
| 96 var target = e.path[0]; | |
| 97 for (var i = 0; this != target; target = e.path[i++]) { | |
|
Dan Beam
2017/04/05 23:42:30
nit:
var i = 0;
for (var target; this != target;
scottchen
2017/04/06 00:44:33
I changed it to a do-while for readability.
| |
| 98 if (target.classList && target.classList.contains('dropdown-item')) { | |
| 99 target.focus(); | |
| 100 return; | |
| 101 } | |
| 102 }; | |
| 103 | |
| 104 // The user moved the mouse off the options. Reset focus to the dialog. | |
| 105 this.focus(); | |
| 106 }, | |
| 107 | |
| 108 /** | |
| 87 * @param {number} step -1 for getting previous option (up), 1 for getting | 109 * @param {number} step -1 for getting previous option (up), 1 for getting |
| 88 * next option (down). | 110 * next option (down). |
| 89 * @return {?Element} The next focusable option, taking into account | 111 * @return {?Element} The next focusable option, taking into account |
| 90 * disabled/hidden attributes, or null if no focusable option exists. | 112 * disabled/hidden attributes, or null if no focusable option exists. |
| 91 * @private | 113 * @private |
| 92 */ | 114 */ |
| 93 getNextOption_: function(step) { | 115 getNextOption_: function(step) { |
| 94 // Using a counter to ensure no infinite loop occurs if all elements are | 116 // Using a counter to ensure no infinite loop occurs if all elements are |
| 95 // hidden/disabled. | 117 // hidden/disabled. |
| 96 var counter = 0; | 118 var counter = 0; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 // Attempt to show the menu starting from the top of the rectangle and | 173 // 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 | 174 // extending downwards. If that does not fit within the window, fallback to |
| 153 // starting from the bottom and extending upwards. | 175 // starting from the bottom and extending upwards. |
| 154 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top : | 176 var top = rect.top + this.offsetHeight <= window.innerHeight ? rect.top : |
| 155 rect.bottom - | 177 rect.bottom - |
| 156 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0); | 178 this.offsetHeight - Math.max(rect.bottom - window.innerHeight, 0); |
| 157 | 179 |
| 158 this.style.top = top + 'px'; | 180 this.style.top = top + 'px'; |
| 159 }, | 181 }, |
| 160 }); | 182 }); |
| OLD | NEW |