Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: report: configure features #12327

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 98 additions & 35 deletions lighthouse-core/report/html/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@

/* globals getFilenamePrefix Util ElementScreenshotRenderer */

/**
* @typedef FeatureSet
* @property {boolean} dropDownMenu
* @property {boolean | {containerEl: Element}} elementScreenshotOverlay
* @property {boolean} fireworks
* @property {boolean} i18n
* @property {boolean} mediaQueryListeners
* @property {boolean} printing
* @property {boolean} showMetricDescriptionsOnError
* @property {boolean} stickyHeader
* @property {boolean} thirdPartyFilter
* @property {boolean} toggleDarkMode
*/

/** @type {FeatureSet} */
const DEFAULT_FEATURE_SET = {
dropDownMenu: true,
elementScreenshotOverlay: true,
fireworks: true,
i18n: true,
mediaQueryListeners: true,
printing: true,
showMetricDescriptionsOnError: true,
stickyHeader: true,
thirdPartyFilter: true,
toggleDarkMode: true,
};

/** @typedef {import('./dom')} DOM */

/**
Expand All @@ -47,10 +75,13 @@ function getAppsOrigin() {
class ReportUIFeatures {
/**
* @param {DOM} dom
* @param {Partial<FeatureSet>} featureSet
*/
constructor(dom) {
constructor(dom, featureSet = {}) {
/** @type {LH.Result} */
this.json; // eslint-disable-line no-unused-expressions
/** @type {FeatureSet} */
this.featureSet = {...DEFAULT_FEATURE_SET, ...featureSet};
/** @type {DOM} */
this._dom = dom;
/** @type {Document} */
Expand Down Expand Up @@ -88,43 +119,69 @@ class ReportUIFeatures {
initFeatures(report) {
this.json = report;

this._setupMediaQueryListeners();
this._dropDown.setup(this.onDropDownMenuClick);
this._setupThirdPartyFilter();
this._setupElementScreenshotOverlay(this._dom.find('.lh-container', this._document));
this._setUpCollapseDetailsAfterPrinting();
this._resetUIState();
this._document.addEventListener('keyup', this.onKeyUp);
this._document.addEventListener('copy', this.onCopy);
if (this.featureSet.mediaQueryListeners) {
this._setupMediaQueryListeners();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

driveby: this fn should be renamed to applyNarrowClassSometimes or something

}

if (this.featureSet.dropDownMenu) {
this._dropDown.setup(this.onDropDownMenuClick);
this._document.addEventListener('copy', this.onCopy);
}

if (this.featureSet.thirdPartyFilter) {
this._setupThirdPartyFilter();
}

if (this.featureSet.elementScreenshotOverlay) {
let overlayContainerEl;
if (this.featureSet.elementScreenshotOverlay === true) {
overlayContainerEl = this._dom.find('.lh-container', this._document);
} else {
overlayContainerEl = this.featureSet.elementScreenshotOverlay.containerEl;
}
this._setupElementScreenshotOverlay(overlayContainerEl);
}

const topbarLogo = this._dom.find('.lh-topbar__logo', this._document);
topbarLogo.addEventListener('click', () => this._toggleDarkTheme());
if (this.featureSet.printing) {
this._setUpCollapseDetailsAfterPrinting();
this._document.addEventListener('keyup', this.onKeyUp);
}

this._resetUIState();

let turnOffTheLights = false;
// Do not query the system preferences for DevTools - DevTools should only apply dark theme
// if dark is selected in the settings panel.
if (!this._dom.isDevTools() && window.matchMedia('(prefers-color-scheme: dark)').matches) {
turnOffTheLights = true;
if (this.featureSet.toggleDarkMode) {
const topbarLogo = this._dom.find('.lh-topbar__logo', this._document);
topbarLogo.addEventListener('click', () => this._toggleDarkTheme());

// Do not query the system preferences for DevTools - DevTools should only apply dark theme
// if dark is selected in the settings panel.
if (!this._dom.isDevTools() && window.matchMedia('(prefers-color-scheme: dark)').matches) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the idea that a devtools rendering config would have toggleDarkMode: false (instead of this isDevTools()) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt consider that but ya, sure

turnOffTheLights = true;
}
}

// Fireworks!
// To get fireworks you need 100 scores in all core categories, except PWA (because going the PWA route is discretionary).
const fireworksRequiredCategoryIds = ['performance', 'accessibility', 'best-practices', 'seo'];
const scoresAll100 = fireworksRequiredCategoryIds.every(id => {
const cat = report.categories[id];
return cat && cat.score === 1;
});
if (scoresAll100) {
turnOffTheLights = true;
this._enableFireworks();
if (this.featureSet.fireworks) {
// To get fireworks you need 100 scores in all core categories, except PWA (because going the PWA route is discretionary).
const fireworksRequiredCategoryIds =
['performance', 'accessibility', 'best-practices', 'seo'];
const scoresAll100 = fireworksRequiredCategoryIds.every(id => {
const cat = report.categories[id];
return cat && cat.score === 1;
});
if (scoresAll100) {
turnOffTheLights = true;
this._enableFireworks();
}
}

if (turnOffTheLights) {
this._toggleDarkTheme(true);
}

// There is only a sticky header when at least 2 categories are present.
if (Object.keys(this.json.categories).length >= 2) {
if (this.featureSet.stickyHeader && Object.keys(this.json.categories).length >= 2) {
this._setupStickyHeaderElements();
const containerEl = this._dom.find('.lh-container', this._document);
const elToAddScrollListener = this._getScrollParent(containerEl);
Expand All @@ -144,19 +201,25 @@ class ReportUIFeatures {
}

// Show the metric descriptions by default when there is an error.
const hasMetricError = report.categories.performance && report.categories.performance.auditRefs
.some(audit => Boolean(audit.group === 'metrics' && report.audits[audit.id].errorMessage));
if (hasMetricError) {
const toggleInputEl = this._dom.find('input.lh-metrics-toggle__input', this._document);
toggleInputEl.checked = true;
if (this.featureSet.showMetricDescriptionsOnError) {
const hasMetricError = report.categories.performance &&
report.categories.performance.auditRefs.some(
audit => Boolean(audit.group === 'metrics' && report.audits[audit.id].errorMessage));
if (hasMetricError) {
const toggleInputEl = this._dom.find('input.lh-metrics-toggle__input', this._document);
toggleInputEl.checked = true;
}
}

// Fill in all i18n data.
for (const node of this._dom.findAll('[data-i18n]', this._dom.document())) {
// These strings are guaranteed to (at least) have a default English string in Util.UIStrings,
// so this cannot be undefined as long as `report-ui-features.data-i18n` test passes.
const i18nAttr = /** @type {keyof LH.I18NRendererStrings} */ (node.getAttribute('data-i18n'));
node.textContent = Util.i18n.strings[i18nAttr];
if (this.featureSet.i18n) {
for (const node of this._dom.findAll('[data-i18n]', this._dom.document())) {
// These strings are guaranteed to (at least) have a default English string in Util.UIStrings,
// so this cannot be undefined as long as `report-ui-features.data-i18n` test passes.
const i18nAttr = /** @type {keyof LH.I18NRendererStrings} */ (
node.getAttribute('data-i18n'));
node.textContent = Util.i18n.strings[i18nAttr];
}
}
}

Expand Down