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 * @extends {WebInspector.Object}
35 WebInspector.FilterBar = function()
37 this._filtersShown = false;
38 this._element = document.createElement("div");
39 this._element.className = "hbox";
41 this._filterButton = new WebInspector.StatusBarButton(WebInspector.UIString("Filter"), "filters-toggle", 3);
42 this._filterButton.element.addEventListener("click", this._handleFilterButtonClick.bind(this), false);
47 WebInspector.FilterBar.Events = {
48 FiltersToggled: "FiltersToggled"
51 WebInspector.FilterBar.FilterBarState = {
52 Inactive : "inactive",
57 WebInspector.FilterBar.prototype = {
59 * @param {string} name
61 setName: function(name)
63 this._stateSetting = WebInspector.settings.createSetting("filterBar-" + name + "-toggled", false);
64 this._setState(this._stateSetting.get());
68 * @return {!WebInspector.StatusBarButton}
70 filterButton: function()
72 return this._filterButton;
78 filtersElement: function()
86 filtersToggled: function()
88 return this._filtersShown;
92 * @param {!WebInspector.FilterUI} filter
94 addFilter: function(filter)
96 this._filters.push(filter);
97 this._element.appendChild(filter.element());
98 filter.addEventListener(WebInspector.FilterUI.Events.FilterChanged, this._filterChanged, this);
99 this._updateFilterButton();
103 * @param {!WebInspector.Event} event
105 _filterChanged: function(event)
107 this._updateFilterButton();
113 _filterBarState: function()
115 if (this._filtersShown)
116 return WebInspector.FilterBar.FilterBarState.Shown;
117 var isActive = false;
118 for (var i = 0; i < this._filters.length; ++i) {
119 if (this._filters[i].isActive())
120 return WebInspector.FilterBar.FilterBarState.Active;
122 return WebInspector.FilterBar.FilterBarState.Inactive;
125 _updateFilterButton: function()
127 this._filterButton.state = this._filterBarState();
131 * @param {!Event} event
133 _handleFilterButtonClick: function(event)
135 this._setState(!this._filtersShown);
139 * @param {boolean} filtersShown
141 _setState: function(filtersShown)
143 if (this._filtersShown === filtersShown)
146 this._filtersShown = filtersShown;
147 if (this._stateSetting)
148 this._stateSetting.set(filtersShown);
150 this._updateFilterButton();
151 this.dispatchEventToListeners(WebInspector.FilterBar.Events.FiltersToggled, this._filtersShown);
152 if (this._filtersShown) {
153 for (var i = 0; i < this._filters.length; ++i) {
154 if (this._filters[i] instanceof WebInspector.TextFilterUI) {
155 var textFilterUI = /** @type {!WebInspector.TextFilterUI} */ (this._filters[i]);
156 textFilterUI.focus();
164 this._element.removeChildren();
166 this._updateFilterButton();
169 __proto__: WebInspector.Object.prototype
174 * @extends {WebInspector.EventTarget}
176 WebInspector.FilterUI = function()
180 WebInspector.FilterUI.Events = {
181 FilterChanged: "FilterChanged"
184 WebInspector.FilterUI.prototype = {
188 isActive: function() { },
193 element: function() { }
198 * @extends {WebInspector.Object}
199 * @implements {WebInspector.FilterUI}
200 * @implements {WebInspector.SuggestBoxDelegate}
201 * @param {boolean=} supportRegex
203 WebInspector.TextFilterUI = function(supportRegex)
205 this._supportRegex = !!supportRegex;
208 this._filterElement = document.createElement("div");
209 this._filterElement.className = "filter-text-filter";
211 this._filterInputElement = /** @type {!HTMLInputElement} */ (this._filterElement.createChild("input", "search-replace toolbar-replace-control"));
212 this._filterInputElement.placeholder = WebInspector.UIString("Filter");
213 this._filterInputElement.id = "filter-input-field";
214 this._filterInputElement.addEventListener("mousedown", this._onFilterFieldManualFocus.bind(this), false); // when the search field is manually selected
215 this._filterInputElement.addEventListener("input", this._onInput.bind(this), false);
216 this._filterInputElement.addEventListener("change", this._onChange.bind(this), false);
217 this._filterInputElement.addEventListener("keydown", this._onInputKeyDown.bind(this), true);
218 this._filterInputElement.addEventListener("blur", this._onBlur.bind(this), true);
220 /** @type {?WebInspector.TextFilterUI.SuggestionBuilder} */
221 this._suggestionBuilder = null;
223 this._suggestBox = new WebInspector.SuggestBox(this);
225 if (this._supportRegex) {
226 this._filterElement.classList.add("supports-regex");
227 this._regexCheckBox = this._filterElement.createChild("input");
228 this._regexCheckBox.type = "checkbox";
229 this._regexCheckBox.id = "text-filter-regex";
230 this._regexCheckBox.addEventListener("change", this._onInput.bind(this), false);
232 this._regexLabel = this._filterElement.createChild("label");
233 this._regexLabel.htmlFor = "text-filter-regex";
234 this._regexLabel.textContent = WebInspector.UIString("Regex");
238 WebInspector.TextFilterUI.prototype = {
244 return !!this._filterInputElement.value;
252 return this._filterElement;
260 return this._filterInputElement.value;
264 * @param {string} value
266 setValue: function(value)
268 this._filterInputElement.value = value;
269 this._valueChanged(false);
281 * @param {!Event} event
283 _onFilterFieldManualFocus: function(event)
285 WebInspector.setCurrentFocusElement(event.target);
289 * @param {!Event} event
291 _onBlur: function(event)
293 this._cancelSuggestion();
296 _cancelSuggestion: function()
298 if (this._suggestionBuilder && this._suggestBox.visible) {
299 this._suggestionBuilder.unapplySuggestion(this._filterInputElement);
300 this._suggestBox.hide();
306 this._valueChanged(true);
309 _onChange: function()
311 this._valueChanged(false);
316 this._filterInputElement.focus();
320 * @param {?WebInspector.TextFilterUI.SuggestionBuilder} suggestionBuilder
322 setSuggestionBuilder: function(suggestionBuilder)
324 this._cancelSuggestion();
325 this._suggestionBuilder = suggestionBuilder;
328 _updateSuggestions: function()
330 if (!this._suggestionBuilder)
332 var suggestions = this._suggestionBuilder.buildSuggestions(this._filterInputElement);
333 if (suggestions && suggestions.length) {
334 if (this._suppressSuggestion)
335 delete this._suppressSuggestion;
337 this._suggestionBuilder.applySuggestion(this._filterInputElement, suggestions[0], true);
338 var anchorBox = this._filterInputElement.boxInWindow().relativeTo(new AnchorBox(-3, 0));
339 this._suggestBox.updateSuggestions(anchorBox, suggestions, 0, true, "");
341 this._suggestBox.hide();
346 * @param {boolean} showSuggestions
348 _valueChanged: function(showSuggestions)
351 this._updateSuggestions();
353 this._suggestBox.hide();
355 var filterQuery = this.value();
358 this._filterInputElement.classList.remove("filter-text-invalid");
360 if (this._supportRegex && this._regexCheckBox.checked) {
362 this._regex = new RegExp(filterQuery, "i");
364 this._filterInputElement.classList.add("filter-text-invalid");
367 this._regex = createPlainTextSearchRegex(filterQuery, "i");
371 this._dispatchFilterChanged();
374 _dispatchFilterChanged: function()
376 this.dispatchEventToListeners(WebInspector.FilterUI.Events.FilterChanged, null);
380 * @param {!Event} event
383 _onInputKeyDown: function(event)
386 if (event.keyIdentifier === "U+0008") { // Backspace
387 this._suppressSuggestion = true;
388 } else if (this._suggestBox.visible()) {
389 if (event.keyIdentifier === "U+001B") { // Esc
390 this._cancelSuggestion();
392 } else if (event.keyIdentifier === "U+0009") { // Tab
393 this._suggestBox.acceptSuggestion();
394 this._valueChanged(true);
397 handled = this._suggestBox.keyPressed(/** @type {!KeyboardEvent} */ (event));
407 * @param {string} suggestion
408 * @param {boolean=} isIntermediateSuggestion
410 applySuggestion: function(suggestion, isIntermediateSuggestion)
412 if (!this._suggestionBuilder)
414 this._suggestionBuilder.applySuggestion(this._filterInputElement, suggestion, !!isIntermediateSuggestion);
415 if (isIntermediateSuggestion)
416 this._dispatchFilterChanged();
420 acceptSuggestion: function()
422 this._filterInputElement.scrollLeft = this._filterInputElement.scrollWidth;
423 this._valueChanged(true);
426 __proto__: WebInspector.Object.prototype
432 WebInspector.TextFilterUI.SuggestionBuilder = function()
436 WebInspector.TextFilterUI.SuggestionBuilder.prototype = {
438 * @param {!HTMLInputElement} input
439 * @return {?Array.<string>}
441 buildSuggestions: function(input) { },
444 * @param {!HTMLInputElement} input
445 * @param {string} suggestion
446 * @param {boolean} isIntermediate
448 applySuggestion: function(input, suggestion, isIntermediate) { },
451 * @param {!HTMLInputElement} input
453 unapplySuggestion: function(input) { }
458 * @extends {WebInspector.Object}
459 * @implements {WebInspector.FilterUI}
460 * @param {!Array.<!WebInspector.NamedBitSetFilterUI.Item>} items
461 * @param {!WebInspector.Setting=} setting
463 WebInspector.NamedBitSetFilterUI = function(items, setting)
465 this._filtersElement = document.createElement("div");
466 this._filtersElement.className = "filter-bitset-filter status-bar-item";
467 this._filtersElement.title = WebInspector.UIString("Use %s Click to select multiple types.", WebInspector.KeyboardShortcut.shortcutToString("", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta));
469 this._allowedTypes = {};
470 this._typeFilterElements = {};
471 this._addBit(WebInspector.NamedBitSetFilterUI.ALL_TYPES, WebInspector.UIString("All"));
472 this._filtersElement.createChild("div", "filter-bitset-filter-divider");
474 for (var i = 0; i < items.length; ++i)
475 this._addBit(items[i].name, items[i].label);
478 this._setting = setting;
479 setting.addChangeListener(this._settingChanged.bind(this));
480 this._settingChanged();
482 this._toggleTypeFilter(WebInspector.NamedBitSetFilterUI.ALL_TYPES, false);
486 /** @typedef {{name: string, label: string}} */
487 WebInspector.NamedBitSetFilterUI.Item;
489 WebInspector.NamedBitSetFilterUI.ALL_TYPES = "all";
491 WebInspector.NamedBitSetFilterUI.prototype = {
497 return !this._allowedTypes[WebInspector.NamedBitSetFilterUI.ALL_TYPES];
505 return this._filtersElement;
509 * @param {string} typeName
512 accept: function(typeName)
514 return !!this._allowedTypes[WebInspector.NamedBitSetFilterUI.ALL_TYPES] || !!this._allowedTypes[typeName];
517 _settingChanged: function()
519 var allowedTypes = this._setting.get();
520 this._allowedTypes = {};
521 for (var typeName in this._typeFilterElements) {
522 if (allowedTypes[typeName])
523 this._allowedTypes[typeName] = true;
530 if ((Object.keys(this._allowedTypes).length === 0) || this._allowedTypes[WebInspector.NamedBitSetFilterUI.ALL_TYPES]) {
531 this._allowedTypes = {};
532 this._allowedTypes[WebInspector.NamedBitSetFilterUI.ALL_TYPES] = true;
534 for (var typeName in this._typeFilterElements)
535 this._typeFilterElements[typeName].classList.toggle("selected", this._allowedTypes[typeName]);
536 this.dispatchEventToListeners(WebInspector.FilterUI.Events.FilterChanged, null);
540 * @param {string} name
541 * @param {string} label
543 _addBit: function(name, label)
545 var typeFilterElement = this._filtersElement.createChild("li", name);
546 typeFilterElement.typeName = name;
547 typeFilterElement.createTextChild(label);
548 typeFilterElement.addEventListener("click", this._onTypeFilterClicked.bind(this), false);
549 this._typeFilterElements[name] = typeFilterElement;
555 _onTypeFilterClicked: function(e)
558 if (WebInspector.isMac())
559 toggle = e.metaKey && !e.ctrlKey && !e.altKey && !e.shiftKey;
561 toggle = e.ctrlKey && !e.metaKey && !e.altKey && !e.shiftKey;
562 this._toggleTypeFilter(e.target.typeName, toggle);
566 * @param {string} typeName
567 * @param {boolean} allowMultiSelect
569 _toggleTypeFilter: function(typeName, allowMultiSelect)
571 if (allowMultiSelect && typeName !== WebInspector.NamedBitSetFilterUI.ALL_TYPES)
572 this._allowedTypes[WebInspector.NamedBitSetFilterUI.ALL_TYPES] = false;
574 this._allowedTypes = {};
576 this._allowedTypes[typeName] = !this._allowedTypes[typeName];
579 this._setting.set(this._allowedTypes);
584 __proto__: WebInspector.Object.prototype
589 * @implements {WebInspector.FilterUI}
590 * @extends {WebInspector.Object}
591 * @param {!Array.<!{value: *, label: string, title: string}>} options
593 WebInspector.ComboBoxFilterUI = function(options)
595 this._filterElement = document.createElement("div");
596 this._filterElement.className = "filter-combobox-filter";
598 this._options = options;
599 this._filterComboBox = new WebInspector.StatusBarComboBox(this._filterChanged.bind(this));
600 for (var i = 0; i < options.length; ++i) {
601 var filterOption = options[i];
602 var option = document.createElement("option");
603 option.text = filterOption.label;
604 option.title = filterOption.title;
605 this._filterComboBox.addOption(option);
606 this._filterComboBox.element.title = this._filterComboBox.selectedOption().title;
608 this._filterElement.appendChild(this._filterComboBox.element);
611 WebInspector.ComboBoxFilterUI.prototype = {
617 return this._filterComboBox.selectedIndex() !== 0;
625 return this._filterElement;
629 * @param {string} typeName
632 value: function(typeName)
634 var option = this._options[this._filterComboBox.selectedIndex()];
639 * @param {number} index
641 setSelectedIndex: function(index)
643 this._filterComboBox.setSelectedIndex(index);
649 selectedIndex: function(index)
651 return this._filterComboBox.selectedIndex();
655 * @param {!Event} event
657 _filterChanged: function(event)
659 var option = this._options[this._filterComboBox.selectedIndex()];
660 this._filterComboBox.element.title = option.title;
661 this.dispatchEventToListeners(WebInspector.FilterUI.Events.FilterChanged, null);
664 __proto__: WebInspector.Object.prototype
669 * @implements {WebInspector.FilterUI}
670 * @extends {WebInspector.Object}
671 * @param {string} className
672 * @param {string} title
673 * @param {boolean=} activeWhenChecked
674 * @param {!WebInspector.Setting=} setting
676 WebInspector.CheckboxFilterUI = function(className, title, activeWhenChecked, setting)
678 this._filterElement = document.createElement("div");
679 this._filterElement.classList.add("filter-checkbox-filter", "filter-checkbox-filter-" + className);
680 this._activeWhenChecked = !!activeWhenChecked;
681 this._createCheckbox(title);
684 this._setting = setting;
685 setting.addChangeListener(this._settingChanged.bind(this));
686 this._settingChanged();
688 this._checked = !this._activeWhenChecked;
693 WebInspector.CheckboxFilterUI.prototype = {
699 return this._activeWhenChecked === this._checked;
707 return this._filterElement;
715 return this._checked;
719 * @param {boolean} state
721 setState: function(state)
723 this._checked = state;
729 this._checkElement.classList.toggle("checkbox-filter-checkbox-checked", this._checked);
730 this.dispatchEventToListeners(WebInspector.FilterUI.Events.FilterChanged, null);
733 _settingChanged: function()
735 this._checked = this._setting.get();
740 * @param {!Event} event
742 _onClick: function(event)
744 this._checked = !this._checked;
746 this._setting.set(this._checked);
752 * @param {string} title
754 _createCheckbox: function(title)
756 var label = this._filterElement.createChild("label");
757 var checkBorder = label.createChild("div", "checkbox-filter-checkbox");
758 this._checkElement = checkBorder.createChild("div", "checkbox-filter-checkbox-check");
759 this._filterElement.addEventListener("click", this._onClick.bind(this), false);
760 var typeElement = label.createChild("span", "type");
761 typeElement.textContent = title;
764 __proto__: WebInspector.Object.prototype