blob: 1e319ab86a569c542871db0115fd61a88a83fe47 [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
Tim van der Lippeeaacb722020-01-10 12:16:005import {ConsoleFilter, FilterType} from './ConsoleFilter.js';
6import {ConsoleViewMessage} from './ConsoleViewMessage.js'; // eslint-disable-line no-unused-vars
7
8export class ConsoleSidebar extends UI.VBox {
Tim van der Lippeb45d9a02019-11-05 17:24:419 constructor() {
Blink Reformat4c46d092018-04-07 15:32:3710 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;
Tim van der Lippeeaacb722020-01-10 12:16:0019 /** @type {!Array<!FilterTreeElement>} */
Blink Reformat4c46d092018-04-07 15:32:3720 this._treeElements = [];
21 const selectedFilterSetting = Common.settings.createSetting('console.sidebarSelectedFilter', null);
22
23 const Levels = SDK.ConsoleMessage.MessageLevel;
Tim van der Lippeeaacb722020-01-10 12:16:0024 const consoleAPIParsedFilters =
25 [{key: FilterType.Source, text: SDK.ConsoleMessage.MessageSource.ConsoleAPI, negative: false}];
Blink Reformat4c46d092018-04-07 15:32:3726 this._appendGroup(
Tim van der Lippeeaacb722020-01-10 12:16:0027 _groupName.All, [], ConsoleFilter.allLevelsFilterValue(), UI.Icon.create('mediumicon-list'),
Blink Reformat4c46d092018-04-07 15:32:3728 selectedFilterSetting);
29 this._appendGroup(
Tim van der Lippeeaacb722020-01-10 12:16:0030 _groupName.ConsoleAPI, consoleAPIParsedFilters, ConsoleFilter.allLevelsFilterValue(),
Paul Lewisbf7aa3c2019-11-20 17:03:3831 UI.Icon.create('mediumicon-account-circle'), selectedFilterSetting);
32 this._appendGroup(
Tim van der Lippeeaacb722020-01-10 12:16:0033 _groupName.Error, [], ConsoleFilter.singleLevelMask(Levels.Error), UI.Icon.create('mediumicon-error-circle'),
34 selectedFilterSetting);
Blink Reformat4c46d092018-04-07 15:32:3735 this._appendGroup(
Tim van der Lippeeaacb722020-01-10 12:16:0036 _groupName.Warning, [], ConsoleFilter.singleLevelMask(Levels.Warning),
Tim van der Lippeb45d9a02019-11-05 17:24:4137 UI.Icon.create('mediumicon-warning-triangle'), selectedFilterSetting);
Blink Reformat4c46d092018-04-07 15:32:3738 this._appendGroup(
Tim van der Lippeeaacb722020-01-10 12:16:0039 _groupName.Info, [], ConsoleFilter.singleLevelMask(Levels.Info), UI.Icon.create('mediumicon-info-circle'),
40 selectedFilterSetting);
Blink Reformat4c46d092018-04-07 15:32:3741 this._appendGroup(
Tim van der Lippeeaacb722020-01-10 12:16:0042 _groupName.Verbose, [], ConsoleFilter.singleLevelMask(Levels.Verbose), UI.Icon.create('mediumicon-bug'),
Paul Lewisbf7aa3c2019-11-20 17:03:3843 selectedFilterSetting);
Blink Reformat4c46d092018-04-07 15:32:3744 const selectedTreeElementName = selectedFilterSetting.get();
45 const defaultTreeElement =
46 this._treeElements.find(x => x.name() === selectedTreeElementName) || this._treeElements[0];
47 defaultTreeElement.select();
48 }
49
50 /**
51 * @param {string} name
52 * @param {!Array<!TextUtils.FilterParser.ParsedFilter>} parsedFilters
53 * @param {!Object<string, boolean>} levelsMask
54 * @param {!Element} icon
Blink Reformat4c46d092018-04-07 15:32:3755 * @param {!Common.Setting} selectedFilterSetting
56 */
Tim van der Lippeb45d9a02019-11-05 17:24:4157 _appendGroup(name, parsedFilters, levelsMask, icon, selectedFilterSetting) {
Tim van der Lippeeaacb722020-01-10 12:16:0058 const filter = new ConsoleFilter(name, parsedFilters, null, levelsMask);
Paul Lewisbf7aa3c2019-11-20 17:03:3859 const treeElement = new FilterTreeElement(filter, icon, selectedFilterSetting);
Blink Reformat4c46d092018-04-07 15:32:3760 this._tree.appendChild(treeElement);
61 this._treeElements.push(treeElement);
62 }
63
64 clear() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3465 for (const treeElement of this._treeElements) {
Blink Reformat4c46d092018-04-07 15:32:3766 treeElement.clear();
Tim van der Lippe1d6e57a2019-09-30 11:55:3467 }
Blink Reformat4c46d092018-04-07 15:32:3768 }
69
70 /**
Tim van der Lippeeaacb722020-01-10 12:16:0071 * @param {!ConsoleViewMessage} viewMessage
Blink Reformat4c46d092018-04-07 15:32:3772 */
73 onMessageAdded(viewMessage) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3474 for (const treeElement of this._treeElements) {
Blink Reformat4c46d092018-04-07 15:32:3775 treeElement.onMessageAdded(viewMessage);
Tim van der Lippe1d6e57a2019-09-30 11:55:3476 }
Blink Reformat4c46d092018-04-07 15:32:3777 }
78
79 /**
Tim van der Lippeeaacb722020-01-10 12:16:0080 * @param {!ConsoleViewMessage} viewMessage
Blink Reformat4c46d092018-04-07 15:32:3781 * @return {boolean}
82 */
83 shouldBeVisible(viewMessage) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3484 if (!this._selectedTreeElement) {
Blink Reformat4c46d092018-04-07 15:32:3785 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:3486 }
Blink Reformat4c46d092018-04-07 15:32:3787 return this._selectedTreeElement._filter.shouldBeVisible(viewMessage);
88 }
89
90 /**
91 * @param {!Common.Event} event
92 */
93 _selectionChanged(event) {
94 this._selectedTreeElement = /** @type {!UI.TreeElement} */ (event.data);
Paul Lewisbf7aa3c2019-11-20 17:03:3895 this.dispatchEventToListeners(Events.FilterSelected);
Blink Reformat4c46d092018-04-07 15:32:3796 }
Paul Lewisbf7aa3c2019-11-20 17:03:3897}
Blink Reformat4c46d092018-04-07 15:32:3798
99/** @enum {symbol} */
Paul Lewisbf7aa3c2019-11-20 17:03:38100export const Events = {
Blink Reformat4c46d092018-04-07 15:32:37101 FilterSelected: Symbol('FilterSelected')
102};
103
Paul Lewisbf7aa3c2019-11-20 17:03:38104export class URLGroupTreeElement extends UI.TreeElement {
Blink Reformat4c46d092018-04-07 15:32:37105 /**
Tim van der Lippeeaacb722020-01-10 12:16:00106 * @param {!ConsoleFilter} filter
Blink Reformat4c46d092018-04-07 15:32:37107 */
Tim van der Lippeb45d9a02019-11-05 17:24:41108 constructor(filter) {
Blink Reformat4c46d092018-04-07 15:32:37109 super(filter.name);
110 this._filter = filter;
111 this._countElement = this.listItemElement.createChild('span', 'count');
112 const leadingIcons = [UI.Icon.create('largeicon-navigator-file')];
Blink Reformat4c46d092018-04-07 15:32:37113 this.setLeadingIcons(leadingIcons);
114 this._messageCount = 0;
115 }
116
117 incrementAndUpdateCounter() {
118 this._messageCount++;
119 this._countElement.textContent = this._messageCount;
120 }
Paul Lewisbf7aa3c2019-11-20 17:03:38121}
Blink Reformat4c46d092018-04-07 15:32:37122
Paul Lewisbf7aa3c2019-11-20 17:03:38123export class FilterTreeElement extends UI.TreeElement {
Blink Reformat4c46d092018-04-07 15:32:37124 /**
Tim van der Lippeeaacb722020-01-10 12:16:00125 * @param {!ConsoleFilter} filter
Blink Reformat4c46d092018-04-07 15:32:37126 * @param {!Element} icon
Blink Reformat4c46d092018-04-07 15:32:37127 * @param {!Common.Setting} selectedFilterSetting
128 */
Tim van der Lippeb45d9a02019-11-05 17:24:41129 constructor(filter, icon, selectedFilterSetting) {
Blink Reformat4c46d092018-04-07 15:32:37130 super(filter.name);
131 this._filter = filter;
Blink Reformat4c46d092018-04-07 15:32:37132 this._selectedFilterSetting = selectedFilterSetting;
Tim van der Lippeeaacb722020-01-10 12:16:00133 /** @type {!Map<?string, !URLGroupTreeElement>} */
Blink Reformat4c46d092018-04-07 15:32:37134 this._urlTreeElements = new Map();
135 this.setLeadingIcons([icon]);
136 this._messageCount = 0;
137 this._updateCounter();
138 }
139
140 clear() {
141 this._urlTreeElements.clear();
142 this.removeChildren();
143 this._messageCount = 0;
144 this._updateCounter();
145 }
146
147 /**
148 * @return {string}
149 */
150 name() {
151 return this._filter.name;
152 }
153
154 /**
155 * @param {boolean=} selectedByUser
156 * @return {boolean}
157 * @override
158 */
159 onselect(selectedByUser) {
160 this._selectedFilterSetting.set(this._filter.name);
161 return super.onselect(selectedByUser);
162 }
163
164 _updateCounter() {
Christy Chenc891ec62019-10-09 00:31:07165 if (!this._messageCount) {
Paul Lewisbf7aa3c2019-11-20 17:03:38166 this.title = _groupNoMessageTitleMap.get(this._filter.name);
Christy Chenc891ec62019-10-09 00:31:07167 } else if (this._messageCount === 1) {
Paul Lewisbf7aa3c2019-11-20 17:03:38168 this.title = _groupSingularTitleMap.get(this._filter.name);
Christy Chenc891ec62019-10-09 00:31:07169 } else {
Paul Lewisbf7aa3c2019-11-20 17:03:38170 this.title = String.sprintf(_groupPluralTitleMap.get(this._filter.name), this._messageCount);
Christy Chenc891ec62019-10-09 00:31:07171 }
172
Blink Reformat4c46d092018-04-07 15:32:37173 this.setExpandable(!!this.childCount());
174 }
175
176 /**
Tim van der Lippeeaacb722020-01-10 12:16:00177 * @param {!ConsoleViewMessage} viewMessage
Blink Reformat4c46d092018-04-07 15:32:37178 */
179 onMessageAdded(viewMessage) {
180 const message = viewMessage.consoleMessage();
181 const shouldIncrementCounter = message.type !== SDK.ConsoleMessage.MessageType.Command &&
182 message.type !== SDK.ConsoleMessage.MessageType.Result && !message.isGroupMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:34183 if (!this._filter.shouldBeVisible(viewMessage) || !shouldIncrementCounter) {
Blink Reformat4c46d092018-04-07 15:32:37184 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34185 }
Blink Reformat4c46d092018-04-07 15:32:37186 const child = this._childElement(message.url);
187 child.incrementAndUpdateCounter();
188 this._messageCount++;
189 this._updateCounter();
190 }
191
192 /**
193 * @param {string=} url
Tim van der Lippeeaacb722020-01-10 12:16:00194 * @return {!URLGroupTreeElement}
Blink Reformat4c46d092018-04-07 15:32:37195 */
196 _childElement(url) {
197 const urlValue = url || null;
198 let child = this._urlTreeElements.get(urlValue);
Tim van der Lippe1d6e57a2019-09-30 11:55:34199 if (child) {
Blink Reformat4c46d092018-04-07 15:32:37200 return child;
Tim van der Lippe1d6e57a2019-09-30 11:55:34201 }
Blink Reformat4c46d092018-04-07 15:32:37202
203 const filter = this._filter.clone();
Peter Marshall3e4e5692019-12-09 16:48:04204 const parsedURL = urlValue ? Common.ParsedURL.fromString(urlValue) : null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34205 if (urlValue) {
Blink Reformat4c46d092018-04-07 15:32:37206 filter.name = parsedURL ? parsedURL.displayName : urlValue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34207 } else {
Blink Reformat4c46d092018-04-07 15:32:37208 filter.name = Common.UIString('<other>');
Tim van der Lippe1d6e57a2019-09-30 11:55:34209 }
Tim van der Lippeeaacb722020-01-10 12:16:00210 filter.parsedFilters.push({key: FilterType.Url, text: urlValue, negative: false});
Paul Lewisbf7aa3c2019-11-20 17:03:38211 child = new URLGroupTreeElement(filter);
Tim van der Lippe1d6e57a2019-09-30 11:55:34212 if (urlValue) {
Blink Reformat4c46d092018-04-07 15:32:37213 child.tooltip = urlValue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34214 }
Blink Reformat4c46d092018-04-07 15:32:37215 this._urlTreeElements.set(urlValue, child);
216 this.appendChild(child);
217 return child;
218 }
Paul Lewisbf7aa3c2019-11-20 17:03:38219}
Blink Reformat4c46d092018-04-07 15:32:37220
221/** @enum {string} */
Tim van der Lippec96ccd92019-11-29 16:23:54222const _groupName = {
Christy Chenc891ec62019-10-09 00:31:07223 ConsoleAPI: 'user message',
224 All: 'message',
225 Error: 'error',
226 Warning: 'warning',
227 Info: 'info',
228 Verbose: 'verbose'
Blink Reformat4c46d092018-04-07 15:32:37229};
230
231/** @const {!Map<string, string>} */
Tim van der Lippec96ccd92019-11-29 16:23:54232const _groupSingularTitleMap = new Map([
Paul Lewisbf7aa3c2019-11-20 17:03:38233 [_groupName.ConsoleAPI, ls`1 user message`], [_groupName.All, ls`1 message`], [_groupName.Error, ls`1 error`],
234 [_groupName.Warning, ls`1 warning`], [_groupName.Info, ls`1 info`], [_groupName.Verbose, ls`1 verbose`]
Christy Chenc891ec62019-10-09 00:31:07235]);
236
237/** @const {!Map<string, string>} */
Tim van der Lippec96ccd92019-11-29 16:23:54238const _groupPluralTitleMap = new Map([
Paul Lewisbf7aa3c2019-11-20 17:03:38239 [_groupName.ConsoleAPI, ls`%d user messages`], [_groupName.All, ls`%d messages`], [_groupName.Error, ls`%d errors`],
240 [_groupName.Warning, ls`%d warnings`], [_groupName.Info, ls`%d info`], [_groupName.Verbose, ls`%d verbose`]
Christy Chenc891ec62019-10-09 00:31:07241]);
242
243/** @const {!Map<string, string>} */
Tim van der Lippec96ccd92019-11-29 16:23:54244const _groupNoMessageTitleMap = new Map([
Paul Lewisbf7aa3c2019-11-20 17:03:38245 [_groupName.ConsoleAPI, ls`No user messages`], [_groupName.All, ls`No messages`], [_groupName.Error, ls`No errors`],
246 [_groupName.Warning, ls`No warnings`], [_groupName.Info, ls`No info`], [_groupName.Verbose, ls`No verbose`]
Blink Reformat4c46d092018-04-07 15:32:37247]);