-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathcore.ts
More file actions
98 lines (86 loc) · 3.24 KB
/
core.ts
File metadata and controls
98 lines (86 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {register} from './register.js'
import {bind, bindShadow} from './bind.js'
import {autoShadowRoot} from './auto-shadow-root.js'
import {defineObservedAttributes, initializeAttrs} from './attr.js'
import type {CustomElementClass} from './custom-element.js'
import {observe} from './lazy-define.js'
const symbol = Symbol.for('catalyst')
export class CatalystDelegate {
constructor(classObject: CustomElementClass, elementName?: string) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const delegate = this
const connectedCallback = classObject.prototype.connectedCallback
classObject.prototype.connectedCallback = function (this: HTMLElement) {
delegate.connectedCallback(this, connectedCallback)
}
const disconnectedCallback = classObject.prototype.disconnectedCallback
classObject.prototype.disconnectedCallback = function (this: HTMLElement) {
delegate.disconnectedCallback(this, disconnectedCallback)
}
const attributeChangedCallback = classObject.prototype.attributeChangedCallback
classObject.prototype.attributeChangedCallback = function (
this: HTMLElement,
name: string,
oldValue: string | null,
newValue: string | null
) {
delegate.attributeChangedCallback(this, name, oldValue, newValue, attributeChangedCallback)
}
let observedAttributes = classObject.observedAttributes || []
Object.defineProperty(classObject, 'observedAttributes', {
configurable: true,
get() {
return delegate.observedAttributes(this, observedAttributes)
},
set(attributes: string[]) {
observedAttributes = attributes
}
})
defineObservedAttributes(classObject)
register(classObject, elementName)
}
observedAttributes(instance: HTMLElement, observedAttributes: string[]) {
return observedAttributes
}
connectedCallback(instance: HTMLElement, connectedCallback: () => void) {
instance.toggleAttribute('data-catalyst', true)
customElements.upgrade(instance)
autoShadowRoot(instance)
initializeAttrs(instance)
bind(instance)
connectedCallback?.call(instance)
if (instance.shadowRoot) {
bindShadow(instance.shadowRoot)
observe(instance.shadowRoot)
}
}
disconnectedCallback(element: HTMLElement, disconnectedCallback: () => void) {
disconnectedCallback?.call(element)
}
attributeChangedCallback(
instance: HTMLElement,
name: string,
oldValue: string | null,
newValue: string | null,
attributeChangedCallback: (...args: unknown[]) => void
) {
initializeAttrs(instance)
if (name !== 'data-catalyst' && attributeChangedCallback) {
attributeChangedCallback.call(instance, name, oldValue, newValue)
}
}
}
export function meta(proto: Record<PropertyKey, unknown>, name: string): Set<string> {
if (!Object.prototype.hasOwnProperty.call(proto, symbol)) {
const parent = proto[symbol] as Map<string, Set<string>> | undefined
const map = (proto[symbol] = new Map<string, Set<string>>())
if (parent) {
for (const [key, value] of parent) {
map.set(key, new Set(value))
}
}
}
const map = proto[symbol] as Map<string, Set<string>>
if (!map.has(name)) map.set(name, new Set<string>())
return map.get(name)!
}