x4js 2.0.34 → 2.1.0-manual
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/README.md +21 -21
- package/package.json +39 -26
- package/src/components/base.scss +25 -89
- package/src/components/boxes/boxes.module.scss +54 -54
- package/src/components/boxes/boxes.ts +513 -513
- package/src/components/breadcrumb/breadcrumb.scss +56 -56
- package/src/components/breadcrumb/breadcrumb.ts +93 -93
- package/src/components/btngroup/btngroup.module.scss +40 -40
- package/src/components/btngroup/btngroup.ts +152 -152
- package/src/components/button/button.module.scss +172 -172
- package/src/components/button/button.ts +232 -232
- package/src/components/calendar/calendar.module.scss +162 -162
- package/src/components/calendar/calendar.ts +326 -326
- package/src/components/canvas/canvas.module.scss +24 -24
- package/src/components/canvas/canvas.ts +195 -195
- package/src/components/canvas/canvas_ex.ts +275 -275
- package/src/components/checkbox/check.svg +3 -3
- package/src/components/checkbox/checkbox.module.scss +141 -141
- package/src/components/checkbox/checkbox.ts +139 -139
- package/src/components/colorinput/colorinput.module.scss +64 -64
- package/src/components/colorinput/colorinput.ts +90 -90
- package/src/components/colorpicker/colorpicker.module.scss +132 -132
- package/src/components/colorpicker/colorpicker.ts +481 -481
- package/src/components/combobox/combobox.module.scss +145 -145
- package/src/components/combobox/combobox.ts +282 -282
- package/src/components/combobox/updown.svg +3 -3
- package/src/components/components.ts +45 -44
- package/src/components/dialog/dialog.module.scss +103 -105
- package/src/components/dialog/dialog.ts +233 -233
- package/src/components/filedrop/filedrop.module.scss +69 -69
- package/src/components/filedrop/filedrop.ts +130 -130
- package/src/components/form/form.module.scss +38 -38
- package/src/components/form/form.ts +172 -172
- package/src/components/gridview/gridview.module.scss +323 -337
- package/src/components/gridview/gridview.ts +1276 -1315
- package/src/components/header/header.module.scss +40 -40
- package/src/components/header/header.ts +141 -141
- package/src/components/icon/icon.module.scss +32 -32
- package/src/components/icon/icon.ts +165 -165
- package/src/components/image/image.module.scss +27 -27
- package/src/components/image/image.ts +168 -168
- package/src/components/input/input.module.scss +74 -74
- package/src/components/input/input.ts +537 -537
- package/src/components/keyboard/keyboard.module.scss +136 -136
- package/src/components/keyboard/keyboard.ts +549 -549
- package/src/components/label/label.module.scss +90 -91
- package/src/components/label/label.ts +101 -101
- package/src/components/link/link.module.scss +44 -44
- package/src/components/link/link.ts +87 -87
- package/src/components/listbox/listbox.module.scss +179 -179
- package/src/components/listbox/listbox.ts +596 -596
- package/src/components/menu/menu.module.scss +128 -128
- package/src/components/menu/menu.ts +174 -174
- package/src/components/messages/messages.module.scss +92 -146
- package/src/components/messages/messages.ts +237 -303
- package/src/components/normalize.scss +391 -391
- package/src/components/notification/notification.module.scss +83 -83
- package/src/components/notification/notification.ts +107 -107
- package/src/components/panel/panel.module.scss +66 -71
- package/src/components/panel/panel.ts +57 -57
- package/src/components/popup/popup.module.scss +51 -51
- package/src/components/popup/popup.ts +457 -457
- package/src/components/progress/progress.module.scss +56 -56
- package/src/components/progress/progress.ts +43 -43
- package/src/components/propgrid/progrid.module.scss +111 -111
- package/src/components/propgrid/propgrid.ts +300 -300
- package/src/components/propgrid/updown.svg +3 -3
- package/src/components/radio/radio.module.scss +163 -163
- package/src/components/radio/radio.svg +3 -3
- package/src/components/radio/radio.ts +141 -141
- package/src/components/rating/rating.module.scss +22 -22
- package/src/components/rating/rating.ts +131 -131
- package/src/components/select/select.module.scss +8 -8
- package/src/components/select/select.ts +134 -134
- package/src/components/shared.scss +141 -71
- package/src/components/sizers/sizer.module.scss +90 -107
- package/src/components/sizers/sizer.ts +131 -134
- package/src/components/slider/slider.module.scss +117 -117
- package/src/components/slider/slider.ts +197 -197
- package/src/components/spreadsheet/spreadsheet.module.scss +307 -307
- package/src/components/spreadsheet/spreadsheet.ts +1223 -1223
- package/src/components/switch/switch.module.scss +126 -126
- package/src/components/switch/switch.ts +61 -61
- package/src/components/tabs/tabs.module.scss +46 -67
- package/src/components/tabs/tabs.ts +229 -234
- package/src/components/textarea/textarea.module.scss +63 -63
- package/src/components/textarea/textarea.ts +131 -131
- package/src/components/textedit/textedit.module.scss +115 -115
- package/src/components/textedit/textedit.ts +122 -122
- package/src/components/themes.scss +90 -90
- package/src/components/tickline/tickline.module.scss +25 -25
- package/src/components/tickline/tickline.ts +81 -81
- package/src/components/tooltips/tooltips.scss +71 -71
- package/src/components/tooltips/tooltips.ts +120 -120
- package/src/components/treeview/treeview.module.scss +192 -192
- package/src/components/treeview/treeview.ts +484 -484
- package/src/components/viewport/viewport.module.scss +31 -31
- package/src/components/viewport/viewport.ts +41 -41
- package/src/core/component.ts +1299 -1299
- package/src/core/core_application.ts +361 -361
- package/src/core/core_colors.ts +512 -512
- package/src/core/core_data.ts +1297 -1297
- package/src/core/core_dom.ts +481 -481
- package/src/core/core_dragdrop.ts +225 -225
- package/src/core/core_element.ts +221 -221
- package/src/core/core_events.ts +214 -214
- package/src/core/core_i18n.ts +395 -395
- package/src/core/core_pdf.ts +454 -454
- package/src/core/core_react.ts +78 -78
- package/src/core/core_router.ts +296 -296
- package/src/core/core_state.ts +62 -62
- package/src/core/core_styles.ts +213 -213
- package/src/core/core_svg.ts +1042 -1042
- package/src/core/core_tools.ts +996 -996
- package/src/types/scss.d.ts +4 -4
- package/src/types/x4react.d.ts +8 -8
- package/src/x4.scss +19 -19
- package/src/x4.ts +36 -36
- package/src/x4tsx.d.ts +26 -26
- package/.vscode/launch.json +0 -14
- package/.vscode/settings.json +0 -2
- package/demo/assets/house-light.svg +0 -1
- package/demo/assets/radio.svg +0 -4
- package/demo/index.html +0 -12
- package/demo/main.scss +0 -23
- package/demo/main.ts +0 -324
- package/demo/package.json +0 -26
- package/demo/scss.d.ts +0 -4
- package/demo/svg.d.ts +0 -1
- package/demo/tsconfig.json +0 -14
- package/src/components/gridview/folder-open.svg +0 -1
- package/src/components/messages/spinner.svg +0 -1
- package/src/x4.d.ts +0 -10
- package/tsconfig.json +0 -11
package/src/core/core_router.ts
CHANGED
|
@@ -1,296 +1,296 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ___ ___ __
|
|
3
|
-
* \ \/ / / _
|
|
4
|
-
* \ / /_| |_
|
|
5
|
-
* / \____ _|
|
|
6
|
-
* /__/\__\ |_|
|
|
7
|
-
*
|
|
8
|
-
* @file core_router.ts
|
|
9
|
-
* @author Etienne Cochard
|
|
10
|
-
*
|
|
11
|
-
* @copyright (c) 2024 R-libre ingenierie
|
|
12
|
-
*
|
|
13
|
-
* Use of this source code is governed by an MIT-style license
|
|
14
|
-
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
-
**/
|
|
16
|
-
|
|
17
|
-
import { EvChange, EvError } from './component';
|
|
18
|
-
import { EventMap, EventSource } from './core_events';
|
|
19
|
-
|
|
20
|
-
type RouteHandler = ( params: any, path: string ) => void;
|
|
21
|
-
|
|
22
|
-
interface Segment {
|
|
23
|
-
/** The names of the parameters extracted from the route string (e.g., `['id']` for `'/detail/:id'`). */
|
|
24
|
-
keys: string[];
|
|
25
|
-
/** The regular expression used to match the route against a URL path. */
|
|
26
|
-
pattern: RegExp;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
interface Route {
|
|
30
|
-
/** The names of the parameters extracted from the route string. */
|
|
31
|
-
keys: string[];
|
|
32
|
-
/** The regular expression used to match the route against a URL path. */
|
|
33
|
-
pattern: RegExp;
|
|
34
|
-
/** The function to call when this route matches the current URL. */
|
|
35
|
-
handler: RouteHandler;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Parses a route string or regular expression into a `Segment` object.
|
|
40
|
-
* This function converts human-readable route patterns (e.g., `'/users/:id'`) into
|
|
41
|
-
* a regular expression that can be used for matching URL paths, and extracts parameter names.
|
|
42
|
-
*
|
|
43
|
-
* @param str - The route pattern as a string (e.g., `'/users/:id'`) or a direct `RegExp` object.
|
|
44
|
-
* @param loose - If `true`, the pattern will match paths that start with the route but may have additional segments.
|
|
45
|
-
* If `false` (default), the pattern must match the entire path.
|
|
46
|
-
* @returns A `Segment` object containing the extracted keys and the compiled regular expression.
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
export function parseRoute(str: string | RegExp, loose = false): Segment {
|
|
50
|
-
|
|
51
|
-
if (str instanceof RegExp) {
|
|
52
|
-
return {
|
|
53
|
-
keys: null,
|
|
54
|
-
pattern: str
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const arr = str.split('/');
|
|
59
|
-
|
|
60
|
-
let keys = [];
|
|
61
|
-
let pattern = '';
|
|
62
|
-
|
|
63
|
-
if( arr[0]=='' ) {
|
|
64
|
-
arr.shift();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
for (const tmp of arr) {
|
|
68
|
-
const c = tmp[0];
|
|
69
|
-
|
|
70
|
-
if (c === '*') {
|
|
71
|
-
keys.push('wild');
|
|
72
|
-
pattern += '/(.*)';
|
|
73
|
-
}
|
|
74
|
-
else if (c === ':') {
|
|
75
|
-
const o = tmp.indexOf('?', 1);
|
|
76
|
-
const ext = tmp.indexOf('.', 1);
|
|
77
|
-
|
|
78
|
-
keys.push(tmp.substring(1, o >= 0 ? o : ext >= 0 ? ext : tmp.length));
|
|
79
|
-
pattern += o >= 0 && ext < 0 ? '(?:/([^/]+?))?' : '/([^/]+?)';
|
|
80
|
-
if (ext >= 0) {
|
|
81
|
-
pattern += (o >= 0 ? '?' : '') + '\\' + tmp.substring(ext);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
pattern += '/' + tmp;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
keys,
|
|
91
|
-
pattern: new RegExp( `^${pattern}${loose ? '(?=$|/)' : '/?$'}`, 'i' )
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Defines the events that the `Router` can emit.
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
interface RouterEvents extends EventMap {
|
|
100
|
-
/** Emitted when the route changes successfully. The `value` property contains the new URL path. */
|
|
101
|
-
change: EvChange;
|
|
102
|
-
/** Emitted when a route is not found (404 error). The `code` is 404 and `message` describes the error. */
|
|
103
|
-
error: EvError;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* micro router
|
|
109
|
-
*
|
|
110
|
-
* ```
|
|
111
|
-
* const router = new Router( );
|
|
112
|
-
*
|
|
113
|
-
* router.get( "/detail/:id", ( params: any ) => {
|
|
114
|
-
* this._showDetail( detail );
|
|
115
|
-
* } );
|
|
116
|
-
*
|
|
117
|
-
* router.get( "/:id", ( params: any ) => {
|
|
118
|
-
* if( params.id==0 )
|
|
119
|
-
* router.navigate( '/home' );
|
|
120
|
-
* }
|
|
121
|
-
* });
|
|
122
|
-
*
|
|
123
|
-
* router.on( "error", ( ) => {
|
|
124
|
-
* router.navigate( '/home' );
|
|
125
|
-
* })
|
|
126
|
-
*
|
|
127
|
-
* router.init( );
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
export class Router extends EventSource< RouterEvents > {
|
|
133
|
-
|
|
134
|
-
private m_routes: Route[];
|
|
135
|
-
private m_useHash: boolean;
|
|
136
|
-
|
|
137
|
-
constructor( useHash = true ) {
|
|
138
|
-
super( );
|
|
139
|
-
|
|
140
|
-
this.m_routes = [];
|
|
141
|
-
this.m_useHash = useHash;
|
|
142
|
-
|
|
143
|
-
window.addEventListener('popstate', (event) => {
|
|
144
|
-
const url = this._getLocation( );
|
|
145
|
-
const found = this._find(url);
|
|
146
|
-
|
|
147
|
-
found.handlers.forEach(h => {
|
|
148
|
-
h(found.params,url);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
this.fire( "change", { value: this._getLocation() } );
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Registers a route handler for the given URI pattern.
|
|
158
|
-
*
|
|
159
|
-
* @param uri - A path string (may include parameter placeholders) or a RegExp used to match request URIs.
|
|
160
|
-
* @param handler - A function conforming to the RouteHandler type that will be executed when a request matches the route.
|
|
161
|
-
*
|
|
162
|
-
* @remarks
|
|
163
|
-
* The provided uri is parsed (via parseRoute) into a RegExp pattern and an ordered list of parameter keys.
|
|
164
|
-
* The resulting route descriptor ({ keys, pattern, handler }) is appended to the router's internal route list.
|
|
165
|
-
*
|
|
166
|
-
* @returns void
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
get(uri: string | RegExp, handler: RouteHandler ) {
|
|
170
|
-
let { keys, pattern } = parseRoute(uri);
|
|
171
|
-
this.m_routes.push({ keys, pattern, handler });
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
init() {
|
|
176
|
-
this.navigate( this._getLocation() );
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
private _getLocation( ) {
|
|
180
|
-
return this.m_useHash ? '/'+document.location.hash.substring(1) : document.location.pathname;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Navigate to a given URI within the router, update browser history, and optionally notify route handlers.
|
|
185
|
-
*
|
|
186
|
-
* Normalizes the supplied URI
|
|
187
|
-
* updates the browser history and emits lifecycle events.
|
|
188
|
-
*
|
|
189
|
-
* Behavior summary:
|
|
190
|
-
* - If no matching route or no handlers are found, logs a message, fires an "error" event with
|
|
191
|
-
* { code: 404, message: "route not found" } and returns false.
|
|
192
|
-
* - If notify is true, invokes the first handler of the matched route with (params, uri).
|
|
193
|
-
* - Always fires a "change" event with { value: this._getLocation() } after performing the navigation.
|
|
194
|
-
*
|
|
195
|
-
* @param uri - Target URI to navigate to. If it does not start with '/', a leading '/' will be added.
|
|
196
|
-
* When m_useHash is true the resulting location will be converted to a '#...' fragment.
|
|
197
|
-
* @param notify - Whether to invoke the matched route handler after updating history. Defaults to true.
|
|
198
|
-
* @param replace - Whether to replace the current history entry (true) or push a new one (false). Defaults to false.
|
|
199
|
-
* @returns True if navigation succeeded (route found and history updated); false if no route or handlers were found.
|
|
200
|
-
*
|
|
201
|
-
* @example
|
|
202
|
-
* // Navigate to /dashboard and notify handlers (pushes new history entry)
|
|
203
|
-
* navigate('/dashboard');
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* // Replace the current history entry without notifying handlers
|
|
207
|
-
* navigate('/login', false, true);
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
navigate( uri: string, notify = true, replace = false ) {
|
|
211
|
-
|
|
212
|
-
if( !uri.startsWith('/') ) {
|
|
213
|
-
uri = '/'+uri;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const found = this._find( uri );
|
|
217
|
-
|
|
218
|
-
if( !found || found.handlers.length==0 ) {
|
|
219
|
-
//window.history.pushState({}, '', 'error')
|
|
220
|
-
console.log( 'route not found: '+uri );
|
|
221
|
-
this.fire( "error", {code: 404, message: "route not found" } );
|
|
222
|
-
return false;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if( this.m_useHash ) {
|
|
226
|
-
while( uri.at(0)=='/' ) {
|
|
227
|
-
uri = uri.substring( 1 );
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
uri = '#'+uri;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if( replace ) {
|
|
234
|
-
window.history.replaceState({}, '', uri );
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
window.history.pushState({}, '', uri );
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if( notify ) {
|
|
241
|
-
//found.handlers.forEach( h => {
|
|
242
|
-
// h( found.params, uri );
|
|
243
|
-
//} );
|
|
244
|
-
|
|
245
|
-
found.handlers[0]( found.params, uri );
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
this.fire( "change", { value: this._getLocation() } );
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
*
|
|
254
|
-
*/
|
|
255
|
-
|
|
256
|
-
private _find( url: string ): { params: Record<string,any>, handlers: RouteHandler[] } {
|
|
257
|
-
|
|
258
|
-
let matches = [];
|
|
259
|
-
let params: Record<string,any> = {};
|
|
260
|
-
let handlers: RouteHandler[] = [];
|
|
261
|
-
|
|
262
|
-
for (const tmp of this.m_routes ) {
|
|
263
|
-
if (!tmp.keys ) {
|
|
264
|
-
matches = tmp.pattern.exec(url);
|
|
265
|
-
if (!matches) {
|
|
266
|
-
continue;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
if (matches['groups']) {
|
|
270
|
-
for (const k in matches['groups']) {
|
|
271
|
-
params[k] = matches['groups'][k];
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
handlers = [...handlers, tmp.handler];
|
|
276
|
-
}
|
|
277
|
-
else if (tmp.keys.length > 0) {
|
|
278
|
-
matches = tmp.pattern.exec(url);
|
|
279
|
-
if (matches === null) {
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
for ( let j = 0; j < tmp.keys.length;) {
|
|
284
|
-
params[tmp.keys[j]] = matches[++j];
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
handlers = [...handlers, tmp.handler];
|
|
288
|
-
}
|
|
289
|
-
else if (tmp.pattern.test(url)) {
|
|
290
|
-
handlers = [...handlers, tmp.handler];
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return { params, handlers };
|
|
295
|
-
}
|
|
296
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file core_router.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
*
|
|
11
|
+
* @copyright (c) 2024 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* Use of this source code is governed by an MIT-style license
|
|
14
|
+
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
+
**/
|
|
16
|
+
|
|
17
|
+
import { EvChange, EvError } from './component';
|
|
18
|
+
import { EventMap, EventSource } from './core_events';
|
|
19
|
+
|
|
20
|
+
type RouteHandler = ( params: any, path: string ) => void;
|
|
21
|
+
|
|
22
|
+
interface Segment {
|
|
23
|
+
/** The names of the parameters extracted from the route string (e.g., `['id']` for `'/detail/:id'`). */
|
|
24
|
+
keys: string[];
|
|
25
|
+
/** The regular expression used to match the route against a URL path. */
|
|
26
|
+
pattern: RegExp;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Route {
|
|
30
|
+
/** The names of the parameters extracted from the route string. */
|
|
31
|
+
keys: string[];
|
|
32
|
+
/** The regular expression used to match the route against a URL path. */
|
|
33
|
+
pattern: RegExp;
|
|
34
|
+
/** The function to call when this route matches the current URL. */
|
|
35
|
+
handler: RouteHandler;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parses a route string or regular expression into a `Segment` object.
|
|
40
|
+
* This function converts human-readable route patterns (e.g., `'/users/:id'`) into
|
|
41
|
+
* a regular expression that can be used for matching URL paths, and extracts parameter names.
|
|
42
|
+
*
|
|
43
|
+
* @param str - The route pattern as a string (e.g., `'/users/:id'`) or a direct `RegExp` object.
|
|
44
|
+
* @param loose - If `true`, the pattern will match paths that start with the route but may have additional segments.
|
|
45
|
+
* If `false` (default), the pattern must match the entire path.
|
|
46
|
+
* @returns A `Segment` object containing the extracted keys and the compiled regular expression.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
export function parseRoute(str: string | RegExp, loose = false): Segment {
|
|
50
|
+
|
|
51
|
+
if (str instanceof RegExp) {
|
|
52
|
+
return {
|
|
53
|
+
keys: null,
|
|
54
|
+
pattern: str
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const arr = str.split('/');
|
|
59
|
+
|
|
60
|
+
let keys = [];
|
|
61
|
+
let pattern = '';
|
|
62
|
+
|
|
63
|
+
if( arr[0]=='' ) {
|
|
64
|
+
arr.shift();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
for (const tmp of arr) {
|
|
68
|
+
const c = tmp[0];
|
|
69
|
+
|
|
70
|
+
if (c === '*') {
|
|
71
|
+
keys.push('wild');
|
|
72
|
+
pattern += '/(.*)';
|
|
73
|
+
}
|
|
74
|
+
else if (c === ':') {
|
|
75
|
+
const o = tmp.indexOf('?', 1);
|
|
76
|
+
const ext = tmp.indexOf('.', 1);
|
|
77
|
+
|
|
78
|
+
keys.push(tmp.substring(1, o >= 0 ? o : ext >= 0 ? ext : tmp.length));
|
|
79
|
+
pattern += o >= 0 && ext < 0 ? '(?:/([^/]+?))?' : '/([^/]+?)';
|
|
80
|
+
if (ext >= 0) {
|
|
81
|
+
pattern += (o >= 0 ? '?' : '') + '\\' + tmp.substring(ext);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
pattern += '/' + tmp;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
keys,
|
|
91
|
+
pattern: new RegExp( `^${pattern}${loose ? '(?=$|/)' : '/?$'}`, 'i' )
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Defines the events that the `Router` can emit.
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
interface RouterEvents extends EventMap {
|
|
100
|
+
/** Emitted when the route changes successfully. The `value` property contains the new URL path. */
|
|
101
|
+
change: EvChange;
|
|
102
|
+
/** Emitted when a route is not found (404 error). The `code` is 404 and `message` describes the error. */
|
|
103
|
+
error: EvError;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* micro router
|
|
109
|
+
*
|
|
110
|
+
* ```
|
|
111
|
+
* const router = new Router( );
|
|
112
|
+
*
|
|
113
|
+
* router.get( "/detail/:id", ( params: any ) => {
|
|
114
|
+
* this._showDetail( detail );
|
|
115
|
+
* } );
|
|
116
|
+
*
|
|
117
|
+
* router.get( "/:id", ( params: any ) => {
|
|
118
|
+
* if( params.id==0 )
|
|
119
|
+
* router.navigate( '/home' );
|
|
120
|
+
* }
|
|
121
|
+
* });
|
|
122
|
+
*
|
|
123
|
+
* router.on( "error", ( ) => {
|
|
124
|
+
* router.navigate( '/home' );
|
|
125
|
+
* })
|
|
126
|
+
*
|
|
127
|
+
* router.init( );
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
export class Router extends EventSource< RouterEvents > {
|
|
133
|
+
|
|
134
|
+
private m_routes: Route[];
|
|
135
|
+
private m_useHash: boolean;
|
|
136
|
+
|
|
137
|
+
constructor( useHash = true ) {
|
|
138
|
+
super( );
|
|
139
|
+
|
|
140
|
+
this.m_routes = [];
|
|
141
|
+
this.m_useHash = useHash;
|
|
142
|
+
|
|
143
|
+
window.addEventListener('popstate', (event) => {
|
|
144
|
+
const url = this._getLocation( );
|
|
145
|
+
const found = this._find(url);
|
|
146
|
+
|
|
147
|
+
found.handlers.forEach(h => {
|
|
148
|
+
h(found.params,url);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
this.fire( "change", { value: this._getLocation() } );
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Registers a route handler for the given URI pattern.
|
|
158
|
+
*
|
|
159
|
+
* @param uri - A path string (may include parameter placeholders) or a RegExp used to match request URIs.
|
|
160
|
+
* @param handler - A function conforming to the RouteHandler type that will be executed when a request matches the route.
|
|
161
|
+
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* The provided uri is parsed (via parseRoute) into a RegExp pattern and an ordered list of parameter keys.
|
|
164
|
+
* The resulting route descriptor ({ keys, pattern, handler }) is appended to the router's internal route list.
|
|
165
|
+
*
|
|
166
|
+
* @returns void
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
get(uri: string | RegExp, handler: RouteHandler ) {
|
|
170
|
+
let { keys, pattern } = parseRoute(uri);
|
|
171
|
+
this.m_routes.push({ keys, pattern, handler });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
init() {
|
|
176
|
+
this.navigate( this._getLocation() );
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private _getLocation( ) {
|
|
180
|
+
return this.m_useHash ? '/'+document.location.hash.substring(1) : document.location.pathname;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Navigate to a given URI within the router, update browser history, and optionally notify route handlers.
|
|
185
|
+
*
|
|
186
|
+
* Normalizes the supplied URI
|
|
187
|
+
* updates the browser history and emits lifecycle events.
|
|
188
|
+
*
|
|
189
|
+
* Behavior summary:
|
|
190
|
+
* - If no matching route or no handlers are found, logs a message, fires an "error" event with
|
|
191
|
+
* { code: 404, message: "route not found" } and returns false.
|
|
192
|
+
* - If notify is true, invokes the first handler of the matched route with (params, uri).
|
|
193
|
+
* - Always fires a "change" event with { value: this._getLocation() } after performing the navigation.
|
|
194
|
+
*
|
|
195
|
+
* @param uri - Target URI to navigate to. If it does not start with '/', a leading '/' will be added.
|
|
196
|
+
* When m_useHash is true the resulting location will be converted to a '#...' fragment.
|
|
197
|
+
* @param notify - Whether to invoke the matched route handler after updating history. Defaults to true.
|
|
198
|
+
* @param replace - Whether to replace the current history entry (true) or push a new one (false). Defaults to false.
|
|
199
|
+
* @returns True if navigation succeeded (route found and history updated); false if no route or handlers were found.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* // Navigate to /dashboard and notify handlers (pushes new history entry)
|
|
203
|
+
* navigate('/dashboard');
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* // Replace the current history entry without notifying handlers
|
|
207
|
+
* navigate('/login', false, true);
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
navigate( uri: string, notify = true, replace = false ) {
|
|
211
|
+
|
|
212
|
+
if( !uri.startsWith('/') ) {
|
|
213
|
+
uri = '/'+uri;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const found = this._find( uri );
|
|
217
|
+
|
|
218
|
+
if( !found || found.handlers.length==0 ) {
|
|
219
|
+
//window.history.pushState({}, '', 'error')
|
|
220
|
+
console.log( 'route not found: '+uri );
|
|
221
|
+
this.fire( "error", {code: 404, message: "route not found" } );
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if( this.m_useHash ) {
|
|
226
|
+
while( uri.at(0)=='/' ) {
|
|
227
|
+
uri = uri.substring( 1 );
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
uri = '#'+uri;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if( replace ) {
|
|
234
|
+
window.history.replaceState({}, '', uri );
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
window.history.pushState({}, '', uri );
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if( notify ) {
|
|
241
|
+
//found.handlers.forEach( h => {
|
|
242
|
+
// h( found.params, uri );
|
|
243
|
+
//} );
|
|
244
|
+
|
|
245
|
+
found.handlers[0]( found.params, uri );
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
this.fire( "change", { value: this._getLocation() } );
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
*
|
|
254
|
+
*/
|
|
255
|
+
|
|
256
|
+
private _find( url: string ): { params: Record<string,any>, handlers: RouteHandler[] } {
|
|
257
|
+
|
|
258
|
+
let matches = [];
|
|
259
|
+
let params: Record<string,any> = {};
|
|
260
|
+
let handlers: RouteHandler[] = [];
|
|
261
|
+
|
|
262
|
+
for (const tmp of this.m_routes ) {
|
|
263
|
+
if (!tmp.keys ) {
|
|
264
|
+
matches = tmp.pattern.exec(url);
|
|
265
|
+
if (!matches) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (matches['groups']) {
|
|
270
|
+
for (const k in matches['groups']) {
|
|
271
|
+
params[k] = matches['groups'][k];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
handlers = [...handlers, tmp.handler];
|
|
276
|
+
}
|
|
277
|
+
else if (tmp.keys.length > 0) {
|
|
278
|
+
matches = tmp.pattern.exec(url);
|
|
279
|
+
if (matches === null) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
for ( let j = 0; j < tmp.keys.length;) {
|
|
284
|
+
params[tmp.keys[j]] = matches[++j];
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
handlers = [...handlers, tmp.handler];
|
|
288
|
+
}
|
|
289
|
+
else if (tmp.pattern.test(url)) {
|
|
290
|
+
handlers = [...handlers, tmp.handler];
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return { params, handlers };
|
|
295
|
+
}
|
|
296
|
+
}
|