-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathDom.mint
623 lines (526 loc) · 14.3 KB
/
Dom.mint
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
This module provides functions for working with the [DOM].
[DOM]: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model
*/
module Dom {
/*
Blurs the active element of the page.
Dom.blurActiveElement()
*/
fun blurActiveElement : Promise(Void) {
`document.activeElement && document.activeElement.blur()`
}
/*
Returns if the element is in an other element that matches the selector.
if let Just(div) = Document.getElementBySelector("div") {
Dom.containedInSelector(div, "body")
}
*/
fun containedInSelector (element : Dom.Element, selector : String) : Bool {
`
(() => {
for (let base of document.querySelectorAll(selector)) {
if (base.contains(element)) {
return true
}
}
return false
})()
`
}
/*
Returns if the base element contains the element.
if let Just(div) = Dom.getElementBySelector("div") {
if let Just(body) = Dom.getElementBySelector("body") {
Dom.contains(body, div)
}
}
*/
fun contains (base : Dom.Element, element : Dom.Element) : Bool {
`#{base}.contains(#{element})`
}
/*
Creates a new `Dom.Element` using the tag.
Dom.createElement("div")
*/
fun createElement (tag : String) : Dom.Element {
`document.createElement(#{tag})`
}
/*
Tries to focus the given element (if exists) in the next 150 milliseconds.
Fails silently if there is no element or if the element cannot be focused.
Dom.focus(Dom.getElementBySelector("input"))
*/
fun focus (maybeElement : Maybe(Dom.Element)) : Promise(Void) {
if let Maybe.Just(element) = maybeElement {
focusWhenVisible(element)
Promise.resolve(void)
}
}
/*
Focuses the first focusable descendant of the element. Fails silently if
there is no element.
if let Just(body) = Document.getElementBySelector("body") {
Dom.focusFirst(body)
}
*/
fun focusFirst (element : Dom.Element) : Promise(Void) {
element
|> getFocusableElements
|> Array.first
|> focus
}
/*
Tries to focus the element in the next 150 milliseconds.
if let Just(input) = Document.getElementBySelector("input") {
Dom.focusWhenVisible(input)
}
*/
fun focusWhenVisible (element : Dom.Element) : Promise(Result(String, Void)) {
`
new Promise((resolve) => {
let counter = 0
let focus = () => {
if (counter > 15) {
resolve(#{Result.Err("Could not focus the element in 150ms. Is it visible?")})
}
#{element}.focus()
if (document.activeElement != #{element}) {
counter++
setTimeout(focus, 10)
} else {
resolve(#{Result.Ok(void)})
}
}
focus()
})
`
}
/*
Returns the active (focused) element of the page.
Dom.getActiveElement() == Dom.getElementBySelector("body")
*/
fun getActiveElement : Maybe(Dom.Element) {
`
(() => {
if (document.activeElement) {
return #{Maybe.Just(`document.activeElement`)}
} else {
return #{Maybe.Nothing}
}
})()
`
}
/*
If the attribute is present, it will return its value on the element.
if let Just(element) = Dom.getElementById("my-div") {
Dom.getAttribute(element, "id") == "my-div"
}
*/
fun getAttribute (element : Dom.Element, attribute : String) : Maybe(String) {
`
(() => {
const value = #{element}.getAttribute(#{attribute})
if (value === "") {
return #{Maybe.Nothing}
} else {
return #{Maybe.Just(`value`)}
}
})()
`
}
/*
Returns all child elements of the element.
if let Just(body) = Dom.getElementBySelector("body") {
Dom.getChildren(body)
}
*/
fun getChildren (element : Dom.Element) : Array(Dom.Element) {
`[...#{element}.children]`
}
/*
Returns the `clientHeight` of the element.
if let Just(div) = Dom.getElementBySelector("div") {
Dom.getClientHeight(div) == 200
}
*/
fun getClientHeight (element : Dom.Element) : Number {
`#{element}.clientHeight || 0`
}
/*
Returns the `clientWidth` of the element.
if let Just(div) = Dom.getElementBySelector("div") {
Dom.getClientWidth(div) == 200
}
*/
fun getClientWidth (element : Dom.Element) : Number {
`#{element}.clientWidth || 0`
}
/*
Returns the dimensions (`BoundingClientRect`) of the element.
Dom.getDimensions(Dom.createElement("div")) = {
bottom: 0,
height: 0,
width: 0,
right: 0,
left: 0,
top: 0,
x: 0,
y: 0
}
*/
fun getDimensions (element : Dom.Element) : Dom.Dimensions {
`
(() => {
const rect = #{element}.getBoundingClientRect()
return #{{
bottom: `rect.bottom`,
height: `rect.height`,
width: `rect.width`,
right: `rect.right`,
left: `rect.left`,
top: `rect.top`,
x: `rect.x`,
y: `rect.y`
}}
})()
`
}
/*
Gets the element with the id from anywhere in the page.
Dom.getElementById("my-div")
*/
fun getElementById (id : String) : Maybe(Dom.Element) {
`
(() => {
let element = document.getElementById(#{id})
if (element) {
return #{Maybe.Just(`element`)}
} else {
return #{Maybe.Nothing}
}
})()
`
}
/*
Gets the first element to match the selector from anywhere in the page.
Dom.getElementBySelector("body section > p:first-child")
*/
fun getElementBySelector (selector : String) : Maybe(Dom.Element) {
`
(() => {
try {
let element = document.querySelector(#{selector})
if (element) {
return #{Maybe.Just(`element`)}
} else {
return #{Maybe.Nothing}
}
} catch (error) {
return #{Maybe.Nothing}
}
})()
`
}
/*
Gets the element from a point on the screen.
Dom.getElementFromPoint(0, 0)
*/
fun getElementFromPoint (left : Number, top : Number) : Maybe(Dom.Element) {
`
(() => {
const element = document.elementFromPoint(#{left}, #{top})
if (element) {
return #{Maybe.Just(`element`)}
} else {
return #{Maybe.Nothing}
}
})()
`
}
/*
Gets all descendant elements of an element which are matching the selector.
if let Just(body) = Dom.getElementBySelector("body") {
Dom.getElementsBySelector(body, "a[name]")
}
*/
fun getElementsBySelector (
element : Dom.Element,
selector : String
) : Array(Dom.Element) {
`Array.from(#{element}.querySelectorAll(#{selector}))`
}
/*
Returns all focusable descendant elements.
if let Just(body) = Dom.getElementBySelector("body") {
Dom.getFocusableElements(body)
}
*/
fun getFocusableElements (element : Dom.Element) : Array(Dom.Element) {
`
(() => {
// Save focused element.
const focused = document.activeElement
// Save scroll position.
const scrollX = window.scrollX
const scrollY = window.scrollY
// Save the scroll position of each element.
const scrollPositions =
[...document.querySelectorAll("*")].reduce((memo, element) => {
if (element.scrollHeight > 0 || element.scrollWidth > 0) {
memo.set(element, [element.scrollLeft, element.scrollTop])
}
return memo
}, new Map)
// Gather the focusable elements by focusing them and comparing it
// with the focused element.
const foundElements =
[...#{element}.querySelectorAll("*")].reduce((memo ,element) => {
element.focus()
if (document.activeElement == element && element.tabIndex !== -1) {
memo.push(element)
}
return memo
}, [])
// Restore scroll positions and focus.
for (let element in scrollPositions) {
const [x, y] = scrollPositions[element]
element.scrollTo(x, y)
}
window.scrollTo(scrollX, scrollY)
focused.focus()
return foundElements
})()
`
}
/*
Returns the scrollable height of the element.
if let Just(div) = Dom.getElementBySelector("div") {
Dom.getScrollHeight(div) == 0
}
*/
fun getScrollHeight (element : Dom.Element) : Number {
`#{element}.scrollHeight || 0`
}
/*
Returns the horizontal scroll position of the element.
if let Just(div) = Dom.getElementBySelector("div") {
Dom.getScrollLeft(div) == 0
}
*/
fun getScrollLeft (element : Dom.Element) : Number {
`#{element}.scrollLeft || 0`
}
/*
Returns the vertical scroll position of the element.
if let Just(div) = Dom.getElementBySelector("div") {
Dom.getScrollTop(div) == 0
}
*/
fun getScrollTop (element : Dom.Element) : Number {
`#{element}.scrollTop || 0`
}
/*
Returns the scrollable width of the element.
if let Just(div) = Dom.getElementBySelector("div") {
Dom.getScrollWidth(div) == 300
}
*/
fun getScrollWidth (element : Dom.Element) : Number {
`#{element}.scrollWidth || 0`
}
/*
Returns the table of contents of the element for the selectors.
if let Just(body) = Dom.getElementBySelector("body") {
Dom.getTableOfContents(body, "h1, h2, h3, h4") == [
{"h1", "The title of the page", "the-title-of-the-page"},
{"h2", "A subtitle of the page", "a-subtitle-of-the-page"},
{"h3", "A sub-subtitle of the page", "a-sub-subtitle-of-the-page"}
]
}
*/
fun getTableOfContents (
element : Dom.Element,
selector : String
) : Array(Tuple(String, String, String)) {
element
|> getElementsBySelector(selector)
|> Array.map(
(item : Dom.Element) : Tuple(String, String, String) {
let tag =
item
|> getTagName()
|> String.toLowerCase()
let text =
getTextContent(item)
let hash =
String.parameterize(text)
{tag, text, hash}
})
}
/*
Returns the tagname of the element.
if let Just(body) = Dom.getElementBySelector("body") {
Dom.getTagName(body) == "BODY"
}
*/
fun getTagName (element : Dom.Element) : String {
`#{element}.tagName`
}
/*
Returns the text content of the element.
if let Just(body) = Document.getElementBySelector("body") {
Dom.getTextContent(body)
}
*/
fun getTextContent (element : Dom.Element) : String {
`#{element}.textContent`
}
/*
Measures width of the text with the font using the canvas.
Dom.getTextWidth("Hello There", "20px sans-serif") = 300
*/
fun getTextWidth (text : String, font : String) : Number {
`
(() => {
const canvas = document.createElement('canvas');
const context = canvas.getContext("2d");
context.font = #{font};
return context.measureText(#{text}).width
})()
`
}
/*
Gets the value as string form an element. If the element supports value
it will return it, otherwise it returns an empty string.
if let Just(input) = Document.getElementBySelector("input[value=hello]") {
Dom.getValue(input) == "hello"
}
*/
fun getValue (element : Dom.Element) : String {
`
(() => {
let value = #{element}.value
if (typeof value === "string") {
return value
} else {
return ""
}
})()
`
}
/*
Returns whether or not the element matches the selector.
Dom.matches(Dom.createElement("div"), "div") == true
Dom.matches(Dom.createElement("div"), "p") == false
*/
fun matches (element : Dom.Element, selector : String) : Bool {
`
(() => {
try {
return #{element}.matches(#{selector})
} catch (error) {
return false
}
})()
`
}
/*
Scrolls the element to the position.
if let Just(body) = Document.getElementBySelector("body") {
Dom.scrollTo(body, 10, 10)
}
*/
fun scrollTo (
element : Dom.Element,
left : Number,
top : Number
) : Promise(Void) {
`#{element}.scrollTo({ left: #{left}, top: #{top} })`
}
/*
Sets the attribute to the value of the element and returns the element.
"a"
|> Dom.createElement
|> Dom.setAttribute("name", "test")
*/
fun setAttribute (
element : Dom.Element,
attribute : String,
value : String
) : Dom.Element {
`#{element}.setAttribute(#{attribute}, #{value}) && element`
}
/*
Sets the style to the value of the element.
if let Just(div) = Document.getElementBySelector("div") {
div
|> Dom.setStyle("background", "red")
|> Dom.setStyle("color", "white")
}
*/
fun setStyle (
element : Dom.Element,
name : String,
value : String
) : Dom.Element {
`
(() => {
#{element}.style[#{name}] = #{value}
return #{element}
})()
`
}
/*
Sets the value property of an element. It is used to set the value of `input`
fields programmatically.
if let Just(input) = Document.getElementBySelector("input") {
Dom.setValue(input, "Hello World!")
}
*/
fun setValue (element : Dom.Element, value : String) : Dom.Element {
`
(() => {
#{element}.value = #{value}
return #{element}
})()
`
}
/*
Smooth scroll the given element to the given position.
if let Just(body) = Document.getElementBySelector("body") {
Dom.smoothScrollTo(body, 10, 10)
}
*/
fun smoothScrollTo (
element : Dom.Element,
left : Number,
top : Number
) : Promise(Void) {
`#{element}.scrollTo({ behavior: 'smooth', left: #{left}, top: #{top} })`
}
/*
Returns the distance from the outer border of the element (including its
margin) to the left padding edge of the closest positioned ancestor element.
If the element is not in the DOM or not an element is passed (for example
`TextNode`) then the value will be `0`.
if let Just(div) = Document.getElementBySelector("div") {
Dom.offsetLeft(div)
}
*/
fun offsetLeft (element : Dom.Element) : Number {
`#{element}.offsetLeft || 0`
}
/*
Returns the distance from the outer border of the element (including its
margin) to the top padding edge of the closest positioned ancestor element.
If the element is not in the DOM or not an element is passed (for example
`TextNode`) then the value will be `0`.
if let Just(div) = Document.getElementBySelector("div") {
Dom.offsetTop(div)
}
*/
fun offsetTop (element : Dom.Element) : Number {
`#{element}.offsetTop || 0`
}
}