-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlElements.js
More file actions
160 lines (144 loc) · 3.89 KB
/
htmlElements.js
File metadata and controls
160 lines (144 loc) · 3.89 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
const html = (() => {
const style = (node, style = "", value = null) => {
if (!node) return
if (!node.style) {
console.error("Set style failed for node: ", node)
return
}
node.style.setProperty(style, value)
return node
}
const styles = (node, stylesObj = {}) => {
if (!node || !stylesObj) return node
for (const style in stylesObj) {
html.style(node, style, stylesObj[style])
}
return node
}
const toElement = (html = "") => {
const template = document.createElement("template")
template.innerHTML = html.trim()
return template.content
}
const textbox = (placeholder = "", value = "") => {
const box = document.createElement("input")
box.type = "text"
box.value = value
if (placeholder) box.placeholder = placeholder
return box
}
const numberBox = (value = 0) => {
const box = document.createElement("input")
box.type = "number"
box.value = value
return box
}
const button = (text = "", fn = C.Noop, title = "") => {
const button = document.createElement("input")
button.type = "button"
if (text) button.value = text
if (typeof fn === "function") button.addEventListener(C.Action.Click, fn, false)
if (title) button.title = title
return button
}
const check = (initialValue, fn = C.Noop) => {
const check = document.createElement("input")
check.type = "checkbox"
check.checked = initialValue
html.styles(check, { height: "20px", width: "20px", outline: "3px solid var(--nc-lk-1)", "border-radius": "5px", "outline-style": "auto" })
if (typeof fn === "function") check.addEventListener(C.Action.Change, ({currentTarget}) => fn(currentTarget.checked), false)
return check
}
const range = (initialValue, min, max, fn = C.Noop) => {
const range = document.createElement("input")
range.type = "range"
range.min = min
range.max = max
range.value = initialValue
// html.styles(range, { height: "20px", width: "20px", outline: "3px solid var(--nc-lk-1)", "border-radius": "5px", "outline-style": "auto" })
if (typeof fn === "function") range.addEventListener(C.Action.Change, ({currentTarget}) => fn(currentTarget.value), false)
return range
}
const img = (src = "", size) => {
const img = document.createElement("img")
img.src = src
if (size) {
img.height = size
img.width = size
}
return img
}
const imgButton = (src = "", fn = C.Noop, size) => {
const img = html.img(src, size)
if (typeof fn === "function") img.addEventListener(C.Action.Click, fn, false)
return img
}
const br = () => document.createElement("br")
const div = (className) => {
const div = document.createElement("div")
if (className) div.className = className
return div
}
const span = (className) => {
const span = document.createElement("span")
if (className) span.className = className
return span
}
const flexDiv = (...children) => {
const flex = html.div()
html.styles(flex, {
"display": "flex",
"gap": "10px",
})
flex.append(...children)
return flex
}
const flexRow = (...children) => {
const flex = flexDiv(...children)
html.styles(flex, { "flex-direction": "row" })
return flex
}
const flexColumn = (...children) => {
const flex = flexDiv(...children)
html.styles(flex, { "flex-direction": "column" })
return flex
}
const kbd = (text) => {
const kbd = document.createElement("kbd")
kbd.innerText = text
return kbd
}
const h2 = (text) => {
const h2 = document.createElement("h2")
h2.innerText = text
html.styles(h2, {"font-size": "19px", "font-weight": "700", "line-height": "24px"})
return h2
}
const title = (text) => {
const title = document.createElement("p")
title.innerText = text
html.styles(title, {"font-size": "16px", "font-weight": 600})
return title
}
const publicApi = {
style,
styles,
toElement,
textbox,
numberBox,
button,
br,
div,
span,
flexRow,
flexColumn,
check,
range,
img,
imgButton,
kbd,
h2,
title,
}
return publicApi
})()