OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google 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 are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 /** |
| 32 * @constructor |
| 33 * @implements {WebInspector.TextFilterUI.SuggestionBuilder} |
| 34 * @param {!Array.<string>} keys |
| 35 */ |
| 36 WebInspector.SuggestionBuilder = function(keys) |
| 37 { |
| 38 this._keys = keys; |
| 39 this._valueSets = {}; |
| 40 this._valueLists = {}; |
| 41 } |
| 42 |
| 43 WebInspector.SuggestionBuilder.prototype = { |
| 44 /** |
| 45 * @param {!HTMLInputElement} input |
| 46 * @return {?Array.<string>} |
| 47 */ |
| 48 buildSuggestions: function(input) |
| 49 { |
| 50 var text = input.value; |
| 51 var cursorPosition = input.selectionStart; |
| 52 if (cursorPosition !== text.length) |
| 53 return null; |
| 54 |
| 55 var prefix = text.substring(text.lastIndexOf(" ") + 1); |
| 56 var valueDelimiterIndex = prefix.indexOf(":"); |
| 57 |
| 58 var suggestions = []; |
| 59 if (valueDelimiterIndex === -1) { |
| 60 for (var j = 0; j < this._keys.length; ++j) { |
| 61 if (this._keys[j].startsWith(prefix)) |
| 62 suggestions.push(this._keys[j] + ":"); |
| 63 } |
| 64 } else { |
| 65 var key = prefix.substring(0, valueDelimiterIndex); |
| 66 var value = prefix.substring(valueDelimiterIndex + 1); |
| 67 var items = this._values(key); |
| 68 for (var i = 0; i < items.length; ++i) { |
| 69 if (items[i].startsWith(value) && (items[i] !== value)) |
| 70 suggestions.push(key + ":" + items[i]); |
| 71 } |
| 72 } |
| 73 return suggestions; |
| 74 }, |
| 75 |
| 76 /** |
| 77 * @param {!HTMLInputElement} input |
| 78 * @param {string} suggestion |
| 79 */ |
| 80 applySuggestion: function(input, suggestion) |
| 81 { |
| 82 var text = input.value; |
| 83 text = text.substring(0, text.lastIndexOf(" ") + 1) + suggestion; |
| 84 input.value = text; |
| 85 }, |
| 86 |
| 87 /** |
| 88 * @param {string} key |
| 89 * @return {!Array.<string>} |
| 90 */ |
| 91 _values: function(key) |
| 92 { |
| 93 var result = this._valueLists[key]; |
| 94 if (!result) |
| 95 return []; |
| 96 |
| 97 result.sort(); |
| 98 return result; |
| 99 }, |
| 100 |
| 101 /** |
| 102 * @param {string} key |
| 103 * @param {?string=} value |
| 104 */ |
| 105 addItem: function(key, value) |
| 106 { |
| 107 if (!value) |
| 108 return; |
| 109 |
| 110 var set = this._valueSets[key]; |
| 111 var list = this._valueLists[key]; |
| 112 if (!set) { |
| 113 set = {}; |
| 114 this._valueSets[key] = set; |
| 115 list = []; |
| 116 this._valueLists[key] = list; |
| 117 } |
| 118 |
| 119 if (set[value]) |
| 120 return; |
| 121 |
| 122 set[value] = true; |
| 123 list.push(value); |
| 124 }, |
| 125 |
| 126 /** |
| 127 * @param {string} query |
| 128 * @return {{textual: ?RegExp, specialized: !Object.<string, string>}} |
| 129 */ |
| 130 parseQuery: function(query) |
| 131 { |
| 132 var specialized = {}; |
| 133 var parts = query.split(/\s+/); |
| 134 var textualParts = []; |
| 135 for (var i = 0; i < parts.length; ++i) { |
| 136 var part = parts[i]; |
| 137 var colonIndex = part.indexOf(":"); |
| 138 if (colonIndex !== -1) { |
| 139 var field = part.substr(0, colonIndex); |
| 140 if (this._keys.indexOf(field) !== -1) { |
| 141 var value = part.substr(colonIndex + 1); |
| 142 specialized[field] = value; |
| 143 continue; |
| 144 } |
| 145 } |
| 146 if (part) |
| 147 textualParts.push(part.escapeForRegExp()); |
| 148 } |
| 149 var textual = textualParts.length ? new RegExp(textualParts.join("\\s*")
, "i") : null; |
| 150 return {textual: textual, specialized: specialized}; |
| 151 } |
| 152 }; |
OLD | NEW |