slicejs-web-framework 2.4.7 → 3.0.0
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/.worktrees/bundling-v2-precompiled-registrars/LICENSE +21 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/ContextManager/ContextManager.js +369 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/ContextManager/ContextManagerDebugger.js +297 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/Controller/Controller.js +972 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/Debugger/Debugger.css +620 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/Debugger/Debugger.html +73 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/Debugger/Debugger.js +1548 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/EventManager/EventManager.js +338 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/EventManager/EventManagerDebugger.js +361 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/Logger/Log.js +10 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/Logger/Logger.js +146 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/Router/Router.js +721 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/StylesManager/StylesManager.js +78 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Components/Structural/StylesManager/ThemeManager/ThemeManager.js +84 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/Slice.js +504 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/tests/bundle-v2-runtime-contract.test.js +268 -0
- package/.worktrees/bundling-v2-precompiled-registrars/Slice/tests/router-loading-finally.test.js +68 -0
- package/.worktrees/bundling-v2-precompiled-registrars/api/index.js +286 -0
- package/.worktrees/bundling-v2-precompiled-registrars/api/middleware/securityMiddleware.js +253 -0
- package/.worktrees/bundling-v2-precompiled-registrars/package.json +37 -0
- package/.worktrees/bundling-v2-precompiled-registrars/sliceConfig.schema.json +109 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/App/index.html +22 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/App/index.js +23 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/App/style.css +40 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/AppComponents/HomePage/HomePage.css +201 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/AppComponents/HomePage/HomePage.html +37 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/AppComponents/HomePage/HomePage.js +210 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/AppComponents/Playground/Playground.css +12 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/AppComponents/Playground/Playground.html +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/AppComponents/Playground/Playground.js +111 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Service/FetchManager/FetchManager.js +133 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Service/IndexedDbManager/IndexedDbManager.js +141 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Service/LocalStorageManager/LocalStorageManager.js +45 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Button/Button.css +47 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Button/Button.html +5 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Button/Button.js +93 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Card/Card.css +68 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Card/Card.html +7 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Card/Card.js +107 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Checkbox/Checkbox.css +87 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Checkbox/Checkbox.html +8 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Checkbox/Checkbox.js +86 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/CodeVisualizer/CodeVisualizer.css +130 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/CodeVisualizer/CodeVisualizer.html +4 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/CodeVisualizer/CodeVisualizer.js +262 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Details/Details.css +70 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Details/Details.html +9 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Details/Details.js +76 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/DropDown/DropDown.css +60 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/DropDown/DropDown.html +5 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/DropDown/DropDown.js +63 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Grid/Grid.css +7 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Grid/Grid.html +1 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Grid/Grid.js +57 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/Icon.css +510 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/Icon.html +1 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/Icon.js +89 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.eot +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.json +555 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.styl +507 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.svg +1485 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.symbol.svg +1059 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.ttf +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.woff +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Icon/slc.woff2 +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Input/Input.css +91 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Input/Input.html +4 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Input/Input.js +215 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Layout/Layout.css +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Layout/Layout.html +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Layout/Layout.js +49 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Link/Link.css +8 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Link/Link.html +1 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Link/Link.js +63 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Loading/Loading.css +56 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Loading/Loading.html +83 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Loading/Loading.js +38 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/MultiRoute/MultiRoute.js +93 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Navbar/Navbar.css +115 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Navbar/Navbar.html +44 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Navbar/Navbar.js +141 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/NotFound/NotFound.css +117 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/NotFound/NotFound.html +24 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/NotFound/NotFound.js +16 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Route/Route.js +93 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Select/Select.css +84 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Select/Select.html +8 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Select/Select.js +195 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Switch/Switch.css +76 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Switch/Switch.html +8 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/Switch/Switch.js +102 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/TreeItem/TreeItem.css +36 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/TreeItem/TreeItem.html +1 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/TreeItem/TreeItem.js +126 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/TreeView/TreeView.css +8 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/TreeView/TreeView.html +1 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/Visual/TreeView/TreeView.js +48 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Components/components.js +27 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Styles/sliceStyles.css +34 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Themes/Dark.css +42 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Themes/Light.css +31 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/Themes/Slice.css +47 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/images/Slice.js-logo.png +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/images/favicon.ico +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/images/im2/Slice.js-logo.png +0 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/routes.js +16 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/sliceConfig.json +73 -0
- package/.worktrees/bundling-v2-precompiled-registrars/src/testing.js +888 -0
- package/Slice/Components/Structural/Controller/Controller.js +77 -31
- package/Slice/Components/Structural/Router/Router.js +25 -24
- package/Slice/Components/Structural/StylesManager/StylesManager.js +15 -5
- package/Slice/Slice.js +60 -63
- package/Slice/tests/bundle-v2-runtime-contract.test.js +268 -0
- package/Slice/tests/router-loading-finally.test.js +68 -0
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Victor Jose Kneider Alnahi and Julio Antonio Graterol Bracho
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ContextManager - Sistema de contextos compartidos para Slice.js
|
|
3
|
+
* Ubicación: /Slice/Components/Structural/ContextManager/ContextManager.js
|
|
4
|
+
*
|
|
5
|
+
* Características:
|
|
6
|
+
* - Estado compartido entre componentes
|
|
7
|
+
* - Usa EventManager internamente para notificaciones
|
|
8
|
+
* - Selectores para observar campos específicos
|
|
9
|
+
* - Auto-limpieza cuando componentes se destruyen
|
|
10
|
+
* - Persistencia opcional en localStorage
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Object} ContextOptions
|
|
14
|
+
* @property {boolean} [persist]
|
|
15
|
+
* @property {string} [storageKey]
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export default class ContextManager {
|
|
19
|
+
constructor() {
|
|
20
|
+
// Map<contextName, { state, options }>
|
|
21
|
+
this.contexts = new Map();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
init() {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ============================================
|
|
29
|
+
// CREAR CONTEXTO
|
|
30
|
+
// ============================================
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Crear un nuevo contexto
|
|
34
|
+
* @param {string} name - Nombre unico del contexto
|
|
35
|
+
* @param {Object} [initialState] - Estado inicial
|
|
36
|
+
* @param {ContextOptions} [options] - Opciones: { persist, storageKey }
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* slice.context.create('auth', {
|
|
41
|
+
* isLoggedIn: false,
|
|
42
|
+
* user: null
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Con persistencia en localStorage
|
|
46
|
+
* slice.context.create('preferences', {
|
|
47
|
+
* theme: 'light',
|
|
48
|
+
* language: 'es'
|
|
49
|
+
* }, { persist: true });
|
|
50
|
+
*/
|
|
51
|
+
create(name, initialState = {}, options = {}) {
|
|
52
|
+
if (this.contexts.has(name)) {
|
|
53
|
+
slice.logger.logWarning('ContextManager', `El contexto "${name}" ya existe`);
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Cargar estado persistido si existe
|
|
58
|
+
let state = initialState;
|
|
59
|
+
if (options.persist) {
|
|
60
|
+
const persisted = this._loadFromStorage(name);
|
|
61
|
+
if (persisted !== null) {
|
|
62
|
+
state = persisted;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.contexts.set(name, {
|
|
67
|
+
state,
|
|
68
|
+
options: {
|
|
69
|
+
persist: options.persist || false,
|
|
70
|
+
storageKey: options.storageKey || `slice_context_${name}`,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
slice.logger.logInfo('ContextManager', `Contexto "${name}" creado`);
|
|
75
|
+
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ============================================
|
|
80
|
+
// LEER ESTADO
|
|
81
|
+
// ============================================
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Obtener el estado actual de un contexto
|
|
85
|
+
* @param {string} name - Nombre del contexto
|
|
86
|
+
* @returns {any|null} Estado actual o null si no existe
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* const auth = slice.context.getState('auth');
|
|
90
|
+
* console.log(auth.user.name);
|
|
91
|
+
*/
|
|
92
|
+
getState(name) {
|
|
93
|
+
if (!this.contexts.has(name)) {
|
|
94
|
+
slice.logger.logError('ContextManager', `El contexto "${name}" no existe`);
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return this.contexts.get(name).state;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ============================================
|
|
102
|
+
// ACTUALIZAR ESTADO
|
|
103
|
+
// ============================================
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Actualizar el estado de un contexto
|
|
107
|
+
* @param {string} name - Nombre del contexto
|
|
108
|
+
* @param {Object|Function} updater - Nuevo estado o funcion (prevState) => newState
|
|
109
|
+
* @returns {void}
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* // Reemplazar con objeto
|
|
113
|
+
* slice.context.setState('auth', {
|
|
114
|
+
* isLoggedIn: true,
|
|
115
|
+
* user: { name: 'Juan' }
|
|
116
|
+
* });
|
|
117
|
+
*
|
|
118
|
+
* // Usar funcion para acceder al estado anterior
|
|
119
|
+
* slice.context.setState('cart', (prev) => ({
|
|
120
|
+
* ...prev,
|
|
121
|
+
* items: [...prev.items, nuevoProducto],
|
|
122
|
+
* total: prev.total + nuevoProducto.price
|
|
123
|
+
* }));
|
|
124
|
+
*/
|
|
125
|
+
setState(name, updater) {
|
|
126
|
+
if (!this.contexts.has(name)) {
|
|
127
|
+
slice.logger.logError('ContextManager', `El contexto "${name}" no existe`);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const context = this.contexts.get(name);
|
|
132
|
+
const prevState = context.state;
|
|
133
|
+
|
|
134
|
+
// Calcular nuevo estado
|
|
135
|
+
let newState;
|
|
136
|
+
if (typeof updater === 'function') {
|
|
137
|
+
newState = updater(prevState);
|
|
138
|
+
} else {
|
|
139
|
+
newState = updater;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Guardar nuevo estado
|
|
143
|
+
context.state = newState;
|
|
144
|
+
|
|
145
|
+
// Persistir si está habilitado
|
|
146
|
+
if (context.options.persist) {
|
|
147
|
+
this._saveToStorage(name, newState, context.options.storageKey);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Emitir evento para notificar a los watchers
|
|
151
|
+
slice.events.emit(`context:${name}`, newState, prevState);
|
|
152
|
+
|
|
153
|
+
slice.logger.logInfo('ContextManager', `Contexto "${name}" actualizado`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ============================================
|
|
157
|
+
// WATCH (OBSERVAR CAMBIOS)
|
|
158
|
+
// ============================================
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Observar cambios en un contexto
|
|
162
|
+
* @param {string} name - Nombre del contexto
|
|
163
|
+
* @param {HTMLElement} component - Componente Slice para auto-cleanup
|
|
164
|
+
* @param {(value: any) => void} callback - Funcion a ejecutar cuando cambia
|
|
165
|
+
* @param {(state: any) => any} [selector] - Opcional. Funcion para seleccionar campos
|
|
166
|
+
* @returns {string|null} subscriptionId
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* // Observar todo el estado
|
|
170
|
+
* slice.context.watch('auth', this, (state) => {
|
|
171
|
+
* this.render(state);
|
|
172
|
+
* });
|
|
173
|
+
*
|
|
174
|
+
* // Observar un campo especifico
|
|
175
|
+
* slice.context.watch('auth', this, (name) => {
|
|
176
|
+
* this.$name.textContent = name;
|
|
177
|
+
* }, state => state.user.name);
|
|
178
|
+
*
|
|
179
|
+
* // Observar multiples campos
|
|
180
|
+
* slice.context.watch('auth', this, (data) => {
|
|
181
|
+
* this.$name.textContent = data.name;
|
|
182
|
+
* this.$avatar.src = data.avatar;
|
|
183
|
+
* }, state => ({
|
|
184
|
+
* name: state.user.name,
|
|
185
|
+
* avatar: state.user.avatar
|
|
186
|
+
* }));
|
|
187
|
+
*
|
|
188
|
+
* // Valores computados
|
|
189
|
+
* slice.context.watch('cart', this, (total) => {
|
|
190
|
+
* this.$total.textContent = `${total}`;
|
|
191
|
+
* }, state => state.items.reduce((sum, i) => sum + i.price, 0));
|
|
192
|
+
*/
|
|
193
|
+
watch(name, component, callback, selector = null) {
|
|
194
|
+
if (!this.contexts.has(name)) {
|
|
195
|
+
slice.logger.logError('ContextManager', `El contexto "${name}" no existe`);
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (!component?.sliceId) {
|
|
200
|
+
slice.logger.logError('ContextManager', 'watch() requiere un componente Slice válido');
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (typeof callback !== 'function') {
|
|
205
|
+
slice.logger.logError('ContextManager', 'El callback debe ser una función');
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Guardar el valor anterior del selector para comparar
|
|
210
|
+
let lastSelectedValue = selector ? this._getSelectedValue(name, selector) : this.getState(name);
|
|
211
|
+
|
|
212
|
+
// Crear wrapper que aplica el selector y compara
|
|
213
|
+
const wrappedCallback = (newState, prevState) => {
|
|
214
|
+
if (selector) {
|
|
215
|
+
const newSelectedValue = this._applySelector(newState, selector);
|
|
216
|
+
const prevSelectedValue = this._applySelector(prevState, selector);
|
|
217
|
+
|
|
218
|
+
// Solo ejecutar si el valor seleccionado cambió
|
|
219
|
+
if (!this._isEqual(newSelectedValue, prevSelectedValue)) {
|
|
220
|
+
lastSelectedValue = newSelectedValue;
|
|
221
|
+
callback(newSelectedValue);
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
// Sin selector, siempre ejecutar
|
|
225
|
+
callback(newState);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// Suscribirse al evento del contexto
|
|
230
|
+
const subscriptionId = slice.events.subscribe(`context:${name}`, wrappedCallback, { component });
|
|
231
|
+
|
|
232
|
+
slice.logger.logInfo('ContextManager', `Watch registrado en "${name}" para ${component.sliceId}`);
|
|
233
|
+
|
|
234
|
+
return subscriptionId;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ============================================
|
|
238
|
+
// UTILIDADES
|
|
239
|
+
// ============================================
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Verificar si un contexto existe
|
|
243
|
+
* @param {string} name - Nombre del contexto
|
|
244
|
+
* @returns {boolean}
|
|
245
|
+
*/
|
|
246
|
+
has(name) {
|
|
247
|
+
return this.contexts.has(name);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Eliminar un contexto
|
|
252
|
+
* @param {string} name - Nombre del contexto
|
|
253
|
+
* @returns {boolean}
|
|
254
|
+
*/
|
|
255
|
+
destroy(name) {
|
|
256
|
+
if (!this.contexts.has(name)) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const context = this.contexts.get(name);
|
|
261
|
+
|
|
262
|
+
// Limpiar storage si existe
|
|
263
|
+
if (context.options.persist) {
|
|
264
|
+
this._removeFromStorage(context.options.storageKey);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
this.contexts.delete(name);
|
|
268
|
+
|
|
269
|
+
slice.logger.logInfo('ContextManager', `Contexto "${name}" eliminado`);
|
|
270
|
+
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Obtener lista de todos los contextos
|
|
276
|
+
* @returns {string[]}
|
|
277
|
+
*/
|
|
278
|
+
list() {
|
|
279
|
+
return Array.from(this.contexts.keys());
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ============================================
|
|
283
|
+
// MÉTODOS PRIVADOS
|
|
284
|
+
// ============================================
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Aplicar selector al estado
|
|
288
|
+
*/
|
|
289
|
+
_applySelector(state, selector) {
|
|
290
|
+
try {
|
|
291
|
+
return selector(state);
|
|
292
|
+
} catch (error) {
|
|
293
|
+
slice.logger.logWarning('ContextManager', 'Error al aplicar selector', error);
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Obtener valor seleccionado del estado actual
|
|
300
|
+
*/
|
|
301
|
+
_getSelectedValue(name, selector) {
|
|
302
|
+
const state = this.getState(name);
|
|
303
|
+
return this._applySelector(state, selector);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Comparar dos valores (shallow)
|
|
308
|
+
*/
|
|
309
|
+
_isEqual(a, b) {
|
|
310
|
+
// Mismo valor primitivo o referencia
|
|
311
|
+
if (a === b) return true;
|
|
312
|
+
|
|
313
|
+
// Si alguno es null/undefined
|
|
314
|
+
if (a == null || b == null) return false;
|
|
315
|
+
|
|
316
|
+
// Si no son objetos, ya sabemos que son diferentes
|
|
317
|
+
if (typeof a !== 'object' || typeof b !== 'object') return false;
|
|
318
|
+
|
|
319
|
+
// Comparación shallow de objetos
|
|
320
|
+
const keysA = Object.keys(a);
|
|
321
|
+
const keysB = Object.keys(b);
|
|
322
|
+
|
|
323
|
+
if (keysA.length !== keysB.length) return false;
|
|
324
|
+
|
|
325
|
+
for (const key of keysA) {
|
|
326
|
+
if (a[key] !== b[key]) return false;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Cargar estado desde localStorage
|
|
334
|
+
*/
|
|
335
|
+
_loadFromStorage(name) {
|
|
336
|
+
try {
|
|
337
|
+
const key = `slice_context_${name}`;
|
|
338
|
+
const data = localStorage.getItem(key);
|
|
339
|
+
if (data) {
|
|
340
|
+
return JSON.parse(data);
|
|
341
|
+
}
|
|
342
|
+
} catch (error) {
|
|
343
|
+
slice.logger.logWarning('ContextManager', `Error cargando "${name}" de localStorage`, error);
|
|
344
|
+
}
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Guardar estado en localStorage
|
|
350
|
+
*/
|
|
351
|
+
_saveToStorage(name, state, storageKey) {
|
|
352
|
+
try {
|
|
353
|
+
localStorage.setItem(storageKey, JSON.stringify(state));
|
|
354
|
+
} catch (error) {
|
|
355
|
+
slice.logger.logWarning('ContextManager', `Error guardando "${name}" en localStorage`, error);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Eliminar estado de localStorage
|
|
361
|
+
*/
|
|
362
|
+
_removeFromStorage(storageKey) {
|
|
363
|
+
try {
|
|
364
|
+
localStorage.removeItem(storageKey);
|
|
365
|
+
} catch (error) {
|
|
366
|
+
// Ignorar errores al eliminar
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ContextManager debug panel.
|
|
3
|
+
*/
|
|
4
|
+
export default class ContextManagerDebugger extends HTMLElement {
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
this.isOpen = false;
|
|
8
|
+
this.filterText = '';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Initialize panel UI.
|
|
13
|
+
* @returns {Promise<void>}
|
|
14
|
+
*/
|
|
15
|
+
async init() {
|
|
16
|
+
this.innerHTML = this.renderTemplate();
|
|
17
|
+
slice.stylesManager.registerComponentStyles('ContextManagerDebugger', this.renderStyles());
|
|
18
|
+
this.cacheElements();
|
|
19
|
+
this.bindEvents();
|
|
20
|
+
this.makeDraggable();
|
|
21
|
+
this.renderList();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Toggle panel visibility.
|
|
26
|
+
* @returns {void}
|
|
27
|
+
*/
|
|
28
|
+
toggle() {
|
|
29
|
+
this.isOpen = !this.isOpen;
|
|
30
|
+
this.container.classList.toggle('active', this.isOpen);
|
|
31
|
+
if (this.isOpen) {
|
|
32
|
+
this.renderList();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Show panel.
|
|
38
|
+
* @returns {void}
|
|
39
|
+
*/
|
|
40
|
+
open() {
|
|
41
|
+
this.isOpen = true;
|
|
42
|
+
this.container.classList.add('active');
|
|
43
|
+
this.renderList();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Hide panel.
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*/
|
|
50
|
+
close() {
|
|
51
|
+
this.isOpen = false;
|
|
52
|
+
this.container.classList.remove('active');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
cacheElements() {
|
|
56
|
+
this.container = this.querySelector('#context-debugger');
|
|
57
|
+
this.header = this.querySelector('.context-header');
|
|
58
|
+
this.list = this.querySelector('#context-list');
|
|
59
|
+
this.filterInput = this.querySelector('#context-filter');
|
|
60
|
+
this.countLabel = this.querySelector('#context-count');
|
|
61
|
+
this.refreshButton = this.querySelector('#context-refresh');
|
|
62
|
+
this.closeButton = this.querySelector('#context-close');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
bindEvents() {
|
|
66
|
+
this.refreshButton.addEventListener('click', () => this.renderList());
|
|
67
|
+
this.closeButton.addEventListener('click', () => this.close());
|
|
68
|
+
this.filterInput.addEventListener('input', (event) => {
|
|
69
|
+
this.filterText = event.target.value.trim().toLowerCase();
|
|
70
|
+
this.renderList();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
makeDraggable() {
|
|
75
|
+
if (!this.header || !this.container) return;
|
|
76
|
+
|
|
77
|
+
let offset = { x: 0, y: 0 };
|
|
78
|
+
let isDragging = false;
|
|
79
|
+
|
|
80
|
+
this.header.style.cursor = 'grab';
|
|
81
|
+
|
|
82
|
+
this.header.addEventListener('mousedown', (event) => {
|
|
83
|
+
if (event.target.closest('.btn')) return;
|
|
84
|
+
isDragging = true;
|
|
85
|
+
offset.x = event.clientX - this.container.getBoundingClientRect().left;
|
|
86
|
+
offset.y = event.clientY - this.container.getBoundingClientRect().top;
|
|
87
|
+
this.header.style.cursor = 'grabbing';
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
document.addEventListener('mousemove', (event) => {
|
|
91
|
+
if (!isDragging) return;
|
|
92
|
+
const rect = this.container.getBoundingClientRect();
|
|
93
|
+
const maxX = window.innerWidth - rect.width;
|
|
94
|
+
const maxY = window.innerHeight - rect.height;
|
|
95
|
+
const x = Math.min(Math.max(event.clientX - offset.x, 0), maxX);
|
|
96
|
+
const y = Math.min(Math.max(event.clientY - offset.y, 0), maxY);
|
|
97
|
+
this.container.style.left = `${x}px`;
|
|
98
|
+
this.container.style.top = `${y}px`;
|
|
99
|
+
this.container.style.right = 'auto';
|
|
100
|
+
this.container.style.bottom = 'auto';
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
document.addEventListener('mouseup', () => {
|
|
104
|
+
if (!isDragging) return;
|
|
105
|
+
isDragging = false;
|
|
106
|
+
this.header.style.cursor = 'grab';
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
renderList() {
|
|
111
|
+
if (!slice?.context?.contexts) {
|
|
112
|
+
this.list.textContent = 'ContextManager not available.';
|
|
113
|
+
this.countLabel.textContent = '0';
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const items = [];
|
|
118
|
+
slice.context.contexts.forEach((value, name) => {
|
|
119
|
+
if (this.filterText && !name.toLowerCase().includes(this.filterText)) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const keys = value?.state ? Object.keys(value.state).length : 0;
|
|
123
|
+
items.push({ name, keys, state: value?.state || {} });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
items.sort((a, b) => a.name.localeCompare(b.name));
|
|
127
|
+
|
|
128
|
+
this.countLabel.textContent = String(items.length);
|
|
129
|
+
this.list.innerHTML = items.length
|
|
130
|
+
? items.map((item) => {
|
|
131
|
+
const preview = JSON.stringify(item.state, null, 2);
|
|
132
|
+
return `
|
|
133
|
+
<div class="context-row">
|
|
134
|
+
<div class="context-header">
|
|
135
|
+
<div class="context-name">${item.name}</div>
|
|
136
|
+
<div class="context-keys">${item.keys} keys</div>
|
|
137
|
+
</div>
|
|
138
|
+
<pre class="context-preview">${this.escapeHtml(preview)}</pre>
|
|
139
|
+
</div>
|
|
140
|
+
`;
|
|
141
|
+
}).join('')
|
|
142
|
+
: '<div class="empty">No contexts</div>';
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
escapeHtml(value) {
|
|
146
|
+
return value
|
|
147
|
+
.replace(/&/g, '&')
|
|
148
|
+
.replace(/</g, '<')
|
|
149
|
+
.replace(/>/g, '>')
|
|
150
|
+
.replace(/"/g, '"')
|
|
151
|
+
.replace(/'/g, ''');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
renderTemplate() {
|
|
155
|
+
return `
|
|
156
|
+
<div id="context-debugger">
|
|
157
|
+
<div class="context-header">
|
|
158
|
+
<div class="title">Contexts</div>
|
|
159
|
+
<div class="actions">
|
|
160
|
+
<button id="context-refresh" class="btn">Refresh</button>
|
|
161
|
+
<button id="context-close" class="btn">Close</button>
|
|
162
|
+
</div>
|
|
163
|
+
</div>
|
|
164
|
+
<div class="context-toolbar">
|
|
165
|
+
<input id="context-filter" type="text" placeholder="Filter contexts" />
|
|
166
|
+
<div class="count">Total: <span id="context-count">0</span></div>
|
|
167
|
+
</div>
|
|
168
|
+
<div class="context-list" id="context-list"></div>
|
|
169
|
+
</div>
|
|
170
|
+
`;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
renderStyles() {
|
|
174
|
+
return `
|
|
175
|
+
#context-debugger {
|
|
176
|
+
position: fixed;
|
|
177
|
+
bottom: 20px;
|
|
178
|
+
left: 20px;
|
|
179
|
+
width: min(380px, calc(100vw - 40px));
|
|
180
|
+
max-height: 60vh;
|
|
181
|
+
background: var(--primary-background-color);
|
|
182
|
+
border: 1px solid var(--medium-color);
|
|
183
|
+
border-radius: 12px;
|
|
184
|
+
box-shadow: 0 16px 32px rgba(0, 0, 0, 0.15);
|
|
185
|
+
display: none;
|
|
186
|
+
flex-direction: column;
|
|
187
|
+
z-index: 10001;
|
|
188
|
+
overflow: hidden;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
#context-debugger.active {
|
|
192
|
+
display: flex;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
#context-debugger * {
|
|
196
|
+
box-sizing: border-box;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
.context-header {
|
|
200
|
+
display: flex;
|
|
201
|
+
justify-content: space-between;
|
|
202
|
+
align-items: center;
|
|
203
|
+
padding: 12px 14px;
|
|
204
|
+
background: var(--tertiary-background-color);
|
|
205
|
+
border-bottom: 1px solid var(--medium-color);
|
|
206
|
+
user-select: none;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
.context-header .title {
|
|
210
|
+
font-weight: 600;
|
|
211
|
+
color: var(--font-primary-color);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.context-header .actions {
|
|
215
|
+
display: flex;
|
|
216
|
+
gap: 8px;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
.context-header .btn {
|
|
220
|
+
padding: 6px 10px;
|
|
221
|
+
border-radius: 6px;
|
|
222
|
+
border: 1px solid var(--medium-color);
|
|
223
|
+
background: var(--primary-background-color);
|
|
224
|
+
color: var(--font-primary-color);
|
|
225
|
+
cursor: pointer;
|
|
226
|
+
font-size: 12px;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
.context-toolbar {
|
|
230
|
+
display: flex;
|
|
231
|
+
gap: 10px;
|
|
232
|
+
align-items: center;
|
|
233
|
+
padding: 10px 12px;
|
|
234
|
+
border-bottom: 1px solid var(--medium-color);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
.context-toolbar input {
|
|
238
|
+
flex: 1;
|
|
239
|
+
min-width: 0;
|
|
240
|
+
padding: 6px 8px;
|
|
241
|
+
border-radius: 6px;
|
|
242
|
+
border: 1px solid var(--medium-color);
|
|
243
|
+
background: var(--primary-background-color);
|
|
244
|
+
color: var(--font-primary-color);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
.context-list {
|
|
248
|
+
padding: 10px 12px;
|
|
249
|
+
overflow: auto;
|
|
250
|
+
display: flex;
|
|
251
|
+
flex-direction: column;
|
|
252
|
+
gap: 10px;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
.context-row {
|
|
256
|
+
border: 1px solid var(--medium-color);
|
|
257
|
+
border-radius: 8px;
|
|
258
|
+
background: var(--tertiary-background-color);
|
|
259
|
+
padding: 10px;
|
|
260
|
+
display: flex;
|
|
261
|
+
flex-direction: column;
|
|
262
|
+
gap: 6px;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
.context-name {
|
|
266
|
+
font-weight: 600;
|
|
267
|
+
color: var(--font-primary-color);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
.context-keys {
|
|
271
|
+
font-size: 12px;
|
|
272
|
+
color: var(--font-secondary-color);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
.context-preview {
|
|
276
|
+
background: var(--primary-background-color);
|
|
277
|
+
border-radius: 6px;
|
|
278
|
+
padding: 8px;
|
|
279
|
+
border: 1px solid var(--medium-color);
|
|
280
|
+
max-height: 180px;
|
|
281
|
+
overflow: auto;
|
|
282
|
+
font-size: 11px;
|
|
283
|
+
font-family: monospace;
|
|
284
|
+
color: var(--font-primary-color);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
.empty {
|
|
288
|
+
color: var(--font-secondary-color);
|
|
289
|
+
font-size: 12px;
|
|
290
|
+
text-align: center;
|
|
291
|
+
padding: 12px 0;
|
|
292
|
+
}
|
|
293
|
+
`;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
customElements.define('slice-contextmanager-debugger', ContextManagerDebugger);
|