blob: 794be32ac25bfed844fe9aa23318c00d0c4d16e6 [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
Paul Irish8f1e33d2018-05-31 02:29:50 +000017 const defaultPassTrace = artifacts.traces.defaultPass;
18 const timelineButton = UI.createTextButton(Common.UIString('View Trace'), onViewTraceClick, 'view-trace');
Paul Irishb5fc8bc2018-10-11 01:07:32 +000019 el.querySelector('.lh-column').appendChild(timelineButton);
Paul Irish8f1e33d2018-05-31 02:29:50 +000020 return el;
21
22 async function onViewTraceClick() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000023 Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsViewTrace);
Paul Irish8f1e33d2018-05-31 02:29:50 +000024 await UI.inspectorView.showPanel('timeline');
25 Timeline.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
26 }
Patrick Hulcea087f622018-05-18 00:37:53 +000027 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000028
29 /**
30 * @param {!Element} el
31 */
32 static async linkifyNodeDetails(el) {
33 const mainTarget = SDK.targetManager.mainTarget();
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000034 const domModel = mainTarget.model(SDK.DOMModel);
35
36 for (const origElement of el.getElementsByClassName('lh-node')) {
37 /** @type {!DetailsRenderer.NodeDetailsJSON} */
38 const detailsItem = origElement.dataset;
39 if (!detailsItem.path)
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000040 continue;
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000041
42 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
43
44 if (!nodeId)
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000045 continue;
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000046 const node = domModel.nodeForId(nodeId);
47 if (!node)
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000048 continue;
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000049
50 const element =
51 await Common.Linkifier.linkify(node, /** @type {!Common.Linkifier.Options} */ ({title: detailsItem.snippet}));
52 origElement.title = '';
53 origElement.textContent = '';
54 origElement.appendChild(element);
55 }
56 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +000057
58 /**
59 * @param {!Element} el
60 */
61 static handleDarkMode(el) {
62 if (UI.themeSupport.themeName() === 'dark')
63 el.classList.add('dark');
64 }
Patrick Hulcea087f622018-05-18 00:37:53 +000065};
cjamcl@google.comc5214af2019-06-25 20:31:21 +000066
67/**
68 * @override
69 */
70Audits.ReportUIFeatures = class extends ReportUIFeatures {
71 /**
72 * Returns the html that recreates this report.
73 * @return {string}
74 * @protected
75 */
76 getReportHtml() {
77 this.resetUIState();
78 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
79 }
80
81 /**
82 * Downloads a file (blob) using the system dialog prompt.
83 * @param {!Blob|!File} blob The file to save.
84 */
85 async _saveFile(blob) {
86 const domain = new Common.ParsedURL(this.json.finalUrl).domain();
87 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
88 const timestamp = new Date(this.json.fetchTime).toISO8601Compact();
89 const ext = blob.type.match('json') ? '.json' : '.html';
90 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
91 const text = await blob.text();
92 Workspace.fileManager.save(basename, text, true /* forceSaveAs */);
93 }
94
95 async _print() {
96 const document = this.getDocument();
97 const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */);
98 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
99 const style = printWindow.document.createElement('style');
100 style.textContent = Runtime.cachedResources['audits/lighthouse/report.css'];
101 printWindow.document.head.appendChild(style);
102 printWindow.document.body.replaceWith(clonedReport);
103 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
104 await Audits.ReportRenderer.linkifyNodeDetails(clonedReport);
105 printWindow.focus();
106 printWindow.print();
107 printWindow.close();
108 }
109
110 /**
111 * @suppress {visibility}
112 * @return {!Document}
113 */
114 getDocument() {
115 return this._document;
116 }
117
118 /**
119 * @suppress {visibility}
120 */
121 resetUIState() {
122 this._resetUIState();
123 }
124};