From 1eed5c0917e0a2eacd7559ba93e9647782381bb5 Mon Sep 17 00:00:00 2001 From: alexperez Date: Wed, 8 Nov 2023 15:01:14 -0300 Subject: [PATCH] chore: fix collapsible behaviour --- src/ApiResourceExampleDocument.js | 48 +++++++++++++++++-------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/ApiResourceExampleDocument.js b/src/ApiResourceExampleDocument.js index 35ffa34..b240d92 100644 --- a/src/ApiResourceExampleDocument.js +++ b/src/ApiResourceExampleDocument.js @@ -568,30 +568,32 @@ export class ApiResourceExampleDocument extends AmfHelperMixin(LitElement) { } /** + * @param {Example} example + * @param {number} index * Collapse the current example panel */ - _handleCollapsePanel() { - const examplePanel = this.shadowRoot.querySelector('.renderer') - const icon = this.shadowRoot.querySelector('.expand-icon') - icon.classList.toggle('expand-icon-collapse') - examplePanel.classList.toggle('collapse') - - this._collapseExamplePanel = !this._collapseExamplePanel + _handleCollapsePanel(example, index) { + const examplePanels = this.shadowRoot.querySelectorAll('.renderer') + const icons = this.shadowRoot.querySelectorAll('.expand-icon') + icons[index].classList.toggle('expand-icon-collapse') + examplePanels[index].classList.toggle('collapse') + example.opened = !example.opened } /** * @param {Example} example - * @returns {TemplateResult|string} + * @param {number} index + * @returns {TemplateResult|string} */ - _titleTemplate(example) { + _titleTemplate(example, index) { if (example.isScalar) { return ''; } const label = this._computeExampleTitle(example); return html`
${label} - +
`; } @@ -630,7 +632,7 @@ export class ApiResourceExampleDocument extends AmfHelperMixin(LitElement) { /** * Returns title to render for example - * @param {Example} example + * @param {Example} example * @returns {String} 'Example' or the example's title */ _computeExampleTitle(example) { @@ -643,7 +645,7 @@ export class ApiResourceExampleDocument extends AmfHelperMixin(LitElement) { /** * Determines whether an example's title is just a variation * of the current media type + a number - * @param {Example} example + * @param {Example} example * @returns {Boolean} */ _exampleTitleIsMediaType(example) { @@ -653,23 +655,27 @@ export class ApiResourceExampleDocument extends AmfHelperMixin(LitElement) { } /** - * @param {Example[]} examples - * @returns {TemplateResult[]} + * @param {Example[]} initialExamples + * @returns {TemplateResult[]} */ - _examplesTemplate(examples) { + _examplesTemplate(initialExamples) { + const examples = initialExamples.map(example => { + example.opened = !!this._collapseExamplePanel + return example + }); let parts = 'content-action-button, code-content-action-button, content-action-button-disabled, '; parts += 'code-content-action-button-disabled content-action-button-active, '; parts += 'code-content-action-button-active, code-wrapper, example-code-wrapper, markdown-html'; - return examples.map((item) => html` + return examples.map((example, index) => html`
- ${this._titleTemplate(item)} - ${this._descriptionTemplate(item)} + ${this._titleTemplate(example, index)} + ${this._descriptionTemplate(example)}