-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.wjst.js
186 lines (155 loc) · 4.13 KB
/
dom.wjst.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
class Dom {
template(elementId, variables = {}) {
const sourceElement = document.getElementById("template-" + elementId);
if (sourceElement) {
let code = sourceElement.innerHTML;
for (const variable in variables) {
code = code.split("{" + variable + "}").join(variables[variable]);
}
return code;
} else {
console.error(`Element with ID '${elementId}' not found.`);
}
}
replace(elementId, childHtml) {
const parentElement = document.getElementById(elementId);
if (parentElement) {
parentElement.innerHTML = childHtml;
} else {
console.error(`Element with ID '${elementId}' not found.`);
}
}
add(elementId, childHtml) {
const parentElement = document.getElementById(elementId);
if (parentElement) {
parentElement.innerHTML += childHtml;
} else {
console.error(`Element with ID '${elementId}' not found.`);
}
}
addToBody(childHtml) {
const childElement = document.createElement("div");
childElement.innerHTML = childHtml;
document.body.appendChild(childElement);
}
clear(elementId) {
const element = document.getElementById(elementId);
if (element) {
element.innerHTML = "";
} else {
console.error(`Element with ID ${elementId} not found.`);
}
}
remove(elementId) {
const element = document.getElementById(elementId);
if (element) {
element.remove();
} else {
console.error(`Element with ID ${elementId} not found.`);
}
}
click(idAttributeClass, callback) {
console.log(idAttributeClass, document.getElementById(idAttributeClass));
if (idAttributeClass.startsWith(".")) {
document.querySelectorAll(idAttributeClass).forEach((element) => {
element.addEventListener("click", (event) => {
callback(element, event);
});
});
} else if (document.getElementById(idAttributeClass)) {
document
.getElementById(idAttributeClass)
.addEventListener("click", (event) => {
callback(document.getElementById(idAttributeClass), event);
});
} else {
document
.querySelectorAll(`[${idAttributeClass}]`)
.forEach((element) => {
element.addEventListener("click", (event) => {
callback(element, event);
});
});
}
}
value(elementId) {
const element = document.getElementById(elementId);
if (element) {
return element.value;
} else {
console.error(`Element with ID '${elementId}' not found.`);
return "";
}
}
enter(elementId, callback) {
const inputElement = document.getElementById(elementId);
if (!inputElement) {
console.error(`Element with ID '${elementId}' not found.`);
return;
}
inputElement.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
callback(event);
}
});
}
keypress(elementId, callback) {
const inputElement = document.getElementById(elementId);
if (!inputElement) {
console.error(`Element with ID '${elementId}' not found.`);
return;
}
inputElement.addEventListener("keypress", (event) => {
callback(event);
});
}
exists(elementId) {
return !!document.getElementById(elementId);
}
attr(elementId, attr) {
const inputElement = document.getElementById(elementId);
if (!inputElement) {
console.error(`Element with ID '${elementId}' not found.`);
return;
}
return inputElement.getAttribute(attr);
}
submit(id, callback) {
document.getElementById(id).addEventListener('submit', function (event) {
event.preventDefault();
const submition = {};
if (event.target.elements) {
for (const input of event.target.elements) {
if (input.name && input.value) {
submition[input.name] = input.value;
}
}
}
callback(submition);
});
}
element(id) {
return document.getElementById(id);
}
change(id, callback) {
const element = document.getElementById(id);
if (!element) {
return callback();
}
element.addEventListener('change', function (event) {
event.preventDefault();
if (event.target.elements) {
const submition = {};
for (const input of event.target.elements) {
if (input.name && input.value) {
submition[input.name] = input.value;
}
}
callback(submition);
} else {
callback(element);
}
});
}
}
export default new Dom();