-
Notifications
You must be signed in to change notification settings - Fork 3
/
dom_utils.js
134 lines (123 loc) · 3.02 KB
/
dom_utils.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
import $ from 'jquery'
import _ from 'lodash'
export const createButton = function(
classes,
icon,
onClick,
asJqElement,
innerText
) {
let button
if (asJqElement) {
button = $('<button></button>')
button.addClass(classes)
if (icon) {
const iconEl = $('<i class="fas fa-' + icon + '"></i>')
button.html(iconEl)
}
if (innerText) {
button.append(window.createSpan('', innerText))
}
if (onClick) {
button.on('click', onClick)
}
} else {
button =
'<button ' +
(onClick ? 'onclick="' + onClick + '"' : '') +
' class="' +
classes +
'">' +
(icon ? '<i class="fas fa-' + icon + '"></i>' : '') +
(innerText || '') +
'</button>'
}
return button
}
export const createSpan = function(classes, text) {
return '<span class="' + (classes || '') + '">' + text + '</span>'
}
export const funcToStr = function(func) {
const args = _.slice(arguments, 1)
return (
func.name +
'(' +
_(args)
.map(function(arg) {
switch (typeof arg) {
case 'string':
return "'" + arg + "'"
default:
return arg
}
})
.join(',') +
')'
)
}
export const createDiv = function(
classes,
innerText,
icon,
id,
onClick,
asJqElement
) {
let element =
'<div ' +
(id ? "id='" + id + "'" : '') +
(onClick && !asJqElement ? 'onclick="' + onClick + '"' : '') +
' class="' +
classes +
'">' +
(icon ? '<i class="fas fa-' + icon + '"></i>' : '') +
(innerText || '') +
'</div>'
if (asJqElement) {
element = $(element)
if (onClick) {
element.on('click', onClick)
}
}
return element
}
export const createEnvsInput = function(key, value) {
return (
'<input class="env-value ansi-bright-blue-fg" onkeypress="this.style.width = ((this.value.length + 1) * 8) + \'px\';" key="' +
key +
'" value="' +
value +
'" style="width:' +
((value.length + 1) * 5.1 + 4) +
'px;"/>'
)
}
export const toggleClass = (element, className, isShow) =>
element && element.classList[isShow ? 'add' : 'remove'](className)
export const addClass = (element, className) =>
element && element.classList.add(className)
export const removeClass = (element, className) =>
element && element.classList.remove(className)
export const el = (query, asList) =>
document[`querySelector${asList ? 'All' : ''}`](query)
export const createEl = (tag, options) => {
const element = document.createElement(tag)
_.each(options, (option, name) => {
if (name !== 'parent' && name !== 'children') element[name] = option
})
if (options.parent) {
const parentElement = _.isString(options.parent)
? el(options.parent)
: options.parent
parentElement.appendChild(element)
}
if (options.children && options.children) {
_.each(options.children, child => {
if (child)
element.appendChild(
typeof child === 'string' ? document.createTextNode(child) : child
)
})
}
return element
}