Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // 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 Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 5 | import {ConsoleFilter, FilterType} from './ConsoleFilter.js'; |
| 6 | import {ConsoleViewMessage} from './ConsoleViewMessage.js'; // eslint-disable-line no-unused-vars |
| 7 | |
| 8 | export class ConsoleSidebar extends UI.VBox { |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 9 | constructor() { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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; |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 19 | /** @type {!Array<!FilterTreeElement>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 20 | this._treeElements = []; |
| 21 | const selectedFilterSetting = Common.settings.createSetting('console.sidebarSelectedFilter', null); |
| 22 | |
| 23 | const Levels = SDK.ConsoleMessage.MessageLevel; |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 24 | const consoleAPIParsedFilters = |
| 25 | [{key: FilterType.Source, text: SDK.ConsoleMessage.MessageSource.ConsoleAPI, negative: false}]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 26 | this._appendGroup( |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 27 | _groupName.All, [], ConsoleFilter.allLevelsFilterValue(), UI.Icon.create('mediumicon-list'), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 28 | selectedFilterSetting); |
| 29 | this._appendGroup( |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 30 | _groupName.ConsoleAPI, consoleAPIParsedFilters, ConsoleFilter.allLevelsFilterValue(), |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 31 | UI.Icon.create('mediumicon-account-circle'), selectedFilterSetting); |
| 32 | this._appendGroup( |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 33 | _groupName.Error, [], ConsoleFilter.singleLevelMask(Levels.Error), UI.Icon.create('mediumicon-error-circle'), |
| 34 | selectedFilterSetting); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 35 | this._appendGroup( |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 36 | _groupName.Warning, [], ConsoleFilter.singleLevelMask(Levels.Warning), |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 37 | UI.Icon.create('mediumicon-warning-triangle'), selectedFilterSetting); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | this._appendGroup( |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 39 | _groupName.Info, [], ConsoleFilter.singleLevelMask(Levels.Info), UI.Icon.create('mediumicon-info-circle'), |
| 40 | selectedFilterSetting); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 41 | this._appendGroup( |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 42 | _groupName.Verbose, [], ConsoleFilter.singleLevelMask(Levels.Verbose), UI.Icon.create('mediumicon-bug'), |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 43 | selectedFilterSetting); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 55 | * @param {!Common.Setting} selectedFilterSetting |
| 56 | */ |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 57 | _appendGroup(name, parsedFilters, levelsMask, icon, selectedFilterSetting) { |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 58 | const filter = new ConsoleFilter(name, parsedFilters, null, levelsMask); |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 59 | const treeElement = new FilterTreeElement(filter, icon, selectedFilterSetting); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 60 | this._tree.appendChild(treeElement); |
| 61 | this._treeElements.push(treeElement); |
| 62 | } |
| 63 | |
| 64 | clear() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 65 | for (const treeElement of this._treeElements) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 66 | treeElement.clear(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 67 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 71 | * @param {!ConsoleViewMessage} viewMessage |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 72 | */ |
| 73 | onMessageAdded(viewMessage) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 74 | for (const treeElement of this._treeElements) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | treeElement.onMessageAdded(viewMessage); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 76 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 80 | * @param {!ConsoleViewMessage} viewMessage |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | * @return {boolean} |
| 82 | */ |
| 83 | shouldBeVisible(viewMessage) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 84 | if (!this._selectedTreeElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 86 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 87 | 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 Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 95 | this.dispatchEventToListeners(Events.FilterSelected); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 96 | } |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 97 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 98 | |
| 99 | /** @enum {symbol} */ |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 100 | export const Events = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 101 | FilterSelected: Symbol('FilterSelected') |
| 102 | }; |
| 103 | |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 104 | export class URLGroupTreeElement extends UI.TreeElement { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 105 | /** |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 106 | * @param {!ConsoleFilter} filter |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 107 | */ |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 108 | constructor(filter) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 109 | 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | this.setLeadingIcons(leadingIcons); |
| 114 | this._messageCount = 0; |
| 115 | } |
| 116 | |
| 117 | incrementAndUpdateCounter() { |
| 118 | this._messageCount++; |
| 119 | this._countElement.textContent = this._messageCount; |
| 120 | } |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 121 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 123 | export class FilterTreeElement extends UI.TreeElement { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 124 | /** |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 125 | * @param {!ConsoleFilter} filter |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | * @param {!Element} icon |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 127 | * @param {!Common.Setting} selectedFilterSetting |
| 128 | */ |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 129 | constructor(filter, icon, selectedFilterSetting) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 130 | super(filter.name); |
| 131 | this._filter = filter; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 132 | this._selectedFilterSetting = selectedFilterSetting; |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 133 | /** @type {!Map<?string, !URLGroupTreeElement>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 134 | 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 Chen | c891ec6 | 2019-10-09 00:31:07 | [diff] [blame] | 165 | if (!this._messageCount) { |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 166 | this.title = _groupNoMessageTitleMap.get(this._filter.name); |
Christy Chen | c891ec6 | 2019-10-09 00:31:07 | [diff] [blame] | 167 | } else if (this._messageCount === 1) { |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 168 | this.title = _groupSingularTitleMap.get(this._filter.name); |
Christy Chen | c891ec6 | 2019-10-09 00:31:07 | [diff] [blame] | 169 | } else { |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 170 | this.title = String.sprintf(_groupPluralTitleMap.get(this._filter.name), this._messageCount); |
Christy Chen | c891ec6 | 2019-10-09 00:31:07 | [diff] [blame] | 171 | } |
| 172 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 173 | this.setExpandable(!!this.childCount()); |
| 174 | } |
| 175 | |
| 176 | /** |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 177 | * @param {!ConsoleViewMessage} viewMessage |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | */ |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 183 | if (!this._filter.shouldBeVisible(viewMessage) || !shouldIncrementCounter) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 184 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 185 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 186 | 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 Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 194 | * @return {!URLGroupTreeElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 195 | */ |
| 196 | _childElement(url) { |
| 197 | const urlValue = url || null; |
| 198 | let child = this._urlTreeElements.get(urlValue); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 199 | if (child) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | return child; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 201 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 202 | |
| 203 | const filter = this._filter.clone(); |
Peter Marshall | 3e4e569 | 2019-12-09 16:48:04 | [diff] [blame] | 204 | const parsedURL = urlValue ? Common.ParsedURL.fromString(urlValue) : null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 205 | if (urlValue) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 206 | filter.name = parsedURL ? parsedURL.displayName : urlValue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 207 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 208 | filter.name = Common.UIString('<other>'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 209 | } |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame^] | 210 | filter.parsedFilters.push({key: FilterType.Url, text: urlValue, negative: false}); |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 211 | child = new URLGroupTreeElement(filter); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 212 | if (urlValue) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 213 | child.tooltip = urlValue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 214 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 215 | this._urlTreeElements.set(urlValue, child); |
| 216 | this.appendChild(child); |
| 217 | return child; |
| 218 | } |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 219 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 220 | |
| 221 | /** @enum {string} */ |
Tim van der Lippe | c96ccd9 | 2019-11-29 16:23:54 | [diff] [blame] | 222 | const _groupName = { |
Christy Chen | c891ec6 | 2019-10-09 00:31:07 | [diff] [blame] | 223 | ConsoleAPI: 'user message', |
| 224 | All: 'message', |
| 225 | Error: 'error', |
| 226 | Warning: 'warning', |
| 227 | Info: 'info', |
| 228 | Verbose: 'verbose' |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | /** @const {!Map<string, string>} */ |
Tim van der Lippe | c96ccd9 | 2019-11-29 16:23:54 | [diff] [blame] | 232 | const _groupSingularTitleMap = new Map([ |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 233 | [_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 Chen | c891ec6 | 2019-10-09 00:31:07 | [diff] [blame] | 235 | ]); |
| 236 | |
| 237 | /** @const {!Map<string, string>} */ |
Tim van der Lippe | c96ccd9 | 2019-11-29 16:23:54 | [diff] [blame] | 238 | const _groupPluralTitleMap = new Map([ |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 239 | [_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 Chen | c891ec6 | 2019-10-09 00:31:07 | [diff] [blame] | 241 | ]); |
| 242 | |
| 243 | /** @const {!Map<string, string>} */ |
Tim van der Lippe | c96ccd9 | 2019-11-29 16:23:54 | [diff] [blame] | 244 | const _groupNoMessageTitleMap = new Map([ |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 245 | [_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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 247 | ]); |