Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 1 | // 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 | |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 5 | import * as Common from '../common/common.js'; |
| 6 | import * as Components from '../components/components.js'; |
| 7 | import * as HostModule from '../host/host.js'; |
| 8 | import * as SDK from '../sdk/sdk.js'; |
| 9 | import * as Timeline from '../timeline/timeline.js'; |
| 10 | import * as UI from '../ui/ui.js'; |
| 11 | |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 12 | const MaxLengthForLinks = 40; |
| 13 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 14 | /** |
| 15 | * @override |
| 16 | */ |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame] | 17 | export class LighthouseReportRenderer extends ReportRenderer { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 18 | /** |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 19 | * @param {!Element} el Parent element to render the report into. |
| 20 | * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 21 | */ |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 22 | static addViewTraceButton(el, artifacts) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 23 | if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) { |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 24 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 25 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 26 | |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 27 | const container = el.querySelector('.lh-audit-group'); |
| 28 | const columnsEl = container.querySelector('.lh-columns'); |
| 29 | // There will be no columns if just the PWA category. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 30 | if (!columnsEl) { |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 31 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 32 | } |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 33 | |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 34 | const defaultPassTrace = artifacts.traces.defaultPass; |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 35 | const timelineButton = |
| 36 | UI.UIUtils.createTextButton(Common.UIString.UIString('View Trace'), onViewTraceClick, 'view-trace'); |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 37 | container.insertBefore(timelineButton, columnsEl.nextSibling); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 38 | |
| 39 | async function onViewTraceClick() { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 40 | HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace); |
Paul Lewis | 0a7c6b6 | 2020-01-23 16:16:22 +0000 | [diff] [blame] | 41 | await self.UI.inspectorView.showPanel('timeline'); |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 42 | Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 43 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 44 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * @param {!Element} el |
| 48 | */ |
| 49 | static async linkifyNodeDetails(el) { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 50 | const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget(); |
| 51 | const domModel = mainTarget.model(SDK.DOMModel.DOMModel); |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 52 | |
| 53 | for (const origElement of el.getElementsByClassName('lh-node')) { |
| 54 | /** @type {!DetailsRenderer.NodeDetailsJSON} */ |
| 55 | const detailsItem = origElement.dataset; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 56 | if (!detailsItem.path) { |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 57 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 58 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 59 | |
| 60 | const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path); |
| 61 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 62 | if (!nodeId) { |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 63 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 64 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 65 | const node = domModel.nodeForId(nodeId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 66 | if (!node) { |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 67 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 68 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 69 | |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 70 | const element = await Common.Linkifier.Linkifier.linkify(node, {tooltip: detailsItem.snippet}); |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 71 | origElement.title = ''; |
| 72 | origElement.textContent = ''; |
| 73 | origElement.appendChild(element); |
| 74 | } |
| 75 | } |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * @param {!Element} el |
| 79 | */ |
Connor Clark | 0403a42 | 2019-11-18 18:03:18 -0800 | [diff] [blame] | 80 | static async linkifySourceLocationDetails(el) { |
| 81 | for (const origElement of el.getElementsByClassName('lh-source-location')) { |
| 82 | /** @type {!DetailsRenderer.SourceLocationDetailsJSON} */ |
| 83 | const detailsItem = origElement.dataset; |
| 84 | if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) { |
| 85 | continue; |
| 86 | } |
| 87 | const url = detailsItem.sourceUrl; |
| 88 | const line = Number(detailsItem.sourceLine); |
| 89 | const column = Number(detailsItem.sourceColumn); |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 90 | const element = await Components.Linkifier.Linkifier.linkifyURL( |
| 91 | url, {lineNumber: line, column, maxLength: MaxLengthForLinks}); |
Connor Clark | 0403a42 | 2019-11-18 18:03:18 -0800 | [diff] [blame] | 92 | origElement.title = ''; |
| 93 | origElement.textContent = ''; |
| 94 | origElement.appendChild(element); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param {!Element} el |
| 100 | */ |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 101 | static handleDarkMode(el) { |
Paul Lewis | 93d8e2c | 2020-01-24 16:34:55 +0000 | [diff] [blame] | 102 | if (self.UI.themeSupport.themeName() === 'dark') { |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 103 | el.classList.add('dark'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 104 | } |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 105 | } |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 106 | } |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * @override |
| 110 | */ |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame] | 111 | export class LighthouseReportUIFeatures extends ReportUIFeatures { |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 112 | /** |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 113 | * @param {!DOM} dom |
| 114 | */ |
| 115 | constructor(dom) { |
| 116 | super(dom); |
| 117 | this._beforePrint = null; |
| 118 | this._afterPrint = null; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param {?function()} beforePrint |
| 123 | */ |
| 124 | setBeforePrint(beforePrint) { |
| 125 | this._beforePrint = beforePrint; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param {?function()} afterPrint |
| 130 | */ |
| 131 | setAfterPrint(afterPrint) { |
| 132 | this._afterPrint = afterPrint; |
| 133 | } |
| 134 | |
| 135 | /** |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 136 | * Returns the html that recreates this report. |
| 137 | * @return {string} |
| 138 | * @protected |
| 139 | */ |
| 140 | getReportHtml() { |
| 141 | this.resetUIState(); |
| 142 | return Lighthouse.ReportGenerator.generateReportHtml(this.json); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Downloads a file (blob) using the system dialog prompt. |
| 147 | * @param {!Blob|!File} blob The file to save. |
| 148 | */ |
| 149 | async _saveFile(blob) { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 150 | const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain(); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 151 | const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_'); |
| 152 | const timestamp = new Date(this.json.fetchTime).toISO8601Compact(); |
| 153 | const ext = blob.type.match('json') ? '.json' : '.html'; |
| 154 | const basename = `${sanitizedDomain}-${timestamp}${ext}`; |
| 155 | const text = await blob.text(); |
Paul Lewis | dff48e4 | 2020-01-24 11:46:48 +0000 | [diff] [blame] | 156 | self.Workspace.fileManager.save(basename, text, true /* forceSaveAs */); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | async _print() { |
| 160 | const document = this.getDocument(); |
| 161 | const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */); |
| 162 | const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1'); |
| 163 | const style = printWindow.document.createElement('style'); |
Connor Clark | 933525c | 2020-02-27 15:27:39 -0800 | [diff] [blame] | 164 | style.textContent = Root.Runtime.cachedResources['third_party/lighthouse/report-assets/report.css']; |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 165 | printWindow.document.head.appendChild(style); |
| 166 | printWindow.document.body.replaceWith(clonedReport); |
| 167 | // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`. |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame] | 168 | await LighthouseReportRenderer.linkifyNodeDetails(clonedReport); |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 169 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 170 | if (this._beforePrint) { |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 171 | this._beforePrint(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 172 | } |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 173 | printWindow.focus(); |
| 174 | printWindow.print(); |
| 175 | printWindow.close(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 176 | if (this._afterPrint) { |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 177 | this._afterPrint(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 178 | } |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @suppress {visibility} |
| 183 | * @return {!Document} |
| 184 | */ |
| 185 | getDocument() { |
| 186 | return this._document; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @suppress {visibility} |
| 191 | */ |
| 192 | resetUIState() { |
| 193 | this._resetUIState(); |
| 194 | } |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 195 | } |