/
esp6.js
120 lines (103 loc) · 2.98 KB
/
esp6.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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var R = { map: new Map() }
var E = module.exports = {
scriptLoaded: {},
router: R
}
R.route = function (regexp, f) {
R.map.set(regexp, f)
return this
}
R.go = function (hash) {
window.location.hash = '#' + hash
return this
}
R.init = function () {
window.onhashchange = function () {
var hash = window.location.hash.trim().substring(1)
for (let [regexp, f] of R.map) {
var m = hash.match(regexp)
if (m) {
f(m, hash)
break
}
}
}
return this
}
E.one = function (query) {
return document.querySelector(query)
}
E.all = function (query) {
return document.querySelectorAll(query)
}
E.plugin = function (query, html) {
E.one(query).innerHTML = html
}
E.hide = function (node) { node.hidden = true }
E.show = function (node) { node.hidden = undefined }
E.styleLoad = function (url) {
var ss = document.createElement('link')
ss.type = 'text/css'
ss.rel = 'stylesheet'
ss.href = url
E.one('head').appendChild(ss)
}
E.scriptLoad = function (url) {
return new Promise(function (resolve, reject) {
var urlLoaded = E.scriptLoaded[url]
if (urlLoaded === true) resolve(url)
var script = document.createElement('script')
script.onload = function () {
E.scriptLoaded[url] = true
resolve()
}
script.onerror = function () {
E.scriptLoaded[url] = false
reject(new Error('Could not load script at ' + url));
}
script.src = url
E.one('head').appendChild(script)
})
}
E.ajax = function (arg) {
var promise = new Promise(function (resolve, reject) {
var xhr = new window.XMLHttpRequest()
xhr.open(arg.method, arg.url, true)
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4) return
if (xhr.status === 200) {
resolve(xhr.responseText)
} else {
reject(new Error(xhr.statusText))
}
}
var str = (arg.obj == null) ? null : JSON.stringify(arg.obj)
xhr.send(str)
})
return promise
}
E.onready = function (init) {
return new Promise(function (resolve, reject) {
window.onload = function () {
console.log('onload')
init()
window.onhashchange()
resolve()
}
})
}
E.init = function () {
R.init()
}
E.init()
ESP6 = E
/*
function ajaxFormPost (path, form, callback) {
var obj = new window.FormData(form)
ajaxPost(path, obj, function (r) {
if (callback != null) callback(r)
})
}
*/
},{}]},{},[1]);