-
Notifications
You must be signed in to change notification settings - Fork 3
/
semanticForms.js
145 lines (135 loc) · 5.25 KB
/
semanticForms.js
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
(function (global) {
// 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) {
return
}
var forms = document.querySelectorAll('form.semanticForms')
var fl = forms.length
var f
var form
var inputs
var input
var nodeName
var type
var label
var newLabel
var dl
var l
var clearfieldHorizontalOffset
var clearfieldVerticalOffset
var i
// don't execute any of the JS if no forms class semanticForms exist
if (fl <= 0) {
return
}
for (f = 0; f < fl; f++) {
form = forms[f]
if (!form.classList.contains('lowFlow')) {
clearfieldHorizontalOffset = parseInt(form.getAttribute('data-clearfield-horizontal-offset')) || 21
clearfieldVerticalOffset = parseInt(form.getAttribute('data-clearfield-vertical-offset')) || 5
inputs = Array.prototype.slice.call(form.getElementsByTagName('input')).concat(Array.prototype.slice.call(form.getElementsByTagName('textarea'))).concat(Array.prototype.slice.call(form.getElementsByTagName('select')))
l = inputs.length
for (i = 0; i < l; i++) {
input = inputs[i]
if (input && input.id) {
nodeName = input.nodeName
type = input.getAttribute('type')
if (nodeName === 'TEXTAREA' || nodeName === 'SELECT' || type === 'text' || type === 'password' || type === 'email' || type === 'date' || type === 'checkbox' || type === 'radio') {
dl = input.parentNode
while (dl && dl.nodeName !== 'DL') {
dl = dl.parentNode
}
if (dl) {
if (!dl.classList.contains('floatLabelForm')) {
dl.classList.add('floatLabelForm')
}
if (type === 'checkbox' || type === 'radio') {
label = document.querySelector('label[data-for=' + input.parentNode.parentNode.id + ']')
} else {
label = document.querySelector('label[for=' + input.id + ']')
}
label = label.innerHTML
if (type === 'checkbox' || type === 'radio') {
dl = input.parentNode
while (dl && dl.nodeName !== 'DD') {
dl = dl.parentNode
}
if (dl.firstChild.nodeName !== 'LABEL') {
newLabel = document.createElement('label')
newLabel.className = 'floatLabelFormAnimatedLabel'
newLabel.innerHTML = label
dl.insertBefore(newLabel, dl.firstChild)
}
} else {
newLabel = document.createElement('label')
newLabel.setAttribute('for', input.id)
newLabel.className = 'floatLabelFormAnimatedLabel'
newLabel.innerHTML = label
insertAfter(newLabel, input)
}
if (nodeName !== 'SELECT' && type !== 'checkbox' && type !== 'radio' && type !== 'date') {
// if it doesn't have a placeholder, add a blank one
if (!input.getAttribute('placeholder')) {
input.setAttribute('placeholder', '')
}
inputHandler(input) // force x to appear on inputs with prefilled value
}
}
}
}
}
}
}
document.addEventListener('input', inputHandler)
document.addEventListener('mousemove', function (e) {
var el = e.target
var nodeName = el.nodeName
var type = el.getAttribute('type')
if (nodeName === 'TEXTAREA' || type === 'text' || type === 'password' || type === 'email') {
inputHandler(e)
if (el.offsetWidth - clearfieldHorizontalOffset < e.clientX - el.getBoundingClientRect().left && clearfieldHorizontalOffset + clearfieldVerticalOffset > e.clientY - el.getBoundingClientRect().top
) {
if (!el.classList.contains('onX')) {
el.classList.add('onX')
}
} else {
el.classList.remove('onX')
}
}
})
document.addEventListener('click', function (e) {
var el = e.target
var nodeName = el.nodeName
var type = el.getAttribute('type')
if (nodeName === 'TEXTAREA' || type === 'text' || type === 'password' || type === 'email') {
if (el.offsetWidth - clearfieldHorizontalOffset < e.clientX - el.getBoundingClientRect().left &&
clearfieldHorizontalOffset + clearfieldVerticalOffset > e.clientY - el.getBoundingClientRect().top
) {
el.value = ''
el.classList.remove('x')
el.classList.remove('onX')
}
}
})
function inputHandler (e) {
var el = e.target || e
var nodeName = el.nodeName
var type = el.getAttribute('type')
if ((nodeName && type) && (nodeName === 'TEXTAREA' || type === 'text' || type === 'password' || type === 'email')) {
if (el.value) {
if (!el.classList.contains('x')) {
el.classList.add('x')
}
} else {
el.classList.remove('x')
}
}
}
function insertAfter (newNode, referenceNode) {
if (referenceNode.nextSibling) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling)
} else {
referenceNode.parentNode.appendChild(newNode)
}
}
})(this)