Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2007 Matt Lilek ([email protected]). |
| 4 | * Copyright (C) 2009 Joseph Pecoraro |
| 5 | * Copyright (C) 2011 Google Inc. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 17 | * its contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /** |
| 33 | * @unrestricted |
| 34 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 35 | export default class SearchableView extends UI.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 37 | * @param {!Searchable} searchable |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | * @param {string=} settingName |
| 39 | */ |
| 40 | constructor(searchable, settingName) { |
| 41 | super(true); |
| 42 | this.registerRequiredCSS('ui/searchableView.css'); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 43 | this.element[_symbol] = this; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | |
| 45 | this._searchProvider = searchable; |
| 46 | this._setting = settingName ? Common.settings.createSetting(settingName, {}) : null; |
| 47 | this._replaceable = false; |
| 48 | |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 49 | this.contentElement.createChild('slot'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 50 | this._footerElementContainer = this.contentElement.createChild('div', 'search-bar hidden'); |
| 51 | this._footerElementContainer.style.order = 100; |
| 52 | this._footerElement = this._footerElementContainer.createChild('div', 'toolbar-search'); |
| 53 | |
| 54 | const replaceToggleToolbar = new UI.Toolbar('replace-toggle-toolbar', this._footerElement); |
| 55 | this._replaceToggleButton = new UI.ToolbarToggle(Common.UIString('Replace'), 'mediumicon-replace'); |
| 56 | this._replaceToggleButton.addEventListener(UI.ToolbarButton.Events.Click, this._toggleReplace, this); |
| 57 | replaceToggleToolbar.appendToolbarItem(this._replaceToggleButton); |
| 58 | |
| 59 | const searchInputElements = this._footerElement.createChild('div', 'toolbar-search-inputs'); |
| 60 | const searchControlElement = searchInputElements.createChild('div', 'toolbar-search-control'); |
| 61 | |
| 62 | this._searchInputElement = UI.HistoryInput.create(); |
| 63 | this._searchInputElement.classList.add('search-replace'); |
| 64 | this._searchInputElement.id = 'search-input-field'; |
| 65 | this._searchInputElement.placeholder = Common.UIString('Find'); |
| 66 | searchControlElement.appendChild(this._searchInputElement); |
| 67 | |
| 68 | this._matchesElement = searchControlElement.createChild('label', 'search-results-matches'); |
| 69 | this._matchesElement.setAttribute('for', 'search-input-field'); |
| 70 | |
| 71 | const searchNavigationElement = searchControlElement.createChild('div', 'toolbar-search-navigation-controls'); |
| 72 | |
| 73 | this._searchNavigationPrevElement = |
| 74 | searchNavigationElement.createChild('div', 'toolbar-search-navigation toolbar-search-navigation-prev'); |
| 75 | this._searchNavigationPrevElement.addEventListener('click', this._onPrevButtonSearch.bind(this), false); |
| 76 | this._searchNavigationPrevElement.title = Common.UIString('Search previous'); |
| 77 | |
| 78 | this._searchNavigationNextElement = |
| 79 | searchNavigationElement.createChild('div', 'toolbar-search-navigation toolbar-search-navigation-next'); |
| 80 | this._searchNavigationNextElement.addEventListener('click', this._onNextButtonSearch.bind(this), false); |
| 81 | this._searchNavigationNextElement.title = Common.UIString('Search next'); |
| 82 | |
| 83 | this._searchInputElement.addEventListener('keydown', this._onSearchKeyDown.bind(this), true); |
| 84 | this._searchInputElement.addEventListener('input', this._onInput.bind(this), false); |
| 85 | |
| 86 | this._replaceInputElement = |
| 87 | searchInputElements.createChild('input', 'search-replace toolbar-replace-control hidden'); |
| 88 | this._replaceInputElement.addEventListener('keydown', this._onReplaceKeyDown.bind(this), true); |
| 89 | this._replaceInputElement.placeholder = Common.UIString('Replace'); |
| 90 | |
| 91 | this._buttonsContainer = this._footerElement.createChild('div', 'toolbar-search-buttons'); |
| 92 | const firstRowButtons = this._buttonsContainer.createChild('div', 'first-row-buttons'); |
| 93 | |
| 94 | const toolbar = new UI.Toolbar('toolbar-search-options', firstRowButtons); |
| 95 | |
| 96 | if (this._searchProvider.supportsCaseSensitiveSearch()) { |
| 97 | this._caseSensitiveButton = new UI.ToolbarToggle(Common.UIString('Match Case')); |
| 98 | this._caseSensitiveButton.setText('Aa'); |
| 99 | this._caseSensitiveButton.addEventListener(UI.ToolbarButton.Events.Click, this._toggleCaseSensitiveSearch, this); |
| 100 | toolbar.appendToolbarItem(this._caseSensitiveButton); |
| 101 | } |
| 102 | |
| 103 | if (this._searchProvider.supportsRegexSearch()) { |
| 104 | this._regexButton = new UI.ToolbarToggle(Common.UIString('Use Regular Expression')); |
| 105 | this._regexButton.setText('.*'); |
| 106 | this._regexButton.addEventListener(UI.ToolbarButton.Events.Click, this._toggleRegexSearch, this); |
| 107 | toolbar.appendToolbarItem(this._regexButton); |
| 108 | } |
| 109 | |
| 110 | const cancelButtonElement = |
| 111 | UI.createTextButton(Common.UIString('Cancel'), this.closeSearch.bind(this), 'search-action-button'); |
| 112 | firstRowButtons.appendChild(cancelButtonElement); |
| 113 | |
| 114 | this._secondRowButtons = this._buttonsContainer.createChild('div', 'second-row-buttons hidden'); |
| 115 | |
| 116 | this._replaceButtonElement = |
| 117 | UI.createTextButton(Common.UIString('Replace'), this._replace.bind(this), 'search-action-button'); |
| 118 | this._replaceButtonElement.disabled = true; |
| 119 | this._secondRowButtons.appendChild(this._replaceButtonElement); |
| 120 | |
| 121 | this._replaceAllButtonElement = |
| 122 | UI.createTextButton(Common.UIString('Replace all'), this._replaceAll.bind(this), 'search-action-button'); |
| 123 | this._secondRowButtons.appendChild(this._replaceAllButtonElement); |
| 124 | this._replaceAllButtonElement.disabled = true; |
| 125 | |
| 126 | this._minimalSearchQuerySize = 3; |
| 127 | this._loadSetting(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param {?Element} element |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 132 | * @return {?SearchableView} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 133 | */ |
| 134 | static fromElement(element) { |
| 135 | let view = null; |
| 136 | while (element && !view) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 137 | view = element[_symbol]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 138 | element = element.parentElementOrShadowHost(); |
| 139 | } |
| 140 | return view; |
| 141 | } |
| 142 | |
| 143 | _toggleCaseSensitiveSearch() { |
| 144 | this._caseSensitiveButton.setToggled(!this._caseSensitiveButton.toggled()); |
| 145 | this._saveSetting(); |
| 146 | this._performSearch(false, true); |
| 147 | } |
| 148 | |
| 149 | _toggleRegexSearch() { |
| 150 | this._regexButton.setToggled(!this._regexButton.toggled()); |
| 151 | this._saveSetting(); |
| 152 | this._performSearch(false, true); |
| 153 | } |
| 154 | |
| 155 | _toggleReplace() { |
| 156 | this._replaceToggleButton.setToggled(!this._replaceToggleButton.toggled()); |
| 157 | this._updateSecondRowVisibility(); |
| 158 | } |
| 159 | |
| 160 | _saveSetting() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 161 | if (!this._setting) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 162 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 163 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 164 | const settingValue = this._setting.get() || {}; |
| 165 | settingValue.caseSensitive = this._caseSensitiveButton.toggled(); |
| 166 | settingValue.isRegex = this._regexButton.toggled(); |
| 167 | this._setting.set(settingValue); |
| 168 | } |
| 169 | |
| 170 | _loadSetting() { |
| 171 | const settingValue = this._setting ? (this._setting.get() || {}) : {}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 172 | if (this._searchProvider.supportsCaseSensitiveSearch()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 173 | this._caseSensitiveButton.setToggled(!!settingValue.caseSensitive); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 174 | } |
| 175 | if (this._searchProvider.supportsRegexSearch()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 176 | this._regexButton.setToggled(!!settingValue.isRegex); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 177 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @param {number} minimalSearchQuerySize |
| 182 | */ |
| 183 | setMinimalSearchQuerySize(minimalSearchQuerySize) { |
| 184 | this._minimalSearchQuerySize = minimalSearchQuerySize; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @param {string} placeholder |
| 189 | */ |
| 190 | setPlaceholder(placeholder) { |
| 191 | this._searchInputElement.placeholder = placeholder; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @param {boolean} replaceable |
| 196 | */ |
| 197 | setReplaceable(replaceable) { |
| 198 | this._replaceable = replaceable; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @param {number} matches |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 203 | * @suppress {checkTypes} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 204 | */ |
| 205 | updateSearchMatchesCount(matches) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 206 | if (this._searchProvider.currentSearchMatches === matches) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 207 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 208 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 209 | this._searchProvider.currentSearchMatches = matches; |
| 210 | this._updateSearchMatchesCountAndCurrentMatchIndex(this._searchProvider.currentQuery ? matches : 0, -1); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @param {number} currentMatchIndex |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 215 | * @suppress {checkTypes} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 216 | */ |
| 217 | updateCurrentMatchIndex(currentMatchIndex) { |
| 218 | this._updateSearchMatchesCountAndCurrentMatchIndex(this._searchProvider.currentSearchMatches, currentMatchIndex); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @return {boolean} |
| 223 | */ |
| 224 | isSearchVisible() { |
| 225 | return this._searchIsVisible; |
| 226 | } |
| 227 | |
| 228 | closeSearch() { |
| 229 | this.cancelSearch(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 230 | if (this._footerElementContainer.hasFocus()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | this.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 232 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | _toggleSearchBar(toggled) { |
| 236 | this._footerElementContainer.classList.toggle('hidden', !toggled); |
| 237 | this.doResize(); |
| 238 | } |
| 239 | |
| 240 | cancelSearch() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 241 | if (!this._searchIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 242 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 243 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 244 | this.resetSearch(); |
| 245 | delete this._searchIsVisible; |
| 246 | this._toggleSearchBar(false); |
| 247 | } |
| 248 | |
| 249 | resetSearch() { |
| 250 | this._clearSearch(); |
| 251 | this._updateReplaceVisibility(); |
| 252 | this._matchesElement.textContent = ''; |
| 253 | } |
| 254 | |
| 255 | refreshSearch() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 256 | if (!this._searchIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 257 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 258 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 259 | this.resetSearch(); |
| 260 | this._performSearch(false, false); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @return {boolean} |
| 265 | */ |
| 266 | handleFindNextShortcut() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 267 | if (!this._searchIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 268 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 269 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 270 | this._searchProvider.jumpToNextSearchResult(); |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @return {boolean} |
| 276 | */ |
| 277 | handleFindPreviousShortcut() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 278 | if (!this._searchIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 279 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 280 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 281 | this._searchProvider.jumpToPreviousSearchResult(); |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @return {boolean} |
| 287 | */ |
| 288 | handleFindShortcut() { |
| 289 | this.showSearchField(); |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @return {boolean} |
| 295 | */ |
| 296 | handleCancelSearchShortcut() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 297 | if (!this._searchIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 298 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 299 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 300 | this.closeSearch(); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param {boolean} enabled |
| 306 | */ |
| 307 | _updateSearchNavigationButtonState(enabled) { |
| 308 | this._replaceButtonElement.disabled = !enabled; |
| 309 | this._replaceAllButtonElement.disabled = !enabled; |
| 310 | this._searchNavigationPrevElement.classList.toggle('enabled', enabled); |
| 311 | this._searchNavigationNextElement.classList.toggle('enabled', enabled); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @param {number} matches |
| 316 | * @param {number} currentMatchIndex |
| 317 | */ |
| 318 | _updateSearchMatchesCountAndCurrentMatchIndex(matches, currentMatchIndex) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 319 | if (!this._currentQuery) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 320 | this._matchesElement.textContent = ''; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 321 | } else if (matches === 0 || currentMatchIndex >= 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 322 | this._matchesElement.textContent = Common.UIString('%d of %d', currentMatchIndex + 1, matches); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 323 | } else if (matches === 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 324 | this._matchesElement.textContent = Common.UIString('1 match'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 325 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 326 | this._matchesElement.textContent = Common.UIString('%d matches', matches); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 327 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 328 | this._updateSearchNavigationButtonState(matches > 0); |
| 329 | } |
| 330 | |
| 331 | showSearchField() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 332 | if (this._searchIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 333 | this.cancelSearch(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 334 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | |
| 336 | let queryCandidate; |
| 337 | if (!this._searchInputElement.hasFocus()) { |
| 338 | const selection = UI.inspectorView.element.window().getSelection(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 339 | if (selection.rangeCount) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 340 | queryCandidate = selection.toString().replace(/\r?\n.*/, ''); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 341 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | this._toggleSearchBar(true); |
| 345 | this._updateReplaceVisibility(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 346 | if (queryCandidate) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 347 | this._searchInputElement.value = queryCandidate; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 348 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 349 | this._performSearch(false, false); |
| 350 | this._searchInputElement.focus(); |
| 351 | this._searchInputElement.select(); |
| 352 | this._searchIsVisible = true; |
| 353 | } |
| 354 | |
| 355 | _updateReplaceVisibility() { |
| 356 | this._replaceToggleButton.setVisible(this._replaceable); |
| 357 | if (!this._replaceable) { |
| 358 | this._replaceToggleButton.setToggled(false); |
| 359 | this._updateSecondRowVisibility(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * @param {!Event} event |
| 365 | */ |
| 366 | _onSearchKeyDown(event) { |
| 367 | if (isEscKey(event)) { |
| 368 | this.closeSearch(); |
| 369 | event.consume(true); |
| 370 | return; |
| 371 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 372 | if (!isEnterKey(event)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 373 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 374 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 375 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 376 | if (!this._currentQuery) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 377 | this._performSearch(true, true, event.shiftKey); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 378 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 379 | this._jumpToNextSearchResult(event.shiftKey); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 380 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @param {!Event} event |
| 385 | */ |
| 386 | _onReplaceKeyDown(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 387 | if (isEnterKey(event)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 388 | this._replace(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 389 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
| 393 | * @param {boolean=} isBackwardSearch |
| 394 | */ |
| 395 | _jumpToNextSearchResult(isBackwardSearch) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 396 | if (!this._currentQuery) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 397 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 398 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 399 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 400 | if (isBackwardSearch) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 401 | this._searchProvider.jumpToPreviousSearchResult(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 402 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 403 | this._searchProvider.jumpToNextSearchResult(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 404 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | _onNextButtonSearch(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 408 | if (!this._searchNavigationNextElement.classList.contains('enabled')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 409 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 410 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 411 | this._jumpToNextSearchResult(); |
| 412 | this._searchInputElement.focus(); |
| 413 | } |
| 414 | |
| 415 | _onPrevButtonSearch(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 416 | if (!this._searchNavigationPrevElement.classList.contains('enabled')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 417 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 418 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 419 | this._jumpToNextSearchResult(true); |
| 420 | this._searchInputElement.focus(); |
| 421 | } |
| 422 | |
| 423 | _onFindClick(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 424 | if (!this._currentQuery) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 425 | this._performSearch(true, true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 426 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 427 | this._jumpToNextSearchResult(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 428 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 429 | this._searchInputElement.focus(); |
| 430 | } |
| 431 | |
| 432 | _onPreviousClick(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 433 | if (!this._currentQuery) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 434 | this._performSearch(true, true, true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 435 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 436 | this._jumpToNextSearchResult(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 437 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 438 | this._searchInputElement.focus(); |
| 439 | } |
| 440 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 441 | /** @suppress {checkTypes} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 442 | _clearSearch() { |
| 443 | delete this._currentQuery; |
| 444 | if (!!this._searchProvider.currentQuery) { |
| 445 | delete this._searchProvider.currentQuery; |
| 446 | this._searchProvider.searchCanceled(); |
| 447 | } |
| 448 | this._updateSearchMatchesCountAndCurrentMatchIndex(0, -1); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * @param {boolean} forceSearch |
| 453 | * @param {boolean} shouldJump |
| 454 | * @param {boolean=} jumpBackwards |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 455 | * @suppress {checkTypes} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 456 | */ |
| 457 | _performSearch(forceSearch, shouldJump, jumpBackwards) { |
| 458 | const query = this._searchInputElement.value; |
| 459 | if (!query || (!forceSearch && query.length < this._minimalSearchQuerySize && !this._currentQuery)) { |
| 460 | this._clearSearch(); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | this._currentQuery = query; |
| 465 | this._searchProvider.currentQuery = query; |
| 466 | |
| 467 | const searchConfig = this._currentSearchConfig(); |
| 468 | this._searchProvider.performSearch(searchConfig, shouldJump, jumpBackwards); |
| 469 | } |
| 470 | |
| 471 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 472 | * @return {!SearchConfig} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 473 | */ |
| 474 | _currentSearchConfig() { |
| 475 | const query = this._searchInputElement.value; |
| 476 | const caseSensitive = this._caseSensitiveButton ? this._caseSensitiveButton.toggled() : false; |
| 477 | const isRegex = this._regexButton ? this._regexButton.toggled() : false; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 478 | return new SearchConfig(query, caseSensitive, isRegex); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | _updateSecondRowVisibility() { |
| 482 | const secondRowVisible = this._replaceToggleButton.toggled(); |
| 483 | this._footerElementContainer.classList.toggle('replaceable', secondRowVisible); |
| 484 | this._secondRowButtons.classList.toggle('hidden', !secondRowVisible); |
| 485 | this._replaceInputElement.classList.toggle('hidden', !secondRowVisible); |
| 486 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 487 | if (secondRowVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 488 | this._replaceInputElement.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 489 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 490 | this._searchInputElement.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 491 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 492 | this.doResize(); |
| 493 | } |
| 494 | |
| 495 | _replace() { |
| 496 | const searchConfig = this._currentSearchConfig(); |
| 497 | /** @type {!UI.Replaceable} */ (this._searchProvider) |
| 498 | .replaceSelectionWith(searchConfig, this._replaceInputElement.value); |
| 499 | delete this._currentQuery; |
| 500 | this._performSearch(true, true); |
| 501 | } |
| 502 | |
| 503 | _replaceAll() { |
| 504 | const searchConfig = this._currentSearchConfig(); |
| 505 | /** @type {!UI.Replaceable} */ (this._searchProvider).replaceAllWith(searchConfig, this._replaceInputElement.value); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * @param {!Event} event |
| 510 | */ |
| 511 | _onInput(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 512 | if (this._valueChangedTimeoutId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 513 | clearTimeout(this._valueChangedTimeoutId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 514 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 515 | const timeout = this._searchInputElement.value.length < 3 ? 200 : 0; |
| 516 | this._valueChangedTimeoutId = setTimeout(this._onValueChanged.bind(this), timeout); |
| 517 | } |
| 518 | |
| 519 | _onValueChanged() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 520 | if (!this._searchIsVisible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 521 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 522 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 523 | delete this._valueChangedTimeoutId; |
| 524 | this._performSearch(false, true); |
| 525 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 526 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 527 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 528 | export const _symbol = Symbol('searchableView'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 529 | |
| 530 | |
| 531 | /** |
| 532 | * @interface |
| 533 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 534 | export class Searchable { |
| 535 | searchCanceled() { |
| 536 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 537 | |
| 538 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 539 | * @param {!SearchConfig} searchConfig |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 540 | * @param {boolean} shouldJump |
| 541 | * @param {boolean=} jumpBackwards |
| 542 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 543 | performSearch(searchConfig, shouldJump, jumpBackwards) { |
| 544 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 545 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 546 | jumpToNextSearchResult() { |
| 547 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 548 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 549 | jumpToPreviousSearchResult() { |
| 550 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 551 | |
| 552 | /** |
| 553 | * @return {boolean} |
| 554 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 555 | supportsCaseSensitiveSearch() { |
| 556 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 557 | |
| 558 | /** |
| 559 | * @return {boolean} |
| 560 | */ |
| 561 | supportsRegexSearch() {} |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 562 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 563 | |
| 564 | /** |
| 565 | * @interface |
| 566 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 567 | export class Replaceable { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 568 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 569 | * @param {!SearchConfig} searchConfig |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | * @param {string} replacement |
| 571 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 572 | replaceSelectionWith(searchConfig, replacement) { |
| 573 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 574 | |
| 575 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 576 | * @param {!SearchConfig} searchConfig |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 577 | * @param {string} replacement |
| 578 | */ |
| 579 | replaceAllWith(searchConfig, replacement) {} |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 580 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 581 | |
| 582 | /** |
| 583 | * @unrestricted |
| 584 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 585 | export class SearchConfig { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 586 | /** |
| 587 | * @param {string} query |
| 588 | * @param {boolean} caseSensitive |
| 589 | * @param {boolean} isRegex |
| 590 | */ |
| 591 | constructor(query, caseSensitive, isRegex) { |
| 592 | this.query = query; |
| 593 | this.caseSensitive = caseSensitive; |
| 594 | this.isRegex = isRegex; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * @param {boolean=} global |
| 599 | * @return {!RegExp} |
| 600 | */ |
| 601 | toSearchRegex(global) { |
| 602 | let modifiers = this.caseSensitive ? '' : 'i'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 603 | if (global) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 604 | modifiers += 'g'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 605 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 606 | const query = this.isRegex ? '/' + this.query + '/' : this.query; |
| 607 | |
| 608 | let regex; |
| 609 | |
| 610 | // First try creating regex if user knows the / / hint. |
| 611 | try { |
| 612 | if (/^\/.+\/$/.test(query)) { |
| 613 | regex = new RegExp(query.substring(1, query.length - 1), modifiers); |
| 614 | regex.__fromRegExpQuery = true; |
| 615 | } |
| 616 | } catch (e) { |
| 617 | // Silent catch. |
| 618 | } |
| 619 | |
| 620 | // Otherwise just do a plain text search. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 621 | if (!regex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 622 | regex = createPlainTextSearchRegex(query, modifiers); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 623 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 624 | |
| 625 | return regex; |
| 626 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 627 | } |
| 628 | |
| 629 | /* Legacy exported object*/ |
| 630 | self.UI = self.UI || {}; |
| 631 | |
| 632 | /* Legacy exported object*/ |
| 633 | UI = UI || {}; |
| 634 | |
| 635 | /** @constructor */ |
| 636 | UI.SearchableView = SearchableView; |
| 637 | |
| 638 | /** |
| 639 | * @constructor |
| 640 | */ |
| 641 | UI.SearchableView.SearchConfig = SearchConfig; |
| 642 | |
| 643 | /** @interface */ |
| 644 | UI.Searchable = Searchable; |
| 645 | |
| 646 | /** @interface */ |
| 647 | UI.Replaceable = Replaceable; |