-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·197 lines (162 loc) · 4.76 KB
/
script.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
console.clear();
function l(obj) {
const varName = Object.keys(obj)[0];
console.log(`${varName} is: `, obj[varName]);
}
class Hijinx {
constructor() {
this.sets = {};
this.index = 0; // Add as property
}
getSelector(selector) {
if (this.sets[selector]) {
return this.sets[selector];
}
return $(selector);
}
getMethodName(el) {
return el.tagName
.toLowerCase()
.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
}
getValue(el) {
const textValue = $(el).text();
if (textValue === "") {
return null; // Return null if the text is empty
}
return textValue;
}
callMethod($el, method, val, currentIndex) {
if (this.sets[val]) {
if (this.sets[val][0] instanceof jQuery) {
return $el[method](this.sets[val][0]);
} else {
return $el[method](this.sets[val]);
}
}
if (val === null) {
return $el[method]();
}
val = val.replace(/\[(\w+)\]/g, (match, name) => {
if (this.sets[name][0] instanceof jQuery) {
return this.sets[name];
} else if (Array.isArray(this.sets[name])) {
return this.sets[name][currentIndex];
} else {
return this.sets[name];
}
});
return $el[method](val);
}
processTag($el, $tag) {
this.index = 0; // Reset to 0 before each processTag operation
$tag.children().each((i, el) => {
const method = this.getMethodName(el);
const val = this.getValue(el);
$el = this.callMethod($el, method, val);
this.index++; // Increment index for each child element processed
});
return $el;
}
processChildElements($el, $children, currentIndex) {
$children.each((i, childEl) => {
const tag = this.getMethodName(childEl);
let val;
switch (tag) {
case "wrap":
val = $(childEl).html();
break;
case "css":
let cssString = "";
$(childEl)
.children()
.each(function () {
const prop = $(this).prop("tagName").toLowerCase();
const value = $(this).text();
cssString += `${prop}: ${value};`;
});
$el.attr({
"x-data": `{ style: "${cssString}" }`,
"x-bind:style": "style"
});
return;
case "attr":
let attrObj = {};
$(childEl)
.children()
.each(function () {
const attrName = $(this).prop("tagName").toLowerCase();
const attrValue = $(this).text();
attrObj[attrName] = attrValue;
});
$el.attr(attrObj);
return; // Skip the rest of the loop iteration
default:
val = this.getValue(childEl);
}
if (val !== undefined) {
$el = this.callMethod($el, tag, val, currentIndex);
}
});
return $el;
}
processSet(setEl) {
const name = $(setEl).attr("name");
const $elements = $($(setEl).attr("targets"));
const results = [];
$elements.each((i, el) => {
const $el = $(el);
const result = this.processTag($el, $(setEl));
results.push(result);
});
this.sets[name] = results;
}
processGet(getEl) {
const targets = $(getEl).attr("targets");
if (targets) {
const $elements = this.getSelector(targets);
$elements.each((i, el) => {
let $el = $(el);
$el = this.processChildElements($el, $(getEl).children(), i);
});
} else {
const name = $(getEl).attr("name");
if (this.sets[name][0] instanceof jQuery) {
let $el = this.sets[name][0];
$el = this.processChildElements($el, $(getEl).children(), 0);
} else {
// Log a user-friendly error
const setErrorElement = $(`set[name="${name}"]`).prop("outerHTML");
const getErrorElement = $(getEl).prop("outerHTML");
console.error(`Oops! Something went wrong. \n
You are trying to do something with a <get> component that the <set> component can't handle.
Here's the <set> that's causing the issue: \n ${setErrorElement} \n
And here's the <get> you're trying to use: \n ${getErrorElement} \n
Make sure that what you're trying to 'get' and 'set' are compatible!`);
}
}
}
init() {
$(document).ready(() => {
$("when").each((i, el) => {
const $when = $(el);
const event = $when.attr("event");
$when.children("set").each((i, el) => {
this.processSet(el);
});
$when.children("get").each((i, el) => {
this.processGet(el);
});
if (event === "ready") {
// Execute actions
} else {
$(window).on(event, () => {
// Execute actions
});
}
});
});
}
}
const hijinx = new Hijinx();
hijinx.init();