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/src/route.mjs CHANGED
@@ -1,34 +1,45 @@
1
- export function routes(options) {
2
- return new SimplyRoute(options)
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
- constructor(options={}) {
7
- this.root = options.root || '/'
8
- this.app = options.app
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
- this.clear()
12
- if (options.routes) {
13
- this.load(options.routes)
14
- }
15
- }
16
-
17
- load(routes) {
18
- parseRoutes(routes, this.routeInfo, this.matchExact)
19
- }
20
-
21
- clear() {
22
- this.routeInfo = []
23
- this.listeners = {
24
- match: {},
25
- call: {},
26
- finish: {}
27
- }
28
- }
29
-
30
- match(path, options) {
31
- let args = {
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(route, params)
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
- runListeners(action, params) {
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
- if (evt.ctrlKey) {
106
- return;
107
- }
108
- if (evt.which != 1) {
109
- return; // not a 'left' mouse click
110
- }
111
- var link = evt.target;
112
- while (link && link.tagName!='A') {
113
- link = link.parentElement;
114
- }
115
- if (link
116
- && link.pathname
117
- && link.hostname==globalThis.location.hostname
118
- && !link.link
119
- && !link.dataset.simplyCommand
120
- ) {
121
- let path = getPath(link.pathname+link.hash, this.root);
122
- if ( !this.has(path) ) {
123
- path = getPath(link.pathname, this.root);
124
- }
125
- if ( this.has(path) ) {
126
- let params = this.runListeners('goto', { path: path});
127
- if (params.path) {
128
- if (this.goto(params.path)) {
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
- path = getPath(path, this.root)
146
- for (let route of this.routeInfo) {
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
- if (options.root) {
179
- this.root = options.root
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
@@ -1,4 +1,9 @@
1
- export function view(options) {
1
+ export function view(options, optionsCompat) {
2
+ if (optionsCompat) {
3
+ let app = options
4
+ options = optionsCompat
5
+ options.app = options
6
+ }
2
7
  if (options.app) {
3
8
  options.app.view = options.view || {}
4
9