semantic-forms 3.0.8 → 3.2.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,89 @@
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.classList.contains('semanticform')) continue
15
+ if (input.id) {
16
+ const nodeName = input.nodeName
17
+ const type = input.getAttribute('type')
43
18
  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
- }
19
+ let dl = input.parentNode
20
+ while (dl && dl.nodeName !== 'DL') dl = dl.parentNode
48
21
  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
22
+ if (!dl.classList.contains('floatLabelForm')) dl.classList.add('floatLabelForm')
23
+ let label
24
+ if (input.parentNode.parentNode.id && (type === 'checkbox' || type === 'radio')) label = document.querySelector('label[data-for=' + input.parentNode.parentNode.id.replace(/\./g, '\\.') + ']')
25
+ else label = document.querySelector('label[for=' + input.id.replace(/\./g, '\\.') + ']')
26
+ input.classList.add('semanticform')
58
27
  if (type === 'checkbox' || type === 'radio') {
59
28
  dl = input.parentNode
60
- while (dl && dl.nodeName !== 'DD') {
61
- dl = dl.parentNode
62
- }
29
+ while (dl && dl.nodeName !== 'DD') dl = dl.parentNode
63
30
  if (dl.firstChild.nodeName !== 'LABEL') {
64
- newLabel = document.createElement('label')
31
+ const newLabel = document.createElement('label')
65
32
  newLabel.className = 'floatLabelFormAnimatedLabel'
66
33
  if (type === 'checkbox' && input.parentNode.nodeName === 'DD') {
67
34
  newLabel.setAttribute('for', input.id)
68
35
  input.parentNode.classList.add('singleCheckbox')
69
36
  newLabel.className = ''
70
37
  }
71
- newLabel.innerHTML = labelHTML
38
+ newLabel.innerHTML = label.innerHTML
72
39
  dl.insertBefore(newLabel, dl.firstChild)
73
40
  }
74
41
  } else {
75
- newLabel = document.createElement('label')
42
+ const newLabel = document.createElement('label')
76
43
  newLabel.setAttribute('for', input.id)
77
44
  newLabel.className = 'floatLabelFormAnimatedLabel'
78
- newLabel.innerHTML = labelHTML
45
+ newLabel.innerHTML = label.innerHTML
79
46
  insertAfter(newLabel, input)
80
47
  label.setAttribute('hidden', 'hidden')
81
48
  }
82
-
83
49
  if (nodeName !== 'SELECT' && type !== 'checkbox' && type !== 'radio') {
84
50
  // if it doesn't have a placeholder, add a blank one
85
- if (!input.getAttribute('placeholder')) {
86
- input.setAttribute('placeholder', ' ')
87
- }
88
- input.classList.add('semanticform')
51
+ if (!input.getAttribute('placeholder')) input.setAttribute('placeholder', ' ')
89
52
  inputHandler(input) // force x to appear on inputs with prefilled value
90
53
  }
54
+ input.addEventListener('input', inputHandler)
55
+ input.addEventListener('mousemove', event => {
56
+ const el = event.target
57
+ const nodeName = el.nodeName
58
+ const type = el.getAttribute('type')
59
+ 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') {
60
+ inputHandler(event)
61
+ if (el.offsetWidth - clearfieldHorizontalOffset < event.clientX - el.getBoundingClientRect().left && clearfieldHorizontalOffset + clearfieldVerticalOffset > event.clientY - el.getBoundingClientRect().top
62
+ ) {
63
+ if (!el.classList.contains('onX')) {
64
+ el.classList.add('onX')
65
+ }
66
+ } else {
67
+ el.classList.remove('onX')
68
+ }
69
+ }
70
+ })
71
+ input.addEventListener('click', event => {
72
+ const el = event.target
73
+ const nodeName = el.nodeName
74
+ const type = el.getAttribute('type')
75
+ 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') {
76
+ if (el.offsetWidth - clearfieldHorizontalOffset < event.clientX - el.getBoundingClientRect().left &&
77
+ clearfieldHorizontalOffset + clearfieldVerticalOffset > event.clientY - el.getBoundingClientRect().top
78
+ ) {
79
+ el.value = ''
80
+ el.dispatchEvent(new Event('input'))
81
+ el.form.dispatchEvent(new Event('input'))
82
+ el.classList.remove('x')
83
+ el.classList.remove('onX')
84
+ }
85
+ }
86
+ })
91
87
  }
92
88
  }
93
89
  }
@@ -95,77 +91,47 @@ window.semanticForms = function () {
95
91
  }
96
92
  }
97
93
 
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')
94
+ // handle keystrokes or other input
95
+ function inputHandler (event) {
96
+ const el = event.target || event
97
+ const nodeName = el.nodeName
98
+ const type = el.getAttribute('type') || nodeName === 'TEXTAREA'
99
+ 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')) {
100
+ if (el.value) {
101
+ if (!el.classList.contains('x')) {
102
+ el.classList.add('x')
114
103
  }
104
+ } else {
105
+ el.classList.remove('x')
115
106
  }
116
107
  }
117
- })
108
+ }
118
109
 
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
- })
110
+ // utility method for inserting an element after another element
111
+ function insertAfter (newNode, referenceNode) {
112
+ if (referenceNode.nextSibling) referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling)
113
+ else referenceNode.parentNode.appendChild(newNode)
114
+ }
137
115
 
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')
116
+ // monitor changes to the DOM and enhance new semanticForms forms that get added
117
+ if (!window.semanticFormsObserver) {
118
+ window.semanticFormsObserver = new window.MutationObserver((mutations) => {
119
+ let stop = false
120
+ for (const mutation of mutations) {
121
+ for (const node of mutation.addedNodes) {
122
+ if (node.nodeName === 'FORM') {
123
+ window.semanticForms()
124
+ stop = true
147
125
  }
148
- } else {
149
- el.classList.remove('x')
150
126
  }
127
+ if (stop) break
151
128
  }
152
- }
153
- }
154
-
155
- function insertAfter (newNode, referenceNode) {
156
- if (referenceNode.nextSibling) {
157
- referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling)
158
- } else {
159
- referenceNode.parentNode.appendChild(newNode)
160
- }
129
+ })
130
+ window.semanticFormsObserver.observe(document.body, { attributes: false, childList: true, characterData: false, subtree: true })
161
131
  }
132
+ }
162
133
 
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
169
- }
134
+ window.semanticForms.reinitialize = (form) => {
135
+ form.classList.remove('semanticFormsActive')
136
+ window.semanticForms()
170
137
  }
171
- window.semanticForms()