Chromium Code Reviews| 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 'tap': 'onTap_', | |
| 34 'keydown': 'onKeyDown_', | |
|
Dan Beam
2016/10/14 23:31:06
nit: alpha
dpapad
2016/10/15 01:02:09
Meh, done.
| |
| 35 }, | |
| 36 | |
| 37 /** override */ | |
| 38 attached: function() { | |
| 39 this.options_ = this.querySelectorAll('.dropdown-item'); | |
| 40 | |
| 41 this.onWindowResize_ = function() { | |
| 42 if (this.open) | |
| 43 this.close(); | |
| 44 }.bind(this); | |
| 45 | |
|
Dan Beam
2016/10/14 23:31:06
remove \n
dpapad
2016/10/15 01:02:09
Done.
| |
| 46 }, | |
| 47 | |
| 48 /** override */ | |
| 49 detached: function() { | |
| 50 window.removeEventListener('resize', this.onWindowResize_); | |
| 51 }, | |
| 52 | |
| 53 /** | |
| 54 * @param {!Event} e | |
| 55 * @private | |
| 56 */ | |
| 57 onTap_: function(e) { | |
| 58 if (e.target == this) { | |
| 59 this.close(); | |
| 60 e.stopPropagation(); | |
| 61 } | |
| 62 }, | |
| 63 | |
| 64 /** | |
| 65 * @param {!KeyboardEvent} e | |
| 66 * @private | |
| 67 */ | |
| 68 onKeyDown_: function(e) { | |
| 69 if (e.key == 'Tab') { | |
| 70 this.close(); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') | |
| 75 return; | |
| 76 | |
| 77 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : - 1); | |
| 78 if (nextOption) | |
| 79 nextOption.focus(); | |
| 80 | |
| 81 e.preventDefault(); | |
| 82 }, | |
| 83 | |
| 84 /** | |
| 85 * @param {number} step -1 for getting previous option (up), 1 for getting | |
| 86 * next option (down). | |
| 87 * @return {?Element} The next focusable option, taking into account | |
| 88 * disabled/hidden attributes, or null if no focusable option exists. | |
| 89 * @private | |
| 90 */ | |
| 91 getNextOption_: function(step) { | |
| 92 // Using a counter to ensure no infinite loop occurs if all elements are | |
| 93 // hidden/disabled. | |
| 94 var counter = 0; | |
| 95 var nextOption = null; | |
| 96 var numOptions = this.options_.length; | |
| 97 | |
| 98 do { | |
| 99 this.focusedIndex_ = | |
| 100 (numOptions + this.focusedIndex_ + step) % numOptions; | |
| 101 nextOption = this.options_[this.focusedIndex_]; | |
| 102 if (nextOption.disabled || nextOption.hidden) | |
| 103 nextOption = null; | |
| 104 counter++; | |
| 105 } while (!nextOption && counter < numOptions); | |
| 106 | |
| 107 return nextOption; | |
| 108 }, | |
| 109 | |
| 110 /** @override */ | |
| 111 close: function() { | |
| 112 // Removing 'resize' listener when dialog is closed. | |
| 113 window.removeEventListener('resize', this.onWindowResize_); | |
|
Dan Beam
2016/10/14 23:31:06
nit: call this.detached() or make a removeResizeLi
dpapad
2016/10/15 01:02:09
I prefer not to call a lifecycle method manually,
| |
| 114 HTMLDialogElement.prototype.close.call(this); | |
| 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 | |
|
Dan Beam
2016/10/14 23:31:06
nit: do this here and only if you haven't already
dpapad
2016/10/15 01:02:09
Done.
| |
| 139 window.addEventListener('resize', this.onWindowResize_); | |
| 140 this.showModal(); | |
| 141 | |
| 142 if (new settings.DirectionDelegateImpl().isRtl()) { | |
| 143 var right = window.innerWidth - rect.left - this.offsetWidth; | |
| 144 this.style.right = right + 'px'; | |
| 145 } else { | |
| 146 var left = rect.right - this.offsetWidth; | |
| 147 this.style.left = left + 'px'; | |
| 148 } | |
| 149 | |
| 150 // Attempt to show the menu starting from the top of the rectangle and | |
| 151 // extending downwards. If that does not fit within the window, fallback to | |
| 152 // starting from the bottom and extending upwards. | |
| 153 var top = rect.top + this.offsetHeight <= window.innerHeight ? | |
| 154 rect.top : | |
| 155 rect.bottom - this.offsetHeight - Math.max( | |
| 156 rect.bottom - window.innerHeight, 0); | |
| 157 | |
| 158 this.style.top = top + 'px'; | |
| 159 }, | |
| 160 }); | |
| OLD | NEW |