- E8 golem15-seo-schema: the type per content kind, the Service grid with price and currency checks, custom JSON-LD with a 300 ms parse status - E9 golem15-seo-graph: nodes and findings from the seo_graph action, the JSON, the Rich Results Test link, 800 ms re-check on fill changes, the ML locale switch, one queued request, the failure line - golem15-seo-parent: the primary category select over the record's parents; the seoparent Decode accepts the loaded value sent back - the rebuilt element bundle and vitest smoke tests
98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import { el, emit, labelsFor, uid, uiLocaleOf } from './labels'
|
|
|
|
export const PARENT_TAG = 'golem15-seo-parent'
|
|
|
|
/** One parent the record can take as its primary category. */
|
|
export interface ParentOption {
|
|
value: string
|
|
label: string
|
|
}
|
|
|
|
/** The loaded value: the stored choice and the record's parents. */
|
|
export interface ParentValue {
|
|
selected: string
|
|
options: ParentOption[]
|
|
}
|
|
|
|
/** Reads the loaded value (an object) or a plain stored choice. */
|
|
export function parseParent(v: unknown): ParentValue {
|
|
if (typeof v === 'string') {
|
|
return { selected: v, options: [] }
|
|
}
|
|
const out: ParentValue = { selected: '', options: [] }
|
|
if (v && typeof v === 'object') {
|
|
const raw = v as { selected?: unknown; options?: unknown }
|
|
out.selected = typeof raw.selected === 'string' ? raw.selected : ''
|
|
if (Array.isArray(raw.options)) {
|
|
for (const o of raw.options) {
|
|
if (o && typeof o === 'object' && typeof (o as ParentOption).value === 'string') {
|
|
const opt = o as ParentOption
|
|
out.options.push({ value: opt.value, label: typeof opt.label === 'string' && opt.label ? opt.label : opt.value })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
/**
|
|
* golem15-seo-parent (P33-23, D-86): the primary category of a record, a
|
|
* 44px select over the record's parents with the "first category" default.
|
|
* The value is the chosen <type>:<id>, "" for the default. A stored choice
|
|
* that is no longer one of the parents falls back to the default (P1 says
|
|
* so).
|
|
*/
|
|
export class SeoParentElement extends HTMLElement {
|
|
static observedAttributes = ['disabled', 'invalid', 'ui-locale']
|
|
|
|
private current: ParentValue = { selected: '', options: [] }
|
|
|
|
get value(): string {
|
|
return this.current.options.some((o) => o.value === this.current.selected) ? this.current.selected : ''
|
|
}
|
|
|
|
set value(v: unknown) {
|
|
const next = parseParent(v)
|
|
if (typeof v === 'string' && this.current.options.length > 0) {
|
|
next.options = this.current.options
|
|
}
|
|
this.current = next
|
|
this.render()
|
|
}
|
|
|
|
connectedCallback(): void {
|
|
this.render()
|
|
}
|
|
|
|
attributeChangedCallback(): void {
|
|
if (this.isConnected) {
|
|
this.render()
|
|
}
|
|
}
|
|
|
|
private render(): void {
|
|
if (!this.isConnected) {
|
|
return
|
|
}
|
|
const labels = labelsFor(uiLocaleOf(this))
|
|
const select = el('select', 'seo-parent__select')
|
|
select.id = uid('seo-parent')
|
|
select.disabled = this.hasAttribute('disabled')
|
|
const def = el('option', '', labels.primaryDefault)
|
|
def.value = ''
|
|
select.append(def)
|
|
for (const o of this.current.options) {
|
|
const opt = el('option', '', o.label)
|
|
opt.value = o.value
|
|
select.append(opt)
|
|
}
|
|
select.value = this.value
|
|
select.addEventListener('change', () => {
|
|
this.current.selected = select.value
|
|
emit(this, 'summer-change', { value: select.value })
|
|
})
|
|
this.classList.toggle('seo-parent--invalid', this.hasAttribute('invalid'))
|
|
this.replaceChildren(select)
|
|
}
|
|
}
|