semantic-forms 3.0.8 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/semanticForms.js CHANGED
@@ -1,93 +1,88 @@
1
- window.semanticForms = function () {
1
+ window.semanticForms = () => {
2
2
  // do some feature detection so none of the JS executes if the browser is too old
3
- if (typeof document.getElementsByClassName !== 'function' || typeof document.querySelector !== 'function' || !document.body.classList) {
4
- return
5
- }
3
+ if (typeof document.getElementsByClassName !== 'function' || typeof document.querySelector !== 'function' || !document.body.classList || !window.MutationObserver) return
6
4
 
5
+ // progressively enhance form elements that have the semanticForms class
7
6
  const forms = document.querySelectorAll('form.semanticForms:not(.semanticFormsActive)')
8
- const fl = forms.length
9
- let f
10
- let form
11
- let inputs
12
- let input
13
- let nodeName
14
- let type
15
- let label
16
- let labelHTML
17
- let newLabel
18
- let dl
19
- let l
20
- let clearfieldHorizontalOffset
21
- let clearfieldVerticalOffset
22
- let i
23
-
24
- // don't execute any of the JS if no forms class semanticForms exist
25
- if (fl <= 0) {
26
- return
27
- }
28
-
29
- for (f = 0; f < fl; f++) {
30
- form = forms[f]
7
+ for (const form of forms) {
31
8
  form.classList.add('semanticFormsActive')
32
9
  if (!form.classList.contains('lowFlow')) {
33
- clearfieldHorizontalOffset = parseInt(form.getAttribute('data-clearfield-horizontal-offset')) || 21
34
- clearfieldVerticalOffset = parseInt(form.getAttribute('data-clearfield-vertical-offset')) || 5
35
- inputs = Array.prototype.slice.call(form.getElementsByTagName('input')).concat(Array.prototype.slice.call(form.getElementsByTagName('textarea'))).concat(Array.prototype.slice.call(form.getElementsByTagName('select')))
36
- l = inputs.length
37
-
38
- for (i = 0; i < l; i++) {
39
- input = inputs[i]
40
- if (input && input.id) {
41
- nodeName = input.nodeName
42
- type = input.getAttribute('type')
10
+ const clearfieldHorizontalOffset = parseInt(form.getAttribute('data-clearfield-horizontal-offset')) || 21
11
+ const clearfieldVerticalOffset = parseInt(form.getAttribute('data-clearfield-vertical-offset')) || 5
12
+ const inputs = Array.prototype.slice.call(form.getElementsByTagName('input')).concat(Array.prototype.slice.call(form.getElementsByTagName('textarea'))).concat(Array.prototype.slice.call(form.getElementsByTagName('select')))
13
+ for (const input of inputs) {
14
+ if (input.id) {
15
+ const nodeName = input.nodeName
16
+ const type = input.getAttribute('type')
43
17
  if (nodeName === 'TEXTAREA' || nodeName === 'SELECT' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'image' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week') {
44
- dl = input.parentNode
45
- while (dl && dl.nodeName !== 'DL') {
46
- dl = dl.parentNode
47
- }
18
+ let dl = input.parentNode
19
+ while (dl && dl.nodeName !== 'DL') dl = dl.parentNode
48
20
  if (dl) {
49
- if (!dl.classList.contains('floatLabelForm')) {
50
- dl.classList.add('floatLabelForm')
51
- }
52
- if (input.parentNode.parentNode.id && (type === 'checkbox' || type === 'radio')) {
53
- label = document.querySelector('label[data-for=' + input.parentNode.parentNode.id.replace(/\./g, '\\.') + ']')
54
- } else {
55
- label = document.querySelector('label[for=' + input.id.replace(/\./g, '\\.') + ']')
56
- }
57
- labelHTML = label.innerHTML
21
+ if (!dl.classList.contains('floatLabelForm')) dl.classList.add('floatLabelForm')
22
+ let label
23
+ if (input.parentNode.parentNode.id && (type === 'checkbox' || type === 'radio')) label = document.querySelector('label[data-for=' + input.parentNode.parentNode.id.replace(/\./g, '\\.') + ']')
24
+ else label = document.querySelector('label[for=' + input.id.replace(/\./g, '\\.') + ']')
58
25
  if (type === 'checkbox' || type === 'radio') {
59
26
  dl = input.parentNode
60
- while (dl && dl.nodeName !== 'DD') {
61
- dl = dl.parentNode
62
- }
27
+ while (dl && dl.nodeName !== 'DD') dl = dl.parentNode
63
28
  if (dl.firstChild.nodeName !== 'LABEL') {
64
- newLabel = document.createElement('label')
29
+ const newLabel = document.createElement('label')
65
30
  newLabel.className = 'floatLabelFormAnimatedLabel'
66
31
  if (type === 'checkbox' && input.parentNode.nodeName === 'DD') {
67
32
  newLabel.setAttribute('for', input.id)
68
33
  input.parentNode.classList.add('singleCheckbox')
69
34
  newLabel.className = ''
70
35
  }
71
- newLabel.innerHTML = labelHTML
36
+ newLabel.innerHTML = label.innerHTML
72
37
  dl.insertBefore(newLabel, dl.firstChild)
73
38
  }
74
39
  } else {
75
- newLabel = document.createElement('label')
40
+ const newLabel = document.createElement('label')
76
41
  newLabel.setAttribute('for', input.id)
77
42
  newLabel.className = 'floatLabelFormAnimatedLabel'
78
- newLabel.innerHTML = labelHTML
43
+ newLabel.innerHTML = label.innerHTML
79
44
  insertAfter(newLabel, input)
80
45
  label.setAttribute('hidden', 'hidden')
81
46
  }
82
-
83
47
  if (nodeName !== 'SELECT' && type !== 'checkbox' && type !== 'radio') {
84
48
  // if it doesn't have a placeholder, add a blank one
85
- if (!input.getAttribute('placeholder')) {
86
- input.setAttribute('placeholder', ' ')
87
- }
49
+ if (!input.getAttribute('placeholder')) input.setAttribute('placeholder', ' ')
88
50
  input.classList.add('semanticform')
89
51
  inputHandler(input) // force x to appear on inputs with prefilled value
90
52
  }
53
+ input.addEventListener('input', inputHandler)
54
+ input.addEventListener('mousemove', event => {
55
+ const el = event.target
56
+ const nodeName = el.nodeName
57
+ const type = el.getAttribute('type')
58
+ if (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week') {
59
+ inputHandler(event)
60
+ if (el.offsetWidth - clearfieldHorizontalOffset < event.clientX - el.getBoundingClientRect().left && clearfieldHorizontalOffset + clearfieldVerticalOffset > event.clientY - el.getBoundingClientRect().top
61
+ ) {
62
+ if (!el.classList.contains('onX')) {
63
+ el.classList.add('onX')
64
+ }
65
+ } else {
66
+ el.classList.remove('onX')
67
+ }
68
+ }
69
+ })
70
+ input.addEventListener('click', event => {
71
+ const el = event.target
72
+ const nodeName = el.nodeName
73
+ const type = el.getAttribute('type')
74
+ if (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week') {
75
+ if (el.offsetWidth - clearfieldHorizontalOffset < event.clientX - el.getBoundingClientRect().left &&
76
+ clearfieldHorizontalOffset + clearfieldVerticalOffset > event.clientY - el.getBoundingClientRect().top
77
+ ) {
78
+ el.value = ''
79
+ el.dispatchEvent(new Event('input'))
80
+ el.form.dispatchEvent(new Event('input'))
81
+ el.classList.remove('x')
82
+ el.classList.remove('onX')
83
+ }
84
+ }
85
+ })
91
86
  }
92
87
  }
93
88
  }
@@ -95,77 +90,42 @@ window.semanticForms = function () {
95
90
  }
96
91
  }
97
92
 
98
- document.addEventListener('input', inputHandler)
99
-
100
- document.addEventListener('mousemove', function (e) {
101
- if (isPatternElement(e)) {
102
- const el = e.target
103
- const nodeName = el.nodeName
104
- const type = el.getAttribute('type')
105
- if (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week') {
106
- inputHandler(e)
107
- if (el.offsetWidth - clearfieldHorizontalOffset < e.clientX - el.getBoundingClientRect().left && clearfieldHorizontalOffset + clearfieldVerticalOffset > e.clientY - el.getBoundingClientRect().top
108
- ) {
109
- if (!el.classList.contains('onX')) {
110
- el.classList.add('onX')
111
- }
112
- } else {
113
- el.classList.remove('onX')
114
- }
115
- }
116
- }
117
- })
118
-
119
- document.addEventListener('click', function (e) {
120
- if (isPatternElement(e)) {
121
- const el = e.target
122
- const nodeName = el.nodeName
123
- const type = el.getAttribute('type')
124
- if (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week') {
125
- if (el.offsetWidth - clearfieldHorizontalOffset < e.clientX - el.getBoundingClientRect().left &&
126
- clearfieldHorizontalOffset + clearfieldVerticalOffset > e.clientY - el.getBoundingClientRect().top
127
- ) {
128
- el.value = ''
129
- el.dispatchEvent(new Event('input'))
130
- el.form.dispatchEvent(new Event('input'))
131
- el.classList.remove('x')
132
- el.classList.remove('onX')
133
- }
134
- }
135
- }
136
- })
137
-
138
- function inputHandler (e) {
139
- if (isPatternElement(e)) {
140
- const el = e.target || e
141
- const nodeName = el.nodeName
142
- const type = el.getAttribute('type') || nodeName === 'TEXTAREA'
143
- if ((nodeName && type) && (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week')) {
144
- if (el.value) {
145
- if (!el.classList.contains('x')) {
146
- el.classList.add('x')
147
- }
148
- } else {
149
- el.classList.remove('x')
93
+ // handle keystrokes or other input
94
+ function inputHandler (event) {
95
+ const el = event.target || event
96
+ const nodeName = el.nodeName
97
+ const type = el.getAttribute('type') || nodeName === 'TEXTAREA'
98
+ if ((nodeName && type) && (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week')) {
99
+ if (el.value) {
100
+ if (!el.classList.contains('x')) {
101
+ el.classList.add('x')
150
102
  }
103
+ } else {
104
+ el.classList.remove('x')
151
105
  }
152
106
  }
153
107
  }
154
108
 
109
+ // utility method for inserting an element after another element
155
110
  function insertAfter (newNode, referenceNode) {
156
- if (referenceNode.nextSibling) {
157
- referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling)
158
- } else {
159
- referenceNode.parentNode.appendChild(newNode)
160
- }
111
+ if (referenceNode.nextSibling) referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling)
112
+ else referenceNode.parentNode.appendChild(newNode)
161
113
  }
162
114
 
163
- function isPatternElement (e) {
164
- const thisEl = e.target || e
165
- if (!thisEl || !thisEl.classList || !thisEl.classList.contains('semanticform')) {
166
- return false
167
- }
168
- return true
115
+ // monitor changes to the DOM and enhance new semanticForms forms that get added
116
+ if (!window.semanticFormsObserver) {
117
+ window.semanticFormsObserver = new window.MutationObserver((mutations) => {
118
+ let stop = false
119
+ for (const mutation of mutations) {
120
+ for (const node of mutation.addedNodes) {
121
+ if (node.nodeName === 'FORM') {
122
+ window.semanticForms()
123
+ stop = true
124
+ }
125
+ }
126
+ if (stop) break
127
+ }
128
+ })
129
+ window.semanticFormsObserver.observe(document.body, { attributes: false, childList: true, characterData: false, subtree: true })
169
130
  }
170
131
  }
171
- window.semanticForms()