Web Inspector: Debugger sidebar should have a filter button for breakpoints
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / FilterBar.js
1 /*
2  * Copyright (C) 2013 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.FilterBar = function(element) {
27     // FIXME: Convert this to a WebInspector.Object subclass, and call super().
28     // WebInspector.Object.call(this);
29
30     this._element = element || document.createElement("div");
31     this._element.classList.add(WebInspector.FilterBar.StyleClassName);
32
33     this._filtersNavigationBar = new WebInspector.NavigationBar;
34     this._element.appendChild(this._filtersNavigationBar.element);
35
36     this._filterFunctionsMap = new Map;
37
38     this._inputField = document.createElement("input");
39     this._inputField.type = "search";
40     this._inputField.spellcheck = false;
41     this._inputField.incremental = true;
42     this._inputField.addEventListener("search", this._handleFilterChanged.bind(this), false);
43     this._element.appendChild(this._inputField);
44 };
45
46 // FIXME: Move to a WebInspector.Object subclass and we can remove this.
47 WebInspector.Object.deprecatedAddConstructorFunctions(WebInspector.FilterBar);
48
49 WebInspector.FilterBar.StyleClassName = "filter-bar";
50
51 WebInspector.FilterBar.Event = {
52     FilterDidChange: "filter-bar-text-filter-did-change"
53 };
54
55 WebInspector.FilterBar.prototype = {
56     constructor: WebInspector.FilterBar,
57     __proto__: WebInspector.Object.prototype,
58
59     // Public
60
61     get element()
62     {
63         return this._element;
64     },
65
66     get placeholder()
67     {
68         return this._inputField.getAttribute("placeholder");
69     },
70
71     set placeholder(text)
72     {
73         this._inputField.setAttribute("placeholder", text);
74     },
75
76     get inputField()
77     {
78         return this._inputField;
79     },
80
81     get filters()
82     {
83         return {text: this._inputField.value, functions: [...this._filterFunctionsMap.values()]};
84     },
85
86     set filters(filters)
87     {
88         filters = filters || {};
89
90         var oldTextValue = this._inputField.value;
91         this._inputField.value = filters.text || "";
92         if (oldTextValue !== this._inputField.value)
93             this._handleFilterChanged();
94     },
95
96     addFilterBarButton: function(identifier, filterFunction, activatedByDefault, defaultToolTip, activatedToolTip, image, imageWidth, imageHeight, suppressEmboss)
97     {
98         var filterBarButton = new WebInspector.FilterBarButton(identifier, filterFunction, activatedByDefault, defaultToolTip, activatedToolTip, image, imageWidth, imageHeight, suppressEmboss);
99         filterBarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._handleFilterBarButtonClicked, this);
100         filterBarButton.addEventListener(WebInspector.FilterBarButton.Event.ActivatedStateToggled, this._handleFilterButtonToggled, this);
101         this._filtersNavigationBar.addNavigationItem(filterBarButton);
102         if (filterBarButton.activated) {
103             this._filterFunctionsMap.set(filterBarButton.identifier, filterBarButton.filterFunction);
104             this._handleFilterChanged();
105         }
106     },
107
108     hasActiveFilters: function()
109     {
110         return !!this._inputField.value || !!this._filterFunctionsMap.size;
111     },
112
113     // Private
114
115     _handleFilterBarButtonClicked(event)
116     {
117         var filterBarButton = event.target;
118         filterBarButton.toggle();
119     },
120
121     _handleFilterButtonToggled: function(event)
122     {
123         var filterBarButton = event.target;
124         if (filterBarButton.activated)
125             this._filterFunctionsMap.set(filterBarButton.identifier, filterBarButton.filterFunction);
126         else
127             this._filterFunctionsMap.delete(filterBarButton.identifier);
128         this._handleFilterChanged();
129     },
130
131     _handleFilterChanged: function()
132     {
133         this.dispatchEventToListeners(WebInspector.FilterBar.Event.FilterDidChange);
134     }
135 };