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 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ |
| 25 |
| 26 WebInspector.DatabaseQueryView = function(database) |
| 27 { |
| 28 WebInspector.View.call(this); |
| 29 |
| 30 this.database = database; |
| 31 |
| 32 this.element.addStyleClass("storage-view"); |
| 33 this.element.addStyleClass("query"); |
| 34 this.element.tabIndex = 0; |
| 35 |
| 36 this.element.addEventListener("selectstart", this._selectStart.bind(this), f
alse); |
| 37 |
| 38 this.promptElement = document.createElement("div"); |
| 39 this.promptElement.className = "database-query-prompt"; |
| 40 this.promptElement.appendChild(document.createElement("br")); |
| 41 this.promptElement.handleKeyEvent = this._promptKeyDown.bind(this); |
| 42 this.element.appendChild(this.promptElement); |
| 43 |
| 44 this.prompt = new WebInspector.TextPrompt(this.promptElement, this.completio
ns.bind(this), " "); |
| 45 } |
| 46 |
| 47 WebInspector.DatabaseQueryView.prototype = { |
| 48 show: function(parentElement) |
| 49 { |
| 50 WebInspector.View.prototype.show.call(this, parentElement); |
| 51 |
| 52 function moveBackIfOutside() |
| 53 { |
| 54 if (!this.prompt.isCaretInsidePrompt() && window.getSelection().isCo
llapsed) |
| 55 this.prompt.moveCaretToEndOfPrompt(); |
| 56 } |
| 57 |
| 58 setTimeout(moveBackIfOutside.bind(this), 0); |
| 59 }, |
| 60 |
| 61 completions: function(wordRange, bestMatchOnly, completionsReadyCallback) |
| 62 { |
| 63 var prefix = wordRange.toString().toLowerCase(); |
| 64 if (!prefix.length) |
| 65 return; |
| 66 |
| 67 var results = []; |
| 68 |
| 69 function accumulateMatches(textArray) |
| 70 { |
| 71 if (bestMatchOnly && results.length) |
| 72 return; |
| 73 for (var i = 0; i < textArray.length; ++i) { |
| 74 var text = textArray[i].toLowerCase(); |
| 75 if (text.length < prefix.length) |
| 76 continue; |
| 77 if (text.indexOf(prefix) !== 0) |
| 78 continue; |
| 79 results.push(textArray[i]); |
| 80 if (bestMatchOnly) |
| 81 return; |
| 82 } |
| 83 } |
| 84 |
| 85 function tableNamesCallback(tableNames) |
| 86 { |
| 87 accumulateMatches(tableNames.map(function(name) { return name + " "
})); |
| 88 accumulateMatches(["SELECT ", "FROM ", "WHERE ", "LIMIT ", "DELETE F
ROM ", "CREATE ", "DROP ", "TABLE ", "INDEX ", "UPDATE ", "INSERT INTO ", "VALUE
S ("]); |
| 89 |
| 90 completionsReadyCallback(results); |
| 91 } |
| 92 this.database.getTableNames(tableNamesCallback); |
| 93 }, |
| 94 |
| 95 _promptKeyDown: function(event) |
| 96 { |
| 97 switch (event.keyIdentifier) { |
| 98 case "Enter": |
| 99 this._enterKeyPressed(event); |
| 100 return; |
| 101 } |
| 102 |
| 103 this.prompt.handleKeyEvent(event); |
| 104 }, |
| 105 |
| 106 _selectStart: function(event) |
| 107 { |
| 108 if (this._selectionTimeout) |
| 109 clearTimeout(this._selectionTimeout); |
| 110 |
| 111 this.prompt.clearAutoComplete(); |
| 112 |
| 113 function moveBackIfOutside() |
| 114 { |
| 115 delete this._selectionTimeout; |
| 116 if (!this.prompt.isCaretInsidePrompt() && window.getSelection().isCo
llapsed) |
| 117 this.prompt.moveCaretToEndOfPrompt(); |
| 118 this.prompt.autoCompleteSoon(); |
| 119 } |
| 120 |
| 121 this._selectionTimeout = setTimeout(moveBackIfOutside.bind(this), 100); |
| 122 }, |
| 123 |
| 124 _enterKeyPressed: function(event) |
| 125 { |
| 126 event.preventDefault(); |
| 127 event.stopPropagation(); |
| 128 |
| 129 this.prompt.clearAutoComplete(true); |
| 130 |
| 131 var query = this.prompt.text; |
| 132 if (!query.length) |
| 133 return; |
| 134 |
| 135 this.prompt.history.push(query); |
| 136 this.prompt.historyOffset = 0; |
| 137 this.prompt.text = ""; |
| 138 |
| 139 this.database.executeSql(query, this._queryFinished.bind(this, query), t
his._queryError.bind(this, query)); |
| 140 }, |
| 141 |
| 142 _queryFinished: function(query, result) |
| 143 { |
| 144 var dataGrid = WebInspector.panels.storage.dataGridForResult(result); |
| 145 if (!dataGrid) |
| 146 return; |
| 147 dataGrid.element.addStyleClass("inline"); |
| 148 this._appendQueryResult(query, dataGrid.element); |
| 149 |
| 150 if (query.match(/^create /i) || query.match(/^drop table /i)) |
| 151 WebInspector.panels.storage.updateDatabaseTables(this.database); |
| 152 }, |
| 153 |
| 154 _queryError: function(query, error) |
| 155 { |
| 156 if (error.code == 1) |
| 157 var message = error.message; |
| 158 else if (error.code == 2) |
| 159 var message = WebInspector.UIString("Database no longer has expected
version."); |
| 160 else |
| 161 var message = WebInspector.UIString("An unexpected error %s occurred
.", error.code); |
| 162 |
| 163 this._appendQueryResult(query, message, "error"); |
| 164 }, |
| 165 |
| 166 _appendQueryResult: function(query, result, resultClassName) |
| 167 { |
| 168 var element = document.createElement("div"); |
| 169 element.className = "database-user-query"; |
| 170 |
| 171 var commandTextElement = document.createElement("span"); |
| 172 commandTextElement.className = "database-query-text"; |
| 173 commandTextElement.textContent = query; |
| 174 element.appendChild(commandTextElement); |
| 175 |
| 176 var resultElement = document.createElement("div"); |
| 177 resultElement.className = "database-query-result"; |
| 178 |
| 179 if (resultClassName) |
| 180 resultElement.addStyleClass(resultClassName); |
| 181 |
| 182 if (typeof result === "string" || result instanceof String) |
| 183 resultElement.textContent = result; |
| 184 else if (result && result.nodeName) |
| 185 resultElement.appendChild(result); |
| 186 |
| 187 if (resultElement.childNodes.length) |
| 188 element.appendChild(resultElement); |
| 189 |
| 190 this.element.insertBefore(element, this.promptElement); |
| 191 this.promptElement.scrollIntoView(false); |
| 192 } |
| 193 } |
| 194 |
| 195 WebInspector.DatabaseQueryView.prototype.__proto__ = WebInspector.View.prototype
; |
OLD | NEW |