blob: d3292fc348e84718501a6f5065887d91c33214d7 [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() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3470 for (const treeElement of this._treeElements) {
Blink Reformat4c46d092018-04-07 15:32:3771 treeElement.clear();
Tim van der Lippe1d6e57a2019-09-30 11:55:3472 }
Blink Reformat4c46d092018-04-07 15:32:3773 }
74
75 /**
76 * @param {!Console.ConsoleViewMessage} viewMessage
77 */
78 onMessageAdded(viewMessage) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3479 for (const treeElement of this._treeElements) {
Blink Reformat4c46d092018-04-07 15:32:3780 treeElement.onMessageAdded(viewMessage);
Tim van der Lippe1d6e57a2019-09-30 11:55:3481 }
Blink Reformat4c46d092018-04-07 15:32:3782 }
83
84 /**
85 * @param {!Console.ConsoleViewMessage} viewMessage
86 * @return {boolean}
87 */
88 shouldBeVisible(viewMessage) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3489 if (!this._selectedTreeElement) {
Blink Reformat4c46d092018-04-07 15:32:3790 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:3491 }
Blink Reformat4c46d092018-04-07 15:32:3792 return this._selectedTreeElement._filter.shouldBeVisible(viewMessage);
93 }
94
95 /**
96 * @param {!Common.Event} event
97 */
98 _selectionChanged(event) {
99 this._selectedTreeElement = /** @type {!UI.TreeElement} */ (event.data);
100 this.dispatchEventToListeners(Console.ConsoleSidebar.Events.FilterSelected);
101 }
102};
103
104/** @enum {symbol} */
105Console.ConsoleSidebar.Events = {
106 FilterSelected: Symbol('FilterSelected')
107};
108
109Console.ConsoleSidebar.URLGroupTreeElement = class extends UI.TreeElement {
110 /**
111 * @param {!Console.ConsoleFilter} filter
112 * @param {?Element} badge
113 */
114 constructor(filter, badge) {
115 super(filter.name);
116 this._filter = filter;
117 this._countElement = this.listItemElement.createChild('span', 'count');
118 const leadingIcons = [UI.Icon.create('largeicon-navigator-file')];
Tim van der Lippe1d6e57a2019-09-30 11:55:34119 if (badge) {
Blink Reformat4c46d092018-04-07 15:32:37120 leadingIcons.push(badge);
Tim van der Lippe1d6e57a2019-09-30 11:55:34121 }
Blink Reformat4c46d092018-04-07 15:32:37122 this.setLeadingIcons(leadingIcons);
123 this._messageCount = 0;
124 }
125
126 incrementAndUpdateCounter() {
127 this._messageCount++;
128 this._countElement.textContent = this._messageCount;
129 }
130};
131
132Console.ConsoleSidebar.FilterTreeElement = class extends UI.TreeElement {
133 /**
134 * @param {!Console.ConsoleFilter} filter
135 * @param {!Element} icon
136 * @param {!ProductRegistry.BadgePool} badgePool
137 * @param {!Common.Setting} selectedFilterSetting
138 */
139 constructor(filter, icon, badgePool, selectedFilterSetting) {
140 super(filter.name);
141 this._filter = filter;
142 this._badgePool = badgePool;
143 this._selectedFilterSetting = selectedFilterSetting;
144 /** @type {!Map<?string, !Console.ConsoleSidebar.URLGroupTreeElement>} */
145 this._urlTreeElements = new Map();
146 this.setLeadingIcons([icon]);
147 this._messageCount = 0;
148 this._updateCounter();
149 }
150
151 clear() {
152 this._urlTreeElements.clear();
153 this.removeChildren();
154 this._messageCount = 0;
155 this._updateCounter();
156 }
157
158 /**
159 * @return {string}
160 */
161 name() {
162 return this._filter.name;
163 }
164
165 /**
166 * @param {boolean=} selectedByUser
167 * @return {boolean}
168 * @override
169 */
170 onselect(selectedByUser) {
171 this._selectedFilterSetting.set(this._filter.name);
172 return super.onselect(selectedByUser);
173 }
174
175 _updateCounter() {
176 const prefix = this._messageCount ? this._messageCount : Common.UIString('No');
177 const pluralizedName = this._messageCount === 1 ? this._filter.name :
178 Console.ConsoleSidebar._groupPluralNameMap.get(this._filter.name);
179 this.title = `${prefix} ${pluralizedName}`;
180 this.setExpandable(!!this.childCount());
181 }
182
183 /**
184 * @param {!Console.ConsoleViewMessage} viewMessage
185 */
186 onMessageAdded(viewMessage) {
187 const message = viewMessage.consoleMessage();
188 const shouldIncrementCounter = message.type !== SDK.ConsoleMessage.MessageType.Command &&
189 message.type !== SDK.ConsoleMessage.MessageType.Result && !message.isGroupMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:34190 if (!this._filter.shouldBeVisible(viewMessage) || !shouldIncrementCounter) {
Blink Reformat4c46d092018-04-07 15:32:37191 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34192 }
Blink Reformat4c46d092018-04-07 15:32:37193 const child = this._childElement(message.url);
194 child.incrementAndUpdateCounter();
195 this._messageCount++;
196 this._updateCounter();
197 }
198
199 /**
200 * @param {string=} url
201 * @return {!Console.ConsoleSidebar.URLGroupTreeElement}
202 */
203 _childElement(url) {
204 const urlValue = url || null;
205 let child = this._urlTreeElements.get(urlValue);
Tim van der Lippe1d6e57a2019-09-30 11:55:34206 if (child) {
Blink Reformat4c46d092018-04-07 15:32:37207 return child;
Tim van der Lippe1d6e57a2019-09-30 11:55:34208 }
Blink Reformat4c46d092018-04-07 15:32:37209
210 const filter = this._filter.clone();
211 const parsedURL = urlValue ? urlValue.asParsedURL() : null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34212 if (urlValue) {
Blink Reformat4c46d092018-04-07 15:32:37213 filter.name = parsedURL ? parsedURL.displayName : urlValue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34214 } else {
Blink Reformat4c46d092018-04-07 15:32:37215 filter.name = Common.UIString('<other>');
Tim van der Lippe1d6e57a2019-09-30 11:55:34216 }
Blink Reformat4c46d092018-04-07 15:32:37217 filter.parsedFilters.push({key: Console.ConsoleFilter.FilterType.Url, text: urlValue, negative: false});
218 const badge = parsedURL ? this._badgePool.badgeForURL(parsedURL) : null;
219 child = new Console.ConsoleSidebar.URLGroupTreeElement(filter, badge);
Tim van der Lippe1d6e57a2019-09-30 11:55:34220 if (urlValue) {
Blink Reformat4c46d092018-04-07 15:32:37221 child.tooltip = urlValue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34222 }
Blink Reformat4c46d092018-04-07 15:32:37223 this._urlTreeElements.set(urlValue, child);
224 this.appendChild(child);
225 return child;
226 }
227};
228
229/** @enum {string} */
230Console.ConsoleSidebar._groupSingularName = {
231 ConsoleAPI: Common.UIString('user message'),
232 All: Common.UIString('message'),
233 Error: Common.UIString('error'),
234 Warning: Common.UIString('warning'),
235 Info: Common.UIString('info'),
236 Verbose: Common.UIString('verbose')
237};
238
239/** @const {!Map<string, string>} */
240Console.ConsoleSidebar._groupPluralNameMap = new Map([
241 [Console.ConsoleSidebar._groupSingularName.ConsoleAPI, Common.UIString('user messages')],
242 [Console.ConsoleSidebar._groupSingularName.All, Common.UIString('messages')],
243 [Console.ConsoleSidebar._groupSingularName.Error, Common.UIString('errors')],
244 [Console.ConsoleSidebar._groupSingularName.Warning, Common.UIString('warnings')],
245 [Console.ConsoleSidebar._groupSingularName.Info, Common.UIString('info')],
246 [Console.ConsoleSidebar._groupSingularName.Verbose, Common.UIString('verbose')]
247]);