slicejs-web-framework 2.0.3 → 2.1.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.
|
@@ -276,10 +276,126 @@ export default class Controller {
|
|
|
276
276
|
return usedProps;
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
279
|
+
/**
|
|
280
|
+
* Destruye uno o múltiples componentes
|
|
281
|
+
* @param {HTMLElement|Array<HTMLElement>|string|Array<string>} components
|
|
282
|
+
* - Componente individual (HTMLElement)
|
|
283
|
+
* - Array de componentes
|
|
284
|
+
* - sliceId individual (string)
|
|
285
|
+
* - Array de sliceIds
|
|
286
|
+
* @returns {number} Cantidad de componentes destruidos
|
|
287
|
+
*/
|
|
288
|
+
destroyComponent(components) {
|
|
289
|
+
// Normalizar entrada a array
|
|
290
|
+
const toDestroy = Array.isArray(components) ? components : [components];
|
|
291
|
+
let destroyedCount = 0;
|
|
292
|
+
|
|
293
|
+
for (const item of toDestroy) {
|
|
294
|
+
let component = null;
|
|
295
|
+
|
|
296
|
+
// Si es string, buscar el componente por sliceId
|
|
297
|
+
if (typeof item === 'string') {
|
|
298
|
+
component = this.activeComponents.get(item);
|
|
299
|
+
|
|
300
|
+
if (!component) {
|
|
301
|
+
slice.logger.logWarning('Controller', `Component with sliceId "${item}" not found`);
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// Si es un componente directamente
|
|
306
|
+
else if (item && item.sliceId) {
|
|
307
|
+
component = item;
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
slice.logger.logWarning('Controller', `Invalid component or sliceId provided to destroyComponent`);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Ejecutar hook beforeDestroy si existe
|
|
315
|
+
if (typeof component.beforeDestroy === 'function') {
|
|
316
|
+
try {
|
|
317
|
+
component.beforeDestroy();
|
|
318
|
+
} catch (error) {
|
|
319
|
+
slice.logger.logError('Controller', `Error in beforeDestroy for ${component.sliceId}`, error);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Eliminar del mapa de componentes activos
|
|
324
|
+
this.activeComponents.delete(component.sliceId);
|
|
325
|
+
|
|
326
|
+
// Remover del DOM si está conectado
|
|
327
|
+
if (component.isConnected) {
|
|
328
|
+
component.remove();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
destroyedCount++;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (destroyedCount > 0) {
|
|
335
|
+
slice.logger.logInfo('Controller', `Destroyed ${destroyedCount} component(s)`);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return destroyedCount;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Destruye todos los componentes Slice dentro de un contenedor
|
|
343
|
+
* @param {HTMLElement} container - Elemento contenedor
|
|
344
|
+
* @returns {number} Cantidad de componentes destruidos
|
|
345
|
+
*/
|
|
346
|
+
destroyByContainer(container) {
|
|
347
|
+
if (!container) {
|
|
348
|
+
slice.logger.logWarning('Controller', 'No container provided to destroyByContainer');
|
|
349
|
+
return 0;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Buscar todos los elementos que sean componentes Slice
|
|
353
|
+
const sliceComponents = container.querySelectorAll('[slice-id]');
|
|
354
|
+
const sliceIdsToDestroy = [];
|
|
355
|
+
|
|
356
|
+
sliceComponents.forEach(element => {
|
|
357
|
+
const sliceId = element.getAttribute('slice-id') || element.sliceId;
|
|
358
|
+
if (sliceId && this.activeComponents.has(sliceId)) {
|
|
359
|
+
sliceIdsToDestroy.push(sliceId);
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
// Destruir usando el método principal
|
|
364
|
+
const count = this.destroyComponent(sliceIdsToDestroy);
|
|
365
|
+
|
|
366
|
+
if (count > 0) {
|
|
367
|
+
slice.logger.logInfo('Controller', `Destroyed ${count} component(s) from container`);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return count;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Destruye componentes cuyos sliceId coincidan con un patrón
|
|
375
|
+
* @param {string|RegExp} pattern - Patrón a buscar (string o expresión regular)
|
|
376
|
+
* @returns {number} Cantidad de componentes destruidos
|
|
377
|
+
*/
|
|
378
|
+
destroyByPattern(pattern) {
|
|
379
|
+
const componentsToDestroy = [];
|
|
380
|
+
|
|
381
|
+
// Convertir string a RegExp si es necesario
|
|
382
|
+
const regex = pattern instanceof RegExp ? pattern : new RegExp(pattern);
|
|
383
|
+
|
|
384
|
+
// Buscar componentes que coincidan con el patrón
|
|
385
|
+
for (const [sliceId, component] of this.activeComponents) {
|
|
386
|
+
if (regex.test(sliceId)) {
|
|
387
|
+
componentsToDestroy.push(component);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Destruir usando el método principal
|
|
392
|
+
const count = this.destroyComponent(componentsToDestroy);
|
|
393
|
+
|
|
394
|
+
if (count > 0) {
|
|
395
|
+
slice.logger.logInfo('Controller', `Destroyed ${count} component(s) matching pattern: ${pattern}`);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return count;
|
|
283
399
|
}
|
|
284
400
|
}
|
|
285
401
|
|