blob: 44da6007e8bb2c69cdf78eacd1d173f6473ed3db [file] [log] [blame]
Patrick Hulcea087f622018-05-18 00:37:53 +00001// Copyright 2018 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
5/**
6 * @override
7 */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +00008Audits.ReportRenderer = class extends ReportRenderer {
Patrick Hulcea087f622018-05-18 00:37:53 +00009 /**
Paul Irish8f1e33d2018-05-31 02:29:50 +000010 * @param {!Element} el Parent element to render the report into.
11 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53 +000012 */
Paul Irish8f1e33d2018-05-31 02:29:50 +000013 static addViewTraceButton(el, artifacts) {
14 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass)
15 return;
Patrick Hulcea087f622018-05-18 00:37:53 +000016
cjamcl@google.com21d2d222019-08-09 01:58:17 +000017 const container = el.querySelector('.lh-audit-group');
18 const columnsEl = container.querySelector('.lh-columns');
19 // There will be no columns if just the PWA category.
20 if (!columnsEl)
21 return;
22
Paul Irish8f1e33d2018-05-31 02:29:50 +000023 const defaultPassTrace = artifacts.traces.defaultPass;
24 const timelineButton = UI.createTextButton(Common.UIString('View Trace'), onViewTraceClick, 'view-trace');
cjamcl@google.com21d2d222019-08-09 01:58:17 +000025 container.insertBefore(timelineButton, columnsEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000026
27 async function onViewTraceClick() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000028 Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsViewTrace);
Paul Irish8f1e33d2018-05-31 02:29:50 +000029 await UI.inspectorView.showPanel('timeline');
30 Timeline.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
31 }
Patrick Hulcea087f622018-05-18 00:37:53 +000032 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000033
34 /**
35 * @param {!Element} el
36 */
37 static async linkifyNodeDetails(el) {
38 const mainTarget = SDK.targetManager.mainTarget();
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000039 const domModel = mainTarget.model(SDK.DOMModel);
40
41 for (const origElement of el.getElementsByClassName('lh-node')) {
42 /** @type {!DetailsRenderer.NodeDetailsJSON} */
43 const detailsItem = origElement.dataset;
44 if (!detailsItem.path)
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000045 continue;
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000046
47 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
48
49 if (!nodeId)
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000050 continue;
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000051 const node = domModel.nodeForId(nodeId);
52 if (!node)
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000053 continue;
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000054
55 const element =
56 await Common.Linkifier.linkify(node, /** @type {!Common.Linkifier.Options} */ ({title: detailsItem.snippet}));
57 origElement.title = '';
58 origElement.textContent = '';
59 origElement.appendChild(element);
60 }
61 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +000062
63 /**
64 * @param {!Element} el
65 */
66 static handleDarkMode(el) {
67 if (UI.themeSupport.themeName() === 'dark')
68 el.classList.add('dark');
69 }
Patrick Hulcea087f622018-05-18 00:37:53 +000070};
cjamcl@google.comc5214af2019-06-25 20:31:21 +000071
72/**
73 * @override
74 */
75Audits.ReportUIFeatures = class extends ReportUIFeatures {
76 /**
77 * Returns the html that recreates this report.
78 * @return {string}
79 * @protected
80 */
81 getReportHtml() {
82 this.resetUIState();
83 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
84 }
85
86 /**
87 * Downloads a file (blob) using the system dialog prompt.
88 * @param {!Blob|!File} blob The file to save.
89 */
90 async _saveFile(blob) {
91 const domain = new Common.ParsedURL(this.json.finalUrl).domain();
92 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
93 const timestamp = new Date(this.json.fetchTime).toISO8601Compact();
94 const ext = blob.type.match('json') ? '.json' : '.html';
95 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
96 const text = await blob.text();
97 Workspace.fileManager.save(basename, text, true /* forceSaveAs */);
98 }
99
100 async _print() {
101 const document = this.getDocument();
102 const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */);
103 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
104 const style = printWindow.document.createElement('style');
105 style.textContent = Runtime.cachedResources['audits/lighthouse/report.css'];
106 printWindow.document.head.appendChild(style);
107 printWindow.document.body.replaceWith(clonedReport);
108 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
109 await Audits.ReportRenderer.linkifyNodeDetails(clonedReport);
110 printWindow.focus();
111 printWindow.print();
112 printWindow.close();
113 }
114
115 /**
116 * @suppress {visibility}
117 * @return {!Document}
118 */
119 getDocument() {
120 return this._document;
121 }
122
123 /**
124 * @suppress {visibility}
125 */
126 resetUIState() {
127 this._resetUIState();
128 }
129};