slicejs-web-framework 2.2.1 → 2.2.4
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/Slice/Components/Structural/Router/Router.js +62 -17
- package/package.json +1 -1
- package/src/App/index.js +15 -55
|
@@ -139,7 +139,7 @@ export default class Router {
|
|
|
139
139
|
* Ejecuta el beforeEach guard si existe
|
|
140
140
|
* @param {Object} to - Información de ruta destino
|
|
141
141
|
* @param {Object} from - Información de ruta origen
|
|
142
|
-
* @returns {
|
|
142
|
+
* @returns {Object|null} Objeto con redirectPath y options, o null si continúa
|
|
143
143
|
*/
|
|
144
144
|
async _executeBeforeEachGuard(to, from) {
|
|
145
145
|
if (!this._beforeEachGuard) {
|
|
@@ -147,18 +147,45 @@ export default class Router {
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
let redirectPath = null;
|
|
150
|
+
let redirectOptions = {};
|
|
150
151
|
let nextCalled = false;
|
|
151
152
|
|
|
152
|
-
const next = (
|
|
153
|
+
const next = (arg) => {
|
|
153
154
|
if (nextCalled) {
|
|
154
155
|
slice.logger.logWarning('Router', 'next() called multiple times in guard');
|
|
155
156
|
return;
|
|
156
157
|
}
|
|
157
158
|
nextCalled = true;
|
|
158
159
|
|
|
159
|
-
|
|
160
|
-
|
|
160
|
+
// Caso 1: Sin argumentos - continuar navegación
|
|
161
|
+
if (arg === undefined) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Caso 2: false - cancelar navegación
|
|
166
|
+
if (arg === false) {
|
|
167
|
+
redirectPath = false;
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Caso 3: String - redirección simple (backward compatibility)
|
|
172
|
+
if (typeof arg === 'string') {
|
|
173
|
+
redirectPath = arg;
|
|
174
|
+
redirectOptions = { replace: false };
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Caso 4: Objeto - redirección con opciones
|
|
179
|
+
if (typeof arg === 'object' && arg.path) {
|
|
180
|
+
redirectPath = arg.path;
|
|
181
|
+
redirectOptions = {
|
|
182
|
+
replace: arg.replace || false
|
|
183
|
+
};
|
|
184
|
+
return;
|
|
161
185
|
}
|
|
186
|
+
|
|
187
|
+
// Argumento inválido
|
|
188
|
+
slice.logger.logError('Router', 'Invalid argument passed to next(). Expected string, object with path, false, or undefined.');
|
|
162
189
|
};
|
|
163
190
|
|
|
164
191
|
try {
|
|
@@ -172,7 +199,8 @@ export default class Router {
|
|
|
172
199
|
);
|
|
173
200
|
}
|
|
174
201
|
|
|
175
|
-
|
|
202
|
+
// Retornar tanto el path como las opciones
|
|
203
|
+
return redirectPath ? { path: redirectPath, options: redirectOptions } : null;
|
|
176
204
|
} catch (error) {
|
|
177
205
|
slice.logger.logError('Router', 'Error in beforeEach guard', error);
|
|
178
206
|
return null; // En caso de error, continuar con la navegación
|
|
@@ -200,7 +228,7 @@ export default class Router {
|
|
|
200
228
|
// ROUTING CORE (MODIFICADO CON GUARDS)
|
|
201
229
|
// ============================================
|
|
202
230
|
|
|
203
|
-
|
|
231
|
+
async navigate(path, _redirectChain = [], _options = {}) {
|
|
204
232
|
const currentPath = window.location.pathname;
|
|
205
233
|
|
|
206
234
|
|
|
@@ -228,21 +256,32 @@ export default class Router {
|
|
|
228
256
|
|
|
229
257
|
// Obtener información de ruta destino
|
|
230
258
|
const { route: toRoute, params: toParams } = this.matchRoute(path);
|
|
231
|
-
const to = this._createRouteInfo(toRoute, toParams, path);
|
|
259
|
+
const to = this._createRouteInfo(toRoute, toParams, path);
|
|
232
260
|
|
|
233
261
|
|
|
234
262
|
// EJECUTAR BEFORE EACH GUARD
|
|
235
|
-
const
|
|
263
|
+
const guardResult = await this._executeBeforeEachGuard(to, from);
|
|
236
264
|
|
|
237
|
-
// Si el guard redirige
|
|
238
|
-
if (
|
|
265
|
+
// Si el guard redirige
|
|
266
|
+
if (guardResult && guardResult.path) {
|
|
239
267
|
const newChain = [..._redirectChain, path];
|
|
268
|
+
return this.navigate(guardResult.path, newChain, guardResult.options);
|
|
269
|
+
}
|
|
240
270
|
|
|
241
|
-
|
|
271
|
+
// Si el guard cancela la navegación (next(false))
|
|
272
|
+
if (guardResult && guardResult.path === false) {
|
|
273
|
+
slice.logger.logInfo('Router', 'Navigation cancelled by guard');
|
|
274
|
+
return;
|
|
242
275
|
}
|
|
243
276
|
|
|
244
277
|
// No hay redirección - continuar con la navegación normal
|
|
245
|
-
|
|
278
|
+
// Usar replace o push según las opciones
|
|
279
|
+
if (_options.replace) {
|
|
280
|
+
window.history.replaceState({}, path, window.location.origin + path);
|
|
281
|
+
} else {
|
|
282
|
+
window.history.pushState({}, path, window.location.origin + path);
|
|
283
|
+
}
|
|
284
|
+
|
|
246
285
|
await this._performNavigation(to, from);
|
|
247
286
|
}
|
|
248
287
|
|
|
@@ -323,19 +362,25 @@ export default class Router {
|
|
|
323
362
|
slice.router.activeRoute = route;
|
|
324
363
|
}
|
|
325
364
|
|
|
326
|
-
|
|
365
|
+
async loadInitialRoute() {
|
|
327
366
|
const path = window.location.pathname;
|
|
328
367
|
const { route, params } = this.matchRoute(path);
|
|
329
368
|
|
|
330
369
|
// Para la carga inicial, también ejecutar guards
|
|
331
370
|
const from = this._createRouteInfo(null, {}, null);
|
|
332
|
-
const to = this._createRouteInfo(route, params, path);
|
|
371
|
+
const to = this._createRouteInfo(route, params, path);
|
|
333
372
|
|
|
334
373
|
// EJECUTAR BEFORE EACH GUARD en carga inicial
|
|
335
|
-
const
|
|
374
|
+
const guardResult = await this._executeBeforeEachGuard(to, from);
|
|
336
375
|
|
|
337
|
-
if (
|
|
338
|
-
return this.navigate(
|
|
376
|
+
if (guardResult && guardResult.path) {
|
|
377
|
+
return this.navigate(guardResult.path, [], guardResult.options);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Si el guard cancela la navegación inicial (caso raro pero posible)
|
|
381
|
+
if (guardResult && guardResult.path === false) {
|
|
382
|
+
slice.logger.logWarning('Router', 'Initial route navigation cancelled by guard');
|
|
383
|
+
return;
|
|
339
384
|
}
|
|
340
385
|
|
|
341
386
|
await this.handleRoute(route, params);
|
package/package.json
CHANGED
package/src/App/index.js
CHANGED
|
@@ -1,63 +1,23 @@
|
|
|
1
1
|
import Slice from '/Slice/Slice.js';
|
|
2
2
|
|
|
3
|
-
const CACHE_NAME = 'slice-cache-v1';
|
|
4
|
-
const urlsToCache = [
|
|
5
|
-
// Añade aquí las rutas de los archivos que deseas almacenar
|
|
6
|
-
'./Slice/Slice.js',
|
|
7
|
-
'./Slice/Components/Structural/Logger/Logger.js',
|
|
8
|
-
'./Slice/Components/Structural/Debugger/Debugger.js',
|
|
9
|
-
'./Slice/Components/Structural/Router/Router.js',
|
|
10
|
-
'./Slice/Components/Structural/Router/routes.js',
|
|
11
|
-
'./Slice/Components/Structural/Controller/Controller.js',
|
|
12
|
-
'./Slice/Components/Structural/StylesManager/StylesManager.js',
|
|
13
|
-
'./Slice/Components/Structural/StylesManager/ThemeManager/ThemeManager.js',
|
|
14
|
-
];
|
|
15
3
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
4
|
+
/*
|
|
5
|
+
slice.router.beforeEach(async (to, from, next) => {
|
|
6
|
+
|
|
7
|
+
if(to.metadata.private){
|
|
8
|
+
const isAuthenticated = await //fetchlogic for validation
|
|
9
|
+
if(!isAuthenticated){
|
|
10
|
+
return next({ path: '/login', replace: true });
|
|
11
|
+
}
|
|
12
|
+
return next();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return next();
|
|
23
16
|
});
|
|
24
17
|
|
|
25
|
-
self.addEventListener('fetch', (event) => {
|
|
26
|
-
event.respondWith(
|
|
27
|
-
caches.match(event.request).then((response) => {
|
|
28
|
-
if (response) {
|
|
29
|
-
return response;
|
|
30
|
-
}
|
|
31
|
-
return fetch(event.request);
|
|
32
|
-
})
|
|
33
|
-
);
|
|
34
|
-
});
|
|
35
18
|
|
|
36
|
-
|
|
37
|
-
const cacheWhitelist = [CACHE_NAME];
|
|
19
|
+
If any beforeEach or afterEach is defined, start the router after defining them
|
|
38
20
|
|
|
39
|
-
|
|
40
|
-
caches.keys().then((cacheNames) => {
|
|
41
|
-
return Promise.all(
|
|
42
|
-
cacheNames.map((cacheName) => {
|
|
43
|
-
if (cacheWhitelist.indexOf(cacheName) === -1) {
|
|
44
|
-
return caches.delete(cacheName);
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
);
|
|
48
|
-
})
|
|
49
|
-
);
|
|
50
|
-
});
|
|
21
|
+
await slice.router.start();
|
|
51
22
|
|
|
52
|
-
|
|
53
|
-
window.addEventListener('load', () => {
|
|
54
|
-
navigator.serviceWorker
|
|
55
|
-
.register('/service-worker.js')
|
|
56
|
-
.then((registration) => {
|
|
57
|
-
console.log('Service Worker registrado con éxito:', registration);
|
|
58
|
-
})
|
|
59
|
-
.catch((error) => {
|
|
60
|
-
console.log('Error al registrar el Service Worker:', error);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
}
|
|
23
|
+
*/
|