-
Notifications
You must be signed in to change notification settings - Fork 425
/
outlet_set.ts
71 lines (57 loc) · 2.13 KB
/
outlet_set.ts
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
import { Scope } from "./scope"
export class OutletSet {
readonly scope: Scope
readonly controllerElement: Element
constructor(scope: Scope, controllerElement: Element) {
this.scope = scope
this.controllerElement = controllerElement
}
get element() {
return this.scope.element
}
get identifier() {
return this.scope.identifier
}
get schema() {
return this.scope.schema
}
has(outletName: string) {
return this.find(outletName) != null
}
find(...outletNames: string[]) {
return outletNames.reduce(
(outlet, outletName) => outlet || this.findOutlet(outletName),
undefined as Element | undefined
)
}
findAll(...outletNames: string[]) {
return outletNames.reduce(
(outlets, outletName) => [...outlets, ...this.findAllOutlets(outletName)],
[] as Element[]
)
}
getSelectorForOutletName(outletName: string) {
const attributeName = this.schema.outletAttributeForScope(this.identifier, outletName)
return this.controllerElement.getAttribute(attributeName)
}
private findOutlet(outletName: string) {
const selector = this.getSelectorForOutletName(outletName)
if (selector) return this.findElement(selector, outletName)
}
private findAllOutlets(outletName: string) {
const selector = this.getSelectorForOutletName(outletName)
return selector ? this.findAllElements(selector, outletName) : []
}
private findElement(selector: string, outletName: string): Element | undefined {
const elements = this.scope.queryElements(selector)
return elements.filter((element) => this.matchesElement(element, selector, outletName))[0]
}
private findAllElements(selector: string, outletName: string): Element[] {
const elements = this.scope.queryElements(selector)
return elements.filter((element) => this.matchesElement(element, selector, outletName))
}
private matchesElement(element: Element, selector: string, outletName: string): boolean {
const controllerAttribute = element.getAttribute(this.scope.schema.controllerAttribute) || ""
return element.matches(selector) && controllerAttribute.split(" ").includes(outletName)
}
}