OLD | NEW |
(Empty) | |
| 1 /* 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 * This file contains JS functions that implement various navigation |
| 10 * features of Monorail. |
| 11 */ |
| 12 |
| 13 |
| 14 /** |
| 15 * Navigate the browser to the given URL. |
| 16 * @param {string} url The URL of the page to browse. |
| 17 * @param {boolean} newWindow Open a new tab or window. |
| 18 */ |
| 19 function TKR_go(url, newWindow) { |
| 20 if (newWindow) |
| 21 window.open(url, '_blank'); |
| 22 else |
| 23 document.location = url; |
| 24 } |
| 25 |
| 26 |
| 27 /** |
| 28 * Tell the browser to scroll to the given anchor on the current page. |
| 29 * @param {string} anchor Name of the <a name="xxx"> anchor on the page. |
| 30 */ |
| 31 function TKR_goToAnchor(anchor) { |
| 32 document.location.hash = anchor; |
| 33 } |
| 34 |
| 35 |
| 36 /** |
| 37 * Get the user-editable colspec form field. This text field is normally |
| 38 * display:none, but it is shown when the user chooses "Edit columns...". |
| 39 * We need a function to get this element because there are multiple form |
| 40 * fields on the page with name="colspec", and an IE misfeature sets their |
| 41 * id attributes as well, which makes document.getElementById() fail. |
| 42 * @return {Element} user editable colspec form field. |
| 43 */ |
| 44 function TKR_getColspecElement() { |
| 45 return document.getElementById('colspec_field').firstChild; |
| 46 } |
| 47 |
| 48 |
| 49 /** |
| 50 * Get the hidden form field for colspec. This is a type="hidden" input field |
| 51 * that is submitted as part of the artfact search query. We need a |
| 52 * function to get this element because there are multiple form fields on the |
| 53 * page with name="colspec", and an IE misfeature sets their id attributes |
| 54 * as well, which makes document.getElementById() fail. |
| 55 * @return {Element} colspec hidden form field. |
| 56 */ |
| 57 function TKR_getSearchColspecElement() { |
| 58 return document.getElementById('search_colspec').firstChild; |
| 59 } |
| 60 |
| 61 |
| 62 /** |
| 63 * Get the artifact search form field. This is a visible text field where |
| 64 * the user enters a query for issues. This function |
| 65 * is needed because there is also the project search field on the each page, |
| 66 * and it has name="q". An IE misfeature confuses name="..." with id="...". |
| 67 * @return {Element} artifact query form field, or undefined. |
| 68 */ |
| 69 function TKR_getArtifactSearchField() { |
| 70 var qq = document.getElementById('qq'); |
| 71 return qq ? qq.firstChild : undefined; |
| 72 } |
| 73 |
| 74 |
| 75 /** |
| 76 * Resize the artifiact search box to be bigger when the user has a long |
| 77 * query. |
| 78 */ |
| 79 var MAX_ARTIFACT_SEARCH_FIELD_SIZE = 75; |
| 80 var AUTOSIZE_STEP = 3; |
| 81 |
| 82 function TKR_autosizeArtifactSerchField() { |
| 83 var qq = TKR_getArtifactSearchField(); |
| 84 if (qq) { |
| 85 var new_size = qq.value.length + AUTOSIZE_STEP; |
| 86 if (new_size > MAX_ARTIFACT_SEARCH_FIELD_SIZE) { |
| 87 new_size = MAX_ARTIFACT_SEARCH_FIELD_SIZE; |
| 88 } |
| 89 if (new_size > qq.size) { |
| 90 qq.size = new_size; |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 window.setInterval(TKR_autosizeArtifactSerchField, 700); |
| 96 |
| 97 |
| 98 /** |
| 99 * Build a query string for all the common contextual values that we use. |
| 100 */ |
| 101 function TKR_formatContextQueryArgs() { |
| 102 var args = ""; |
| 103 var colspec = TKR_getColspecElement().value; |
| 104 if (_ctxCan != 2) args += "&can=" + _ctxCan; |
| 105 if (_ctxQuery != "") args += "&q=" + encodeURIComponent(_ctxQuery); |
| 106 if (_ctxSortspec != "") args += "&sort=" + _ctxSortspec; |
| 107 if (_ctxGroupBy != "") args += "&groupby=" + _ctxGroupBy; |
| 108 if (colspec != _ctxDefaultColspec) args += "&colspec=" + colspec; |
| 109 if (_ctxStart != 0) args += "&start=" + _ctxStart; |
| 110 if (_ctxNum != _ctxResultsPerPage) args += "&num=" + _ctxNum; |
| 111 return args; |
| 112 } |
| 113 |
| 114 // Fields that should use ":" when filtering. |
| 115 var _PRETOKENIZED_FIELDS = [ |
| 116 'owner', 'reporter', 'cc', 'commentby', 'component']; |
| 117 |
| 118 /** |
| 119 * The user wants to narrow his/her search results by adding a search term |
| 120 * for the given prefix and value. Reload the issue list page with that |
| 121 * additional search term. |
| 122 * @param {string} prefix Field or label prefix, e.g., "Priority". |
| 123 * @param {string} suffix Field or label value, e.g., "High". |
| 124 */ |
| 125 function TKR_filterTo(prefix, suffix) { |
| 126 var newQuery = TKR_getArtifactSearchField().value; |
| 127 if (newQuery != '') newQuery += ' '; |
| 128 |
| 129 var op = '='; |
| 130 for (var i = 0; i < _PRETOKENIZED_FIELDS.length; i++) { |
| 131 if (prefix == _PRETOKENIZED_FIELDS[i]) { |
| 132 op = ':'; |
| 133 break; |
| 134 } |
| 135 } |
| 136 |
| 137 newQuery += prefix + op + suffix; |
| 138 var url = 'list?can=' + $('can').value + '&q=' + newQuery; |
| 139 if ($('sort') && $('sort').value) url += '&sort=' + $('sort').value; |
| 140 url += '&colspec=' + TKR_getColspecElement().value; |
| 141 TKR_go(url); |
| 142 } |
| 143 |
| 144 |
| 145 /** |
| 146 * The user wants to sort his/her search results by adding a sort spec |
| 147 * for the given column. Reload the issue list page with that |
| 148 * additional sort spec. |
| 149 * @param {string} colname Field or label prefix, e.g., "Priority". |
| 150 * @param {boolean} descending True if the values should be reversed. |
| 151 */ |
| 152 function TKR_addSort(colname, descending) { |
| 153 var existingSortSpec = ''; |
| 154 if ($('sort')) { existingSortSpec = $('sort').value; } |
| 155 var oldSpecs = existingSortSpec.split(/ +/); |
| 156 var sortDirective = colname; |
| 157 if (descending) sortDirective = '-' + colname; |
| 158 var specs = [sortDirective]; |
| 159 for (var i = 0; i < oldSpecs.length; i++) { |
| 160 if (oldSpecs[i] != "" && oldSpecs[i] != colname && |
| 161 oldSpecs[i] != '-' + colname) { |
| 162 specs.push(oldSpecs[i]) |
| 163 } |
| 164 } |
| 165 |
| 166 var url = ('list?can='+ $('can').value + '&q=' + |
| 167 TKR_getArtifactSearchField().value); |
| 168 url += '&sort=' + specs.join('+'); |
| 169 url += '&colspec=' + TKR_getColspecElement().value; |
| 170 TKR_go(url) |
| 171 } |
| 172 |
| 173 /** Convenience function for sorting in ascending order. */ |
| 174 function TKR_sortUp(colname) { |
| 175 TKR_addSort(colname, false); |
| 176 } |
| 177 |
| 178 /** Convenience function for sorting in descending order. */ |
| 179 function TKR_sortDown(colname) { |
| 180 TKR_addSort(colname, true); |
| 181 } |
OLD | NEW |