From 194e6e9f6cb837c57f8317abe3394714ca8faa32 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 13 Jun 2024 11:54:50 -0300 Subject: content, client, css: quick description expand/collapse --- .../dependencies/generateQuickDescription.js | 120 +++++++++++++++------ 1 file changed, 86 insertions(+), 34 deletions(-) (limited to 'src/content/dependencies/generateQuickDescription.js') diff --git a/src/content/dependencies/generateQuickDescription.js b/src/content/dependencies/generateQuickDescription.js index 3dadfbcf..f555af28 100644 --- a/src/content/dependencies/generateQuickDescription.js +++ b/src/content/dependencies/generateQuickDescription.js @@ -2,40 +2,92 @@ export default { contentDependencies: ['transformContent'], extraDependencies: ['html', 'language'], - relations: (relation, thing) => - ({description: - (thing.descriptionShort || thing.description - ? relation('transformContent', - thing.descriptionShort ?? thing.description) - : null)}), - - data: (thing) => - ({hasLongerDescription: - thing.description && - thing.descriptionShort && - thing.descriptionShort !== thing.description}), - - slots: { - infoPageLink: { - type: 'html', - mutable: true, - }, - }, - - generate: (data, relations, slots, {html, language}) => - html.tag('p', {class: 'quick-info'}, - {[html.joinChildren]: html.tag('br')}, - {[html.onlyIfContent]: true}, - - [ - relations.description?.slot('mode', 'inline'), + query: (thing) => ({ + hasDescription: + !!thing.description, + + hasLongerDescription: + thing.description && + thing.descriptionShort && + thing.descriptionShort !== thing.description, + }), + + relations: (relation, query, thing) => ({ + description: + (query.hasLongerDescription || !thing.description + ? null + : relation('transformContent', thing.description)), + + descriptionShort: + (query.hasLongerDescription + ? relation('transformContent', thing.descriptionShort) + : null), + + descriptionLong: + (query.hasLongerDescription + ? relation('transformContent', thing.description) + : null), + }), + + data: (query) => ({ + hasDescription: query.hasDescription, + hasLongerDescription: query.hasLongerDescription, + }), + + generate(data, relations, {html, language}) { + const prefix = 'misc.quickDescription'; + + const wrapExpandCollapseLink = (expandCollapse, content) => + html.tag('a', {class: `${expandCollapse}-link`}, + {href: '#'}, + content); + + const actionsWhenCollapsed = + (data.hasLongerDescription + ? language.$(prefix, 'expandDescription', { + expand: + wrapExpandCollapseLink('expand', + language.$(prefix, 'expandDescription.expand')), + }) + : null); + + const actionsWhenExpanded = + (data.hasLongerDescription + ? language.$(prefix, 'collapseDescription', { + collapse: + wrapExpandCollapseLink('collapse', + language.$(prefix, 'collapseDescription.collapse')), + }) + : null); + + const wrapActions = (attributes, children) => + html.tag('p', {class: 'quick-description-actions'}, + {[html.onlyIfContent]: true}, + attributes, + + children); + + const wrapContent = (attributes, content) => + html.tag('div', {class: 'description-content'}, + {[html.onlyIfContent]: true}, + attributes, + + content?.slot('mode', 'multiline')); + + return ( + html.tag('div', {class: 'quick-description'}, + {[html.onlyIfContent]: true}, data.hasLongerDescription && - slots.infoPageLink && - language.$('misc.quickDescription.moreInfo', { - link: - slots.infoPageLink - .slot('content', language.$('misc.quickDescription.moreInfo.link')), - }), - ]), + {class: 'collapsed'}, + + [ + wrapContent(null, relations.description), + wrapContent({class: 'short'}, relations.descriptionShort), + wrapContent({class: 'long'}, relations.descriptionLong), + + wrapActions({class: 'when-collapsed'}, actionsWhenCollapsed), + wrapActions({class: 'when-expanded'}, actionsWhenExpanded), + ])); + } }; -- cgit 1.3.0-6-gf8a5