-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hat.ts
514 lines (433 loc) · 12.3 KB
/
Hat.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
*
*/
namespace Hat
{
/** */
export interface IHat
{
readonly head: Element;
}
/** */
type Constructor<T = any> =
(abstract new (...args: any) => T) |
(new (...args: any) => T);
/**
* Marks an object as a Hat. (Or formally–an "Anonymous Controller Class").
*/
export function wear(hat: IHat)
{
const names = getConstructorClassNames(hat);
const hatsArray = hats.get(hat.head) || [];
hatsArray.push(hat);
hats.set(hat.head, hatsArray);
(hat.head as any)._hat = hat;
hat.head.classList.add(...names);
}
/**
* Enables a hat to have the ability to respond to signaling functions.
* Marks the hat argument as a hat, if it has not been done so already.
*/
export function watch<H extends F, F extends (...args: any[]) => void>(hat: IHat, signal: F, handler: H)
{
if ((hats.get(hat.head) || []).length === 0)
Hat.wear(hat);
const name = getSignalClassName(signal);
const signalsArray = signals.get(hat.head) || [];
signalsArray.push([signal, handler.bind(hat)]);
signals.set(hat.head, signalsArray);
hat.head.classList.add(name);
}
/**
* Sends a call signal to all Hats in the document that have subscribed
* to invokations of the specified signal function.
*/
export function signal<A extends any[], F extends (...args: A) => void>(ref: F, ...args: A)
{
const cls = getSignalClassName(ref);
const elements = document.body.getElementsByClassName(cls);
for (let i = -1; ++i < elements.length;)
{
const e = elements.item(i);
if (!e)
continue;
const signalsArray = signals.get(e) || [];
for (const [signalFunction, boundFunction] of signalsArray)
if (signalFunction === ref)
boundFunction(...args);
}
}
/** */
function getSignalClassName(fn: (...args: any) => void)
{
if (!fn.name)
throw new Error("Cannot use an unnamed function as signaler");
return signalPrefix + fn.name;
}
const signalPrefix = "signal:";
/**
* @returns An array that contains all that hats that have been assigned to
* the specified Node.
*/
export function of<T extends IHat>(e: Node | null | undefined): T[];
/**
* @returns The Hat of the specified Node with the specified Hat type,
* or null in the case when the Node is not wearing a Hat of the specified type.
*/
export function of<T extends IHat>(e: Node | null | undefined, type: Constructor<T>): T | null;
export function of<T extends IHat>(e: Node | null | undefined, type?: Constructor<T>)
{
if (!e)
return null;
if (!type)
return (hats.get(e as Element) || []).slice();
let current: Node | null = e;
for (;;)
{
const array = hats.get(current as Element);
if (array)
for (const obj of array)
if (obj instanceof type)
return obj;
if (!(current.parentElement instanceof Element))
break;
current = current.parentElement;
}
return null;
}
/**
* Scans upward through the DOM, starting at the specified object,
* and performs a look-down at each layer in order to find a Hat of
* the specified type, which is nearest in the DOM to the specified
* Node or Hat.
*
* @returns A reference to the Hat that is nearest to the specified
* object.
*/
export function nearest<T extends IHat>(
via: Node | IHat,
type: Constructor<T>)
{
let current: Node | null = via instanceof Node ? via : via.head;
while (current instanceof Node)
{
if (current instanceof Element)
{
const maybe = Hat.down(current, type);
if (maybe)
return maybe;
}
current = current.parentElement;
}
return null;
}
/**
* Scans upward through the DOM, starting at the specified Node,
* looking for the first element wearing a Hat of the specified type.
*
* @returns A reference to the Hat that exists above the specified Node
* in the DOM, or null if no such Hat was found.
*/
export function up<T extends IHat>(
via: Node | IHat,
type: Constructor<T>)
{
let current: Node | null = via instanceof Node ? via : via.head;
while (current instanceof Node)
{
if (current instanceof Element)
{
const hat = Hat.of(current, type);
if (hat)
return hat;
}
current = current.parentElement;
}
return null;
}
/**
* Finds the first descendent element that has an attached Hat of the
* specified type, that exists underneath the specified Node or Hat.
*
* @returns The Hat associated with the descendent element, or
* null if no such Hat is found.
*/
export function down<T extends IHat>(via: Node | IHat, type: Constructor<T>)
{
const hats = within(via, type, true);
return hats.length > 0 ? hats[0] : null;
}
/**
* Scans upward through the DOM, starting at the specified Node,
* looking for the first element wearing a Hat of the specified type.
*
* @throws An exception if no Hat of the specified type is found.
* @returns The ancestor Hat of the specified type.
*/
export function over<T extends IHat>(
via: Node | IHat,
type: Constructor<T>)
{
const hat = up(via, type);
if (!hat)
throw new Error("Hat not found.");
return hat;
}
/**
* Finds all descendent elements that have an attached Hat of the
* specified type, that exist underneath the specified Node or Hat.
*
* @returns An array of Hats whose type matches the type specified.
*/
export function under<T extends IHat>(via: Node | IHat, type: Constructor<T>)
{
return within(via, type, false);
}
/**
* Returns an array of Hats of the specified type,
* which are extracted from the specified array of elements.
*/
export function map<T extends IHat>(elements: Element[], type: Constructor<T>): T[];
export function map<T extends IHat>(elementContainer: Element | IHat, type: Constructor<T>): T[];
export function map<T extends IHat>(e: IHat | Element | Element[], type: Constructor<T>): T[]
{
e = (!(e instanceof Element) && !window.Array.isArray(e)) ? e.head : e;
const elements = e instanceof Element ? window.Array.from(e.children) : e;
return elements
.map(e => of(e, type))
.filter((o): o is T => o instanceof type);
}
/**
* Returns the element succeeding the specified element in
* the DOM that is connected to a hat of the specified type.
*/
export function next<T extends IHat>(via: Element | IHat, type: Constructor<T>): T | null
{
via = via instanceof Element ? via : via.head;
for (;;)
{
via = via.nextElementSibling as Element;
if (!(via instanceof Element))
return null;
const hat = of(via, type);
if (hat)
return hat;
}
}
/**
* Returns the element preceeding the specified element in the DOM
* that is connected to a hat of the specified type.
*/
export function previous<T extends IHat>(via: Element | IHat, type: Constructor<T>): T | null
{
via = via instanceof Element ? via : via.head;
for (;;)
{
via = via.previousElementSibling as Element;
if (!(via instanceof Element))
return null;
const hat = of(via, type);
if (hat)
return hat;
}
}
/** */
function within<T extends IHat>(via: Node | IHat, type: Constructor<T>, one: boolean)
{
const e =
via instanceof Element ? via :
via instanceof Node ? via.parentElement :
via.head;
if (!e)
throw "Cannot perform this method using the specified node.";
const names = ctorNames.get(type);
// If there is no class name found for the specified hat type,
// this could possibly be an error (meaning that the hat type
// wasn't registered). But it could also be a legitimate case of the
// hat simply not having been registered at the time of this
// function being called.
if (!names || names.length === 0)
return [];
const descendents = names.length === 1 ?
e.getElementsByClassName(names[0]) :
e.querySelectorAll(names.map(n => "." + n).join());
const hats: T[] = [];
const len = one && descendents.length > 0 ? 1 : descendents.length;
for (let i = -1; ++i < len;)
{
const descendent = descendents[i];
const hat = Hat.of(descendent, type);
if (hat)
hats.push(hat);
}
return hats;
}
/** */
function childrenOf<T extends IHat>(e: Element, hatType?: Constructor<T>)
{
let children = globalThis.Array.from(e.children);
if (hatType)
children = children.filter(e => Hat.of(e, hatType));
return children;
}
/**
* Returns a unique CSS class name that corresponds to the type
* of the object.
*/
function getConstructorClassNames(object: object)
{
const existingNames = ctorNames.get(object.constructor);
if (existingNames)
return existingNames;
const ctors: any[] = [object.constructor];
const names: string[] = [];
for (;;)
{
const ctor = ctors[ctors.length - 1];
const next = Object.getPrototypeOf(ctor);
if (next === null || next === Object || next === Function)
break;
ctors.push(next);
}
for (const ctor of ctors)
{
let name = ctor.name || "";
if (name.length < 3)
name = "_hat_" + name + (++inc);
names.push(name);
}
for (let i = ctors.length; i-- > 0;)
{
const ctor = ctors[i];
if (!ctorNames.has(ctor))
ctorNames.set(ctor, names.slice(i));
}
return names;
}
const ctorNames = new WeakMap<Function, string[]>();
const hats = new WeakMap<Element, object[]>();
const signals = new WeakMap<object, [Function, Function][]>();
let inc = 0;
/**
*
*/
export class Array<THat extends IHat = IHat>
{
/** */
constructor(
private readonly parentElement: Element,
private readonly hatType: Constructor<THat>)
{
this.marker = document.createComment("");
parentElement.append(this.marker);
}
private readonly marker: Comment;
/** */
* [Symbol.iterator]()
{
for (let i = -1; ++i < this.parentElement.children.length;)
{
const child = this.parentElement.children.item(i);
if (child)
{
const hat = Hat.of(child, this.hatType);
if (hat)
yield hat;
}
}
}
/** */
map(): THat[];
map<T>(mapFn: (value: THat, index: number, array: THat[]) => T): T[];
map(mapFn?: (value: THat, index: number, array: THat[]) => any)
{
const elements = childrenOf(this.parentElement, this.hatType);
const hats = Hat.map(elements, this.hatType);
return mapFn ? hats.map(mapFn) : hats;
}
/** */
at(index: number)
{
return this.map().at(index) || null;
}
/** */
insert(...hats: THat[]): number;
insert(index: number, ...hats: THat[]): number;
insert(a: number | THat, ...newHats: THat[])
{
const index = typeof a === "number" ? (a || 0) : -1;
const existingHats = this.map();
if (typeof a === "object")
newHats.unshift(a);
if (newHats.length === 0)
return;
if (existingHats.length === 0)
{
this.parentElement.prepend(...newHats.map(c => c.head));
}
else
{
const target = index >= existingHats.length ?
(existingHats.at(index) as IHat).head :
this.marker;
for (const hat of newHats)
this.parentElement.insertBefore(hat.head, target);
}
return index < 0 ? existingHats.length + newHats.length : index;
}
/** */
move(fromIndex: number, toIndex: number)
{
const children = childrenOf(this.parentElement, this.hatType);
const target = children.at(toIndex);
const source = children.at(fromIndex);
if (source && target)
target.insertAdjacentElement("beforebegin", source);
}
/** */
indexOf(hat: THat)
{
const children = childrenOf(this.parentElement, this.hatType);
for (let i = -1; ++i < children.length;)
if (children[i] === hat.head)
return i;
return -1;
}
/** */
get length()
{
return childrenOf(this.parentElement, this.hatType).length;
}
/** */
observe(callback: (mut: MutationRecord) => void)
{
if (this.observers.length === 0)
{
const mo = new MutationObserver(mutations =>
{
for (const mut of mutations)
for (const fn of this.observers)
fn(mut);
});
mo.observe(this.parentElement, { childList: true });
}
this.observers.push(callback);
}
private readonly observers: ((mut: MutationRecord) => void)[] = [];
/** */
private toJSON()
{
return this.map();
}
}
}
//@ts-ignore CommonJS compatibility
if (typeof module === "object") Object.assign(module.exports, { Hat });
// ES module compatibility
declare module "hatjs"
{
const __export: { Hat: typeof Hat };
export = __export;
}
// The comment and + prefix is removed during npm run bundle
//+ export { Hat }