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