blob: 23b9dcf7107f2362542d65c9999ebdafcb720b7f [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001/* Copyright 2016 The Chromium Authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file or at
5 * https://developers.google.com/open-source/licenses/bsd
6 */
7
8/**
9 * Functions used by Monorail to control the display of elements on
10 * the page, rollovers, and popup menus.
11 *
12 */
13
14
15/**
16 * Show a popup menu below a specified element. Optional x and y deltas can be
17 * used to fine-tune placement.
18 * @param {string} id The HTML id of the popup menu.
19 * @param {Element} el The HTML element that the popup should appear near.
20 * @param {number} opt_deltaX Optional X offset to finetune placement.
21 * @param {number} opt_deltaY Optional Y offset to finetune placement.
22 * @param {Element} opt_menuButton The HTML element for a menu button that
23 * was pressed to open the menu. When a button was used, we need to ignore
24 * the first "click" event, otherwise the menu will immediately close.
25 * @return Always returns false to indicate that the browser should handle the
26 * event normally.
27 */
28function TKR_showBelow(id, el, opt_deltaX, opt_deltaY, opt_menuButton) {
29 let popupDiv = $(id);
30 let elBounds = nodeBounds(el);
31 let startX = elBounds.x;
32 let startY = elBounds.y + elBounds.h;
33 if (BR_IsIE()) {
34 startX -= 1;
35 startY -= 2;
36 }
37 if (BR_IsSafari()) {
38 startX += 1;
39 }
40 popupDiv.style.display = 'block'; // needed so that offsetWidth != 0
41
42 popupDiv.style.left = '-2000px';
43 if (id == 'pop_dot' || id == 'redoMenu') {
44 startX = startX - popupDiv.offsetWidth + el.offsetWidth;
45 }
46 if (opt_deltaX) startX += opt_deltaX;
47 if (opt_deltaY) startY += opt_deltaY;
48 popupDiv.style.left = (startX)+'px';
49 popupDiv.style.top = (startY)+'px';
50 let popup = new TKR_MyPopup(popupDiv, opt_menuButton);
51 popup.show();
52 return false;
53}
54
55
56/**
57 * Show a popup menu to the right of a specified element. If there is not
58 * enough space to the right, then it will open to the left side instead.
59 * Optional x and y deltas can be used to fine-tune placement.
60 * TODO(jrobbins): reduce redundancy with function above.
61 * @param {string} id The HTML id of the popup menu.
62 * @param {Element} el The HTML element that the popup should appear near.
63 * @param {number} opt_deltaX Optional X offset to finetune placement.
64 * @param {number} opt_deltaY Optional Y offset to finetune placement.
65 * @return Always returns false to indicate that the browser should handle the
66 * event normally.
67 */
68function TKR_showRight(id, el, opt_deltaX, opt_deltaY) {
69 let popupDiv = $(id);
70 let elBounds = nodeBounds(el);
71 let startX = elBounds.x + elBounds.w;
72 let startY = elBounds.y;
73
74 // Calculate pageSize.w and pageSize.h
75 let docElemWidth = document.documentElement.clientWidth;
76 let docElemHeight = document.documentElement.clientHeight;
77 let pageSize = {
78 w: (window.innerWidth || docElemWidth && docElemWidth > 0 ?
79 docElemWidth : document.body.clientWidth) || 1,
80 h: (window.innerHeight || docElemHeight && docElemHeight > 0 ?
81 docElemHeight : document.body.clientHeight) || 1,
82 };
83
84 // We need to make the popupDiv visible in order to capture its width
85 popupDiv.style.display = 'block';
86 let popupDivBounds = nodeBounds(popupDiv);
87
88 // Show popup to the left
89 if (startX + popupDivBounds.w > pageSize.w) {
90 startX = elBounds.x - popupDivBounds.w;
91 if (BR_IsIE()) {
92 startX -= 4;
93 startY -= 2;
94 }
95 if (BR_IsNav()) {
96 startX -= 2;
97 }
98 if (BR_IsSafari()) {
99 startX += -1;
100 }
101
102 // Show popup to the right
103 } else {
104 if (BR_IsIE()) {
105 startY -= 2;
106 }
107 if (BR_IsNav()) {
108 startX += 2;
109 }
110 if (BR_IsSafari()) {
111 startX += 3;
112 }
113 }
114
115 popupDiv.style.left = '-2000px';
116 popupDiv.style.position = 'absolute';
117 if (opt_deltaX) startX += opt_deltaX;
118 if (opt_deltaY) startY += opt_deltaY;
119 popupDiv.style.left = (startX)+'px';
120 popupDiv.style.top = (startY)+'px';
121 let popup = new TKR_MyPopup(popupDiv);
122 popup.show();
123 return false;
124}
125
126
127/**
128 * Close the specified popup menu and unregister it with the popup
129 * controller, otherwise old leftover popup instances can mess with
130 * the future display of menus.
131 * @param {string} id The HTML ID of the element to hide.
132 */
133function TKR_closePopup(id) {
134 let e = $(id);
135 if (e) {
136 for (let i = 0; i < gPopupController.activePopups_.length; ++i) {
137 if (e === gPopupController.activePopups_[i]._div) {
138 let popup = gPopupController.activePopups_[i];
139 popup.hide();
140 gPopupController.activePopups_.splice(i, 1);
141 return;
142 }
143 }
144 }
145}
146
147
148var TKR_allColumnNames = []; // Will be defined in HTML file.
149
150/**
151 * Close all popup menus. Also, reset the hover state of the menu item that
152 * was selected. The list of popup menu names is computed from the list of
153 * columns specified in the HTML for the issue list page.
154 * @param menuItem {Element} The menu item that the user clicked.
155 * @return Always returns false to indicate that the browser should handle the
156 * event normally.
157 */
158function TKR_closeAllPopups(menuItem) {
159 for (let col_index = 0; col_index < TKR_allColumnNames.length; col_index++) {
160 TKR_closePopup('pop_' + col_index);
161 TKR_closePopup('filter_' + col_index);
162 }
163 TKR_closePopup('pop_dot');
164 TKR_closePopup('redoMenu');
165 menuItem.classList.remove('hover');
166 return false;
167}
168
169
170/**
171 * Close all the submenus (of which, one may be currently open).
172 * @return Always returns false to indicate that the browser should handle the
173 * event normally.
174 */
175function TKR_closeSubmenus() {
176 for (let col_index = 0; col_index < TKR_allColumnNames.length; col_index++) {
177 TKR_closePopup('filter_' + col_index);
178 }
179 return false;
180}
181
182
183/**
184 * Find the enclosing HTML element that controls this section of the
185 * page and set it to use CSS class "opened". That will make the
186 * section display in the opened state, regardless of what state is
187 * was in before.
188 * @param {Element} el The HTML element that the user clicked on.
189 * @return Always returns false to indicate that the browser should handle the
190 * event normally.
191 */
192function TKR_showHidden(el) {
193 while (el) {
194 if (el.classList.contains('closed')) {
195 el.classList.remove('closed');
196 el.classList.add('opened');
197 return false;
198 }
199 if (el.classList.contains('opened')) {
200 return false;
201 }
202 el = el.parentNode;
203 }
204}
205
206
207/**
208 * Toggle the display of a column in the issue list page. That is
209 * done by adding or removing a CSS class of an enclosing HTML
210 * element, and by CSS rules that trigger based on that CSS class.
211 * @param {string} colName The name of the column to toggle,
212 * corresponds to a CSS class.
213 * @return Always returns false to indicate that the browser should
214 * handle the event normally.
215 */
216function TKR_toggleColumn(colName) {
217 let controlDiv = $('colcontrol');
218 if (controlDiv.classList.contains(colName)) {
219 controlDiv.classList.remove(colName);
220 } else {
221 controlDiv.classList.add(colName);
222 }
223 return false;
224}
225
226
227/**
228 * Toggle the display of a set of rows in the issue list page. That is
229 * done by adding or removing a CSS class of an enclosing HTML
230 * element, and by CSS rules that trigger based on that CSS class.
231 * TODO(jrobbins): actually, this automatically hides the other groups.
232 * @param {string} rowClassName The name of the row group to toggle,
233 * corresponds to a CSS class.
234 * @return Always returns false to indicate that the browser should
235 * handle the event normally.
236 */
237function TKR_toggleRows(rowClassName) {
238 let controlDiv = $('colcontrol');
239 controlDiv.classList.add('hide_pri_groups');
240 controlDiv.classList.add('hide_mile_groups');
241 controlDiv.classList.add('hide_stat_groups');
242 TKR_toggleColumn(rowClassName);
243 return false;
244}
245
246
247/**
248 * A simple class that can manage the display of a popup menu. Instances
249 * of this class are used by popup_controller.js.
250 * @param {Element} div The div that contains the popup menu.
251 * @param {Element} opt_launcherEl The button that launched the popup menu,
252 * if any.
253 * @constructor
254 */
255function TKR_MyPopup(div, opt_launcherEl) {
256 this._div = div;
257 this._launcher = opt_launcherEl;
258 this._isVisible = false;
259}
260
261
262/**
263 * Show a popup menu. This method registers the popup with popup_controller.
264 */
265TKR_MyPopup.prototype.show = function() {
266 this._div.style.display = 'block';
267 this._isVisible = true;
268 PC_addPopup(this);
269};
270
271
272/**
273 * Show a popup menu. This method is called from the deactive method,
274 * which is called by popup_controller.
275 */
276TKR_MyPopup.prototype.hide = function() {
277 this._div.style.display = 'none';
278 this._isVisible = false;
279};
280
281
282/**
283 * When the popup_controller gets a user click, it calls deactive() on
284 * every active popup to check if the click should close that popup.
285 */
286TKR_MyPopup.prototype.deactivate = function(e) {
287 if (this._isVisible) {
288 let p = GetMousePosition(e);
289 if (nodeBounds(this._div).contains(p)) {
290 return false; // use clicked on popup, remain visible
291 } else if (this._launcher && nodeBounds(this._launcher).contains(p)) {
292 this._launcher = null;
293 return false; // mouseup element that launched menu, remain visible
294 } else {
295 this.hide();
296 return true; // clicked outside popup, make invisible
297 }
298 } else {
299 return true; // already deactivated, not visible
300 }
301};
302
303
304/**
305 * Highlight the issue row on the list page that contains the given
306 * checkbox.
307 * @param {Element} cb The checkbox that the user changed.
308 * @return Always returns false to indicate that the browser should
309 * handle the event normally.
310 */
311function TKR_highlightRow(el) {
312 let checked = el.checked;
313 while (el && el.tagName != 'TR') {
314 el = el.parentNode;
315 }
316 if (checked) {
317 el.classList.add('selected');
318 } else {
319 el.classList.remove('selected');
320 }
321 return false;
322}