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