|
| 1 | +import type {CustomElementClass} from './custom-element.js' |
| 2 | +import type {ControllableClass} from './controllable.js' |
| 3 | +import {registerTag, observeElementForTags} from './tag-observer.js' |
| 4 | +import {createMark} from './mark.js' |
| 5 | +import {controllable, attachShadowCallback} from './controllable.js' |
| 6 | +import {dasherize} from './dasherize.js' |
| 7 | +import {createAbility} from './ability.js' |
| 8 | + |
| 9 | +export interface Targetable { |
| 10 | + [targetChangedCallback](key: PropertyKey, target: Element): void |
| 11 | + [targetsChangedCallback](key: PropertyKey, targets: Element[]): void |
| 12 | +} |
| 13 | +export interface TargetableClass { |
| 14 | + new (): Targetable |
| 15 | +} |
| 16 | + |
| 17 | +const targetChangedCallback = Symbol() |
| 18 | +const targetsChangedCallback = Symbol() |
| 19 | + |
| 20 | +const [target, getTarget, initializeTarget] = createMark<Element>( |
| 21 | + ({name, kind}) => { |
| 22 | + if (kind === 'getter') throw new Error(`@target cannot decorate get ${String(name)}`) |
| 23 | + }, |
| 24 | + (instance: Element, {name, access}) => { |
| 25 | + const selector = [ |
| 26 | + `[data-target~="${instance.tagName.toLowerCase()}.${dasherize(name)}"]`, |
| 27 | + `[data-target~="${instance.tagName.toLowerCase()}.${String(name)}"]` |
| 28 | + ] |
| 29 | + const find = findTarget(instance, selector.join(', '), false) |
| 30 | + return { |
| 31 | + get: find, |
| 32 | + set: () => { |
| 33 | + if (access?.set) access.set.call(instance, find()) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +) |
| 38 | +const [targets, getTargets, initializeTargets] = createMark<Element>( |
| 39 | + ({name, kind}) => { |
| 40 | + if (kind === 'getter') throw new Error(`@target cannot decorate get ${String(name)}`) |
| 41 | + }, |
| 42 | + (instance: Element, {name, access}) => { |
| 43 | + const selector = [ |
| 44 | + `[data-targets~="${instance.tagName.toLowerCase()}.${dasherize(name)}"]`, |
| 45 | + `[data-targets~="${instance.tagName.toLowerCase()}.${String(name)}"]` |
| 46 | + ] |
| 47 | + const find = findTarget(instance, selector.join(', '), true) |
| 48 | + return { |
| 49 | + get: find, |
| 50 | + set: () => { |
| 51 | + if (access?.set) access.set.call(instance, find()) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +) |
| 56 | + |
| 57 | +function setTarget(el: Element, controller: Element | ShadowRoot, tag: string, key: string): void { |
| 58 | + const get = tag === 'data-targets' ? getTargets : getTarget |
| 59 | + if (controller instanceof ShadowRoot) { |
| 60 | + controller = controllers.get(controller)! |
| 61 | + } |
| 62 | + if (controller && get(controller)?.has(key)) { |
| 63 | + ;(controller as unknown as Record<PropertyKey, unknown>)[key] = {} |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +registerTag('data-target', (str: string) => str.split('.'), setTarget) |
| 68 | +registerTag('data-targets', (str: string) => str.split('.'), setTarget) |
| 69 | +const shadows = new WeakMap<Element, ShadowRoot>() |
| 70 | +const controllers = new WeakMap<ShadowRoot, Element>() |
| 71 | + |
| 72 | +const findTarget = (controller: Element, selector: string, many: boolean) => () => { |
| 73 | + const nodes = [] |
| 74 | + const shadow = shadows.get(controller) |
| 75 | + if (shadow) { |
| 76 | + for (const el of shadow.querySelectorAll(selector)) { |
| 77 | + if (!el.closest(controller.tagName)) { |
| 78 | + nodes.push(el) |
| 79 | + if (!many) break |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + if (many || !nodes.length) { |
| 84 | + for (const el of controller.querySelectorAll(selector)) { |
| 85 | + if (el.closest(controller.tagName) === controller) { |
| 86 | + nodes.push(el) |
| 87 | + if (!many) break |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return many ? nodes : nodes[0] |
| 92 | +} |
| 93 | + |
| 94 | +export {target, getTarget, targets, getTargets, targetChangedCallback, targetsChangedCallback} |
| 95 | +export const targetable = createAbility( |
| 96 | + <T extends CustomElementClass>(Class: T): T & ControllableClass & TargetableClass => |
| 97 | + class extends controllable(Class) { |
| 98 | + constructor() { |
| 99 | + super() |
| 100 | + observeElementForTags(this) |
| 101 | + initializeTarget(this) |
| 102 | + initializeTargets(this) |
| 103 | + } |
| 104 | + |
| 105 | + [targetChangedCallback]() { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + [targetsChangedCallback]() { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + [attachShadowCallback](root: ShadowRoot) { |
| 114 | + super[attachShadowCallback]?.(root) |
| 115 | + shadows.set(this, root) |
| 116 | + controllers.set(root, this) |
| 117 | + observeElementForTags(root) |
| 118 | + } |
| 119 | + } |
| 120 | +) |
0 commit comments