| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 Polymer({ |
| 6 is: 'settings-action-menu', |
| 7 extends: 'dialog', |
| 8 |
| 9 /** |
| 10 * List of all options in this action menu. |
| 11 * @private {?NodeList<!Element>} |
| 12 */ |
| 13 options_: null, |
| 14 |
| 15 /** |
| 16 * Index of the currently focused item. |
| 17 * @private {number} |
| 18 */ |
| 19 focusedIndex_: -1, |
| 20 |
| 21 /** |
| 22 * Reference to the bound window's resize listener, such that it can be |
| 23 * removed on detach. |
| 24 * @private {?Function} |
| 25 */ |
| 26 onWindowResize_: null, |
| 27 |
| 28 hostAttributes: { |
| 29 tabindex: 0, |
| 30 }, |
| 31 |
| 32 listeners: { |
| 33 'keydown': 'onKeyDown_', |
| 34 'tap': 'onTap_', |
| 35 }, |
| 36 |
| 37 /** override */ |
| 38 attached: function() { |
| 39 this.options_ = this.querySelectorAll('.dropdown-item'); |
| 40 }, |
| 41 |
| 42 /** override */ |
| 43 detached: function() { |
| 44 this.removeResizeListener_(); |
| 45 }, |
| 46 |
| 47 /** @private */ |
| 48 removeResizeListener_: function() { |
| 49 window.removeEventListener('resize', this.onWindowResize_); |
| 50 }, |
| 51 |
| 52 /** |
| 53 * @param {!Event} e |
| 54 * @private |
| 55 */ |
| 56 onTap_: function(e) { |
| 57 if (e.target == this) { |
| 58 this.close(); |
| 59 e.stopPropagation(); |
| 60 } |
| 61 }, |
| 62 |
| 63 /** |
| 64 * @param {!KeyboardEvent} e |
| 65 * @private |
| 66 */ |
| 67 onKeyDown_: function(e) { |
| 68 if (e.key == 'Tab') { |
| 69 this.close(); |
| 70 return; |
| 71 } |
| 72 |
| 73 if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') |
| 74 return; |
| 75 |
| 76 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : - 1); |
| 77 if (nextOption) |
| 78 nextOption.focus(); |
| 79 |
| 80 e.preventDefault(); |
| 81 }, |
| 82 |
| 83 /** |
| 84 * @param {number} step -1 for getting previous option (up), 1 for getting |
| 85 * next option (down). |
| 86 * @return {?Element} The next focusable option, taking into account |
| 87 * disabled/hidden attributes, or null if no focusable option exists. |
| 88 * @private |
| 89 */ |
| 90 getNextOption_: function(step) { |
| 91 // Using a counter to ensure no infinite loop occurs if all elements are |
| 92 // hidden/disabled. |
| 93 var counter = 0; |
| 94 var nextOption = null; |
| 95 var numOptions = this.options_.length; |
| 96 |
| 97 do { |
| 98 this.focusedIndex_ = |
| 99 (numOptions + this.focusedIndex_ + step) % numOptions; |
| 100 nextOption = this.options_[this.focusedIndex_]; |
| 101 if (nextOption.disabled || nextOption.hidden) |
| 102 nextOption = null; |
| 103 counter++; |
| 104 } while (!nextOption && counter < numOptions); |
| 105 |
| 106 return nextOption; |
| 107 }, |
| 108 |
| 109 /** @override */ |
| 110 close: function() { |
| 111 // Removing 'resize' listener when dialog is closed. |
| 112 this.removeResizeListener_(); |
| 113 HTMLDialogElement.prototype.close.call(this); |
| 114 window.dispatchEvent(new CustomEvent('resize')); |
| 115 }, |
| 116 |
| 117 /** |
| 118 * Shows the menu anchored to the given element. |
| 119 * @param {!Element} anchorElement |
| 120 */ |
| 121 showAt: function(anchorElement) { |
| 122 var rect = anchorElement.getBoundingClientRect(); |
| 123 |
| 124 // Ensure that the correct item is focused when the dialog is shown, by |
| 125 // setting the 'autofocus' attribute. |
| 126 this.focusedIndex_ = -1; |
| 127 var nextOption = this.getNextOption_(1); |
| 128 |
| 129 /** @suppress {checkTypes} */ |
| 130 (function(options) { |
| 131 options.forEach(function(option) { |
| 132 option.removeAttribute('autofocus'); |
| 133 }); |
| 134 })(this.options_); |
| 135 |
| 136 if (nextOption) |
| 137 nextOption.setAttribute('autofocus', true); |
| 138 |
| 139 this.onWindowResize_ = this.onWindowResize_ || function() { |
| 140 if (this.open) |
| 141 this.close(); |
| 142 }.bind(this); |
| 143 window.addEventListener('resize', this.onWindowResize_); |
| 144 |
| 145 this.showModal(); |
| 146 |
| 147 if (new settings.DirectionDelegateImpl().isRtl()) { |
| 148 var right = window.innerWidth - rect.left - this.offsetWidth; |
| 149 this.style.right = right + 'px'; |
| 150 } else { |
| 151 var left = rect.right - this.offsetWidth; |
| 152 this.style.left = left + 'px'; |
| 153 } |
| 154 |
| 155 // Attempt to show the menu starting from the top of the rectangle and |
| 156 // extending downwards. If that does not fit within the window, fallback to |
| 157 // starting from the bottom and extending upwards. |
| 158 var top = rect.top + this.offsetHeight <= window.innerHeight ? |
| 159 rect.top : |
| 160 rect.bottom - this.offsetHeight - Math.max( |
| 161 rect.bottom - window.innerHeight, 0); |
| 162 |
| 163 this.style.top = top + 'px'; |
| 164 }, |
| 165 }); |
| OLD | NEW |