Skip to content

Commit

Permalink
clients(psi): render treemap button (#12570)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed May 27, 2021
1 parent 1f27da7 commit 2c6afcd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
24 changes: 22 additions & 2 deletions lighthouse-core/report/html/renderer/psi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
'use strict';

/* globals self DOM PerformanceCategoryRenderer Util I18n DetailsRenderer ElementScreenshotRenderer */

/* globals self DOM PerformanceCategoryRenderer Util I18n DetailsRenderer ElementScreenshotRenderer ReportUIFeatures */

/**
* Returns all the elements that PSI needs to render the report
Expand Down Expand Up @@ -81,6 +80,9 @@ function prepareLabData(LHResult, document) {
const clonedScoreTemplate = dom.cloneTemplate('#tmpl-lh-scorescale', dom.document());
const scoreScaleEl = dom.find('.lh-scorescale', clonedScoreTemplate);

const reportUIFeatures = new ReportUIFeatures(dom);
reportUIFeatures.json = lhResult;

/** @param {HTMLElement} reportEl */
const installFeatures = (reportEl) => {
if (fullPageScreenshot) {
Expand Down Expand Up @@ -109,6 +111,17 @@ function prepareLabData(LHResult, document) {
ElementScreenshotRenderer.installFullPageScreenshot(
screenshotEl, fullPageScreenshot.screenshot);
}

const showTreemapApp =
lhResult.audits['script-treemap-data'] && lhResult.audits['script-treemap-data'].details;
if (showTreemapApp) {
reportUIFeatures.addButton({
container: reportEl.querySelector('.lh-audit-group--metrics'),
text: Util.i18n.strings.viewTreemapLabel,
icon: 'treemap',
onClick: () => ReportUIFeatures.openTreemap(lhResult, 'url'),
});
}
};

return {scoreGaugeEl, perfCategoryEl, finalScreenshotDataUri, scoreScaleEl, installFeatures};
Expand All @@ -126,6 +139,13 @@ function _getFinalScreenshot(perfCategory) {
return details.data;
}

// Defined by lib/file-namer.js, but that file does not exist in PSI. PSI doesn't use it, but
// needs some basic definition so closure compiler accepts report-ui-features.js
// @ts-expect-error - unused by typescript, used by closure compiler
// eslint-disable-next-line no-unused-vars
function getFilenamePrefix(lhr) {
}

if (typeof module !== 'undefined' && module.exports) {
module.exports = prepareLabData;
} else {
Expand Down
10 changes: 6 additions & 4 deletions lighthouse-core/report/html/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,17 @@ class ReportUIFeatures {
}

/**
* @param {{text: string, icon?: string, onClick: () => void}} opts
* @param {{container?: Element, text: string, icon?: string, onClick: () => void}} opts
*/
addButton(opts) {
// report-ui-features doesn't have a reference to the root report el, and PSI has
// 2 reports on the page (and not even attached to DOM when installFeatures is called..)
// so we need a container option to specify where the element should go.
const metricsEl = this._document.querySelector('.lh-audit-group--metrics');
// Not supported without metrics group.
const containerEl = opts.container || metricsEl;
if (!metricsEl) return;

let buttonsEl = metricsEl.querySelector('.lh-buttons');
let buttonsEl = containerEl.querySelector('.lh-buttons');
if (!buttonsEl) buttonsEl = this._dom.createChildOf(metricsEl, 'div', 'lh-buttons');

const classes = [
Expand Down Expand Up @@ -551,7 +554,6 @@ class ReportUIFeatures {
* Opens a new tab to the treemap app and sends the JSON results using postMessage.
* @param {LH.Result} json
* @param {'postMessage'|'url'} method
* @protected
*/
static openTreemap(json, method = 'postMessage') {
const treemapData = json.audits['script-treemap-data'].details;
Expand Down
4 changes: 4 additions & 0 deletions lighthouse-core/test/report/html/renderer/psi-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const CriticalRequestChainRenderer =
require('../../../../report/html/renderer/crc-details-renderer.js');
const ElementScreenshotRenderer =
require('../../../../report/html/renderer/element-screenshot-renderer.js');
const ReportUIFeatures =
require('../../../../report/html/renderer/report-ui-features.js');

const {itIfProtoExists, sampleResultsRoundtripStr} = testUtils.getProtoRoundTrip();
const sampleResultsStr = fs.readFileSync(__dirname + '/../../../results/sample_v2.json', 'utf-8');
Expand Down Expand Up @@ -48,6 +50,7 @@ describe('DOM', () => {
global.PerformanceCategoryRenderer = PerformanceCategoryRenderer;
global.CriticalRequestChainRenderer = CriticalRequestChainRenderer;
global.ElementScreenshotRenderer = ElementScreenshotRenderer;
global.ReportUIFeatures = ReportUIFeatures;

const {window} = new jsdom.JSDOM(TEMPLATE_FILE);
document = window.document;
Expand All @@ -62,6 +65,7 @@ describe('DOM', () => {
global.PerformanceCategoryRenderer = undefined;
global.CriticalRequestChainRenderer = undefined;
global.ElementScreenshotRenderer = undefined;
global.ReportUIFeatures = undefined;
});

describe('psi prepareLabData helpers', () => {
Expand Down

0 comments on commit 2c6afcd

Please sign in to comment.