blob: 406bdf9d489c66ff760eeef80fedf27c64b34d21 [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 /**
Connor Clark99508362019-08-20 19:52:23 +000077 * @param {!DOM} dom
78 */
79 constructor(dom) {
80 super(dom);
81 this._beforePrint = null;
82 this._afterPrint = null;
83 }
84
85 /**
86 * @param {?function()} beforePrint
87 */
88 setBeforePrint(beforePrint) {
89 this._beforePrint = beforePrint;
90 }
91
92 /**
93 * @param {?function()} afterPrint
94 */
95 setAfterPrint(afterPrint) {
96 this._afterPrint = afterPrint;
97 }
98
99 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000100 * Returns the html that recreates this report.
101 * @return {string}
102 * @protected
103 */
104 getReportHtml() {
105 this.resetUIState();
106 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
107 }
108
109 /**
110 * Downloads a file (blob) using the system dialog prompt.
111 * @param {!Blob|!File} blob The file to save.
112 */
113 async _saveFile(blob) {
114 const domain = new Common.ParsedURL(this.json.finalUrl).domain();
115 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
116 const timestamp = new Date(this.json.fetchTime).toISO8601Compact();
117 const ext = blob.type.match('json') ? '.json' : '.html';
118 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
119 const text = await blob.text();
120 Workspace.fileManager.save(basename, text, true /* forceSaveAs */);
121 }
122
123 async _print() {
124 const document = this.getDocument();
125 const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */);
126 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
127 const style = printWindow.document.createElement('style');
128 style.textContent = Runtime.cachedResources['audits/lighthouse/report.css'];
129 printWindow.document.head.appendChild(style);
130 printWindow.document.body.replaceWith(clonedReport);
131 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
132 await Audits.ReportRenderer.linkifyNodeDetails(clonedReport);
Connor Clark99508362019-08-20 19:52:23 +0000133
134 if (this._beforePrint)
135 this._beforePrint();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000136 printWindow.focus();
137 printWindow.print();
138 printWindow.close();
Connor Clark99508362019-08-20 19:52:23 +0000139 if (this._afterPrint)
140 this._afterPrint();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000141 }
142
143 /**
144 * @suppress {visibility}
145 * @return {!Document}
146 */
147 getDocument() {
148 return this._document;
149 }
150
151 /**
152 * @suppress {visibility}
153 */
154 resetUIState() {
155 this._resetUIState();
156 }
157};