| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2011 Google Inc. All rights reserved. | 3 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| 11 * 2. Redistributions in binary form must reproduce the above copyright | 11 * 2. Redistributions in binary form must reproduce the above copyright |
| 12 * notice, this list of conditions and the following disclaimer in the | 12 * notice, this list of conditions and the following disclaimer in the |
| 13 * documentation and/or other materials provided with the distribution. | 13 * documentation and/or other materials provided with the distribution. |
| 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 15 * its contributors may be used to endorse or promote products derived | 15 * its contributors may be used to endorse or promote products derived |
| 16 * from this software without specific prior written permission. | 16 * from this software without specific prior written permission. |
| 17 * | 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 */ | 28 */ |
| 29 /** | 29 /** |
| 30 * @implements {WebInspector.SuggestBoxDelegate} | 30 * @implements {UI.SuggestBoxDelegate} |
| 31 * @unrestricted | 31 * @unrestricted |
| 32 */ | 32 */ |
| 33 WebInspector.TextPrompt = class extends WebInspector.Object { | 33 UI.TextPrompt = class extends Common.Object { |
| 34 constructor() { | 34 constructor() { |
| 35 super(); | 35 super(); |
| 36 /** | 36 /** |
| 37 * @type {!Element|undefined} | 37 * @type {!Element|undefined} |
| 38 */ | 38 */ |
| 39 this._proxyElement; | 39 this._proxyElement; |
| 40 this._proxyElementDisplay = 'inline-block'; | 40 this._proxyElementDisplay = 'inline-block'; |
| 41 this._autocompletionTimeout = WebInspector.TextPrompt.DefaultAutocompletionT
imeout; | 41 this._autocompletionTimeout = UI.TextPrompt.DefaultAutocompletionTimeout; |
| 42 this._title = ''; | 42 this._title = ''; |
| 43 this._queryRange = null; | 43 this._queryRange = null; |
| 44 this._previousText = ''; | 44 this._previousText = ''; |
| 45 this._currentSuggestion = ''; | 45 this._currentSuggestion = ''; |
| 46 this._completionRequestId = 0; | 46 this._completionRequestId = 0; |
| 47 this._ghostTextElement = createElementWithClass('span', 'auto-complete-text'
); | 47 this._ghostTextElement = createElementWithClass('span', 'auto-complete-text'
); |
| 48 } | 48 } |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * @param {function(!Element, !Range, boolean, function(!Array.<string>, numbe
r=))} completions | 51 * @param {function(!Element, !Range, boolean, function(!Array.<string>, numbe
r=))} completions |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 _attachInternal(element) { | 108 _attachInternal(element) { |
| 109 if (this._proxyElement) | 109 if (this._proxyElement) |
| 110 throw 'Cannot attach an attached TextPrompt'; | 110 throw 'Cannot attach an attached TextPrompt'; |
| 111 this._element = element; | 111 this._element = element; |
| 112 | 112 |
| 113 this._boundOnKeyDown = this.onKeyDown.bind(this); | 113 this._boundOnKeyDown = this.onKeyDown.bind(this); |
| 114 this._boundOnInput = this.onInput.bind(this); | 114 this._boundOnInput = this.onInput.bind(this); |
| 115 this._boundOnMouseWheel = this.onMouseWheel.bind(this); | 115 this._boundOnMouseWheel = this.onMouseWheel.bind(this); |
| 116 this._boundClearAutocomplete = this.clearAutocomplete.bind(this); | 116 this._boundClearAutocomplete = this.clearAutocomplete.bind(this); |
| 117 this._proxyElement = element.ownerDocument.createElement('span'); | 117 this._proxyElement = element.ownerDocument.createElement('span'); |
| 118 var shadowRoot = WebInspector.createShadowRootWithCoreStyles(this._proxyElem
ent, 'ui/textPrompt.css'); | 118 var shadowRoot = UI.createShadowRootWithCoreStyles(this._proxyElement, 'ui/t
extPrompt.css'); |
| 119 this._contentElement = shadowRoot.createChild('div'); | 119 this._contentElement = shadowRoot.createChild('div'); |
| 120 this._contentElement.createChild('content'); | 120 this._contentElement.createChild('content'); |
| 121 this._proxyElement.style.display = this._proxyElementDisplay; | 121 this._proxyElement.style.display = this._proxyElementDisplay; |
| 122 element.parentElement.insertBefore(this._proxyElement, element); | 122 element.parentElement.insertBefore(this._proxyElement, element); |
| 123 this._proxyElement.appendChild(element); | 123 this._proxyElement.appendChild(element); |
| 124 this._element.classList.add('text-prompt'); | 124 this._element.classList.add('text-prompt'); |
| 125 this._element.addEventListener('keydown', this._boundOnKeyDown, false); | 125 this._element.addEventListener('keydown', this._boundOnKeyDown, false); |
| 126 this._element.addEventListener('input', this._boundOnInput, false); | 126 this._element.addEventListener('input', this._boundOnInput, false); |
| 127 this._element.addEventListener('mousewheel', this._boundOnMouseWheel, false)
; | 127 this._element.addEventListener('mousewheel', this._boundOnMouseWheel, false)
; |
| 128 this._element.addEventListener('selectstart', this._boundClearAutocomplete,
false); | 128 this._element.addEventListener('selectstart', this._boundClearAutocomplete,
false); |
| 129 this._element.addEventListener('blur', this._boundClearAutocomplete, false); | 129 this._element.addEventListener('blur', this._boundClearAutocomplete, false); |
| 130 this._element.ownerDocument.defaultView.addEventListener('resize', this._bou
ndClearAutocomplete, false); | 130 this._element.ownerDocument.defaultView.addEventListener('resize', this._bou
ndClearAutocomplete, false); |
| 131 | 131 |
| 132 if (this._suggestBoxEnabled) | 132 if (this._suggestBoxEnabled) |
| 133 this._suggestBox = new WebInspector.SuggestBox(this, 20, true); | 133 this._suggestBox = new UI.SuggestBox(this, 20, true); |
| 134 | 134 |
| 135 if (this._title) | 135 if (this._title) |
| 136 this._proxyElement.title = this._title; | 136 this._proxyElement.title = this._title; |
| 137 | 137 |
| 138 return this._proxyElement; | 138 return this._proxyElement; |
| 139 } | 139 } |
| 140 | 140 |
| 141 detach() { | 141 detach() { |
| 142 this._removeFromElement(); | 142 this._removeFromElement(); |
| 143 this._proxyElement.parentElement.insertBefore(this._element, this._proxyElem
ent); | 143 this._proxyElement.parentElement.insertBefore(this._element, this._proxyElem
ent); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 _startEditing(blurListener) { | 223 _startEditing(blurListener) { |
| 224 this._isEditing = true; | 224 this._isEditing = true; |
| 225 this._contentElement.classList.add('text-prompt-editing'); | 225 this._contentElement.classList.add('text-prompt-editing'); |
| 226 if (blurListener) { | 226 if (blurListener) { |
| 227 this._blurListener = blurListener; | 227 this._blurListener = blurListener; |
| 228 this._element.addEventListener('blur', this._blurListener, false); | 228 this._element.addEventListener('blur', this._blurListener, false); |
| 229 } | 229 } |
| 230 this._oldTabIndex = this._element.tabIndex; | 230 this._oldTabIndex = this._element.tabIndex; |
| 231 if (this._element.tabIndex < 0) | 231 if (this._element.tabIndex < 0) |
| 232 this._element.tabIndex = 0; | 232 this._element.tabIndex = 0; |
| 233 this._focusRestorer = new WebInspector.ElementFocusRestorer(this._element); | 233 this._focusRestorer = new UI.ElementFocusRestorer(this._element); |
| 234 if (!this.text()) | 234 if (!this.text()) |
| 235 this.autoCompleteSoon(); | 235 this.autoCompleteSoon(); |
| 236 } | 236 } |
| 237 | 237 |
| 238 _stopEditing() { | 238 _stopEditing() { |
| 239 this._element.tabIndex = this._oldTabIndex; | 239 this._element.tabIndex = this._oldTabIndex; |
| 240 if (this._blurListener) | 240 if (this._blurListener) |
| 241 this._element.removeEventListener('blur', this._blurListener, false); | 241 this._element.removeEventListener('blur', this._blurListener, false); |
| 242 this._contentElement.classList.remove('text-prompt-editing'); | 242 this._contentElement.classList.remove('text-prompt-editing'); |
| 243 delete this._isEditing; | 243 delete this._isEditing; |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 /** | 420 /** |
| 421 * @return {?Range} | 421 * @return {?Range} |
| 422 * @suppressGlobalPropertiesCheck | 422 * @suppressGlobalPropertiesCheck |
| 423 */ | 423 */ |
| 424 _createRange() { | 424 _createRange() { |
| 425 return document.createRange(); | 425 return document.createRange(); |
| 426 } | 426 } |
| 427 | 427 |
| 428 /** | 428 /** |
| 429 * @param {string} query | 429 * @param {string} query |
| 430 * @return {!WebInspector.SuggestBox.Suggestions} | 430 * @return {!UI.SuggestBox.Suggestions} |
| 431 */ | 431 */ |
| 432 additionalCompletions(query) { | 432 additionalCompletions(query) { |
| 433 return []; | 433 return []; |
| 434 } | 434 } |
| 435 | 435 |
| 436 /** | 436 /** |
| 437 * @param {number} completionRequestId | 437 * @param {number} completionRequestId |
| 438 * @param {!Selection} selection | 438 * @param {!Selection} selection |
| 439 * @param {!Range} originalWordQueryRange | 439 * @param {!Range} originalWordQueryRange |
| 440 * @param {boolean} reverse | 440 * @param {boolean} reverse |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 selectedIndex = (this._disableDefaultSuggestionForEmptyInput && !this.text()
) ? -1 : (selectedIndex || 0); | 484 selectedIndex = (this._disableDefaultSuggestionForEmptyInput && !this.text()
) ? -1 : (selectedIndex || 0); |
| 485 | 485 |
| 486 if (this._suggestBox) | 486 if (this._suggestBox) |
| 487 this._suggestBox.updateSuggestions( | 487 this._suggestBox.updateSuggestions( |
| 488 this._boxForAnchorAtStart(selection, fullWordRange), annotatedCompleti
ons, selectedIndex, | 488 this._boxForAnchorAtStart(selection, fullWordRange), annotatedCompleti
ons, selectedIndex, |
| 489 !this._isCaretAtEndOfPrompt(), this.text()); | 489 !this._isCaretAtEndOfPrompt(), this.text()); |
| 490 | 490 |
| 491 var beforeRange = this._createRange(); | 491 var beforeRange = this._createRange(); |
| 492 beforeRange.setStart(this._element, 0); | 492 beforeRange.setStart(this._element, 0); |
| 493 beforeRange.setEnd(fullWordRange.startContainer, fullWordRange.startOffset); | 493 beforeRange.setEnd(fullWordRange.startContainer, fullWordRange.startOffset); |
| 494 this._queryRange = new WebInspector.TextRange( | 494 this._queryRange = new Common.TextRange( |
| 495 0, beforeRange.toString().length, 0, beforeRange.toString().length + ful
lWordRange.toString().length); | 495 0, beforeRange.toString().length, 0, beforeRange.toString().length + ful
lWordRange.toString().length); |
| 496 | 496 |
| 497 if (selectedIndex === -1) | 497 if (selectedIndex === -1) |
| 498 return; | 498 return; |
| 499 this.applySuggestion(annotatedCompletions[selectedIndex].title, true); | 499 this.applySuggestion(annotatedCompletions[selectedIndex].title, true); |
| 500 } | 500 } |
| 501 | 501 |
| 502 /** | 502 /** |
| 503 * @override | 503 * @override |
| 504 * @param {string} suggestion | 504 * @param {string} suggestion |
| 505 * @param {boolean=} isIntermediateSuggestion | 505 * @param {boolean=} isIntermediateSuggestion |
| 506 */ | 506 */ |
| 507 applySuggestion(suggestion, isIntermediateSuggestion) { | 507 applySuggestion(suggestion, isIntermediateSuggestion) { |
| 508 if (!this._queryRange) | 508 if (!this._queryRange) |
| 509 return; | 509 return; |
| 510 this._currentSuggestion = suggestion; | 510 this._currentSuggestion = suggestion; |
| 511 this._refreshGhostText(); | 511 this._refreshGhostText(); |
| 512 if (isIntermediateSuggestion) | 512 if (isIntermediateSuggestion) |
| 513 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemApplied); | 513 this.dispatchEventToListeners(UI.TextPrompt.Events.ItemApplied); |
| 514 } | 514 } |
| 515 | 515 |
| 516 /** | 516 /** |
| 517 * @override | 517 * @override |
| 518 */ | 518 */ |
| 519 acceptSuggestion() { | 519 acceptSuggestion() { |
| 520 this._acceptSuggestionInternal(); | 520 this._acceptSuggestionInternal(); |
| 521 } | 521 } |
| 522 | 522 |
| 523 /** | 523 /** |
| 524 * @return {boolean} | 524 * @return {boolean} |
| 525 */ | 525 */ |
| 526 _acceptSuggestionInternal() { | 526 _acceptSuggestionInternal() { |
| 527 if (!this._queryRange) | 527 if (!this._queryRange) |
| 528 return false; | 528 return false; |
| 529 | 529 |
| 530 this._element.textContent = this.textWithCurrentSuggestion(); | 530 this._element.textContent = this.textWithCurrentSuggestion(); |
| 531 this.setDOMSelection( | 531 this.setDOMSelection( |
| 532 this._queryRange.startColumn + this._currentSuggestion.length, | 532 this._queryRange.startColumn + this._currentSuggestion.length, |
| 533 this._queryRange.startColumn + this._currentSuggestion.length); | 533 this._queryRange.startColumn + this._currentSuggestion.length); |
| 534 | 534 |
| 535 this.clearAutocomplete(); | 535 this.clearAutocomplete(); |
| 536 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemAccepted); | 536 this.dispatchEventToListeners(UI.TextPrompt.Events.ItemAccepted); |
| 537 | 537 |
| 538 return true; | 538 return true; |
| 539 } | 539 } |
| 540 | 540 |
| 541 /** | 541 /** |
| 542 * @param {number} startColumn | 542 * @param {number} startColumn |
| 543 * @param {number} endColumn | 543 * @param {number} endColumn |
| 544 */ | 544 */ |
| 545 setDOMSelection(startColumn, endColumn) { | 545 setDOMSelection(startColumn, endColumn) { |
| 546 this._element.normalize(); | 546 this._element.normalize(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 } | 631 } |
| 632 | 632 |
| 633 /** | 633 /** |
| 634 * @return {?Element} | 634 * @return {?Element} |
| 635 */ | 635 */ |
| 636 proxyElementForTests() { | 636 proxyElementForTests() { |
| 637 return this._proxyElement || null; | 637 return this._proxyElement || null; |
| 638 } | 638 } |
| 639 }; | 639 }; |
| 640 | 640 |
| 641 WebInspector.TextPrompt.DefaultAutocompletionTimeout = 250; | 641 UI.TextPrompt.DefaultAutocompletionTimeout = 250; |
| 642 | 642 |
| 643 /** @enum {symbol} */ | 643 /** @enum {symbol} */ |
| 644 WebInspector.TextPrompt.Events = { | 644 UI.TextPrompt.Events = { |
| 645 ItemApplied: Symbol('text-prompt-item-applied'), | 645 ItemApplied: Symbol('text-prompt-item-applied'), |
| 646 ItemAccepted: Symbol('text-prompt-item-accepted') | 646 ItemAccepted: Symbol('text-prompt-item-accepted') |
| 647 }; | 647 }; |
| OLD | NEW |