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) { |
| 14 | if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) |
| 15 | return; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 16 | |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 17 | 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 Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 23 | const defaultPassTrace = artifacts.traces.defaultPass; |
| 24 | const timelineButton = UI.createTextButton(Common.UIString('View Trace'), onViewTraceClick, 'view-trace'); |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 25 | container.insertBefore(timelineButton, columnsEl.nextSibling); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 26 | |
| 27 | async function onViewTraceClick() { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 28 | Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsViewTrace); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 29 | await UI.inspectorView.showPanel('timeline'); |
| 30 | Timeline.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents); |
| 31 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 32 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * @param {!Element} el |
| 36 | */ |
| 37 | static async linkifyNodeDetails(el) { |
| 38 | const mainTarget = SDK.targetManager.mainTarget(); |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 39 | 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.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 45 | continue; |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 46 | |
| 47 | const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path); |
| 48 | |
| 49 | if (!nodeId) |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 50 | continue; |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 51 | const node = domModel.nodeForId(nodeId); |
| 52 | if (!node) |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 53 | continue; |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 54 | |
| 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.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * @param {!Element} el |
| 65 | */ |
| 66 | static handleDarkMode(el) { |
| 67 | if (UI.themeSupport.themeName() === 'dark') |
| 68 | el.classList.add('dark'); |
| 69 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 70 | }; |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * @override |
| 74 | */ |
| 75 | Audits.ReportUIFeatures = class extends ReportUIFeatures { |
| 76 | /** |
| 77 | * Returns the html that recreates this report. |
| 78 | * @return {string} |
| 79 | * @protected |
| 80 | */ |
| 81 | getReportHtml() { |
| 82 | this.resetUIState(); |
| 83 | return Lighthouse.ReportGenerator.generateReportHtml(this.json); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Downloads a file (blob) using the system dialog prompt. |
| 88 | * @param {!Blob|!File} blob The file to save. |
| 89 | */ |
| 90 | async _saveFile(blob) { |
| 91 | const domain = new Common.ParsedURL(this.json.finalUrl).domain(); |
| 92 | const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_'); |
| 93 | const timestamp = new Date(this.json.fetchTime).toISO8601Compact(); |
| 94 | const ext = blob.type.match('json') ? '.json' : '.html'; |
| 95 | const basename = `${sanitizedDomain}-${timestamp}${ext}`; |
| 96 | const text = await blob.text(); |
| 97 | Workspace.fileManager.save(basename, text, true /* forceSaveAs */); |
| 98 | } |
| 99 | |
| 100 | async _print() { |
| 101 | const document = this.getDocument(); |
| 102 | const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */); |
| 103 | const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1'); |
| 104 | const style = printWindow.document.createElement('style'); |
| 105 | style.textContent = Runtime.cachedResources['audits/lighthouse/report.css']; |
| 106 | printWindow.document.head.appendChild(style); |
| 107 | printWindow.document.body.replaceWith(clonedReport); |
| 108 | // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`. |
| 109 | await Audits.ReportRenderer.linkifyNodeDetails(clonedReport); |
| 110 | printWindow.focus(); |
| 111 | printWindow.print(); |
| 112 | printWindow.close(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @suppress {visibility} |
| 117 | * @return {!Document} |
| 118 | */ |
| 119 | getDocument() { |
| 120 | return this._document; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @suppress {visibility} |
| 125 | */ |
| 126 | resetUIState() { |
| 127 | this._resetUIState(); |
| 128 | } |
| 129 | }; |