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.FilterSuggestionBuilder = function(keys) 37 { 38 this._keys = keys; 39 this._valueSets = {}; 40 this._valueLists = {}; 41 } 42 43 WebInspector.FilterSuggestionBuilder.prototype = { 44 /** 45 * @param {!HTMLInputElement} input 46 * @return {?Array.<string>} 47 */ 48 buildSuggestions: function(input) 49 { 50 var text = input.value; 51 var end = input.selectionEnd; 52 if (end != text.length) 53 return null; 54 55 var start = input.selectionStart; 56 text = text.substring(0, start); 57 var prefixIndex = text.lastIndexOf(" ") + 1; 58 59 var prefix = text.substring(prefixIndex); 60 if (!prefix) 61 return []; 62 63 var valueDelimiterIndex = prefix.indexOf(":"); 64 65 var suggestions = []; 66 if (valueDelimiterIndex === -1) { 67 for (var j = 0; j < this._keys.length; ++j) { 68 if (this._keys[j].startsWith(prefix)) 69 suggestions.push(this._keys[j] + ":"); 70 } 71 } else { 72 var key = prefix.substring(0, valueDelimiterIndex); 73 var value = prefix.substring(valueDelimiterIndex + 1); 74 var items = this._values(key); 75 for (var i = 0; i < items.length; ++i) { 76 if (items[i].startsWith(value) && (items[i] !== value)) 77 suggestions.push(key + ":" + items[i]); 78 } 79 } 80 return suggestions; 81 }, 82 83 /** 84 * @param {!HTMLInputElement} input 85 * @param {string} suggestion 86 * @param {boolean} isIntermediate 87 */ 88 applySuggestion: function(input, suggestion, isIntermediate) 89 { 90 var text = input.value; 91 92 var start = input.selectionStart; 93 text = text.substring(0, start); 94 var prefixIndex = text.lastIndexOf(" ") + 1; 95 96 text = text.substring(0, prefixIndex) + suggestion; 97 input.value = text; 98 if (!isIntermediate) 99 start = text.length; 100 input.setSelectionRange(start, text.length); 101 }, 102 103 /** 104 * @param {!HTMLInputElement} input 105 */ 106 unapplySuggestion: function(input) 107 { 108 var start = input.selectionStart; 109 var end = input.selectionEnd; 110 var text = input.value; 111 if (start !== end && end === text.length) 112 input.value = text.substring(0, start); 113 }, 114 115 /** 116 * @param {string} key 117 * @return {!Array.<string>} 118 */ 119 _values: function(key) 120 { 121 var result = this._valueLists[key]; 122 if (!result) 123 return []; 124 125 result.sort(); 126 return result; 127 }, 128 129 /** 130 * @param {string} key 131 * @param {?string=} value 132 */ 133 addItem: function(key, value) 134 { 135 if (!value) 136 return; 137 138 var set = this._valueSets[key]; 139 var list = this._valueLists[key]; 140 if (!set) { 141 set = {}; 142 this._valueSets[key] = set; 143 list = []; 144 this._valueLists[key] = list; 145 } 146 147 if (set[value]) 148 return; 149 150 set[value] = true; 151 list.push(value); 152 }, 153 154 /** 155 * @param {string} query 156 * @return {{text: !Array.<string>, filters: !Object.<string, string>}} 157 */ 158 parseQuery: function(query) 159 { 160 var filters = {}; 161 var text = []; 162 var i = 0; 163 var j = 0; 164 var part; 165 while (true) { 166 var colonIndex = query.indexOf(":", i); 167 if (colonIndex == -1) { 168 part = query.substring(j); 169 if (part) 170 text.push(part); 171 break; 172 } 173 var spaceIndex = query.lastIndexOf(" ", colonIndex); 174 var key = query.substring(spaceIndex + 1, colonIndex); 175 if (this._keys.indexOf(key) == -1) { 176 i = colonIndex + 1; 177 continue; 178 } 179 part = spaceIndex > j ? query.substring(j, spaceIndex) : ""; 180 if (part) 181 text.push(part); 182 var nextSpace = query.indexOf(" ", colonIndex + 1); 183 if (nextSpace == -1) { 184 filters[key] = query.substring(colonIndex + 1); 185 break; 186 } 187 filters[key] = query.substring(colonIndex + 1, nextSpace); 188 i = nextSpace + 1; 189 j = i; 190 } 191 return {text: text, filters: filters}; 192 } 193 }; 194