-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
352 lines (326 loc) · 7.68 KB
/
index.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const q = function (selector, scope, fn) {
if (typeof scope == 'function') {
fn = scope
scope = undefined
}
if (typeof selector == 'string') {
selector = (scope ? q(scope) || document : document).querySelector(selector)
}
if (typeof fn == 'function') {
fn(selector, scope)
}
return selector
}
const qa = function (selector, scope, fn) {
if (typeof scope == 'function') {
fn = scope
scope = undefined
}
var nodes = (scope ? q(scope) || document : document).querySelectorAll(
selector
)
if (typeof fn == 'function') {
for (var i = 0; i < nodes.length; i++) {
fn(nodes[i], scope)
}
}
return nodes
}
const esc = function (str) {
if (typeof str != 'string') return str
return str.replace(
/[&<>'"]/g,
(m) =>
({
'<': '<',
'>': '>',
"'": ''',
'"': '"',
'&': '&'
}[m] || m)
)
}
const raw = function (str) {
if (typeof str != 'string') return str
return str.replace(
/<|>|'|"|&/g,
(m) =>
({
'<': '<',
'>': '>',
''': "'",
'"': '"',
'&': '&'
}[m] || m)
)
}
const css = function (selector, atts) {
var el = q(selector)
if (!el) return null
if (typeof atts == 'string') {
if (atts.indexOf(':') > -1) {
el.style.cssText = atts
} else {
return el.style[atts]
}
} else {
for (var key in atts) {
el.style[key] = atts[key]
}
}
return el
}
const html = function (selector, h, x) {
var el = q(selector)
if (!el) return null
if (typeof h == 'undefined') {
return el.innerHTML
} else if (!x) {
el.innerHTML = h
} else if (x[0] == 'r') {
el.outerHTML = h
} else {
el.insertAdjacentHTML(
(x[0] == 'b' && 'beforebegin') ||
(x[0] == 'a' && 'afterend') ||
(x[0] == 't' && 'afterbegin') ||
(x[0] == 'e' && 'beforeend'),
h
)
}
return el
}
const text = function (selector, t) {
var el = q(selector)
if (!el) return null
if (typeof t == 'undefined') {
return el.textContent
}
el.textContent = t
return el
}
const attr = function (selector, atts, value) {
var el = q(selector)
if (!el) return null
if (typeof atts == 'string') {
if (typeof value == 'undefined') {
return el.getAttribute(atts)
} else {
el.setAttribute(atts, value)
}
} else {
for (var key in atts) {
atts[key] == null
? el.removeAttribute(key)
: el.setAttribute(key, atts[key])
}
}
return el
}
const time = function (date, opt) {
if (!date) return ''
if (typeof date == 'string') date = new Date(date)
if (typeof opt == 'string') opt = { lang: opt }
if (!opt) opt = {}
var lang = opt.lang || 'en'
delete opt.lang
var formatter = new Intl.DateTimeFormat(lang, opt)
var format = opt.format
if (format) {
var parts = {}
formatter.formatToParts(date).forEach(function (part) {
parts[part.type] = part.value
})
var matches = format.match(/%[A-z]+/gi) || []
matches.forEach(function (match) {
var key = match.slice(1).toLowerCase()
var value = parts[key]
if (value) {
if (typeof value == 'string' && /[A-Z]/.test(match[1])) {
value = value[0].toUpperCase() + value.slice(1)
}
format = format.replace(match, value)
}
})
return format
}
return formatter.format(date)
}
const num = function (n, opt) {
if (typeof n == 'string') n = parseFloat(n.replace(/_/g, ''))
if (typeof opt == 'string') opt = { lang: opt }
if (!opt) opt = {}
var lang = opt.lang || 'en'
delete opt.lang
return new Intl.NumberFormat(lang, opt).format(n)
}
const params = function (id) {
if (id == null) return ''
if (typeof id != 'string') {
id = parseInt(id || 0) + 1
return location.pathname.split('/')[id]
}
id = id.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]')
var matcher = new RegExp('[\\?&]' + id + '=([^&#]*)')
var result = matcher.exec(location.search)
return result != null ? decodeURIComponent(result[1].replace(/\+/g, ' ')) : ''
}
const cookie = function (key, val, opt) {
if (typeof val == 'undefined') {
val = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)')
return val ? decodeURIComponent(val[2]) : null
}
if (!opt) opt = {}
if (val === null) {
val = ''
opt.days = -1
}
var days = opt.days || 30,
path = opt.path || '/',
domain = opt.domain ? ';domain=' + opt.domain : '',
sameSite = opt.sameSite || 'Lax',
httpOnly = opt.httpOnly ? ';HttpOnly' : '',
secure = opt.secure ? ';Secure' : ''
var date = new Date()
date.setTime(date.getTime() + 864e5 * days)
document.cookie =
key +
'=' +
encodeURIComponent(val) +
';path=' +
path +
domain +
';expires=' +
date.toUTCString() +
';SameSite=' +
sameSite +
httpOnly +
secure
}
const store = function (key, val) {
function get() {
var item = sessionStorage.getItem(key)
if (item != null) {
return JSON.parse(item)
}
}
if (!key) {
return sessionStorage.clear()
}
if (val === null) {
var item = get()
sessionStorage.removeItem(key)
return item
} else if (val != null) {
sessionStorage.setItem(key, JSON.stringify(val))
return val
}
return get()
}
const serialize = function (form) {
form = q(form)
if (!form) return {}
var data = {}
function get(el) {
var val = el.value || el.getAttribute('data-default')
if (val == null) {
return
}
var type = el.getAttribute('data-type') || el.type
if (type == 'array') {
if (val == '[]') return []
return val.split(',').map((x) => x.trim())
}
if (type == 'number') {
return +val
}
if (type == 'date') {
var timestamp = Date.parse(val) || new Date().getTime()
return new Date(timestamp)
}
if (type == 'null') {
return null
}
if (type == 'bool') {
if (['false', '0', 'off'].indexOf(val) > -1) {
return false
}
return !!val
}
return val
}
for (var i = 0; i < form.elements.length; i++) {
var field = form.elements[i]
var eligible = ['file', 'reset', 'submit', 'button'].indexOf(field.type) < 0
if (!eligible || !field.name || field.disabled) continue
if (field.type == 'select-multiple') {
for (var j = 0, values = []; j < field.options.length; j++) {
var option = field.options[j]
if (option.selected) {
values.push(get(option))
}
}
data[field.name] = values
} else if (field.type == 'checkbox') {
var key = field.name
if (!data[key]) {
data[key] = []
}
if (field.checked) {
data[key].push(get(field))
}
} else if (field.type != 'radio' || field.checked) {
var val = get(field)
if (typeof val != 'undefined') {
data[field.name] = val
}
}
}
return data
}
const flash = function (message, opt) {
if (!opt) opt = {}
var el = q(opt.el || '#flash'),
time = opt.time || 5000,
name = opt.name || 'flash'
if (!el) return null
if (typeof window.__$flash != 'undefined') {
clearTimeout(window.__$flash)
}
message = (message || cookie(name) || '').trim()
cookie(name, null)
if (opt.scroll != false) {
scroll(0, 0)
}
if (opt.class) {
el.classList.add(opt.class)
}
el.textContent = message
el.style.opacity = 1
if (time) {
window.__$flash = setTimeout(function () {
el.style.opacity = 0
if (opt.class) {
el.classList.remove(opt.class)
}
}, time)
}
return el
}
module.exports = {
q,
qa,
esc,
raw,
css,
html,
text,
attr,
time,
num,
params,
cookie,
store,
serialize,
flash
}