2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @implements {WebInspector.TextFilterUI.SuggestionBuilder}
34 * @param {!Array.<string>} keys
36 WebInspector.FilterSuggestionBuilder = function(keys)
40 this._valueLists = {};
43 /** @typedef {{type: string, data: string, negative: boolean}} */
44 WebInspector.FilterSuggestionBuilder.Filter;
46 WebInspector.FilterSuggestionBuilder.prototype = {
48 * @param {!HTMLInputElement} input
49 * @return {?Array.<string>}
51 buildSuggestions: function(input)
53 var text = input.value;
54 var end = input.selectionEnd;
55 if (end != text.length)
58 var start = input.selectionStart;
59 text = text.substring(0, start);
60 var prefixIndex = text.lastIndexOf(" ") + 1;
62 var prefix = text.substring(prefixIndex);
66 var negative = prefix.startsWith("-");
68 prefix = prefix.substring(1);
69 var modifier = negative ? "-" : "";
70 var valueDelimiterIndex = prefix.indexOf(":");
73 if (valueDelimiterIndex === -1) {
74 var matcher = new RegExp("^" + prefix.escapeForRegExp(), "i");
75 for (var j = 0; j < this._keys.length; ++j) {
76 if (this._keys[j].match(matcher))
77 suggestions.push(modifier + this._keys[j] + ":");
80 var key = prefix.substring(0, valueDelimiterIndex);
81 var value = prefix.substring(valueDelimiterIndex + 1);
82 var matcher = new RegExp("^" + value.escapeForRegExp(), "i");
83 var items = this._values(key);
84 for (var i = 0; i < items.length; ++i) {
85 if (items[i].match(matcher) && (items[i] !== value))
86 suggestions.push(modifier + key + ":" + items[i]);
93 * @param {!HTMLInputElement} input
94 * @param {string} suggestion
95 * @param {boolean} isIntermediate
97 applySuggestion: function(input, suggestion, isIntermediate)
99 var text = input.value;
101 var start = input.selectionStart;
102 text = text.substring(0, start);
103 var prefixIndex = text.lastIndexOf(" ") + 1;
105 text = text.substring(0, prefixIndex) + suggestion;
109 input.setSelectionRange(start, text.length);
113 * @param {!HTMLInputElement} input
115 unapplySuggestion: function(input)
117 var start = input.selectionStart;
118 var end = input.selectionEnd;
119 var text = input.value;
120 if (start !== end && end === text.length)
121 input.value = text.substring(0, start);
125 * @param {string} key
126 * @return {!Array.<string>}
128 _values: function(key)
130 var result = this._valueLists[key];
139 * @param {string} key
140 * @param {?string=} value
142 addItem: function(key, value)
147 var set = this._valueSets[key];
148 var list = this._valueLists[key];
151 this._valueSets[key] = set;
153 this._valueLists[key] = list;
164 * @param {string} query
165 * @return {{text: !Array.<string>, filters: !Array.<!WebInspector.FilterSuggestionBuilder.Filter>}}
167 parseQuery: function(query)
175 var colonIndex = query.indexOf(":", i);
176 if (colonIndex == -1) {
177 part = query.substring(j);
182 var spaceIndex = query.lastIndexOf(" ", colonIndex);
183 var key = query.substring(spaceIndex + 1, colonIndex);
184 var negative = key.startsWith("-");
186 key = key.substring(1);
187 if (this._keys.indexOf(key) == -1) {
191 part = spaceIndex > j ? query.substring(j, spaceIndex) : "";
194 var nextSpace = query.indexOf(" ", colonIndex + 1);
195 if (nextSpace == -1) {
196 filters.push({type: key, data: query.substring(colonIndex + 1), negative: negative});
199 filters.push({type: key, data: query.substring(colonIndex + 1, nextSpace), negative: negative});
203 return {text: text, filters: filters};