-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathlabel.ts
233 lines (185 loc) · 7.17 KB
/
label.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// @ts-strict-ignore
import type { Label } from "../components/label/label";
import { closestElementCrossShadowBoundary, isBefore, queryElementRoots } from "./dom";
export interface LabelableComponent {
/** When true, disabled prevents interaction. */
disabled: boolean;
/** The host element. */
readonly el: HTMLElement;
/** Text label. */
label?: string;
/** The label this component is associated with. */
labelEl: Label["el"];
/** Hook for components to provide custom label click behavior. */
onLabelClick: (event: CustomEvent<any>) => void;
}
/**
* Exported for testing purposes only
*
* @private
*/
export const labelClickEvent = "calciteInternalLabelClick";
export const labelConnectedEvent = "calciteInternalLabelConnected";
export const labelDisconnectedEvent = "calciteInternalLabelDisconnected";
const labelTagName = "calcite-label";
const labelToLabelables = new WeakMap<Label["el"], LabelableComponent[]>();
const onLabelClickMap = new WeakMap<Label["el"], typeof onLabelClick>();
const onLabelConnectedMap = new WeakMap<LabelableComponent, typeof onLabelConnected>();
const onLabelDisconnectedMap = new WeakMap<LabelableComponent, typeof onLabelDisconnected>();
const unlabeledComponents = new Set<LabelableComponent>();
const findLabelForComponent = (componentEl: HTMLElement): Label["el"] | null => {
const { id } = componentEl;
const forLabel = id && queryElementRoots<Label["el"]>(componentEl, { selector: `${labelTagName}[for="${id}"]` });
if (forLabel) {
return forLabel;
}
const parentLabel = closestElementCrossShadowBoundary<Label["el"]>(componentEl, labelTagName);
if (
!parentLabel ||
// labelable components within other custom elements are not considered labelable
hasAncestorCustomElements(parentLabel, componentEl)
) {
return null;
}
return parentLabel;
};
function hasAncestorCustomElements(label: Label["el"], componentEl: HTMLElement): boolean {
let traversedElements: HTMLElement[];
const customElementAncestorCheckEventType = "custom-element-ancestor-check";
const listener = (event: CustomEvent) => {
event.stopImmediatePropagation();
const composedPath = event.composedPath() as HTMLElement[];
traversedElements = composedPath.slice(composedPath.indexOf(componentEl), composedPath.indexOf(label));
};
label.addEventListener(customElementAncestorCheckEventType, listener, { once: true });
componentEl.dispatchEvent(new CustomEvent(customElementAncestorCheckEventType, { composed: true, bubbles: true }));
label.removeEventListener(customElementAncestorCheckEventType, listener);
const ancestorCustomElements = traversedElements
.filter((el) => el !== componentEl && el !== label)
.filter((el) => el.tagName?.includes("-"));
return ancestorCustomElements.length > 0;
}
/**
* Helper to set up label interactions on connectedCallback.
*
* @param component
*/
export function connectLabel(component: LabelableComponent): void {
if (!component) {
return;
}
const labelEl = findLabelForComponent(component.el);
if (
(onLabelClickMap.has(labelEl) && labelEl === component.labelEl) ||
(!labelEl && unlabeledComponents.has(component))
) {
return;
}
const boundOnLabelDisconnected = onLabelDisconnected.bind(component);
if (labelEl) {
component.labelEl = labelEl;
const labelables = labelToLabelables.get(labelEl) || [];
labelables.push(component);
labelToLabelables.set(labelEl, labelables.sort(sortByDOMOrder));
if (!onLabelClickMap.has(component.labelEl)) {
onLabelClickMap.set(component.labelEl, onLabelClick);
component.labelEl.addEventListener(labelClickEvent, onLabelClick);
}
unlabeledComponents.delete(component);
document.removeEventListener(labelConnectedEvent, onLabelConnectedMap.get(component));
onLabelDisconnectedMap.set(component, boundOnLabelDisconnected);
document.addEventListener(labelDisconnectedEvent, boundOnLabelDisconnected);
} else if (!unlabeledComponents.has(component)) {
boundOnLabelDisconnected();
document.removeEventListener(labelDisconnectedEvent, onLabelDisconnectedMap.get(component));
}
}
/**
* Helper to tear down label interactions on disconnectedCallback on labelable components.
*
* @param component
*/
export function disconnectLabel(component: LabelableComponent): void {
if (!component) {
return;
}
unlabeledComponents.delete(component);
document.removeEventListener(labelConnectedEvent, onLabelConnectedMap.get(component));
document.removeEventListener(labelDisconnectedEvent, onLabelDisconnectedMap.get(component));
onLabelConnectedMap.delete(component);
onLabelDisconnectedMap.delete(component);
if (!component.labelEl) {
return;
}
const labelables = labelToLabelables.get(component.labelEl);
if (labelables.length === 1) {
component.labelEl.removeEventListener(labelClickEvent, onLabelClickMap.get(component.labelEl));
onLabelClickMap.delete(component.labelEl);
}
labelToLabelables.set(
component.labelEl,
labelables.filter((labelable) => labelable !== component).sort(sortByDOMOrder),
);
component.labelEl = null;
}
function sortByDOMOrder(a: LabelableComponent, b: LabelableComponent): number {
return isBefore(a.el, b.el) ? -1 : 1;
}
/**
* Helper to get the label text from a component.
*
* @param component
*/
export function getLabelText(component: LabelableComponent): string {
return component.label || component.labelEl?.textContent?.trim() || "";
}
function onLabelClick(this: Label["el"], event: CustomEvent<{ sourceEvent: MouseEvent }>): void {
const labelClickTarget = event.detail.sourceEvent.target as HTMLElement;
const labelables = labelToLabelables.get(this);
const clickedLabelable = labelables.find((labelable) => labelable.el === labelClickTarget);
const labelableChildClicked = labelables.includes(clickedLabelable);
if (labelableChildClicked) {
// no need to forward click as labelable will receive focus
return;
}
const firstLabelable = labelables[0];
if (firstLabelable.disabled) {
return;
}
firstLabelable.onLabelClick(event);
}
function onLabelConnected(this: LabelableComponent): void {
if (unlabeledComponents.has(this)) {
connectLabel(this);
}
}
function onLabelDisconnected(this: LabelableComponent): void {
unlabeledComponents.add(this);
const boundOnLabelConnected = onLabelConnectedMap.get(this) || onLabelConnected.bind(this);
onLabelConnectedMap.set(this, boundOnLabelConnected);
document.addEventListener(labelConnectedEvent, boundOnLabelConnected);
}
/**
* Helper to associate an explicit label (i.e., using `for`) with a labelable component that does not have an associated label.
*
* @param label - the label element
*/
export async function associateExplicitLabelToUnlabeledComponent(label: Label["el"]): Promise<void> {
await label.componentOnReady();
const alreadyLabeled = labelToLabelables.has(label);
if (alreadyLabeled) {
return;
}
const forComponentEl = label.ownerDocument?.getElementById(label.for);
if (!forComponentEl) {
return;
}
requestAnimationFrame(() => {
for (const labelable of unlabeledComponents) {
if (labelable.el === forComponentEl) {
connectLabel(labelable);
break;
}
}
});
}