-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlx.js
More file actions
77 lines (68 loc) · 2.2 KB
/
Copy pathhtmlx.js
File metadata and controls
77 lines (68 loc) · 2.2 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
/**
* HTMLX.JS
* (c) DecOps Decentralized Operations
*/
const HTMLX = {
init() {
document.body.addEventListener("click", this.handleClick.bind(this));
this.processElements(document.body);
},
processElements(root) {
root.querySelectorAll("[lmx-trigger]").forEach((el) => {
const triggerType = el.getAttribute("lmx-trigger");
if (triggerType && !el.dataset.lmxProcessed) {
el.addEventListener(triggerType, this.handleTrigger.bind(this, el));
el.dataset.lmxProcessed = "true";
}
});
},
handleClick(e) {
const target = e.target.closest('[lmx-trigger="click"]');
if (target) {
this.handleTrigger(target, e);
}
},
handleTrigger(el, event) {
const target = el.getAttribute("lmx-target");
const templateId = el.getAttribute("lmx-template-id");
const condition = el.getAttribute("lmx-condition");
if (condition && !eval(condition)) {
return;
}
const targetEl = document.querySelector(target);
const template = document.getElementById(templateId);
if (targetEl && template) {
const content = template.content.cloneNode(true);
this.swapContent(targetEl, content, "innerHTML");
this.addDynamicAttributes(content);
}
},
swapContent(target, content, swapType) {
switch (swapType) {
case "innerHTML":
target.innerHTML = "";
target.appendChild(content);
break;
case "outerHTML":
target.outerHTML = content.firstElementChild.outerHTML;
break;
case "beforebegin":
target.parentNode.insertBefore(content, target);
break;
case "afterend":
target.parentNode.insertBefore(content, target.nextSibling);
break;
// Add more swap types as needed
}
},
addDynamicAttributes(content) {
// Add dynamic attributes to the rendered HTML
content.querySelectorAll("[lmx-dynamic-attribute]").forEach((el) => {
const attribute = el.getAttribute("lmx-dynamic-attribute");
const value = el.getAttribute("lmx-dynamic-attribute-value");
el.setAttribute(attribute, value);
});
},
};
// Initialize HTMLX when the DOM is ready
document.addEventListener("DOMContentLoaded", () => HTMLX.init());