simplyview 3.0.4 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/simply.app.js +67 -9
- package/dist/simply.app.min.js +1 -1
- package/dist/simply.app.min.js.map +3 -3
- package/dist/simply.everything.js +93 -9
- package/dist/simply.everything.min.js +1 -1
- package/dist/simply.everything.min.js.map +4 -4
- package/package.json +5 -2
- package/src/action.mjs +6 -1
- package/src/app.mjs +13 -0
- package/src/command.mjs +30 -1
- package/src/everything.mjs +1 -0
- package/src/key.mjs +8 -3
- package/src/render.mjs +27 -0
- package/src/route.mjs +96 -74
- package/src/view.mjs +6 -1
package/src/route.mjs
CHANGED
|
@@ -1,34 +1,45 @@
|
|
|
1
|
-
export function routes(options)
|
|
2
|
-
|
|
1
|
+
export function routes(options, optionsCompat)
|
|
2
|
+
{
|
|
3
|
+
if (optionsCompat) {
|
|
4
|
+
let app = options
|
|
5
|
+
options = optionsCompat
|
|
6
|
+
options.app = options
|
|
7
|
+
}
|
|
8
|
+
return new SimplyRoute(options)
|
|
3
9
|
}
|
|
4
10
|
|
|
5
|
-
class SimplyRoute
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
class SimplyRoute
|
|
12
|
+
{
|
|
13
|
+
constructor(options={})
|
|
14
|
+
{
|
|
15
|
+
this.root = options.root || '/'
|
|
16
|
+
this.app = options.app || {}
|
|
9
17
|
this.addMissingSlash = !!options.addMissingSlash
|
|
10
18
|
this.matchExact = !!options.matchExact
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
19
|
+
this.clear()
|
|
20
|
+
if (options.routes) {
|
|
21
|
+
this.load(options.routes)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
load(routes)
|
|
26
|
+
{
|
|
27
|
+
parseRoutes(routes, this.routeInfo, this.matchExact)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
clear()
|
|
31
|
+
{
|
|
32
|
+
this.routeInfo = []
|
|
33
|
+
this.listeners = {
|
|
34
|
+
match: {},
|
|
35
|
+
call: {},
|
|
36
|
+
finish: {}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
match(path, options)
|
|
41
|
+
{
|
|
42
|
+
let args = {
|
|
32
43
|
path,
|
|
33
44
|
options
|
|
34
45
|
}
|
|
@@ -68,15 +79,16 @@ class SimplyRoute {
|
|
|
68
79
|
args.params = params
|
|
69
80
|
args = this.runListeners('call', args)
|
|
70
81
|
params = args.params ? args.params : params
|
|
71
|
-
args.result = route.action.call(
|
|
82
|
+
args.result = route.action.call(this.app, params)
|
|
72
83
|
this.runListeners('finish', args)
|
|
73
84
|
return args.result
|
|
74
85
|
}
|
|
75
86
|
}
|
|
76
87
|
return false
|
|
77
|
-
|
|
88
|
+
}
|
|
78
89
|
|
|
79
|
-
|
|
90
|
+
runListeners(action, params)
|
|
91
|
+
{
|
|
80
92
|
if (!Object.keys(this.listeners[action])) {
|
|
81
93
|
return
|
|
82
94
|
}
|
|
@@ -95,55 +107,58 @@ class SimplyRoute {
|
|
|
95
107
|
return params
|
|
96
108
|
}
|
|
97
109
|
|
|
98
|
-
handleEvents()
|
|
110
|
+
handleEvents()
|
|
111
|
+
{
|
|
99
112
|
globalThis.addEventListener('popstate', () => {
|
|
100
113
|
if (this.match(getPath(document.location.pathname + document.location.hash, this.root)) === false) {
|
|
101
114
|
this.match(getPath(document.location.pathname, this.root))
|
|
102
115
|
}
|
|
103
116
|
})
|
|
104
117
|
this.app.container.addEventListener('click', (evt) => {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
118
|
+
if (evt.ctrlKey) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (evt.which != 1) {
|
|
122
|
+
return; // not a 'left' mouse click
|
|
123
|
+
}
|
|
124
|
+
var link = evt.target;
|
|
125
|
+
while (link && link.tagName!='A') {
|
|
126
|
+
link = link.parentElement;
|
|
127
|
+
}
|
|
128
|
+
if (link
|
|
129
|
+
&& link.pathname
|
|
130
|
+
&& link.hostname==globalThis.location.hostname
|
|
131
|
+
&& !link.link
|
|
132
|
+
&& !link.dataset.simplyCommand
|
|
133
|
+
) {
|
|
134
|
+
let path = getPath(link.pathname+link.hash, this.root);
|
|
135
|
+
if ( !this.has(path) ) {
|
|
136
|
+
path = getPath(link.pathname, this.root);
|
|
137
|
+
}
|
|
138
|
+
if ( this.has(path) ) {
|
|
139
|
+
let params = this.runListeners('goto', { path: path});
|
|
140
|
+
if (params.path) {
|
|
141
|
+
if (this.goto(params.path)) {
|
|
129
142
|
// now cancel the browser navigation, since a route handler was found
|
|
130
143
|
evt.preventDefault();
|
|
131
144
|
return false;
|
|
132
145
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
})
|
|
137
150
|
}
|
|
138
151
|
|
|
139
|
-
goto(path)
|
|
152
|
+
goto(path)
|
|
153
|
+
{
|
|
140
154
|
history.pushState({},'',getURL(path))
|
|
141
155
|
return this.match(path)
|
|
142
156
|
}
|
|
143
157
|
|
|
144
|
-
has(path)
|
|
145
|
-
|
|
146
|
-
|
|
158
|
+
has(path)
|
|
159
|
+
{
|
|
160
|
+
path = getPath(path, this.root)
|
|
161
|
+
for (let route of this.routeInfo) {
|
|
147
162
|
var matches = route.match.exec(path)
|
|
148
163
|
if (matches && matches.length) {
|
|
149
164
|
return true
|
|
@@ -152,7 +167,8 @@ class SimplyRoute {
|
|
|
152
167
|
return false
|
|
153
168
|
}
|
|
154
169
|
|
|
155
|
-
addListener(action, route, callback)
|
|
170
|
+
addListener(action, route, callback)
|
|
171
|
+
{
|
|
156
172
|
if (['goto','match','call','finish'].indexOf(action)==-1) {
|
|
157
173
|
throw new Error('Unknown action '+action)
|
|
158
174
|
}
|
|
@@ -162,7 +178,8 @@ class SimplyRoute {
|
|
|
162
178
|
this.listeners[action][route].push(callback)
|
|
163
179
|
}
|
|
164
180
|
|
|
165
|
-
removeListener(action, route, callback)
|
|
181
|
+
removeListener(action, route, callback)
|
|
182
|
+
{
|
|
166
183
|
if (['match','call','finish'].indexOf(action)==-1) {
|
|
167
184
|
throw new Error('Unknown action '+action)
|
|
168
185
|
}
|
|
@@ -174,14 +191,16 @@ class SimplyRoute {
|
|
|
174
191
|
})
|
|
175
192
|
}
|
|
176
193
|
|
|
177
|
-
init(options)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
194
|
+
init(options)
|
|
195
|
+
{
|
|
196
|
+
if (options.root) {
|
|
197
|
+
this.root = options.root
|
|
198
|
+
}
|
|
181
199
|
}
|
|
182
200
|
}
|
|
183
201
|
|
|
184
|
-
function getPath(path, root='/')
|
|
202
|
+
function getPath(path, root='/')
|
|
203
|
+
{
|
|
185
204
|
if (path.substring(0,root.length)==root
|
|
186
205
|
||
|
|
187
206
|
( root[root.length-1]=='/'
|
|
@@ -197,7 +216,8 @@ function getPath(path, root='/') {
|
|
|
197
216
|
return path
|
|
198
217
|
}
|
|
199
218
|
|
|
200
|
-
function getURL(path, root)
|
|
219
|
+
function getURL(path, root)
|
|
220
|
+
{
|
|
201
221
|
path = getPath(path, root)
|
|
202
222
|
if (root[root.length-1]==='/' && path[0]==='/') {
|
|
203
223
|
path = path.substring(1)
|
|
@@ -205,14 +225,16 @@ function getURL(path, root) {
|
|
|
205
225
|
return root + path;
|
|
206
226
|
}
|
|
207
227
|
|
|
208
|
-
function getRegexpFromRoute(route, exact=false)
|
|
228
|
+
function getRegexpFromRoute(route, exact=false)
|
|
229
|
+
{
|
|
209
230
|
if (exact) {
|
|
210
231
|
return new RegExp('^'+route.replace(/:\w+/g, '([^/]+)').replace(/:\*/, '(.*)')+'(\\?|$)')
|
|
211
232
|
}
|
|
212
233
|
return new RegExp('^'+route.replace(/:\w+/g, '([^/]+)').replace(/:\*/, '(.*)'))
|
|
213
234
|
}
|
|
214
235
|
|
|
215
|
-
function parseRoutes(routes, routeInfo, exact=false)
|
|
236
|
+
function parseRoutes(routes, routeInfo, exact=false)
|
|
237
|
+
{
|
|
216
238
|
const paths = Object.keys(routes)
|
|
217
239
|
const matchParams = /:(\w+|\*)/g
|
|
218
240
|
for (let path of paths) {
|
|
@@ -231,4 +253,4 @@ function parseRoutes(routes, routeInfo, exact=false) {
|
|
|
231
253
|
})
|
|
232
254
|
}
|
|
233
255
|
return routeInfo
|
|
234
|
-
}
|
|
256
|
+
}
|
package/src/view.mjs
CHANGED