This repository has been archived by the owner on Nov 1, 2020. It is now read-only.
/
float-labels.js
331 lines (301 loc) · 9.08 KB
/
float-labels.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
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
/*!
* Float Labels
*
* @version: 3.1.0
* @author: Paul Ryley (http://geminilabs.io)
* @url: https://geminilabs.github.io/float-labels.js
* @license: MIT
*/
/** global: NodeList, Option */
;(function( window, document, undefined )
{
"use strict";
var Plugin = function( el, options )
{
this.el = this.isString( el ) ? document.querySelectorAll( el ) : el;
if( !NodeList.prototype.isPrototypeOf( this.el ))return;
this.config = [];
this.options = options;
this.selectors = [];
this.init();
this.destroy = function() {
this.loop( function( el ) {
el.removeEventListener( 'reset', this.events.reset );
this.removeClasses( el );
}, function( field ) {
this.reset( field );
});
};
this.rebuild = function() {
this.loop( null, function( field ) {
this.floatLabel( field, true );
});
};
};
Plugin.prototype = {
defaults: {
customEvent : null,
customLabel : null,
exclude : '.no-label',
inputRegex : /email|number|password|search|tel|text|url/,
prefix : 'fl-',
prioritize : 'label', // label|placeholder
requiredClass: 'required',
style : 0, // 0|1|2
transform : 'input,select,textarea'
},
/** @return void */
init: function()
{
this.initEvents();
this.loop( function( el, i ) {
var style = this.config[i].style;
el.addEventListener( 'reset', this.events.reset );
el.classList.add( this.prefixed( 'form' ));
if( style ) {
el.classList.add( this.prefixed( 'style-' + style ));
}
}, function( field ) {
this.floatLabel( field );
});
},
/** @return void */
initEvents: function()
{
this.events = {
blur: this.onBlur.bind( this ),
change: this.onInput.bind( this ),
focus: this.onFocus.bind( this ),
input: this.onInput.bind( this ),
reset: this.onReset.bind( this ),
};
},
/** @return null|void */
build: function( el )
{
var labelEl = this.getLabel( el );
if( !labelEl )return;
var labelText = this.getLabelText( labelEl, el );
el.classList.add( this.prefixed( el.tagName.toLowerCase() ));
labelEl.classList.add( this.prefixed( 'label' ));
labelEl.text = labelText;
this.setPlaceholder( labelText, el );
this.wrapLabel( labelEl, el );
this.handleEvents( el, 'add' );
if( typeof this.config[this.current].customEvent === 'function' ) {
this.config[this.current].customEvent.call( this, el );
}
},
/** @return Element */
createEl: function( tag, attributes )
{
var el = ( typeof tag === 'string' ) ? document.createElement( tag ) : tag;
attributes = attributes || {};
for( var key in attributes ) {
if( !attributes.hasOwnProperty( key ))continue;
el.setAttribute( key, attributes[key] );
}
return el;
},
/** @return object */
extend: function()
{
var args = [].slice.call( arguments );
var result = args[0];
var extenders = args.slice(1);
Object.keys( extenders ).forEach( function( i ) {
for( var key in extenders[i] ) {
if( !extenders[i].hasOwnProperty( key ))continue;
result[key] = extenders[i][key];
}
});
return result;
},
/** @return null|void */
floatLabel: function( el, rebuild )
{
if( !el.getAttribute( 'id' ) || (
el.tagName === 'INPUT' && !this.config[this.current].inputRegex.test( el.getAttribute( 'type' ))
))return;
if( this.hasParent( el )) {
if( rebuild !== true )return;
this.reset( el );
}
this.build( el );
},
/** @return string|false */
getLabel: function( el )
{
var label = 'label[for="' + el.getAttribute( 'id' ) + '"]';
var labelEl = this.el[this.current].querySelectorAll( label );
// check for multiple labels with identical 'for' attributes
if( labelEl.length > 1 ) {
labelEl = el.parentNode.querySelectorAll( label );
}
if( labelEl.length === 1 ) {
return labelEl[0];
}
return false;
},
/** @return string */
getLabelText: function( labelEl, el )
{
var labelText = labelEl.textContent.replace( /[*:]/g, '' ).trim();
var placeholderText = el.getAttribute( 'placeholder' );
if( !labelText || ( labelText && placeholderText && this.config[this.current].prioritize === 'placeholder' )) {
labelText = placeholderText;
}
// call the custom defined label event
if( typeof this.config[this.current].customLabel === 'function' ) {
var customLabel = this.config[this.current].customLabel.call( this, labelEl, el );
if( customLabel !== undefined ) {
labelText = customLabel;
}
}
return labelText;
},
/** @return void */
handleEvents: function( el, action )
{
var events = this.events;
['blur','input','focus'].forEach( function( event ) {
if( el.type === 'file' && event === 'input' ) {
event = 'change';
}
el[ action + 'EventListener']( event, events[event] );
});
},
/** @return bool */
hasParent: function( el )
{
return el.parentNode.classList.contains( this.prefixed( 'wrap' ));
},
/** @return bool */
isString: function( str ) {
return Object.prototype.toString.call( str ) === "[object String]";
},
/** @return void */
loop: function( elCallback, fieldCallback ) {
for( var i = 0; i < this.el.length; ++i ) {
if( typeof this.selectors[i] === 'undefined' ) {
var config = this.extend( {}, this.defaults, this.options, this.el[i].getAttribute( 'data-options' ));
var exclude = ':not(' + config.exclude.split( /[\s,]+/ ).join( '):not(' ) + ')';
this.selectors[i] = config.transform.replace( /,/g, exclude + ',' ) + exclude;
this.config[i] = config;
}
var fields = this.el[i].querySelectorAll( this.selectors[i] );
this.current = i;
if( typeof elCallback === 'function' ) {
elCallback.call( this, this.el[i], i );
}
for( var x = 0; x < fields.length; ++x ) {
if( typeof fieldCallback === 'function' ) {
fieldCallback.call( this, fields[x], i );
}
}
}
},
/** @return void */
onBlur: function( ev )
{
ev.target.parentNode.classList.remove( this.prefixed( 'has-focus' ));
},
/** @return void */
onInput: function( ev )
{
var event = ev.target.value.length ? 'add' : 'remove';
ev.target.parentNode.classList[event]( this.prefixed( 'is-active' ));
},
/** @return void */
onFocus: function( ev )
{
ev.target.parentNode.classList.add( this.prefixed( 'has-focus' ));
},
/** @return void */
onReset: function()
{
var fields = this.el[this.current].querySelectorAll( this.selectors[this.current] );
for( var i = 0; i < fields.length; ++i ) {
fields[i].parentNode.classList.remove( this.prefixed( 'is-active' ));
}
},
/** @return string */
prefixed: function( value )
{
return this.config[this.current].prefix + value;
},
/** @return void */
removeClasses: function( el )
{
var prefix = this.config[this.current].prefix;
var classes = el.className.split( ' ' ).filter( function( c ) {
return c.lastIndexOf( prefix, 0 ) !== 0;
});
el.className = classes.join( ' ' ).trim();
},
/** @return null|void */
reset: function( el )
{
var parent = el.parentNode;
if( !this.hasParent( el ))return;
var fragment = document.createDocumentFragment();
while( parent.firstElementChild ) {
var childEl = parent.firstElementChild;
this.removeClasses( childEl );
fragment.appendChild( childEl );
}
parent.parentNode.replaceChild( fragment, parent );
this.resetPlaceholder( el );
this.handleEvents( el, 'remove' );
},
/** @return void */
resetPlaceholder: function( el )
{
var dataPlaceholder = 'data-placeholder';
var originalPlaceholder = el.getAttribute( dataPlaceholder );
if( originalPlaceholder !== null ) {
el.removeAttribute( dataPlaceholder );
el.setAttribute( 'placeholder', originalPlaceholder );
}
},
/** @return void */
setPlaceholder: function( labelText, el )
{
var placeholderText = el.getAttribute( 'placeholder' );
// add a placholder option to the select if it doesn't already exist
if( el.tagName === 'SELECT' ) {
var childEl = el.firstElementChild;
if( childEl.value ) {
el.insertBefore( new Option( labelText, '', true, true ), childEl );
}
else if( childEl.text === '' ) {
childEl.text = labelText;
}
}
// add a textarea/input placeholder attribute if it doesn't exist
else if( !placeholderText || this.config[this.current].prioritize === 'label' ) {
if( placeholderText ) {
el.setAttribute( 'data-placeholder', placeholderText );
}
el.setAttribute( 'placeholder', labelText );
}
},
/** @return void */
wrapLabel: function( labelEl, el )
{
var wrapper = this.createEl( 'div', {
class: this.prefixed( 'wrap' ) + ' ' + this.prefixed( 'wrap-' + el.tagName.toLowerCase() ),
});
if( el.value.length ) {
wrapper.classList.add( this.prefixed( 'is-active' ));
}
if( el.getAttribute( 'required' ) !== null || el.classList.contains( this.config[this.current].requiredClass )) {
wrapper.classList.add( this.prefixed( 'is-required' ));
}
el.parentNode.insertBefore( wrapper, el );
wrapper.appendChild( labelEl );
wrapper.appendChild( el );
},
};
window.FloatLabels = Plugin;
})( window, document );