Skip to content

Commit

Permalink
swappable sections
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Feb 13, 2025
1 parent e329fcc commit 6b3bce7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
25 changes: 25 additions & 0 deletions report/renderer/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class DOM {
/** @type {HTMLElement} */
// For legacy Report API users, this'll be undefined, but set in renderReport
this.rootEl = rootEl;
/** @type {WeakMap<Element, Element>} */
this._swappableSections = new WeakMap();
}

/**
Expand Down Expand Up @@ -323,4 +325,27 @@ export class DOM {
this._document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(a.href), 500);
}

/**
* @param {Element} section1
* @param {Element} section2
*/
registerSwappableSections(section1, section2) {
this._swappableSections.set(section1, section2);
this._swappableSections.set(section2, section1);
}

/**
* @param {Element} section
*/
swapSectionIfPossible(section) {
const newSection = this._swappableSections.get(section);
if (!newSection) return;

const parent = section.parentNode;
if (!parent) return;

parent.insertBefore(newSection, section);
section.remove();
}
}
23 changes: 10 additions & 13 deletions report/renderer/performance-category-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,24 @@ export class PerformanceCategoryRenderer extends CategoryRenderer {

const legacyAuditsSection =
this.renderFilterableSection(category, groups, ['diagnostics'], metricAudits);
legacyAuditsSection?.classList.add('lh-perf-audits--legacy');
legacyAuditsSection?.classList.add('lh-perf-audits--swappable', 'lh-perf-audits--legacy');

const experimentalInsightsSection =
this.renderFilterableSection(category, groups, ['insights', 'diagnostics'], metricAudits);
experimentalInsightsSection?.classList.add('lh-perf-audits--experimental');
experimentalInsightsSection?.classList.add(
'lh-perf-audits--swappable', 'lh-perf-audits--experimental');

// Many tests expect just one of these sections to be in the DOM at a given time.
// To prevent the hidden section from tripping up these tests, we will just remove the hidden
// section from the DOM and store it in memory.
if (legacyAuditsSection) {
// @ts-expect-error
legacyAuditsSection.__swapSection = experimentalInsightsSection;
}
element.append(legacyAuditsSection);

if (experimentalInsightsSection) {
// @ts-expect-error
experimentalInsightsSection.__swapSection = legacyAuditsSection;
// Many tests expect just one of these sections to be in the DOM at a given time.
// To prevent the hidden section from tripping up these tests, we will just remove the hidden
// section from the DOM and store it in memory.
if (experimentalInsightsSection) {
this.dom.registerSwappableSections(legacyAuditsSection, experimentalInsightsSection);
}
}

if (legacyAuditsSection) element.append(legacyAuditsSection);

const isNavigationMode = !options || options?.gatherMode === 'navigation';
if (isNavigationMode && category.score !== null) {
const el = createGauge(this.dom);
Expand Down
12 changes: 3 additions & 9 deletions report/renderer/topbar-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,9 @@ export class TopbarFeatures {
break;
}
case 'toggle-insights': {
const insightsGroup = this._dom.maybeFind('.lh-perf-audits--experimental');
const diagnosticsGroup = this._dom.maybeFind('.lh-perf-audits--legacy');

const renderedGroup = insightsGroup || diagnosticsGroup;
// @ts-expect-error
if (renderedGroup && renderedGroup.__swapSection) {
// @ts-expect-error
renderedGroup.parentNode?.insertBefore(renderedGroup.__swapSection, insightsGroup);
renderedGroup.remove();
const swappableSection = this._dom.maybeFind('.lh-perf-audits--swappable');
if (swappableSection) {
this._dom.swapSectionIfPossible(swappableSection);
}
break;
}
Expand Down
10 changes: 2 additions & 8 deletions report/test/renderer/performance-category-renderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ describe('PerfCategoryRenderer', () => {
let sampleResults;

function swapLegacyAndExperimentalPerfInsights(rootEl) {
const insightsGroup = rootEl.querySelector('.lh-perf-audits--experimental');
const diagnosticsGroup = rootEl.querySelector('.lh-perf-audits--legacy');

const renderedGroup = insightsGroup || diagnosticsGroup;
if (renderedGroup && renderedGroup.__swapSection) {
renderedGroup.parentNode?.insertBefore(renderedGroup.__swapSection, insightsGroup);
renderedGroup.remove();
}
const section = rootEl.querySelector('.lh-perf-audits--swappable');
renderer.dom.swapSectionIfPossible(section);
}

before(() => {
Expand Down

0 comments on commit 6b3bce7

Please sign in to comment.