This repository was archived by the owner on Mar 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (95 loc) · 2.45 KB
/
index.js
File metadata and controls
114 lines (95 loc) · 2.45 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
import compose from 'uc-compose';
import { get, on, off } from 'uc-dom';
import html from 'uc-dom/methods';
import cookie from 'uc-cookie';
import events from 'uc-events';
import Router from 'uc-router';
import { log, LOG_LEVEL } from 'uc-log';
import { localStorage, JSONStorage } from 'uc-storage'
export const App = function() {
this.events = {};
this.state = {};
this.router = new Router((name, params) => {
this.emit(`app.route.${name}`, params)
this.emit('app.route', name, params)
});
};
App.prototype = compose(
html,
events,
log,
{
logName: 'app',
logLevel: LOG_LEVEL.NONE,
storage: JSONStorage(localStorage),
cookie: cookie,
init: function(settings = {}, cb) {
if (settings.logLevel) {
this.logLevel = settings.logLevel;
}
if (settings.logName) {
this.logName = settings.logName;
}
this.container(settings.container);
if (this.api) {
this.api.init(() => this.didInit(cb))
} else {
this.didInit(cb);
}
return this;
},
didInit: function(cb) {
setTimeout(() => {
cb && cb();
this.emit('app.ready');
}, 0);
},
setAPI: function(api) {
this.api = api;
this.call = (...args) => api.call(...args);
this.host = () => api.host;
api.log = (...args) => this.log(...args);
api.emit = (...args) => this.emit(...args);
api.setApp(this);
return this;
},
isAuthenticated: function(...args) {
if (!this.api) {
return null;
}
return this.api.isAuthenticated(...args);
},
routes: function(routes) {
for (const route in routes) {
this.router.add(route, routes[route]);
}
return this;
},
start: function() {
this.router.start()
},
go: function(...args) {
this.router.go(...args);
},
container: function(container) {
if (typeof container === 'string') {
const el = get(container);
// if it is HTMLCollection or NodeList;
this.el = el.forEach ? el.item(0) : el;
} else {
this.el = container;
}
const router = this.router;
this.onclick = on(this.el, 'click', '[soft]', function(e) {
e.preventDefault();
router.go(this.pathname);
});
},
remove: function() {
off(this.el, 'click', this.onclick);
delete this.el;
this.router.remove();
}
}
);
export default new App();