-
Notifications
You must be signed in to change notification settings - Fork 1
/
element.js
119 lines (92 loc) · 4.26 KB
/
element.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
"use strict"; // silly safari
if (typeof elem == "undefined") {
function elem(tagName, attributes, children, isHTML) {
let parent;
if (typeof tagName == "string") {
parent = document.createElement(tagName);
} else if (tagName instanceof HTMLElement) {
parent = tagName;
}
// I'm tired of using null as the attributes, e.g.: elem("div", null, ["some", "elements"])
// Wouldn't it be nice if I could just do: elem("div", ["some", "elements"])
// attributes expects a plain object; we can use that to differentiate
if (typeof attributes != "undefined" && ["undefined", "boolean"].includes(typeof children) && typeof isHTML == "undefined") {
let attrType = typeof attributes;
if (["string", "number"].includes(attrType)
|| (attrType == "object" && attributes instanceof Array)
|| (attrType == "object" && attributes instanceof HTMLElement) ) {
isHTML = children;
children = attributes;
attributes = null;
}
}
if (attributes) {
for (let attribute in attributes) {
if (attribute.startsWith("on")) {
let callback = attributes[attribute];
if (typeof callback == "string") {
parent.setAttribute(attribute, callback);
}
else if (typeof callback == "function") {
let eventMatch = attribute.match(/^on([a-zA-Z]+)/);
if (eventMatch) {
let event = eventMatch[1];
// TODO: make sure it's a valid event?
parent.addEventListener(event, callback);
parent.eventListeners = parent.eventListeners || {};
parent.eventListeners[event] = parent.eventListeners[event] || [];
parent.eventListeners[event].push(callback);
}
}
} else {
parent.setAttribute(attribute, attributes[attribute]);
}
}
}
if (typeof children != "undefined" || children === 0) {
elem.append(parent, children, isHTML);
}
return parent;
};
elem.append = function (parent, children, isHTML) {
if (parent instanceof HTMLTextAreaElement || parent instanceof HTMLInputElement) {
if (children instanceof Text || typeof children == "string" || typeof children == "number") {
parent.value = children;
}
else if (children instanceof Array) {
children.forEach(function (child) {
elem.append(parent, child);
});
}
else if (typeof children == "function") {
elem.append(parent, children());
}
} else {
if (children instanceof HTMLElement || children instanceof Text) {
parent.appendChild(children);
}
else if (typeof children == "string" || typeof children == "number") {
if (isHTML) {
parent.innerHTML += children;
} else {
parent.appendChild(document.createTextNode(children));
}
}
else if (children instanceof Array) {
children.forEach(function (child) {
elem.append(parent, child);
});
}
else if (typeof children == "function") {
elem.append(parent, children());
}
}
};
window.elem = elem;
} else {
if (typeof elem == "function" && elem.hasOwnProperty("append")) {
console.warn("elem() is already initialized.");
} else {
console.warn("The name \"elem\" is already in use by some other script.");
}
}