-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrystalErrorBind.js
More file actions
275 lines (255 loc) · 8.16 KB
/
crystalErrorBind.js
File metadata and controls
275 lines (255 loc) · 8.16 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
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
/*
***********SnoJS v4.2************
**********Snocrystal***********
*********License: MIT**********
***(c) 2025 The Guyot Project**
*/
function random(max) {
return Math.floor(Math.random() * max);
}
function toggle(name){
if(name){return false;}else{return true;}
}
// Query for all dom elements, store in elements[]
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();
// Mark the data tag and push jsonified vars to array
// Totally hacked by taking a string that looks like a JS object and making evaluating a string that converts it to a function
// Data is actually created as a proxy to allow for var manipulation in script tags with render running on every var change
const parseData = () =>{
var possible = false;
var hasDataTag = document.querySelector('[data]');
let data = hasDataTag.getAttribute("data");
data = `()=>{ return new Proxy(${data}, {set(target,property,value){target[property] = value;$render();return true;}})}`
let okay = eval(data)
return okay();
};
const data = parseData();
// Collect all elements with react attribute, store in reval[]
const parseReval = () => {
var revals = [];
var revelements = Array.from(document.querySelectorAll('[react]'));
for(let i=0;i<revelements.length;i++){
revals.push({"elem":revelements[i],"oldTxt":revelements[i].innerHTML,"pre":"","post":""});
}
return revals;
}
const reval = parseReval();
// The revaluate functions takes any string with JS encoded curly brackets and switchs it to its value
// "hi:{{Math.PI}}" returns "hi:3.1415......."
const revaluate = (template) =>{
return template.replace(/{{(.*?)}}/g, (_, expression) => {
try {
for(let i=0;i<Object.keys(data).length;i++){
if(expression.includes(Object.keys(data)[i])){
expression = expression.replaceAll(`${Object.keys(data)[i]}`,`data.${Object.keys(data)[i]}`)
}
}
return eval(expression); // Evaluate the JavaScript inside the double curly brackets
} catch (error) {
return `{{${expression}}}`; // Leave it unchanged if evaluation fails
}
});
}
// Cycle through all reval[] replace variables inside {{ }} with data.varName,
// Evaluate inside {{ }} stich pre + eval({{ }}) + post together
const renderReval = () =>{
if(reval.length > 0){
for(i=0;i<reval.length;i++){
reval[i].elem.innerHTML = revaluate(reval[i].oldTxt)
}
}
}
// Find and store any emelent with for attribute in fors[]
const parseFors = () =>{
let hasFor;
let fors = [];
for(i=0;i<elements.length;i++){
hasFor = elements[i].getAttribute("for");
if(hasFor != null){
fors.push({elem:elements[i],attr:hasFor})
}
}
return fors;
};
const fors = parseFors();
// For each fors[] use given array and template to render
const renderFors = () =>{
let rendered = []
for(i=0;i<fors.length;i++){
// Clears container of previously rendered fors
fors[i].elem.innerHTML = ''
// enewItem stores the complete list of rendered items. <li>hi</li><li>bye</li>...
let newItem = "";
let itemType = fors[i].elem.getAttribute("item");
// arr stores the array that the user provides in the for=""
let arr = fors[i].attr; // when accesing use data[arr]
for(q=0;q<data[arr].length;q++){
newItem += itemType.replaceAll("{{item}}",data[arr][q]).replaceAll("{{i}}",q);
}
fors[i].elem.innerHTML = newItem;
}
};
/*
Meaningful comments end here
*/
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();
}
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 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 exe = () => {
var hasExe;
for(i=0;i<elements.length;i++){
hasExe = elements[i].getAttribute("eval");
if(hasExe != null){
for(q=0;q<Object.keys(data).length;q++){
if(hasExe.includes(Object.keys(data)[q])){
hasExe = hasExe.replace(Object.keys(data)[q], `data[Object.keys(data)[${q}]]`);
}
}
eval(hasExe)
}
}
$render();
};exe();
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([elements[i],hasBind])
}
}
}
}
return binds;
};
const bound = parseBind();
const renderBinds = () =>{
let fix;
for(i=0;i<bound.length;i++){
bound[i][0].addEventListener('input',function(evt){
setTimeout(()=>{
fix = this.getAttribute("bind");
data[fix] = this.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.replaceAll(Object.keys(data)[i],`data.${Object.keys(data)[i]}`);
}
}
eval(update);
};
window.main = function(){
requestAnimationFrame( main );
renderScreens();
};main();
$render();