-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextfields.js
67 lines (57 loc) · 2.12 KB
/
textfields.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
"use strict";
// Create a DynamicTextFields object using the "new" operator, with the only argument passed in being
// the ID of an element that will contain all of the text fields you will want registered and styled.
function DynamicTextFields(containerID) {
if (!containerID) {
console.log("ERROR: You must pass in the ID of the containing element");
return;
}
this.container = document.getElementById(containerID);
}
DynamicTextFields.prototype.resetStyle = function(evt) {
if (evt.target.value.trim() === "") {
if (evt.target.classList.contains("required")) {
evt.target.classList.add("not-set");
} else {
this.classList.remove("notempty");
}
} else {
if (evt.target.classList.contains("required")) {
evt.target.classList.remove("not-set");
}
this.classList.add("notempty");
}
}
DynamicTextFields.prototype.resetAllStyles = function() {
let els = this.container.getElementsByClassName("dyn-textfield-input");
for (let i = 0; i < els.length; i++) {
if (els[i].value === "") {
els[i].classList.remove("notempty");
} else {
els[i].classList.add("notempty");
}
}
}
DynamicTextFields.prototype.deregisterAll = function() {
let els = this.container.getElementsByClassName("dyn-textfield-input");
for (let i = 0; i < els.length; i++) {
els[i].removeEventListener("change", this.resetStyle);
}
}
// registerRefresher registers a listener on a text field that, when changed, affects the the value
// of another text field (making the affected field needing a style reset). Register a refresher to
// ensure that the input being set programmatically will appear correctly based on whether it's empty.
DynamicTextFields.prototype.registerRefresher = function(toListenID, toRefreshID) {
let r = document.getElementById(toRefreshID);
document.getElementById(toListenID).addEventListener("input", this.resetStyle.bind(r));
}
DynamicTextFields.prototype.registerAll = function() {
let fs = this.container.getElementsByClassName("dyn-textfield-input");
if (fs.length === 0) {
return;
}
for (let i = 0; i < fs.length; i++) {
fs[i].addEventListener("change", this.resetStyle);
}
}
export default DynamicTextFields;