This repository was archived by the owner on Mar 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.js
More file actions
72 lines (55 loc) · 1.93 KB
/
State.js
File metadata and controls
72 lines (55 loc) · 1.93 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
/*
* Copyright (c) 2017 - present, De Nieuwe Zaak
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
var util = require('./util');
var State = {};
function setState(inputElement, fieldElement) {
if(!inputElement.value || inputElement.value.length === 0) {
util.removeClass(fieldElement, 'has-value');
return;
}
if (inputElement.value.length > 0) {
util.addClass(fieldElement, 'has-value');
}
//TODO: Additional checks.
}
// Attaches events to input elements and pass along the field element for adjusting its classes.
var attachEvents = function(inputElement, fieldElement) {
inputElement.addEventListener('focus', function() {
util.addClass(fieldElement, 'has-focus');
}, false);
inputElement.addEventListener('focusout', function() {
util.removeClass(fieldElement, 'has-focus');
}, false);
inputElement.addEventListener('keyup', function(e) {
setState(e.target, fieldElement);
}, false);
inputElement.addEventListener('change', function(e) {
setState(e.target, fieldElement);
}, false);
}
// Initializes this module by setting initial values and attaching events.
State.init = function(selector) {
var _selector = selector || '[data-castlecss-field]';
// Once the window has loaded, we are ready to query for elements.
window.addEventListener('load', function() {
var fieldElements = document.querySelectorAll(_selector);
// Loop through each element to set their initial values and attach events.
for (var i = 0; i < fieldElements.length; i++) {
var fieldElement = fieldElements[i];
var inputElements = fieldElement.querySelectorAll('input, textarea, select');
for (var j = 0; j < inputElements.length; j++) {
var inputElement = inputElements[j];
setState(inputElement, fieldElement);
attachEvents(inputElement, fieldElement);
}
}
}, false);
};
module.exports = State;