-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
251 lines (241 loc) · 11.2 KB
/
index.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
/*made by ExtravagantBoost*/
var l = (query) => {
let element = document.querySelector(query);
if (element != undefined && !element.apCh) {
l.__actions__.addLattrs(element);
}
return element;
};
l.CE = (TagName) => {
let element = document.createElement(TagName);
l.__actions__.addLattrs(element);
return element;
};
Object.defineProperty(l, '__actions__', {
value: {
lookInto(dict) {
//console.log(this.lookInto)
if (typeof dict == "object") {
let ele = Object.entries(dict)[0];
let element = document.createElement(ele[0]);
Object.entries(dict[Object.keys(dict)[0]]).forEach((entry) => {
let value = entry[1];
//console.log(key,value)
if (typeof value == 'object') {
if (value instanceof Element) {
element.appendChild(value);
} else {
element.appendChild(this.lookInto(value));
}
} else if (typeof value == 'string') {
element.appendChild(document.createTextNode(value));
}
});
let sa = [false, false, false];
Object.entries(dict).forEach((entry, index) => {
if (index != 0/*skip first index due to it being the creation state of dictionary*/) {
let [key, value] = entry;
if (key == 'style' && !sa[0]) {
sa[0] = true;
let style = Object.entries(value);
style.forEach((value) => {
let [attr, val] = value;
element.style.setProperty(attr, val);
if (
Object.values(element.style).indexOf(attr) !=
-1 /*check if attribute has been added else brute force the attribute*/
) {
element.style[attr] = val; //will trigger the setter function of style
}
});
}
if (key == 'class' && !sa[1]) {
sa[1] = false;
let classes = value;
if (typeof classes == "string") {
element.className = classes;
} else classes.forEach((klase) => {
if (typeof klase == 'string') element.classList.add(klase);
});
}
if (key == 'on' && !sa[2]) {
sa[2] = true;
let events = value;
Object.entries(events).forEach((value) => {
let [event, cond] = value;
//console.log(typeof cond)
if (typeof cond == "function") {
element.addEventListener(event, cond, true);
} else {
element.addEventListener(event, cond[0], cond[1]);
}
});
}
if (key != 'class' && key != 'on' && key != 'style') {
element.setAttribute(key, value);
}
}
});
this.addLattrs(element);
return element;
}
},
addLattrs: (ele) => {
if (ele.apCh == undefined) {
Object.defineProperties(ele, {
l: {
value(query) {
if (typeof query == 'string') {
let evl = ele.querySelector(query);
if (evl != undefined && !evl.apCh) {
l.__actions__.addLattrs(evl);
}
return evl;
}
return undefined;
}
},
apCh: {
value(...ch) {
ch.forEach((child) => {
if (typeof child == "string") {
this.appendChild(document.createTextNode(child));
}
if (typeof child == "object") {
if (child instanceof Element
) {
this.appendChild(child);
} else {
this.appendChild(l.__actions__.lookInto(child));
}
}
});
},
},
__style__: {
value: Object.create(null),
},
inHTML: {
get() {
return this.innerHTML;
},
set(val) {
this.innerHTML = val;
},
},
clsnm: {
get() {
return this.className;
},
set(val) {
this.className = val;
},
},
setStyle: {
value(styles, method) {
if (method) {
if (method == "add") {
//do nothing, add to original
} else if (method == "replace") {
//clear current style
ele.style = "";
ele.__style__ = styles;
}
} //else default to additive
Object.entries(styles).forEach((style) => {
let [attr, value] = style;
ele.style.setProperty(attr, value);
if (
Object.values(ele.style).indexOf(attr) !=
-1 /*check if attribute has been added else brute force the attribute*/
) {
ele.style[attr] = value; //will trigger the setter function of style
}
if (method != "replace") {
ele.__style__[attr] = value;
}
});
},
},
setattr: {
value(dict) {
let sa = [false, false, false];
Object.entries(dict).forEach((entry) => {
if (entry[0] == "style" && !sa[0]) {
sa[0] = true;
let style = Object.entries(dict.style);
style.forEach((value) => {
let [attr, val] = value;
ele.style.setProperty(attr, val);
if (
Object.values(ele.style).indexOf(attr) !=
-1 /*check if attribute has been added else brute force the attribute*/
) {
ele.style[attr] = val; //will trigger the setter function of style
}
});
}
if (entry[0] == "class" && !sa[2]) {
sa[2] = true;
let classes = dict.class;
if (typeof classes == 'string') {
ele.className = classes;
} else {
classes.forEach((klase) => {
if (typeof klase == "string" /*to avoid errors*/) {
ele.classList.add(klase);
}
});
}
}
if (entry[0] == "on" && !sa[1]) {
sa[1] = true;
let events = Object.entries(dict.on);
events.forEach((value) => {
let [event, cond] = value;
//console.log(typeof cond)
if (typeof cond == "function") {
ele.addEventListener(event, cond, true);
} else {
ele.addEventListener(event, cond[0], cond[1]);
}
});
}
if (
["class", "on", "style"].indexOf(entry[0]) == -1
) {
ele.setAttribute(entry[0], entry[1]
);
}
});
},
},
clsLst: {
value: {
add(...classes) {
classes.forEach((klase) => {
ele.classList.add(klase);
});
},
rm(...classes) {
classes.forEach((klase) => {
ele.classList.remove(klase);
});
},
rp(cls1, cls2) {
ele.classList.replace(cls1, cls2);
},
has(cls) {
ele.classList.contains(cls);
},
},
},
});
} else {
if (console) {
console.warn("element already initialized");
}
}
}
}
});