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