| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2008 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
| 14 * its contributors may be used to endorse or promote products derived | |
| 15 * from this software without specific prior written permission. | |
| 16 * | |
| 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 */ | |
| 28 | |
| 29 WebInspector.TextPrompt = function(element, completions, stopCharacters) | |
| 30 { | |
| 31 this.element = element; | |
| 32 this.completions = completions; | |
| 33 this.completionStopCharacters = stopCharacters; | |
| 34 this.history = []; | |
| 35 this.historyOffset = 0; | |
| 36 } | |
| 37 | |
| 38 WebInspector.TextPrompt.prototype = { | |
| 39 get text() | |
| 40 { | |
| 41 return this.element.textContent; | |
| 42 }, | |
| 43 | |
| 44 set text(x) | |
| 45 { | |
| 46 if (!x) { | |
| 47 // Append a break element instead of setting textContent to make sur
e the selection is inside the prompt. | |
| 48 this.element.removeChildren(); | |
| 49 this.element.appendChild(document.createElement("br")); | |
| 50 } else | |
| 51 this.element.textContent = x; | |
| 52 | |
| 53 this.moveCaretToEndOfPrompt(); | |
| 54 }, | |
| 55 | |
| 56 handleKeyEvent: function(event) | |
| 57 { | |
| 58 switch (event.keyIdentifier) { | |
| 59 case "Up": | |
| 60 this._upKeyPressed(event); | |
| 61 break; | |
| 62 case "Down": | |
| 63 this._downKeyPressed(event); | |
| 64 break; | |
| 65 case "U+0009": // Tab | |
| 66 this._tabKeyPressed(event); | |
| 67 break; | |
| 68 case "Right": | |
| 69 if (!this.acceptAutoComplete()) | |
| 70 this.autoCompleteSoon(); | |
| 71 break; | |
| 72 default: | |
| 73 this.clearAutoComplete(); | |
| 74 this.autoCompleteSoon(); | |
| 75 break; | |
| 76 } | |
| 77 }, | |
| 78 | |
| 79 acceptAutoComplete: function() | |
| 80 { | |
| 81 if (!this.autoCompleteElement || !this.autoCompleteElement.parentNode) | |
| 82 return false; | |
| 83 | |
| 84 var text = this.autoCompleteElement.textContent; | |
| 85 var textNode = document.createTextNode(text); | |
| 86 this.autoCompleteElement.parentNode.replaceChild(textNode, this.autoComp
leteElement); | |
| 87 delete this.autoCompleteElement; | |
| 88 | |
| 89 var finalSelectionRange = document.createRange(); | |
| 90 finalSelectionRange.setStart(textNode, text.length); | |
| 91 finalSelectionRange.setEnd(textNode, text.length); | |
| 92 | |
| 93 var selection = window.getSelection(); | |
| 94 selection.removeAllRanges(); | |
| 95 selection.addRange(finalSelectionRange); | |
| 96 | |
| 97 return true; | |
| 98 }, | |
| 99 | |
| 100 clearAutoComplete: function(includeTimeout) | |
| 101 { | |
| 102 if (includeTimeout && "_completeTimeout" in this) { | |
| 103 clearTimeout(this._completeTimeout); | |
| 104 delete this._completeTimeout; | |
| 105 } | |
| 106 | |
| 107 if (!this.autoCompleteElement) | |
| 108 return; | |
| 109 | |
| 110 if (this.autoCompleteElement.parentNode) | |
| 111 this.autoCompleteElement.parentNode.removeChild(this.autoCompleteEle
ment); | |
| 112 delete this.autoCompleteElement; | |
| 113 | |
| 114 if (!this._userEnteredRange || !this._userEnteredText) | |
| 115 return; | |
| 116 | |
| 117 this._userEnteredRange.deleteContents(); | |
| 118 | |
| 119 var userTextNode = document.createTextNode(this._userEnteredText); | |
| 120 this._userEnteredRange.insertNode(userTextNode); | |
| 121 | |
| 122 var selectionRange = document.createRange(); | |
| 123 selectionRange.setStart(userTextNode, this._userEnteredText.length); | |
| 124 selectionRange.setEnd(userTextNode, this._userEnteredText.length); | |
| 125 | |
| 126 var selection = window.getSelection(); | |
| 127 selection.removeAllRanges(); | |
| 128 selection.addRange(selectionRange); | |
| 129 | |
| 130 delete this._userEnteredRange; | |
| 131 delete this._userEnteredText; | |
| 132 }, | |
| 133 | |
| 134 autoCompleteSoon: function() | |
| 135 { | |
| 136 if (!("_completeTimeout" in this)) | |
| 137 this._completeTimeout = setTimeout(this.complete.bind(this, true), 2
50); | |
| 138 }, | |
| 139 | |
| 140 complete: function(auto) | |
| 141 { | |
| 142 this.clearAutoComplete(true); | |
| 143 | |
| 144 var selection = window.getSelection(); | |
| 145 if (!selection.rangeCount) | |
| 146 return; | |
| 147 | |
| 148 var selectionRange = selection.getRangeAt(0); | |
| 149 if (!selectionRange.commonAncestorContainer.isDescendant(this.element)) | |
| 150 return; | |
| 151 if (auto && !this.isCaretAtEndOfPrompt()) | |
| 152 return; | |
| 153 | |
| 154 var wordPrefixRange = selectionRange.startContainer.rangeOfWord(selectio
nRange.startOffset, this.completionStopCharacters, this.element, "backward"); | |
| 155 var completions = this.completions(wordPrefixRange, auto); | |
| 156 | |
| 157 if (!completions || !completions.length) | |
| 158 return; | |
| 159 | |
| 160 var fullWordRange = document.createRange(); | |
| 161 fullWordRange.setStart(wordPrefixRange.startContainer, wordPrefixRange.s
tartOffset); | |
| 162 fullWordRange.setEnd(selectionRange.endContainer, selectionRange.endOffs
et); | |
| 163 | |
| 164 if (completions.length === 1 || selection.isCollapsed || auto) { | |
| 165 var completionText = completions[0]; | |
| 166 } else { | |
| 167 var currentText = fullWordRange.toString(); | |
| 168 | |
| 169 var foundIndex = null; | |
| 170 for (var i = 0; i < completions.length; ++i) { | |
| 171 if (completions[i] === currentText) | |
| 172 foundIndex = i; | |
| 173 } | |
| 174 | |
| 175 if (foundIndex === null || (foundIndex + 1) >= completions.length) | |
| 176 var completionText = completions[0]; | |
| 177 else | |
| 178 var completionText = completions[foundIndex + 1]; | |
| 179 } | |
| 180 | |
| 181 var wordPrefixLength = wordPrefixRange.toString().length; | |
| 182 | |
| 183 this._userEnteredRange = fullWordRange; | |
| 184 this._userEnteredText = fullWordRange.toString(); | |
| 185 | |
| 186 fullWordRange.deleteContents(); | |
| 187 | |
| 188 var finalSelectionRange = document.createRange(); | |
| 189 | |
| 190 if (auto) { | |
| 191 var prefixText = completionText.substring(0, wordPrefixLength); | |
| 192 var suffixText = completionText.substring(wordPrefixLength); | |
| 193 | |
| 194 var prefixTextNode = document.createTextNode(prefixText); | |
| 195 fullWordRange.insertNode(prefixTextNode); | |
| 196 | |
| 197 this.autoCompleteElement = document.createElement("span"); | |
| 198 this.autoCompleteElement.className = "auto-complete-text"; | |
| 199 this.autoCompleteElement.textContent = suffixText; | |
| 200 | |
| 201 prefixTextNode.parentNode.insertBefore(this.autoCompleteElement, pre
fixTextNode.nextSibling); | |
| 202 | |
| 203 finalSelectionRange.setStart(prefixTextNode, wordPrefixLength); | |
| 204 finalSelectionRange.setEnd(prefixTextNode, wordPrefixLength); | |
| 205 } else { | |
| 206 var completionTextNode = document.createTextNode(completionText); | |
| 207 fullWordRange.insertNode(completionTextNode); | |
| 208 | |
| 209 if (completions.length > 1) | |
| 210 finalSelectionRange.setStart(completionTextNode, wordPrefixLengt
h); | |
| 211 else | |
| 212 finalSelectionRange.setStart(completionTextNode, completionText.
length); | |
| 213 | |
| 214 finalSelectionRange.setEnd(completionTextNode, completionText.length
); | |
| 215 } | |
| 216 | |
| 217 selection.removeAllRanges(); | |
| 218 selection.addRange(finalSelectionRange); | |
| 219 }, | |
| 220 | |
| 221 isCaretInsidePrompt: function() | |
| 222 { | |
| 223 return this.element.isInsertionCaretInside(); | |
| 224 }, | |
| 225 | |
| 226 isCaretAtEndOfPrompt: function() | |
| 227 { | |
| 228 var selection = window.getSelection(); | |
| 229 if (!selection.rangeCount || !selection.isCollapsed) | |
| 230 return false; | |
| 231 | |
| 232 var selectionRange = selection.getRangeAt(0); | |
| 233 var node = selectionRange.startContainer; | |
| 234 if (node !== this.element && !node.isDescendant(this.element)) | |
| 235 return false; | |
| 236 | |
| 237 if (node.nodeType === Node.TEXT_NODE && selectionRange.startOffset < nod
e.nodeValue.length) | |
| 238 return false; | |
| 239 | |
| 240 var foundNextText = false; | |
| 241 while (node) { | |
| 242 if (node.nodeType === Node.TEXT_NODE && node.nodeValue.length) { | |
| 243 if (foundNextText) | |
| 244 return false; | |
| 245 foundNextText = true; | |
| 246 } | |
| 247 | |
| 248 node = node.traverseNextNode(false, this.element); | |
| 249 } | |
| 250 | |
| 251 return true; | |
| 252 }, | |
| 253 | |
| 254 moveCaretToEndOfPrompt: function() | |
| 255 { | |
| 256 var selection = window.getSelection(); | |
| 257 var selectionRange = document.createRange(); | |
| 258 | |
| 259 var offset = this.element.childNodes.length; | |
| 260 selectionRange.setStart(this.element, offset); | |
| 261 selectionRange.setEnd(this.element, offset); | |
| 262 | |
| 263 selection.removeAllRanges(); | |
| 264 selection.addRange(selectionRange); | |
| 265 }, | |
| 266 | |
| 267 _tabKeyPressed: function(event) | |
| 268 { | |
| 269 event.preventDefault(); | |
| 270 event.stopPropagation(); | |
| 271 | |
| 272 this.complete(); | |
| 273 }, | |
| 274 | |
| 275 _upKeyPressed: function(event) | |
| 276 { | |
| 277 event.preventDefault(); | |
| 278 event.stopPropagation(); | |
| 279 | |
| 280 if (this.historyOffset == this.history.length) | |
| 281 return; | |
| 282 | |
| 283 this.clearAutoComplete(true); | |
| 284 | |
| 285 if (this.historyOffset == 0) | |
| 286 this.tempSavedCommand = this.text; | |
| 287 | |
| 288 ++this.historyOffset; | |
| 289 this.text = this.history[this.history.length - this.historyOffset]; | |
| 290 }, | |
| 291 | |
| 292 _downKeyPressed: function(event) | |
| 293 { | |
| 294 event.preventDefault(); | |
| 295 event.stopPropagation(); | |
| 296 | |
| 297 if (this.historyOffset == 0) | |
| 298 return; | |
| 299 | |
| 300 this.clearAutoComplete(true); | |
| 301 | |
| 302 --this.historyOffset; | |
| 303 | |
| 304 if (this.historyOffset == 0) { | |
| 305 this.text = this.tempSavedCommand; | |
| 306 delete this.tempSavedCommand; | |
| 307 return; | |
| 308 } | |
| 309 | |
| 310 this.text = this.history[this.history.length - this.historyOffset]; | |
| 311 } | |
| 312 } | |
| OLD | NEW |