Chromium Code Reviews| Index: chrome/browser/resources/settings/settings_action_menu.js |
| diff --git a/chrome/browser/resources/settings/settings_action_menu.js b/chrome/browser/resources/settings/settings_action_menu.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2374ff35001d5f3a0241edcaccd1499a6b572138 |
| --- /dev/null |
| +++ b/chrome/browser/resources/settings/settings_action_menu.js |
| @@ -0,0 +1,143 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +Polymer({ |
| + is: 'settings-action-menu', |
| + extends: 'dialog', |
| + |
| + /** |
| + * List of all options in this action menu. |
| + * @private {?NodeList<!Element>} |
| + */ |
| + options_: null, |
| + |
| + /** |
| + * Index of the currently focused item. |
| + * @private {number} |
| + */ |
| + focusedIndex_: 0, |
| + |
| + /** |
| + * Reference to the bound window's resize listener, such that it can be |
| + * removed on detach. |
| + * @private {?Function} |
| + */ |
| + onWindowResize_: null, |
| + |
| + /** override */ |
| + attached: function() { |
| + this.setAttribute('tabindex', 0); |
| + this.options_ = this.querySelectorAll('.dropdown-item'); |
| + |
| + this.onWindowResize_ = function() { |
| + if (this.open) |
| + this.close(); |
| + }.bind(this); |
| + |
| + window.addEventListener('resize', this.onWindowResize_); |
| + }, |
| + |
| + /** override */ |
| + detached: function() { |
| + window.removeEventListener('resize', this.onWindowResize_); |
| + }, |
| + |
| + /** @override */ |
| + ready: function() { |
| + this.addEventListener('click', function(e) { |
| + if (e.target == this) { |
| + this.close(); |
| + e.stopPropagation(); |
| + } |
| + }.bind(this)); |
| + |
| + this.addEventListener('keydown', function(e) { |
| + if (e.key == 'Tab') { |
| + this.close(); |
| + return; |
| + } |
| + |
| + if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp' && |
| + e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') { |
| + return; |
| + } |
| + |
| + var nextOption = this.getNextOption_( |
| + e.key == 'ArrowDown' || e.key == 'ArrowRight' ? 1 : - 1); |
| + if (nextOption !== null) |
| + nextOption.focus(); |
| + |
| + e.preventDefault(); |
| + }.bind(this)); |
| + }, |
| + |
| + /** |
| + * @param {number} step -1 for getting previous option (up), 1 for getting |
| + * next option (down). |
| + * @return {?HTMLElement} The next focusable option, taking into account |
| + * disabled/hidden attributes, or null if no focusable option exists. |
| + * @private |
| + */ |
| + getNextOption_: function(step) { |
| + // Storing the focused index before searching, to avoid infinite loop if all |
| + // options are disabled. |
| + var previousFocusedIndex = this.focusedIndex_; |
| + |
| + var getNextOptionRec_ = function() { |
| + // Advance index. |
| + this.focusedIndex_ = (this.focusedIndex_ + step) % this.options_.length; |
| + if (this.focusedIndex_ == -1) |
| + this.focusedIndex_ = this.options_.length - 1; |
| + |
| + // Adjust index in case item is disabled/hidden. |
| + var nextOption = this.options_[this.focusedIndex_]; |
| + if (nextOption.disabled || nextOption.hidden) { |
| + return this.focusedIndex_ != previousFocusedIndex ? |
| + getNextOptionRec_() : null; |
| + } else { |
| + return this.options_[this.focusedIndex_]; |
| + } |
| + }.bind(this); |
| + |
| + return getNextOptionRec_(); |
| + }, |
| + |
| + /** |
| + * Shows the menu anchored to the given rectangle. |
| + * @param {!ClientRect} rect |
| + */ |
| + showAtLocation: function(rect) { |
| + // Ensure that the correct item is focused when the dialog is shown, by |
| + // setting the 'autofocus' attribute. |
| + this.focusedIndex_ = 0; |
| + var nextOption = this.options_[this.focusedIndex_]; |
| + if (nextOption.disabled || nextOption.hidden) |
| + nextOption = this.getNextOption_(1); |
| + for (var option of this.options_) |
| + option.removeAttribute('autofocus'); |
| + if (nextOption !== null) |
| + nextOption.setAttribute('autofocus', true); |
| + |
| + this.showModal(); |
| + |
| + var directionDelegate = new settings.DirectionDelegateImpl(); |
| + if (directionDelegate.isRtl()) { |
| + var right = window.innerWidth - rect.left - this.offsetWidth; |
| + this.style.right = right + 'px'; |
|
dpapad
2016/10/13 00:23:17
I don't know if this is a blink bug, but
- when R
Dan Beam
2016/10/13 00:28:34
no, that's known
|
| + } else { |
| + var left = rect.right - this.offsetWidth; |
| + this.style.left = left + 'px'; |
| + } |
| + |
| + // Attempt to show the menu starting from the top of the rectangle and |
| + // extending downwards. If that does not fit within the window, fallback to |
| + // starting from the bottom and extending upwards. |
| + var top = rect.top + this.offsetHeight <= window.innerHeight ? |
| + rect.top : |
| + rect.bottom - this.offsetHeight - Math.max( |
| + rect.bottom - window.innerHeight, 0); |
| + |
| + this.style.top = top + 'px'; |
| + }, |
| +}); |