[go: nahoru, domu]

blob: fe7609108c52c86c76a4ed72d35b2e571673a8e9 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright (c) 2016 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.
Paul Lewis9950e182019-12-16 16:06:074
Tim van der Lippeaa76aa22020-02-14 14:38:245import * as ARIAUtils from './ARIAUtils.js';
Paul Lewis9950e182019-12-16 16:06:076import {Toolbar} from './Toolbar.js';
Tim van der Lippe70842f32020-11-23 16:56:577import {Tooltip} from './Tooltip.js';
Paul Lewis9950e182019-12-16 16:06:078import {VBox} from './Widget.js';
9
Simon Zünd1edfa262020-12-09 08:30:0510/**
11 * @deprecated Please consider using the web component version of this widget
Tim van der Lippefeb2a1e2021-04-14 11:39:5412 * (`ui/components/report_view/ReportView.ts`) for new code.
Simon Zünd1edfa262020-12-09 08:30:0513 */
Paul Lewis9950e182019-12-16 16:06:0714export class ReportView extends VBox {
Jan Scheffler01eab3c2021-08-16 17:18:0715 private readonly contentBox: HTMLElement;
16 private headerElement: HTMLElement;
17 private titleElement: HTMLElement;
18 private readonly sectionList: HTMLElement;
19 private subtitleElement?: HTMLElement;
20 private urlElement?: HTMLElement;
Jan Scheffler16b7c0c2021-04-14 15:58:1421 constructor(title?: string) {
Blink Reformat4c46d092018-04-07 15:32:3722 super(true);
Jack Franklin84442c62021-06-28 15:02:0623 this.registerRequiredCSS('ui/legacy/reportView.css');
Blink Reformat4c46d092018-04-07 15:32:3724
Jan Scheffler01eab3c2021-08-16 17:18:0725 this.contentBox = this.contentElement.createChild('div', 'report-content-box');
26 this.headerElement = this.contentBox.createChild('div', 'report-header vbox');
27 this.titleElement = this.headerElement.createChild('div', 'report-title');
Simon Zündd038deb2020-08-18 10:33:4228 if (title) {
Jan Scheffler01eab3c2021-08-16 17:18:0729 this.titleElement.textContent = title;
Wolfgang Beyer92395c92021-02-02 10:43:2330 } else {
Jan Scheffler01eab3c2021-08-16 17:18:0731 this.headerElement.classList.add('hidden');
Simon Zündd038deb2020-08-18 10:33:4232 }
Jan Scheffler01eab3c2021-08-16 17:18:0733 ARIAUtils.markAsHeading(this.titleElement, 1);
Blink Reformat4c46d092018-04-07 15:32:3734
Jan Scheffler01eab3c2021-08-16 17:18:0735 this.sectionList = this.contentBox.createChild('div', 'vbox');
Blink Reformat4c46d092018-04-07 15:32:3736 }
37
Jan Scheffler16b7c0c2021-04-14 15:58:1438 setTitle(title: string): void {
Jan Scheffler01eab3c2021-08-16 17:18:0739 if (this.titleElement.textContent === title) {
Blink Reformat4c46d092018-04-07 15:32:3740 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3441 }
Jan Scheffler01eab3c2021-08-16 17:18:0742 this.titleElement.textContent = title;
43 this.headerElement.classList.toggle('hidden', Boolean(title));
Blink Reformat4c46d092018-04-07 15:32:3744 }
45
Jan Scheffler16b7c0c2021-04-14 15:58:1446 setSubtitle(subtitle: string): void {
Jan Scheffler01eab3c2021-08-16 17:18:0747 if (this.subtitleElement && this.subtitleElement.textContent === subtitle) {
Blink Reformat4c46d092018-04-07 15:32:3748 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3449 }
Jan Scheffler01eab3c2021-08-16 17:18:0750 if (!this.subtitleElement) {
51 this.subtitleElement = this.headerElement.createChild('div', 'report-subtitle');
Tim van der Lippe1d6e57a2019-09-30 11:55:3452 }
Jan Scheffler01eab3c2021-08-16 17:18:0753 this.subtitleElement.textContent = subtitle;
Blink Reformat4c46d092018-04-07 15:32:3754 }
55
Jan Scheffler16b7c0c2021-04-14 15:58:1456 setURL(link: Element|null): void {
Jan Scheffler01eab3c2021-08-16 17:18:0757 if (!this.urlElement) {
58 this.urlElement = this.headerElement.createChild('div', 'report-url link');
Tim van der Lippe1d6e57a2019-09-30 11:55:3459 }
Jan Scheffler01eab3c2021-08-16 17:18:0760 this.urlElement.removeChildren();
Tim van der Lippe1d6e57a2019-09-30 11:55:3461 if (link) {
Jan Scheffler01eab3c2021-08-16 17:18:0762 this.urlElement.appendChild(link);
Tim van der Lippe1d6e57a2019-09-30 11:55:3463 }
Blink Reformat4c46d092018-04-07 15:32:3764 }
65
Jan Scheffler16b7c0c2021-04-14 15:58:1466 createToolbar(): Toolbar {
Paul Lewis9950e182019-12-16 16:06:0767 const toolbar = new Toolbar('');
Jan Scheffler01eab3c2021-08-16 17:18:0768 this.headerElement.appendChild(toolbar.element);
Blink Reformat4c46d092018-04-07 15:32:3769 return toolbar;
70 }
71
Jan Scheffler16b7c0c2021-04-14 15:58:1472 appendSection(title: string, className?: string): Section {
Tim van der Lippe0830b3d2019-10-03 13:20:0773 const section = new Section(title, className);
Jan Scheffler01eab3c2021-08-16 17:18:0774 section.show(this.sectionList);
Blink Reformat4c46d092018-04-07 15:32:3775 return section;
76 }
77
Jan Scheffler16b7c0c2021-04-14 15:58:1478 sortSections(comparator: (arg0: Section, arg1: Section) => number): void {
79 const sections = (this.children().slice() as Section[]);
Blink Reformat4c46d092018-04-07 15:32:3780 const sorted = sections.every((e, i, a) => !i || comparator(a[i - 1], a[i]) <= 0);
Tim van der Lippe1d6e57a2019-09-30 11:55:3481 if (sorted) {
Blink Reformat4c46d092018-04-07 15:32:3782 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3483 }
Blink Reformat4c46d092018-04-07 15:32:3784
85 this.detachChildWidgets();
86 sections.sort(comparator);
Tim van der Lippe1d6e57a2019-09-30 11:55:3487 for (const section of sections) {
Jan Scheffler01eab3c2021-08-16 17:18:0788 section.show(this.sectionList);
Tim van der Lippe1d6e57a2019-09-30 11:55:3489 }
Blink Reformat4c46d092018-04-07 15:32:3790 }
91
Jan Scheffler16b7c0c2021-04-14 15:58:1492 setHeaderVisible(visible: boolean): void {
Jan Scheffler01eab3c2021-08-16 17:18:0793 this.headerElement.classList.toggle('hidden', !visible);
Blink Reformat4c46d092018-04-07 15:32:3794 }
95
Jan Scheffler16b7c0c2021-04-14 15:58:1496 setBodyScrollable(scrollable: boolean): void {
Jan Scheffler01eab3c2021-08-16 17:18:0797 this.contentBox.classList.toggle('no-scroll', !scrollable);
Blink Reformat4c46d092018-04-07 15:32:3798 }
Tim van der Lippe0830b3d2019-10-03 13:20:0799}
Blink Reformat4c46d092018-04-07 15:32:37100
Paul Lewis9950e182019-12-16 16:06:07101export class Section extends VBox {
Jan Scheffler01eab3c2021-08-16 17:18:07102 private readonly headerElement: HTMLElement;
103 private titleElement: HTMLElement;
104 private fieldList: HTMLElement;
105 private readonly fieldMap: Map<string, Element>;
Jan Scheffler16b7c0c2021-04-14 15:58:14106 constructor(title: string, className?: string) {
Blink Reformat4c46d092018-04-07 15:32:37107 super();
108 this.element.classList.add('report-section');
Tim van der Lippe1d6e57a2019-09-30 11:55:34109 if (className) {
Blink Reformat4c46d092018-04-07 15:32:37110 this.element.classList.add(className);
Tim van der Lippe1d6e57a2019-09-30 11:55:34111 }
Jan Scheffler01eab3c2021-08-16 17:18:07112 this.headerElement = this.element.createChild('div', 'report-section-header');
113 this.titleElement = this.headerElement.createChild('div', 'report-section-title');
Junyi Xiao3ddd8d12019-05-23 23:15:44114 this.setTitle(title);
Jan Scheffler01eab3c2021-08-16 17:18:07115 ARIAUtils.markAsHeading(this.titleElement, 2);
116 this.fieldList = this.element.createChild('div', 'vbox');
117 this.fieldMap = new Map();
Blink Reformat4c46d092018-04-07 15:32:37118 }
119
Jan Scheffler16b7c0c2021-04-14 15:58:14120 title(): string {
Jan Scheffler01eab3c2021-08-16 17:18:07121 return this.titleElement.textContent || '';
Blink Reformat4c46d092018-04-07 15:32:37122 }
123
Jan Scheffler16b7c0c2021-04-14 15:58:14124 setTitle(title: string, tooltip?: string): void {
Jan Scheffler01eab3c2021-08-16 17:18:07125 if (this.titleElement.textContent !== title) {
126 this.titleElement.textContent = title;
Tim van der Lippe1d6e57a2019-09-30 11:55:34127 }
Jan Scheffler01eab3c2021-08-16 17:18:07128 Tooltip.install(this.titleElement, tooltip || '');
129 this.titleElement.classList.toggle('hidden', !this.titleElement.textContent);
Blink Reformat4c46d092018-04-07 15:32:37130 }
131
132 /**
Rob Pavezacb832182019-10-16 23:13:00133 * Declares the overall container to be a group and assigns a title.
Rob Pavezacb832182019-10-16 23:13:00134 */
Jan Scheffler16b7c0c2021-04-14 15:58:14135 setUiGroupTitle(groupTitle: string): void {
Tim van der Lippeaa76aa22020-02-14 14:38:24136 ARIAUtils.markAsGroup(this.element);
137 ARIAUtils.setAccessibleName(this.element, groupTitle);
Rob Pavezacb832182019-10-16 23:13:00138 }
139
Jan Scheffler16b7c0c2021-04-14 15:58:14140 createToolbar(): Toolbar {
Paul Lewis9950e182019-12-16 16:06:07141 const toolbar = new Toolbar('');
Jan Scheffler01eab3c2021-08-16 17:18:07142 this.headerElement.appendChild(toolbar.element);
Blink Reformat4c46d092018-04-07 15:32:37143 return toolbar;
144 }
145
Jan Scheffler16b7c0c2021-04-14 15:58:14146 appendField(title: string, textValue?: string): HTMLElement {
Jan Scheffler01eab3c2021-08-16 17:18:07147 let row = this.fieldMap.get(title);
Blink Reformat4c46d092018-04-07 15:32:37148 if (!row) {
Jan Scheffler01eab3c2021-08-16 17:18:07149 row = this.fieldList.createChild('div', 'report-field');
Blink Reformat4c46d092018-04-07 15:32:37150 row.createChild('div', 'report-field-name').textContent = title;
Jan Scheffler01eab3c2021-08-16 17:18:07151 this.fieldMap.set(title, row);
Blink Reformat4c46d092018-04-07 15:32:37152 row.createChild('div', 'report-field-value');
153 }
Simon Zündd038deb2020-08-18 10:33:42154 if (textValue && row.lastElementChild) {
Blink Reformat4c46d092018-04-07 15:32:37155 row.lastElementChild.textContent = textValue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34156 }
Jan Scheffler16b7c0c2021-04-14 15:58:14157 return /** @type {!HTMLElement} */ row.lastElementChild as HTMLElement;
Blink Reformat4c46d092018-04-07 15:32:37158 }
159
Jan Scheffler16b7c0c2021-04-14 15:58:14160 appendFlexedField(title: string, textValue?: string): Element {
Jan Scheffler227ae822019-12-02 12:45:42161 const field = this.appendField(title, textValue);
162 field.classList.add('report-field-value-is-flexed');
163 return field;
164 }
165
Jan Scheffler16b7c0c2021-04-14 15:58:14166 removeField(title: string): void {
Jan Scheffler01eab3c2021-08-16 17:18:07167 const row = this.fieldMap.get(title);
Tim van der Lippe1d6e57a2019-09-30 11:55:34168 if (row) {
Blink Reformat4c46d092018-04-07 15:32:37169 row.remove();
Tim van der Lippe1d6e57a2019-09-30 11:55:34170 }
Jan Scheffler01eab3c2021-08-16 17:18:07171 this.fieldMap.delete(title);
Blink Reformat4c46d092018-04-07 15:32:37172 }
173
Jan Scheffler16b7c0c2021-04-14 15:58:14174 setFieldVisible(title: string, visible: boolean): void {
Jan Scheffler01eab3c2021-08-16 17:18:07175 const row = this.fieldMap.get(title);
Tim van der Lippe1d6e57a2019-09-30 11:55:34176 if (row) {
Blink Reformat4c46d092018-04-07 15:32:37177 row.classList.toggle('hidden', !visible);
Tim van der Lippe1d6e57a2019-09-30 11:55:34178 }
Blink Reformat4c46d092018-04-07 15:32:37179 }
180
Jan Scheffler16b7c0c2021-04-14 15:58:14181 fieldValue(title: string): Element|null {
Jan Scheffler01eab3c2021-08-16 17:18:07182 const row = this.fieldMap.get(title);
Blink Reformat4c46d092018-04-07 15:32:37183 return row ? row.lastElementChild : null;
184 }
185
Jan Scheffler16b7c0c2021-04-14 15:58:14186 appendRow(): HTMLElement {
Jan Scheffler01eab3c2021-08-16 17:18:07187 return /** @type {!HTMLElement} */ this.fieldList.createChild('div', 'report-row') as HTMLElement;
Blink Reformat4c46d092018-04-07 15:32:37188 }
189
Jan Scheffler16b7c0c2021-04-14 15:58:14190 appendSelectableRow(): HTMLElement {
Jan Scheffler01eab3c2021-08-16 17:18:07191 return /** @type {!HTMLElement} */ this.fieldList.createChild('div', 'report-row report-row-selectable') as
Jan Scheffler16b7c0c2021-04-14 15:58:14192 HTMLElement;
Joey Arhar53f63452019-05-25 01:00:13193 }
194
Jan Scheffler16b7c0c2021-04-14 15:58:14195 clearContent(): void {
Jan Scheffler01eab3c2021-08-16 17:18:07196 this.fieldList.removeChildren();
197 this.fieldMap.clear();
Blink Reformat4c46d092018-04-07 15:32:37198 }
Junyi Xiao3ddd8d12019-05-23 23:15:44199
Jan Scheffler16b7c0c2021-04-14 15:58:14200 markFieldListAsGroup(): void {
Jan Scheffler01eab3c2021-08-16 17:18:07201 ARIAUtils.markAsGroup(this.fieldList);
202 ARIAUtils.setAccessibleName(this.fieldList, this.title());
Junyi Xiao3ddd8d12019-05-23 23:15:44203 }
Mathias Bynenscdae9e02019-12-09 14:21:18204
Jan Scheffler16b7c0c2021-04-14 15:58:14205 setIconMasked(masked: boolean): void {
Mathias Bynenscdae9e02019-12-09 14:21:18206 this.element.classList.toggle('show-mask', masked);
207 }
Tim van der Lippe0830b3d2019-10-03 13:20:07208}