|
| 1 | +class Entity { |
| 2 | + constructor(e) { |
| 3 | + this.element = e, this.tagName = e.tagName, this.initialState = { |
| 4 | + className: e.className |
| 5 | + }, this.variables = [], this.id = this.generateEntityUUID(); |
| 6 | + } |
| 7 | + setAsParent() { |
| 8 | + this.uuid = this.id, this.element.dataset.uuid = this.uuid; |
| 9 | + } |
| 10 | + isParent() { |
| 11 | + return !!this.uuid; |
| 12 | + } |
| 13 | + getVariablesFromEvents() { |
| 14 | + this.allEvents.forEach((e) => { |
| 15 | + const t = this.element.getAttribute(e); |
| 16 | + if (t) { |
| 17 | + const a = /(\$?\w+(\.\w+)?)\s*=/g; |
| 18 | + let s; |
| 19 | + for (; (s = a.exec(t)) !== null; ) |
| 20 | + if (s && !window.hasOwnProperty(s[1]) && !s[1].includes("this.")) { |
| 21 | + if (s[1].includes("el.")) { |
| 22 | + const c = s[1].replace("el.", ""); |
| 23 | + this.setAsParent(), window[this.uuid] || (window[this.uuid] = {}), window[this.uuid][c] = MiniJS.tryFromLocal(s[1].replace("el.", this.uuid)), MiniJS.variables.push(this.uuid); |
| 24 | + } else |
| 25 | + window[s[1]] = MiniJS.tryFromLocal(s[1]); |
| 26 | + MiniJS.variables.push(s[1]); |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + } |
| 31 | + getVariables() { |
| 32 | + const e = MiniJS.variables, t = Array.from(this.element.attributes).map((s) => s.value), a = [...new Set(e.filter((s) => t.find((c) => c.includes(s))))]; |
| 33 | + for (let s of a) { |
| 34 | + if (typeof window[s] == "function") { |
| 35 | + const c = e.filter((h) => window[s].toString().includes(h)); |
| 36 | + a.concat(c); |
| 37 | + } |
| 38 | + s.includes("el.") && !this.parent && (this.parent = this.getParent()); |
| 39 | + } |
| 40 | + this.variables = a; |
| 41 | + } |
| 42 | + get allEvents() { |
| 43 | + const e = MiniJS.allEvents, t = new Set(e); |
| 44 | + return Array.from(this.element.attributes).map((c) => c.name).filter((c) => t.has(c)); |
| 45 | + } |
| 46 | + get baseClasses() { |
| 47 | + return this.initialState.className.split(" "); |
| 48 | + } |
| 49 | + _eventAction(e) { |
| 50 | + const t = this.element.getAttribute(e); |
| 51 | + return this._sanitizeExpression(t); |
| 52 | + } |
| 53 | + _sanitizeExpression(expr) { |
| 54 | + return console.log(expr, this.variables), this.variables.forEach((variable) => { |
| 55 | + const exp = expr.split(";").find((e) => e.includes(variable)); |
| 56 | + if (exp) |
| 57 | + if (exp.includes("el.")) { |
| 58 | + window.temp = eval(`proxyWindow['${this.parent.uuid}']`); |
| 59 | + let tempExpr = exp; |
| 60 | + tempExpr = tempExpr.replaceAll(variable, `temp['${variable.replace("el.", "")}']`), eval(tempExpr); |
| 61 | + const newVal = JSON.stringify(window.temp), newExpr = exp.replace(exp, `proxyWindow.${this.parent.uuid} = ${newVal};`); |
| 62 | + expr = expr.replace(exp, newExpr); |
| 63 | + } else |
| 64 | + expr = expr.replace(variable, `proxyWindow.${variable}`); |
| 65 | + }), expr = expr.replace("this", "this.element"), expr; |
| 66 | + } |
| 67 | + _sanitizeContentExpression(e) { |
| 68 | + if (e.includes("el.")) { |
| 69 | + let t = this.parent; |
| 70 | + this.variables.forEach((a) => { |
| 71 | + if (a.includes("el.")) { |
| 72 | + const s = `proxyWindow.${t.uuid}['${a.replace("el.", "")}']`; |
| 73 | + e = e.replace(a, s); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + return e; |
| 78 | + } |
| 79 | + getParent() { |
| 80 | + if (this.isParent()) |
| 81 | + return this; |
| 82 | + { |
| 83 | + let e = this.element, t = e.parentNode; |
| 84 | + for (; !t.dataset.uuid; ) |
| 85 | + e = t, t = e.parentNode; |
| 86 | + return MiniJS.elements.find((s) => s.uuid == t.dataset.uuid); |
| 87 | + } |
| 88 | + } |
| 89 | + generateEntityUUID() { |
| 90 | + return "Entity" + Date.now() + Math.floor(Math.random() * 1e4); |
| 91 | + } |
| 92 | + evaluateEventAction(attrName) { |
| 93 | + eval(this._eventAction(attrName)); |
| 94 | + } |
| 95 | + evaluateClass() { |
| 96 | + const classExpr = this.element.getAttribute(":class"); |
| 97 | + if (classExpr) { |
| 98 | + const newClassNames = eval(this._sanitizeContentExpression(classExpr)), classesCombined = [...this.baseClasses, ...newClassNames.split(" ")].join(" "); |
| 99 | + this.element.className = classesCombined; |
| 100 | + } |
| 101 | + } |
| 102 | + evaluateEach() { |
| 103 | + const eachExpr = this.element.getAttribute(":each"); |
| 104 | + if (eachExpr) { |
| 105 | + const [variable, iterable] = eachExpr.split(" in "), items = eval(iterable); |
| 106 | + this.childClone || (this.childClone = this.element.innerHTML); |
| 107 | + let newHTML = ""; |
| 108 | + items.forEach((e) => { |
| 109 | + newHTML += this.childClone.replaceAll(variable, `'${e}'`); |
| 110 | + }), this.element.innerHTML = newHTML; |
| 111 | + const elements = this.element.querySelectorAll("*"); |
| 112 | + for (let e = 0; e < elements.length; e++) { |
| 113 | + const t = new Entity(elements[e]); |
| 114 | + t.getVariablesFromEvents(), t.getVariables(), t.applyEventBindings(), t.evaluateAll(), MiniJS.elements.push(t); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + evaluateAll() { |
| 119 | + this.evaluateValue(), this.evaluateClass(), this.evaluateText(); |
| 120 | + } |
| 121 | + evaluateText() { |
| 122 | + const textExpr = this.element.getAttribute(":text"); |
| 123 | + if (textExpr) { |
| 124 | + const newText = eval(this._sanitizeContentExpression(textExpr)); |
| 125 | + (newText || newText == "") && (this.element.innerText = newText); |
| 126 | + } |
| 127 | + } |
| 128 | + evaluateValue() { |
| 129 | + const valueExpr = this.element.getAttribute(":value"); |
| 130 | + if (valueExpr) { |
| 131 | + const newValue = eval(valueExpr); |
| 132 | + newValue && (this.element.value = newValue); |
| 133 | + } |
| 134 | + const checkedExpr = this.element.getAttribute(":checked"); |
| 135 | + if (checkedExpr) { |
| 136 | + const newValue = eval(checkedExpr); |
| 137 | + newValue && (this.element.checked = newValue); |
| 138 | + } |
| 139 | + } |
| 140 | + applyEventBindings() { |
| 141 | + const e = this.element; |
| 142 | + this.allEvents.forEach((t) => { |
| 143 | + e[t] = () => { |
| 144 | + this.evaluateEventAction(t); |
| 145 | + }; |
| 146 | + }), e.hasAttribute(":click") && e.addEventListener("click", (t) => { |
| 147 | + this.evaluateEventAction(":click"); |
| 148 | + }), e.hasAttribute(":change") && (e.type == "checkbox" || e.tagName == "select" ? e.addEventListener("change", (t) => { |
| 149 | + this.evaluateEventAction(":change"); |
| 150 | + }) : e.addEventListener("input", (t) => { |
| 151 | + this.evaluateEventAction(":change"); |
| 152 | + })), e.hasAttribute(":enter") && e.addEventListener("keypress", (t) => { |
| 153 | + t.key == "Enter" && this.evaluateEventAction(":enter"); |
| 154 | + }), e.hasAttribute(":keypress") && e.addEventListener("keypress", (t) => { |
| 155 | + this.evaluateEventAction(":keypress"); |
| 156 | + }), e.hasAttribute(":keydown") && e.addEventListener("keydown", (t) => { |
| 157 | + this.evaluateEventAction(":keydown"); |
| 158 | + }), e.hasAttribute(":keyup") && e.addEventListener("keyup", (t) => { |
| 159 | + this.evaluateEventAction(":keyup"); |
| 160 | + }), document.addEventListener("click", (t) => { |
| 161 | + e.hasAttribute(":clickout") && !e.contains(t.target) && this.evaluateEventAction(":clickout"); |
| 162 | + }); |
| 163 | + } |
| 164 | + hasAttribute(e) { |
| 165 | + return !!this.element.getAttribute(e); |
| 166 | + } |
| 167 | +} |
| 168 | +let nativeProps = Object.getOwnPropertyNames(window); |
| 169 | +const MiniJS$1 = (() => { |
| 170 | + window.proxyWindow = null; |
| 171 | + let e = [], t = [], a = [], s = [":click", ":change", ":input", ":clickout"]; |
| 172 | + const c = { |
| 173 | + set: function(n, i, o) { |
| 174 | + return n[i] = o, i[0] === "$" && localStorage.setItem(i, JSON.stringify(o)), t.includes(i) && (m(i), f([i])), !0; |
| 175 | + } |
| 176 | + }; |
| 177 | + function h() { |
| 178 | + const n = ["div", "a", "input", "textarea", "select", "button", "video", "audio", "img", "form", "details", "iframe", "canvas"], i = /* @__PURE__ */ new Set(); |
| 179 | + n.forEach((o) => { |
| 180 | + const d = document.createElement(o); |
| 181 | + for (let p in d) |
| 182 | + p.startsWith("on") && i.add(p); |
| 183 | + }), a = [...i]; |
| 184 | + } |
| 185 | + async function A() { |
| 186 | + await _(); |
| 187 | + let n = performance.now(); |
| 188 | + S(), h(), L(), W(), f(), M(), k(), m(); |
| 189 | + const o = performance.now() - n; |
| 190 | + console.log(`myFunction took ${o}ms to run.`); |
| 191 | + } |
| 192 | + function k() { |
| 193 | + Object.defineProperty(Number.prototype, "times", { |
| 194 | + get: function() { |
| 195 | + return Array.from({ length: this }); |
| 196 | + } |
| 197 | + }); |
| 198 | + } |
| 199 | + function f(n = t) { |
| 200 | + n.forEach((i) => { |
| 201 | + if (Array.isArray(proxyWindow[i])) { |
| 202 | + let w = function() { |
| 203 | + return proxyWindow[i][0]; |
| 204 | + }, E = function() { |
| 205 | + return proxyWindow[i][proxyWindow[i].length - 1]; |
| 206 | + }, x = function(r) { |
| 207 | + let l; |
| 208 | + if (Array.isArray(r)) |
| 209 | + l = r; |
| 210 | + else { |
| 211 | + let u = this.indexOf(r); |
| 212 | + u === -1 ? l = this.concat(r) : l = this.slice(0, u).concat(this.slice(u + 1)); |
| 213 | + } |
| 214 | + proxyWindow[i] = l; |
| 215 | + }, v = function(r) { |
| 216 | + let l; |
| 217 | + this.indexOf(r) === -1 && (l = this.concat(r), proxyWindow[i] = l); |
| 218 | + }, g = function(r) { |
| 219 | + let l, u = this.indexOf(r); |
| 220 | + u !== -1 && (l = this.slice(0, u).concat(this.slice(u + 1)), proxyWindow[i] = l); |
| 221 | + }, y = function(r, l) { |
| 222 | + const u = r.toLowerCase().split(/\s+/); |
| 223 | + return l.filter((P) => { |
| 224 | + const C = P.toLowerCase(); |
| 225 | + return u.every((J) => C.includes(J)); |
| 226 | + }); |
| 227 | + }, b = function(r) { |
| 228 | + return y(r, this); |
| 229 | + }; |
| 230 | + var o = w, d = E, p = x, $ = v, O = g, z = y, F = b; |
| 231 | + proxyWindow[i].first = w(), proxyWindow[i].last = E(), proxyWindow[i].remove = g, proxyWindow[i].add = v, proxyWindow[i].toggle = x, proxyWindow[i].search = b; |
| 232 | + } |
| 233 | + }); |
| 234 | + } |
| 235 | + function S() { |
| 236 | + proxyWindow = new Proxy(window, c); |
| 237 | + } |
| 238 | + function V() { |
| 239 | + t = Object.getOwnPropertyNames(window).filter((i) => !nativeProps.includes(i)); |
| 240 | + } |
| 241 | + function W() { |
| 242 | + V(), e.forEach((n, i) => { |
| 243 | + n.getVariablesFromEvents(i); |
| 244 | + }), e.forEach((n, i) => { |
| 245 | + n.getVariables(); |
| 246 | + }); |
| 247 | + } |
| 248 | + function N(n) { |
| 249 | + return n.startsWith("$") && JSON.parse(localStorage.getItem(n)) || void 0; |
| 250 | + } |
| 251 | + function m(n = null) { |
| 252 | + e.forEach((i) => { |
| 253 | + var o; |
| 254 | + (i.variables.includes(n) || n == null || i.uuid == n || ((o = i.parent) == null ? void 0 : o.uuid) == n) && (i.evaluateEach(), i.evaluateAll()); |
| 255 | + }); |
| 256 | + } |
| 257 | + function M() { |
| 258 | + e.forEach((n) => { |
| 259 | + n.applyEventBindings(); |
| 260 | + }); |
| 261 | + } |
| 262 | + function L() { |
| 263 | + var n = document.body.getElementsByTagName("*"); |
| 264 | + for (let i = 0; i < n.length; i++) { |
| 265 | + const o = n[i], d = new Entity(o); |
| 266 | + T(d.element.parentElement) || e.push(d); |
| 267 | + } |
| 268 | + } |
| 269 | + function T(n) { |
| 270 | + for (; n; ) { |
| 271 | + if (n.hasAttribute && n.hasAttribute(":each")) |
| 272 | + return !0; |
| 273 | + n = n.parentElement; |
| 274 | + } |
| 275 | + return !1; |
| 276 | + } |
| 277 | + function _() { |
| 278 | + return new Promise((n) => { |
| 279 | + document.readyState == "loading" ? document.addEventListener("DOMContentLoaded", n) : n(); |
| 280 | + }); |
| 281 | + } |
| 282 | + return A().catch((n) => { |
| 283 | + console.error("Error initializing MiniJS:", n); |
| 284 | + }), { |
| 285 | + get elements() { |
| 286 | + return e; |
| 287 | + }, |
| 288 | + set elements(n) { |
| 289 | + return n; |
| 290 | + }, |
| 291 | + get variables() { |
| 292 | + return t; |
| 293 | + }, |
| 294 | + set variables(n) { |
| 295 | + t = n; |
| 296 | + }, |
| 297 | + get allEvents() { |
| 298 | + return [...a, ...s]; |
| 299 | + }, |
| 300 | + get window() { |
| 301 | + return proxyWindow; |
| 302 | + }, |
| 303 | + tryFromLocal: N |
| 304 | + }; |
| 305 | +})(); |
| 306 | +window.MiniJS = MiniJS$1; |
0 commit comments