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