blob: 8154fb9fb2e45b1b10123359e9f188585d06a147 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5Console.ConsoleSidebar = class extends UI.VBox {
6 /**
7 * @param {!ProductRegistry.BadgePool} badgePool
8 */
9 constructor(badgePool) {
10 super(true);
11 this.setMinimumSize(125, 0);
12
13 this._tree = new UI.TreeOutlineInShadow();
14 this._tree.registerRequiredCSS('console/consoleSidebar.css');
15 this._tree.addEventListener(UI.TreeOutline.Events.ElementSelected, this._selectionChanged.bind(this));
16 this.contentElement.appendChild(this._tree.element);
17 /** @type {?UI.TreeElement} */
18 this._selectedTreeElement = null;
19 /** @type {!Array<!Console.ConsoleSidebar.FilterTreeElement>} */
20 this._treeElements = [];
21 const selectedFilterSetting = Common.settings.createSetting('console.sidebarSelectedFilter', null);
22
23 const Levels = SDK.ConsoleMessage.MessageLevel;
24 const consoleAPIParsedFilters = [{
25 key: Console.ConsoleFilter.FilterType.Source,
26 text: SDK.ConsoleMessage.MessageSource.ConsoleAPI,
27 negative: false
28 }];
29 this._appendGroup(
30 Console.ConsoleSidebar._groupSingularName.All, [], Console.ConsoleFilter.allLevelsFilterValue(),
31 UI.Icon.create('mediumicon-list'), badgePool, selectedFilterSetting);
32 this._appendGroup(
33 Console.ConsoleSidebar._groupSingularName.ConsoleAPI, consoleAPIParsedFilters,
34 Console.ConsoleFilter.allLevelsFilterValue(), UI.Icon.create('mediumicon-account-circle'), badgePool,
35 selectedFilterSetting);
36 this._appendGroup(
37 Console.ConsoleSidebar._groupSingularName.Error, [], Console.ConsoleFilter.singleLevelMask(Levels.Error),
38 UI.Icon.create('mediumicon-error-circle'), badgePool, selectedFilterSetting);
39 this._appendGroup(
40 Console.ConsoleSidebar._groupSingularName.Warning, [], Console.ConsoleFilter.singleLevelMask(Levels.Warning),
41 UI.Icon.create('mediumicon-warning-triangle'), badgePool, selectedFilterSetting);
42 this._appendGroup(
43 Console.ConsoleSidebar._groupSingularName.Info, [], Console.ConsoleFilter.singleLevelMask(Levels.Info),
44 UI.Icon.create('mediumicon-info-circle'), badgePool, selectedFilterSetting);
45 this._appendGroup(
46 Console.ConsoleSidebar._groupSingularName.Verbose, [], Console.ConsoleFilter.singleLevelMask(Levels.Verbose),
47 UI.Icon.create('mediumicon-bug'), badgePool, selectedFilterSetting);
48 const selectedTreeElementName = selectedFilterSetting.get();
49 const defaultTreeElement =
50 this._treeElements.find(x => x.name() === selectedTreeElementName) || this._treeElements[0];
51 defaultTreeElement.select();
52 }
53
54 /**
55 * @param {string} name
56 * @param {!Array<!TextUtils.FilterParser.ParsedFilter>} parsedFilters
57 * @param {!Object<string, boolean>} levelsMask
58 * @param {!Element} icon
59 * @param {!ProductRegistry.BadgePool} badgePool
60 * @param {!Common.Setting} selectedFilterSetting
61 */
62 _appendGroup(name, parsedFilters, levelsMask, icon, badgePool, selectedFilterSetting) {
63 const filter = new Console.ConsoleFilter(name, parsedFilters, null, levelsMask);
64 const treeElement = new Console.ConsoleSidebar.FilterTreeElement(filter, icon, badgePool, selectedFilterSetting);
65 this._tree.appendChild(treeElement);
66 this._treeElements.push(treeElement);
67 }
68
69 clear() {
70 for (const treeElement of this._treeElements)
71 treeElement.clear();
72 }
73
74 /**
75 * @param {!Console.ConsoleViewMessage} viewMessage
76 */
77 onMessageAdded(viewMessage) {
78 for (const treeElement of this._treeElements)
79 treeElement.onMessageAdded(viewMessage);
80 }
81
82 /**
83 * @param {!Console.ConsoleViewMessage} viewMessage
84 * @return {boolean}
85 */
86 shouldBeVisible(viewMessage) {
87 if (!this._selectedTreeElement)
88 return true;
89 return this._selectedTreeElement._filter.shouldBeVisible(viewMessage);
90 }
91
92 /**
93 * @param {!Common.Event} event
94 */
95 _selectionChanged(event) {
96 this._selectedTreeElement = /** @type {!UI.TreeElement} */ (event.data);
97 this.dispatchEventToListeners(Console.ConsoleSidebar.Events.FilterSelected);
98 }
99};
100
101/** @enum {symbol} */
102Console.ConsoleSidebar.Events = {
103 FilterSelected: Symbol('FilterSelected')
104};
105
106Console.ConsoleSidebar.URLGroupTreeElement = class extends UI.TreeElement {
107 /**
108 * @param {!Console.ConsoleFilter} filter
109 * @param {?Element} badge
110 */
111 constructor(filter, badge) {
112 super(filter.name);
113 this._filter = filter;
114 this._countElement = this.listItemElement.createChild('span', 'count');
115 const leadingIcons = [UI.Icon.create('largeicon-navigator-file')];
116 if (badge)
117 leadingIcons.push(badge);
118 this.setLeadingIcons(leadingIcons);
119 this._messageCount = 0;
120 }
121
122 incrementAndUpdateCounter() {
123 this._messageCount++;
124 this._countElement.textContent = this._messageCount;
125 }
126};
127
128Console.ConsoleSidebar.FilterTreeElement = class extends UI.TreeElement {
129 /**
130 * @param {!Console.ConsoleFilter} filter
131 * @param {!Element} icon
132 * @param {!ProductRegistry.BadgePool} badgePool
133 * @param {!Common.Setting} selectedFilterSetting
134 */
135 constructor(filter, icon, badgePool, selectedFilterSetting) {
136 super(filter.name);
137 this._filter = filter;
138 this._badgePool = badgePool;
139 this._selectedFilterSetting = selectedFilterSetting;
140 /** @type {!Map<?string, !Console.ConsoleSidebar.URLGroupTreeElement>} */
141 this._urlTreeElements = new Map();
142 this.setLeadingIcons([icon]);
143 this._messageCount = 0;
144 this._updateCounter();
145 }
146
147 clear() {
148 this._urlTreeElements.clear();
149 this.removeChildren();
150 this._messageCount = 0;
151 this._updateCounter();
152 }
153
154 /**
155 * @return {string}
156 */
157 name() {
158 return this._filter.name;
159 }
160
161 /**
162 * @param {boolean=} selectedByUser
163 * @return {boolean}
164 * @override
165 */
166 onselect(selectedByUser) {
167 this._selectedFilterSetting.set(this._filter.name);
168 return super.onselect(selectedByUser);
169 }
170
171 _updateCounter() {
172 const prefix = this._messageCount ? this._messageCount : Common.UIString('No');
173 const pluralizedName = this._messageCount === 1 ? this._filter.name :
174 Console.ConsoleSidebar._groupPluralNameMap.get(this._filter.name);
175 this.title = `${prefix} ${pluralizedName}`;
176 this.setExpandable(!!this.childCount());
177 }
178
179 /**
180 * @param {!Console.ConsoleViewMessage} viewMessage
181 */
182 onMessageAdded(viewMessage) {
183 const message = viewMessage.consoleMessage();
184 const shouldIncrementCounter = message.type !== SDK.ConsoleMessage.MessageType.Command &&
185 message.type !== SDK.ConsoleMessage.MessageType.Result && !message.isGroupMessage();
186 if (!this._filter.shouldBeVisible(viewMessage) || !shouldIncrementCounter)
187 return;
188 const child = this._childElement(message.url);
189 child.incrementAndUpdateCounter();
190 this._messageCount++;
191 this._updateCounter();
192 }
193
194 /**
195 * @param {string=} url
196 * @return {!Console.ConsoleSidebar.URLGroupTreeElement}
197 */
198 _childElement(url) {
199 const urlValue = url || null;
200 let child = this._urlTreeElements.get(urlValue);
201 if (child)
202 return child;
203
204 const filter = this._filter.clone();
205 const parsedURL = urlValue ? urlValue.asParsedURL() : null;
206 if (urlValue)
207 filter.name = parsedURL ? parsedURL.displayName : urlValue;
208 else
209 filter.name = Common.UIString('<other>');
210 filter.parsedFilters.push({key: Console.ConsoleFilter.FilterType.Url, text: urlValue, negative: false});
211 const badge = parsedURL ? this._badgePool.badgeForURL(parsedURL) : null;
212 child = new Console.ConsoleSidebar.URLGroupTreeElement(filter, badge);
213 if (urlValue)
214 child.tooltip = urlValue;
215 this._urlTreeElements.set(urlValue, child);
216 this.appendChild(child);
217 return child;
218 }
219};
220
221/** @enum {string} */
222Console.ConsoleSidebar._groupSingularName = {
223 ConsoleAPI: Common.UIString('user message'),
224 All: Common.UIString('message'),
225 Error: Common.UIString('error'),
226 Warning: Common.UIString('warning'),
227 Info: Common.UIString('info'),
228 Verbose: Common.UIString('verbose')
229};
230
231/** @const {!Map<string, string>} */
232Console.ConsoleSidebar._groupPluralNameMap = new Map([
233 [Console.ConsoleSidebar._groupSingularName.ConsoleAPI, Common.UIString('user messages')],
234 [Console.ConsoleSidebar._groupSingularName.All, Common.UIString('messages')],
235 [Console.ConsoleSidebar._groupSingularName.Error, Common.UIString('errors')],
236 [Console.ConsoleSidebar._groupSingularName.Warning, Common.UIString('warnings')],
237 [Console.ConsoleSidebar._groupSingularName.Info, Common.UIString('info')],
238 [Console.ConsoleSidebar._groupSingularName.Verbose, Common.UIString('verbose')]
239]);