-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnoflake.js
285 lines (265 loc) · 7.45 KB
/
snoflake.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
// SnoJS Snoflake
// Changes => Nothing is looping, it only updates on variable change. etc..
// click is no loger a valid attribute now use browser supported onclick='$("count++;")'
// trash stored and retrieve
function random(max) {
return Math.floor(Math.random() * max);
}
function toggle(name){
if(name){return false;}else{return true;}
}
// Grab all html elements to parse later
const getElms = () =>{
var z;
var arr = []
z= document.getElementsByTagName("*");
for(i=0;i<z.length;i++){
arr.push(z[i]);
}
return arr;
};
const elements = getElms();
const renderReval = () =>{
// reval checker basically react + something, similar to the react components + string manipulation
if(reval.length > 0){
// Cycle through each Reval
for(i=0;i<reval.length;i++){
// Check for and replace every variable instance
// True text is the version with var turned into data[Object.keys(data)[index]]
let trueTxt = reval[i].oldTxt;
for(q=0;q<Object.keys(data).length;q++){
if(trueTxt.includes(Object.keys(data)[q])){
trueTxt = trueTxt.replace(Object.keys(data)[q],`data[Object.keys(data)[${q}]]`);
}
}
let pre = trueTxt.split("{");
let post = trueTxt.split("}");
reval[i].pre = pre[0]
reval[i].post = post[post.length-1]
trueTxt = pre[pre.length-1].split("}")[0];
// Now that that mess is joever lets actually show the vars
reval[i].elem.innerHTML = pre[0]+eval(trueTxt)+post[post.length-1]
}
}
}
const parseFors = () =>{
let hasFor;
let fors = [];
for(i=0;i<elements.length;i++){
hasFor = elements[i].getAttribute("for");
if(hasFor != null){
// Pushed each element that has the for="" attribute attached then sends it off too renderFors
fors.push({elem:elements[i],attr:hasFor})
}
}
return fors;
};
const fors = parseFors();
const renderFors = () =>{
let rendered = []
for(i=0;i<fors.length;i++){
// clears every container from any previous children
fors[i].elem.innerHTML = ''
// create an item for each item in the array based on whats in the for attr
let newItem;
let itemClass = fors[i].elem.getAttribute("item-class");
let itemType = fors[i].elem.getAttribute("item");
// arr just shows the actually array name;
let arr = fors[i].attr; // always use data[arr] not just arr
for(q=0;q<data[arr].length;q++){
newItem = document.createElement(itemType);
newItem.classList = itemClass;
newItem.innerHTML = data[arr][q];
fors[i].elem.appendChild(newItem)
}
}
};
const renderIfs = () =>{
// If reactor
if(ifs.length > 0){
// loop both the object and the ifs to check for equality
for(i=0;i<ifs.length;i++){
// replace stuff
let trueAttr = ifs[i].attr
for(q=0;q<Object.keys(data).length;q++){
if(trueAttr.includes(Object.keys(data)[q])){
trueAttr = trueAttr.replace(Object.keys(data)[q],`data[Object.keys(data)[${q}]]`);
}
}
if(eval(trueAttr)){
ifs[i].elem.style.display = "";
}else{
ifs[i].elem.style.display = "none";
}
}
}
}
const $render = () =>{
renderReval();
renderIfs();
renderFors();
}
// Mark the data tag and push jsonified vars to array
const parseData = () =>{
var hasDataTag, jsonified;
var possible = false;
for(i=0;i<elements.length;i++){
hasDataTag = elements[i].getAttribute("data");
if(hasDataTag != null){
possible = true;
hasDataTag = JSON.parse(hasDataTag);
return hasDataTag;
}
}
if(!possible){
hasDataTag = '{"undefined":"undefined"}';
hasDataTag = JSON.parse(hasDataTag);
return hasDataTag;
}
};
const data = parseData();
const parseScreen = () =>{
var hasEither;
var screens = [];
for(i=0;i<elements.length;i++){
hasEither = elements[i].getAttribute("computer");
// computer first mobile second
if(hasEither != null){
screens.push({elem:elements[i],attr:"computer"})
}else if(elements[i].getAttribute("mobile") != null){
screens.push({elem:elements[i],attr:"mobile"})
}
}
return screens;
};
const screens = parseScreen();
const renderScreens = () =>{
for(i=0;i<screens.length;i++){
if(screens[i].attr == "mobile"){
if(window.innerWidth <= 600){
screens[i].elem.style.display="block";
}else{
screens[i].elem.style.display="none";
}
}
if(screens[i].attr == "computer"){
if(window.innerWidth > 600){
screens[i].elem.style.display="block";
}else{
screens[i].elem.style.display="none";
}
}
}
}
const parseReval = () => {
var hasReval;
var revals = [];
for(i=0;i<elements.length;i++){
//check for reval attr
hasReval = elements[i].getAttribute("react")
if(hasReval != null){
revals.push({"elem":elements[i],"oldTxt":elements[i].innerHTML,"pre":"","post":""});
}else{
if(elements[i].getAttribute("lint") != null){
revals.push({"elem":elements[i],"oldTxt":elements[i].innerHTML,"pre":"","post":""});
}
}
}
return revals;
}
const reval = parseReval();
const parseIf = () =>{
var hasIf;
var ifs = [];
for(i=0;i<elements.length;i++){
// Check for React attr
hasIf = elements[i].getAttribute("if");
if(hasIf != null){
// Dont push data of React attr, only elem
ifs.push({"elem":elements[i],"attr":hasIf,"computed":undefined,"pre":"","post":""});
}
}
return ifs;
};
const ifs = parseIf();
const exc = () => {
var hasExc;
for(i=0;i<elements.length;i++){
hasExc = elements[i].getAttribute("exc");
if(hasExc != null){
for(q=0;q<Object.keys(data).length;q++){
if(hasExc.includes(Object.keys(data)[q])){
hasExc = hasExc.replace(Object.keys(data)[q], `data[Object.keys(data)[${q}]]`);
}
}
eval(hasExc)
}
}
$render();
};exc();
const allowInclReturn = (elem,hasIncl) =>{
// This function only exists because when i return the other one it doesnt allow more than 1 incl
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elem.innerHTML = this.responseText;}
}
}
xhttp.open("GET", hasIncl, true);
xhttp.send();
return;
}
const parseIncl = () =>{
let hasIncl;
for(i=0;i<elements.length;i++){
hasIncl = elements[i].getAttribute("incl");
elem = elements[i]
if(hasIncl != null){
allowInclReturn(elem,hasIncl);
}
}
};parseIncl();
const parseBind = () =>{
let hasBind;
let binds = []
for(i=0;i<elements.length;i++){
hasBind = elements[i].getAttribute("bind")
if(hasBind!=null){
for(q=0;q<Object.keys(data).length;q++){
if(hasBind.includes(Object.keys(data)[q])){
binds.push({elem:elements[i],attr:hasBind})
}
}
}
}
return binds;
};
const bound = parseBind();
const renderBinds = () =>{
let fix;
for(i=0;i<bound.length;i++){
bound[i].elem.addEventListener('input',function(evt){
setTimeout(()=>{
fix = bound[i].attr;
data[fix] = bound[i].elem.value;
$render()
},0)
})
}
};renderBinds();
const $ = (change) =>{
let update = change;
// this is the new click and in script tag modification function
for(let i=0;i<Object.keys(data).length;i++){
if(update.includes(Object.keys(data)[i])){
update = update.replace(Object.keys(data)[i],`data.${Object.keys(data)[i]}`);
}
}
eval(update);
$render();
};
window.main = function(){
requestAnimationFrame( main );
renderScreens();
};main();
$render();