@wikala/spmkt 4.0.0-alpha.4 → 4.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,8 @@ import { SpmktElement } from '../../lit/spmkt-element';
6
6
  * - sort (optional): a GET form whose changes re-render the section; apply is its submit button
7
7
  *
8
8
  * Both work as plain links and forms; inside a theme section, the next page is fetched as that section and
9
- * its items appended, and a sort re-renders the section. The page's URL does not change.
9
+ * its items appended (from this element's own copy, when the section holds more than one paged list), and a
10
+ * sort re-renders the section. The page's URL does not change.
10
11
  */
11
12
  export interface PagedSectionParts {
12
13
  items?: HTMLElement;
@@ -20,6 +21,11 @@ export declare abstract class PagedSectionElement extends SpmktElement {
20
21
  connectedCallback(): void;
21
22
  /** Appends the items of the page at `url` and takes over its paging block. */
22
23
  loadMore(url: string): Promise<void>;
24
+ /**
25
+ * This element's copy in the fetched section: a section may hold more than one paged list (reviews with the
26
+ * questions in a tab), so the parts are looked up inside the element of the same tag at the same position.
27
+ */
28
+ private ownCopyIn;
23
29
  /** Re-renders the section for `url` (a sort). */
24
30
  show(url: string): Promise<void>;
25
31
  private formUrl;
@@ -39,16 +39,26 @@ class PagedSectionElement extends SpmktElement {
39
39
  }
40
40
  const page = document.createElement("template");
41
41
  page.innerHTML = html;
42
- const items = page.content.querySelector('[data-part="items"]');
42
+ const own = this.ownCopyIn(page.content);
43
+ const items = own.querySelector('[data-part="items"]');
43
44
  if (items) {
44
45
  this.findPart("items")?.append(...Array.from(items.children));
45
46
  }
46
- const nextMore = page.content.querySelector('[data-part="more"]');
47
+ const nextMore = own.querySelector('[data-part="more"]');
47
48
  if (more) {
48
49
  if (nextMore) more.replaceChildren(...Array.from(nextMore.childNodes));
49
50
  else more.hidden = true;
50
51
  }
51
52
  }
53
+ /**
54
+ * This element's copy in the fetched section: a section may hold more than one paged list (reviews with the
55
+ * questions in a tab), so the parts are looked up inside the element of the same tag at the same position.
56
+ */
57
+ ownCopyIn(fetched) {
58
+ const section = this.closest("[data-section-id][data-theme-document]");
59
+ const index = section ? Array.from(section.querySelectorAll(this.localName)).indexOf(this) : 0;
60
+ return fetched.querySelectorAll(this.localName)[Math.max(index, 0)] ?? fetched;
61
+ }
52
62
  /** Re-renders the section for `url` (a sort). */
53
63
  async show(url) {
54
64
  const key = sectionKey(this);
@@ -1 +1 @@
1
- {"version":3,"file":"paged-section.js","sources":["../../../src/elements/shared/paged-section.ts"],"sourcesContent":["import { SpmktElement } from '../../lit/spmkt-element'\nimport { fetchFragments, sectionKey, swapSection } from '../../lit/fragments'\nimport { isAbortError } from '../../core/errors'\nimport { t } from '../../core/i18n'\nimport { showToast } from '../toaster/toaster'\n\n/**\n * Parts every paged list in a theme section has (reviews, questions):\n * - items (element-owned for appending): the list; leave it out when there is nothing to list\n * - more (optional): the paging block, with load-more: a link to the next page's URL\n * - sort (optional): a GET form whose changes re-render the section; apply is its submit button\n *\n * Both work as plain links and forms; inside a theme section, the next page is fetched as that section and\n * its items appended, and a sort re-renders the section. The page's URL does not change.\n */\nexport interface PagedSectionParts {\n items?: HTMLElement\n more?: HTMLElement\n 'load-more'?: HTMLAnchorElement\n sort?: HTMLFormElement\n apply?: HTMLElement\n}\n\nexport abstract class PagedSectionElement extends SpmktElement {\n private inFlight: AbortController | undefined\n\n override connectedCallback(): void {\n super.connectedCallback()\n if (!sectionKey(this)) {\n return\n }\n const sort = this.findPart<HTMLFormElement>('sort')\n if (sort) {\n const apply = this.findPart('apply')\n if (apply) apply.hidden = true\n this.listen(sort, 'change', () => void this.show(this.formUrl(sort)))\n this.listen(sort, 'submit', (event) => {\n event.preventDefault()\n void this.show(this.formUrl(sort))\n })\n }\n this.listen(this, 'click', (event) => {\n const link = (event.target as Element).closest<HTMLAnchorElement>('a[data-part=\"load-more\"]')\n if (\n link &&\n this.contains(link) &&\n !event.defaultPrevented &&\n event.button === 0 &&\n !event.metaKey &&\n !event.ctrlKey\n ) {\n event.preventDefault()\n void this.loadMore(link.href)\n }\n })\n this.onDisconnect(() => this.inFlight?.abort())\n }\n\n /** Appends the items of the page at `url` and takes over its paging block. */\n async loadMore(url: string): Promise<void> {\n const more = this.findPart('more')\n more?.setAttribute('aria-busy', 'true')\n const html = await this.fetchSection(url)\n more?.removeAttribute('aria-busy')\n if (html === null) {\n return\n }\n const page = document.createElement('template')\n page.innerHTML = html\n const items = page.content.querySelector('[data-part=\"items\"]')\n if (items) {\n this.findPart('items')?.append(...Array.from(items.children))\n }\n const nextMore = page.content.querySelector('[data-part=\"more\"]')\n if (more) {\n // The paging block is the server's: the next one, or none on the last page\n if (nextMore) more.replaceChildren(...Array.from(nextMore.childNodes))\n else more.hidden = true\n }\n }\n\n /** Re-renders the section for `url` (a sort). */\n async show(url: string): Promise<void> {\n const key = sectionKey(this)\n const items = this.findPart('items')\n items?.setAttribute('aria-busy', 'true')\n const html = await this.fetchSection(url)\n if (html === null) {\n items?.removeAttribute('aria-busy')\n return\n }\n if (key) swapSection(this, html)\n }\n\n private formUrl(form: HTMLFormElement): string {\n const url = new URL(form.action || location.href, location.href)\n url.search = ''\n url.hash = ''\n for (const [name, value] of new FormData(form)) {\n if (typeof value === 'string' && value !== '') url.searchParams.append(name, value)\n }\n return url.toString()\n }\n\n /** The section's HTML for `url`, or null when it could not be had (the visitor is told). */\n private async fetchSection(url: string): Promise<string | null> {\n const key = sectionKey(this)\n if (!key) return null\n this.inFlight?.abort()\n const controller = (this.inFlight = new AbortController())\n try {\n const target = new URL(url, location.href)\n target.hash = ''\n const response = await fetchFragments(target, { sections: [key], signal: controller.signal })\n return response.sections[key] ?? null\n } catch (error) {\n if (!isAbortError(error)) showToast({ message: t('common.error'), tone: 'error' })\n return null\n }\n }\n}\n"],"names":[],"mappings":";;;;;AAuBO,MAAe,4BAA4B,aAAa;AAAA,EAGpD,oBAA0B;AACjC,UAAM,kBAAA;AACN,QAAI,CAAC,WAAW,IAAI,GAAG;AACrB;AAAA,IACF;AACA,UAAM,OAAO,KAAK,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ACF;AAAA;AAAA,EAGA,MAAM,KAAK,KAA4B;AACrC,UAAM,MAAM,WAAW,IAAI;AAC3B,UAAM,QAAQ,KAAK,SAAS,OAAO;AACnC,WAAO,aAAa,aAAa,MAAM;AACvC,UAAM,OAAO,MAAM,KAAK,aAAa,GAAG;AACxC,QAAI,SAAS,MAAM;AACjB,aAAO,gBAAgB,WAAW;AAClC;AAAA,IACF;AACA,QAAI,IAAK,aAAY,MAAM,IAAI;AAAA,EACjC;AAAA,EAEQ,QAAQ,MAA+B;AAC7C,UAAM,MAAM,IAAI,IAAI,KAAK,UAAU,SAAS,MAAM,SAAS,IAAI;AAC/D,QAAI,SAAS;AACb,QAAI,OAAO;AACX,eAAW,CAAC,MAAM,KAAK,KAAK,IAAI,SAAS,IAAI,GAAG;AAC9C,UAAI,OAAO,UAAU,YAAY,UAAU,GAAI,KAAI,aAAa,OAAO,MAAM,KAAK;AAAA,IACpF;AACA,WAAO,IAAI,SAAA;AAAA,EACb;AAAA;AAAA,EAGA,MAAc,aAAa,KAAqC;AAC9D,UAAM,MAAM,WAAW,IAAI;AAC3B,QAAI,CAAC,IAAK,QAAO;AACjB,SAAK,UAAU,MAAA;AACf,UAAM,aAAc,KAAK,WAAW,IAAI,gBAAA;AACxC,QAAI;AACF,YAAM,SAAS,IAAI,IAAI,KAAK,SAAS,IAAI;AACzC,aAAO,OAAO;AACd,YAAM,WAAW,MAAM,eAAe,QAAQ,EAAE,UAAU,CAAC,GAAG,GAAG,QAAQ,WAAW,OAAA,CAAQ;AAC5F,aAAO,SAAS,SAAS,GAAG,KAAK;AAAA,IACnC,SAAS,OAAO;AACd,UAAI,CAAC,aAAa,KAAK,EAAG,WAAU,EAAE,SAAS,EAAE,cAAc,GAAG,MAAM,QAAA,CAAS;AACjF,aAAO;AAAA,IACT;AAAA,EACF;AACF;"}
1
+ {"version":3,"file":"paged-section.js","sources":["../../../src/elements/shared/paged-section.ts"],"sourcesContent":["import { SpmktElement } from '../../lit/spmkt-element'\nimport { fetchFragments, sectionKey, swapSection } from '../../lit/fragments'\nimport { isAbortError } from '../../core/errors'\nimport { t } from '../../core/i18n'\nimport { showToast } from '../toaster/toaster'\n\n/**\n * Parts every paged list in a theme section has (reviews, questions):\n * - items (element-owned for appending): the list; leave it out when there is nothing to list\n * - more (optional): the paging block, with load-more: a link to the next page's URL\n * - sort (optional): a GET form whose changes re-render the section; apply is its submit button\n *\n * Both work as plain links and forms; inside a theme section, the next page is fetched as that section and\n * its items appended (from this element's own copy, when the section holds more than one paged list), and a\n * sort re-renders the section. The page's URL does not change.\n */\nexport interface PagedSectionParts {\n items?: HTMLElement\n more?: HTMLElement\n 'load-more'?: HTMLAnchorElement\n sort?: HTMLFormElement\n apply?: HTMLElement\n}\n\nexport abstract class PagedSectionElement extends SpmktElement {\n private inFlight: AbortController | undefined\n\n override connectedCallback(): void {\n super.connectedCallback()\n if (!sectionKey(this)) {\n return\n }\n const sort = this.findPart<HTMLFormElement>('sort')\n if (sort) {\n const apply = this.findPart('apply')\n if (apply) apply.hidden = true\n this.listen(sort, 'change', () => void this.show(this.formUrl(sort)))\n this.listen(sort, 'submit', (event) => {\n event.preventDefault()\n void this.show(this.formUrl(sort))\n })\n }\n this.listen(this, 'click', (event) => {\n const link = (event.target as Element).closest<HTMLAnchorElement>('a[data-part=\"load-more\"]')\n if (\n link &&\n this.contains(link) &&\n !event.defaultPrevented &&\n event.button === 0 &&\n !event.metaKey &&\n !event.ctrlKey\n ) {\n event.preventDefault()\n void this.loadMore(link.href)\n }\n })\n this.onDisconnect(() => this.inFlight?.abort())\n }\n\n /** Appends the items of the page at `url` and takes over its paging block. */\n async loadMore(url: string): Promise<void> {\n const more = this.findPart('more')\n more?.setAttribute('aria-busy', 'true')\n const html = await this.fetchSection(url)\n more?.removeAttribute('aria-busy')\n if (html === null) {\n return\n }\n const page = document.createElement('template')\n page.innerHTML = html\n const own = this.ownCopyIn(page.content)\n const items = own.querySelector('[data-part=\"items\"]')\n if (items) {\n this.findPart('items')?.append(...Array.from(items.children))\n }\n const nextMore = own.querySelector('[data-part=\"more\"]')\n if (more) {\n // The paging block is the server's: the next one, or none on the last page\n if (nextMore) more.replaceChildren(...Array.from(nextMore.childNodes))\n else more.hidden = true\n }\n }\n\n /**\n * This element's copy in the fetched section: a section may hold more than one paged list (reviews with the\n * questions in a tab), so the parts are looked up inside the element of the same tag at the same position.\n */\n private ownCopyIn(fetched: DocumentFragment): ParentNode {\n const section = this.closest('[data-section-id][data-theme-document]')\n const index = section ? Array.from(section.querySelectorAll(this.localName)).indexOf(this) : 0\n return fetched.querySelectorAll(this.localName)[Math.max(index, 0)] ?? fetched\n }\n\n /** Re-renders the section for `url` (a sort). */\n async show(url: string): Promise<void> {\n const key = sectionKey(this)\n const items = this.findPart('items')\n items?.setAttribute('aria-busy', 'true')\n const html = await this.fetchSection(url)\n if (html === null) {\n items?.removeAttribute('aria-busy')\n return\n }\n if (key) swapSection(this, html)\n }\n\n private formUrl(form: HTMLFormElement): string {\n const url = new URL(form.action || location.href, location.href)\n url.search = ''\n url.hash = ''\n for (const [name, value] of new FormData(form)) {\n if (typeof value === 'string' && value !== '') url.searchParams.append(name, value)\n }\n return url.toString()\n }\n\n /** The section's HTML for `url`, or null when it could not be had (the visitor is told). */\n private async fetchSection(url: string): Promise<string | null> {\n const key = sectionKey(this)\n if (!key) return null\n this.inFlight?.abort()\n const controller = (this.inFlight = new AbortController())\n try {\n const target = new URL(url, location.href)\n target.hash = ''\n const response = await fetchFragments(target, { sections: [key], signal: controller.signal })\n return response.sections[key] ?? null\n } catch (error) {\n if (!isAbortError(error)) showToast({ message: t('common.error'), tone: 'error' })\n return null\n }\n }\n}\n"],"names":[],"mappings":";;;;;AAwBO,MAAe,4BAA4B,aAAa;AAAA,EAGpD,oBAA0B;AACjC,UAAM,kBAAA;AACN,QAAI,CAAC,WAAW,IAAI,GAAG;AACrB;AAAA,IACF;AACA,UAAM,OAAO,KAAK,SAA0B,MAAM;AAClD,QAAI,MAAM;AACR,YAAM,Q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AAG,KAAK;AAAA,IACnC,SAAS,OAAO;AACd,UAAI,CAAC,aAAa,KAAK,EAAG,WAAU,EAAE,SAAS,EAAE,cAAc,GAAG,MAAM,QAAA,CAAS;AACjF,aAAO;AAAA,IACT;AAAA,EACF;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wikala/spmkt",
3
- "version": "4.0.0-alpha.4",
3
+ "version": "4.0.0-alpha.5",
4
4
  "private": false,
5
5
  "description": "Headless storefront core and unstyled web components for themes",
6
6
  "type": "module",