Repository navigation
Expand file tree
/
Copy pathsemanticForms.js
More file actions
101 lines (82 loc) · 4.41 KB
/
Copy pathsemanticForms.js
File metadata and controls
101 lines (82 loc) · 4.41 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
99
100
101
const { createKeyboardShortcut, shortcutListener } = require('./lib/keyboardShortcuts.js')
const { enhanceInput, handleUndoRedo, setInvalid, clearInvalid } = require('./lib/inputEnhancements.js')
const { enhanceTabs } = require('./lib/tabs.js')
const { setInkOffset } = require('./lib/helpers.js')
// one observer per tree that has been handed to semanticForms, so that a shadow root gets its own without replacing the document's
const observers = new WeakMap()
// the keyboard shortcuts every form has asked for, kept in one list with one listener for them all, since semanticForms runs again for every form added to the page. a shortcut whose field has left the page is dropped
const keyboardShortcuts = []
let listeningForShortcuts = false
const pruneShortcuts = () => {
for (let i = keyboardShortcuts.length - 1; i >= 0; i--) if (!keyboardShortcuts[i].input.isConnected) keyboardShortcuts.splice(i, 1)
}
// progressively enhances every semanticForms form in a tree
//
// root: document or shadow root to search; defaults to the main document
const semanticForms = (root = document) => {
// do some feature detection so none of the JS executes if the browser is too old
if (typeof document.getElementsByClassName !== 'function' || typeof document.querySelector !== 'function' || !document.body.classList || !window.MutationObserver) {
console.warn('semantic-forms was loaded into an unsupported browser and will not execute.')
return
}
// custom keyboard shortcut listener
if (!listeningForShortcuts) {
document.addEventListener('keydown', (e) => shortcutListener(e, keyboardShortcuts))
listeningForShortcuts = true
}
pruneShortcuts()
// progressively enhance form elements that have the semanticForms class
const forms = root.querySelectorAll('form.semanticForms:not(.semanticFormsActive), table.semanticForms:not(.semanticFormsActive)')
for (const form of forms) {
form.classList.add('semanticFormsActive')
if (form.classList.contains('lowFlow')) continue
// how far this font's letters sit from the middle of their own line box, which the stylesheet subtracts wherever it centers text. it is measured per form rather than per field, so that a form holding nothing but buttons is corrected too
setInkOffset(form, form)
// update each input in the semantic form
const inputs = Array.from(form.querySelectorAll('input, textarea, select'))
for (const input of inputs) {
enhanceInput(input, form)
// handle keyboard shortcuts
if (input.getAttribute('data-focus-key') !== null && !keyboardShortcuts.some(shortcut => shortcut.input === input)) {
const shortcut = createKeyboardShortcut(input, keyboardShortcuts)
keyboardShortcuts.push(shortcut)
}
}
// done after the inputs so that anything sizing itself measures while it is still on screen
enhanceTabs(form)
}
// prevents multiple listeners
document.removeEventListener('keydown', handleUndoRedo)
document.addEventListener('keydown', handleUndoRedo)
// monitor changes to this tree and enhance new semanticForms forms that get added to it
if (!observers.has(root)) {
const observer = new window.MutationObserver(mutations => {
let stop = false
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeName === 'FORM' || node?.querySelector?.('form')) {
semanticForms(root)
stop = true
}
}
if (stop) break
}
})
// a shadow root is observed directly; the document is observed through its body
observer.observe(root === document ? document.body : root, { attributes: false, childList: true, characterData: false, subtree: true })
observers.set(root, observer)
// long standing name for the document's observer, kept for anything already using it
if (root === document) window.semanticFormsObserver = observer
}
semanticForms.reinitialize = form => {
// with no form given this just rescans for forms that are not active yet
if (!form) return semanticForms(root)
form.classList.remove('semanticFormsActive')
// the form knows which tree it belongs to, so a form in a shadow root reinitializes there
semanticForms(form.getRootNode())
}
}
// marks a field invalid with a message saying why, or clears that: see the docs for setInvalid
semanticForms.setInvalid = setInvalid
semanticForms.clearInvalid = clearInvalid
module.exports = semanticForms