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