Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | /** |
| 6 | * Functions used by the Project Hosting to control the display of |
| 7 | * elements on the page, rollovers, and popup menus. |
| 8 | * |
| 9 | * Most of these functions are extracted from dit-display.js |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | /** |
| 14 | * Hide the HTML element with the given ID. |
| 15 | * @param {string} id The HTML element ID. |
| 16 | * @return {boolean} Always returns false to cancel the browser event |
| 17 | * if used as an event handler. |
| 18 | */ |
| 19 | function CS_hideID(id) { |
| 20 | $(id).style.display = 'none'; |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Show the HTML element with the given ID. |
| 27 | * @param {string} id The HTML element ID. |
| 28 | * @return {boolean} Always returns false to cancel the browser event |
| 29 | * if used as an event handler. |
| 30 | */ |
| 31 | function CS_showID(id) { |
| 32 | $(id).style.display = ''; |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Hide the given HTML element. |
| 39 | * @param {Element} el The HTML element. |
| 40 | * @return {boolean} Always returns false to cancel the browser event |
| 41 | * if used as an event handler. |
| 42 | */ |
| 43 | function CS_hideEl(el) { |
| 44 | el.style.display = 'none'; |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Show the given HTML element. |
| 51 | * @param {Element} el The HTML element. |
| 52 | * @return {boolean} Always returns false to cancel the browser event |
| 53 | * if used as an event handler. |
| 54 | */ |
| 55 | function CS_showEl(el) { |
| 56 | el.style.display = ''; |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Show one element instead of another. That is to say, show a new element and |
| 63 | * hide an old one. Usually the element is the element that the user clicked |
| 64 | * on with the intention of "expanding it" to access the new element. |
| 65 | * @param {string} newID The ID of the HTML element to show. |
| 66 | * @param {Element} oldEl The HTML element to hide. |
| 67 | * @return {boolean} Always returns false to cancel the browser event |
| 68 | * if used as an event handler. |
| 69 | */ |
| 70 | function CS_showInstead(newID, oldEl) { |
| 71 | $(newID).style.display = ''; |
| 72 | oldEl.style.display = 'none'; |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Toggle the open/closed state of a section of the page. As a result, CSS |
| 78 | * rules will make certain elements displayed and other elements hidden. The |
| 79 | * section is some HTML element that encloses the element that the user clicked |
| 80 | * on. |
| 81 | * @param {Element} el The element that the user clicked on. |
| 82 | * @return {boolean} Always returns false to cancel the browser event |
| 83 | * if used as an event handler. |
| 84 | */ |
| 85 | function CS_toggleHidden(el) { |
| 86 | while (el) { |
| 87 | if (el.classList.contains('closed')) { |
| 88 | el.classList.remove('closed'); |
| 89 | el.classList.add('opened'); |
| 90 | return false; |
| 91 | } |
| 92 | if (el.classList.contains('opened')) { |
| 93 | el.classList.remove('opened'); |
| 94 | el.classList.add('closed'); |
| 95 | return false; |
| 96 | } |
| 97 | el = el.parentNode; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Toggle the expand/collapse state of a section of the page. As a result, CSS |
| 104 | * rules will make certain elements displayed and other elements hidden. The |
| 105 | * section is some HTML element that encloses the element that the user clicked |
| 106 | * on. |
| 107 | * TODO(jrobbins): eliminate redundancy with function above. |
| 108 | * @param {Element} el The element that the user clicked on. |
| 109 | * @return {boolean} Always returns false to cancel the browser event |
| 110 | * if used as an event handler. |
| 111 | */ |
| 112 | function CS_toggleCollapse(el) { |
| 113 | while (el) { |
| 114 | if (el.classList.contains('collapse')) { |
| 115 | el.classList.remove('collapse'); |
| 116 | el.classList.add('expand'); |
| 117 | return false; |
| 118 | } |
| 119 | if (el.classList.contains('expand')) { |
| 120 | el.classList.remove('expand'); |
| 121 | el.classList.add('collapse'); |
| 122 | return false; |
| 123 | } |
| 124 | el = el.parentNode; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * Register a function for mouse clicks on the results table. We |
| 131 | * listen on the table to avoid adding 1000 individual listeners on |
| 132 | * the cells. This is needed because some browsers (now including |
| 133 | * Chrome) do not generate click events for mouse buttons other than |
| 134 | * the primary mouse button. Chrome and Firefox generate auxclick |
| 135 | * events, but Edge does not. |
| 136 | */ |
| 137 | |
| 138 | function CS_addClickListener(tableEl, handler) { |
| 139 | function maybeClick(event) { |
| 140 | const target = getTargetFromEvent(event); |
| 141 | |
| 142 | const inLink = target.tagName == 'A' || target.parentNode.tagName == 'A'; |
| 143 | |
| 144 | if (inLink && !target.classList.contains('computehref')) { |
| 145 | // The <a> elements already have the correct hrefs. |
| 146 | return; |
| 147 | } |
| 148 | if (event.button == 2) { |
| 149 | // User is trying to open a context menu, not trying to navigate. |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | let td = target; |
| 154 | while (td && td.tagName != 'TD' && td.tagName != 'TH') { |
| 155 | td = td.parentNode; |
| 156 | } |
| 157 | if (td.classList.contains('rowwidgets')) { |
| 158 | // User clicked on a checkbox. |
| 159 | return; |
| 160 | } |
| 161 | // User clicked on an issue ID link or text or cell. |
| 162 | event.preventDefault(); |
| 163 | handler(event); |
| 164 | } |
| 165 | tableEl.addEventListener('click', maybeClick); |
| 166 | tableEl.addEventListener('auxclick', maybeClick); |
| 167 | } |
| 168 | |
| 169 | function getTargetFromEvent(event) { |
| 170 | let target = event.target || event.srcElement; |
| 171 | if (target.shadowRoot) { |
| 172 | // Find the element within the shadowDOM. |
| 173 | const path = event.path || event.composedPath(); |
| 174 | target = path[0]; |
| 175 | } |
| 176 | return target; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | // Exports |
| 181 | _hideID = CS_hideID; |
| 182 | _showID = CS_showID; |
| 183 | _hideEl = CS_hideEl; |
| 184 | _showEl = CS_showEl; |
| 185 | _showInstead = CS_showInstead; |
| 186 | _toggleHidden = CS_toggleHidden; |
| 187 | _toggleCollapse = CS_toggleCollapse; |
| 188 | _addClickListener = CS_addClickListener; |