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

Side by Side Diff: chrome/browser/resources/settings/settings_action_menu.js

Issue 2461113002: WebUI: Make settings-action-menu re-usable as cr-action-menu. (Closed)
Patch Set: getComputedStyle instead of util.js Created 4 years, 1 month 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
(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 * Reference to the bound window's resize listener, such that it can be
17 * removed on detach.
18 * @private {?Function}
19 */
20 onWindowResize_: null,
21
22 hostAttributes: {
23 tabindex: 0,
24 },
25
26 listeners: {
27 'keydown': 'onKeyDown_',
28 'tap': 'onTap_',
29 },
30
31 /** override */
32 attached: function() {
33 this.options_ = this.querySelectorAll('.dropdown-item');
34 },
35
36 /** override */
37 detached: function() {
38 this.removeResizeListener_();
39 },
40
41 /** @private */
42 removeResizeListener_: function() {
43 window.removeEventListener('resize', this.onWindowResize_);
44 },
45
46 /**
47 * @param {!Event} e
48 * @private
49 */
50 onTap_: function(e) {
51 if (e.target == this) {
52 this.close();
53 e.stopPropagation();
54 }
55 },
56
57 /**
58 * @param {!KeyboardEvent} e
59 * @private
60 */
61 onKeyDown_: function(e) {
62 if (e.key == 'Tab') {
63 this.close();
64 return;
65 }
66
67 if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp')
68 return;
69
70 var nextOption = this.getNextOption_(e.key == 'ArrowDown' ? 1 : - 1);
71 if (nextOption)
72 nextOption.focus();
73
74 e.preventDefault();
75 },
76
77 /**
78 * @param {number} step -1 for getting previous option (up), 1 for getting
79 * next option (down).
80 * @return {?Element} The next focusable option, taking into account
81 * disabled/hidden attributes, or null if no focusable option exists.
82 * @private
83 */
84 getNextOption_: function(step) {
85 // Using a counter to ensure no infinite loop occurs if all elements are
86 // hidden/disabled.
87 var counter = 0;
88 var nextOption = null;
89 var numOptions = this.options_.length;
90 var focusedIndex = Array.prototype.indexOf.call(
91 this.options_, this.root.activeElement);
92
93 do {
94 focusedIndex = (numOptions + focusedIndex + step) % numOptions;
95 nextOption = this.options_[focusedIndex];
96 if (nextOption.disabled || nextOption.hidden)
97 nextOption = null;
98 counter++;
99 } while (!nextOption && counter < numOptions);
100
101 return nextOption;
102 },
103
104 /** @override */
105 close: function() {
106 // Removing 'resize' listener when dialog is closed.
107 this.removeResizeListener_();
108 HTMLDialogElement.prototype.close.call(this);
109 },
110
111 /**
112 * Shows the menu anchored to the given element.
113 * @param {!Element} anchorElement
114 */
115 showAt: function(anchorElement) {
116 this.onWindowResize_ = this.onWindowResize_ || function() {
117 if (this.open)
118 this.close();
119 }.bind(this);
120 window.addEventListener('resize', this.onWindowResize_);
121
122 this.showModal();
123
124 var rect = anchorElement.getBoundingClientRect();
125 if (new settings.DirectionDelegateImpl().isRtl()) {
126 var right = window.innerWidth - rect.left - this.offsetWidth;
127 this.style.right = right + 'px';
128 } else {
129 var left = rect.right - this.offsetWidth;
130 this.style.left = left + 'px';
131 }
132
133 // Attempt to show the menu starting from the top of the rectangle and
134 // extending downwards. If that does not fit within the window, fallback to
135 // starting from the bottom and extending upwards.
136 var top = rect.top + this.offsetHeight <= window.innerHeight ?
137 rect.top :
138 rect.bottom - this.offsetHeight - Math.max(
139 rect.bottom - window.innerHeight, 0);
140
141 this.style.top = top + 'px';
142 },
143 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/settings/settings_action_menu.html ('k') | chrome/browser/resources/settings/settings_resources.grd » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698