Actualizar src/index.js
This commit is contained in:
195
src/index.js
195
src/index.js
@@ -1,4 +1,4 @@
|
|||||||
// src/index.js - TODO INCLUIDO (sin import dinámico)
|
import { Tag, Watch, onUnmount } from "../sigpro.js";
|
||||||
import {
|
import {
|
||||||
ModuleRegistry,
|
ModuleRegistry,
|
||||||
ValidationModule,
|
ValidationModule,
|
||||||
@@ -12,7 +12,6 @@ import {
|
|||||||
iconSetQuartzLight,
|
iconSetQuartzLight,
|
||||||
createGrid
|
createGrid
|
||||||
} from "ag-grid-community";
|
} from "ag-grid-community";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
MultiFilterModule,
|
MultiFilterModule,
|
||||||
CellSelectionModule,
|
CellSelectionModule,
|
||||||
@@ -46,7 +45,7 @@ ModuleRegistry.registerModules([
|
|||||||
ClipboardModule,
|
ClipboardModule,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
export const Grid = (props, lang) => {
|
export const Grid = (props) => {
|
||||||
const { data, options, api, on, class: className, style = "height: 100%; width: 100%;" } = props;
|
const { data, options, api, on, class: className, style = "height: 100%; width: 100%;" } = props;
|
||||||
let gridApi = null;
|
let gridApi = null;
|
||||||
|
|
||||||
@@ -59,112 +58,110 @@ export const Grid = (props, lang) => {
|
|||||||
return dark ? 'ag-theme-alpine-dark' : 'ag-theme-alpine';
|
return dark ? 'ag-theme-alpine-dark' : 'ag-theme-alpine';
|
||||||
};
|
};
|
||||||
|
|
||||||
return $html("div", {
|
const initGrid = (container) => {
|
||||||
style,
|
if (!container) return;
|
||||||
class: className,
|
|
||||||
ref: (container) => {
|
|
||||||
try {
|
|
||||||
const initialData = typeof data === "function" ? data() : data;
|
|
||||||
const initialOptions = typeof options === "function" ? options() : options;
|
|
||||||
|
|
||||||
const commonEvents = [
|
const initialData = typeof data === "function" ? data() : data;
|
||||||
'onFilterChanged', 'onModelUpdated', 'onGridSizeChanged',
|
const initialOptions = typeof options === "function" ? options() : options;
|
||||||
'onFirstDataRendered', 'onRowValueChanged', 'onSelectionChanged',
|
|
||||||
'onCellClicked', 'onCellDoubleClicked', 'onCellValueChanged',
|
|
||||||
'onRowClicked', 'onSortChanged', 'onContextMenu',
|
|
||||||
'onColumnResized', 'onColumnMoved', 'onRowDataUpdated',
|
|
||||||
'onCellEditingStarted', 'onCellEditingStopped',
|
|
||||||
'onPaginationChanged', 'onBodyScroll'
|
|
||||||
];
|
|
||||||
|
|
||||||
const eventHandlers = {};
|
const commonEvents = [
|
||||||
commonEvents.forEach(eventName => {
|
'onFilterChanged', 'onModelUpdated', 'onGridSizeChanged',
|
||||||
if (on?.[eventName]) {
|
'onFirstDataRendered', 'onRowValueChanged', 'onSelectionChanged',
|
||||||
eventHandlers[eventName] = (params) => on[eventName](params);
|
'onCellClicked', 'onCellDoubleClicked', 'onCellValueChanged',
|
||||||
}
|
'onRowClicked', 'onSortChanged', 'onContextMenu',
|
||||||
});
|
'onColumnResized', 'onColumnMoved', 'onRowDataUpdated',
|
||||||
|
'onCellEditingStarted', 'onCellEditingStopped',
|
||||||
|
'onPaginationChanged', 'onBodyScroll'
|
||||||
|
];
|
||||||
|
|
||||||
const gridOptions = {
|
const eventHandlers = {};
|
||||||
...initialOptions,
|
commonEvents.forEach(eventName => {
|
||||||
theme: getTheme(isDark()),
|
if (on?.[eventName]) {
|
||||||
rowData: initialData || [],
|
eventHandlers[eventName] = (params) => on[eventName](params);
|
||||||
onGridReady: (params) => {
|
}
|
||||||
gridApi = params.api;
|
});
|
||||||
if (api) api.current = gridApi;
|
|
||||||
if (on?.onGridReady) on.onGridReady(params);
|
|
||||||
|
|
||||||
if (initialOptions?.autoSizeColumns) {
|
const gridOptions = {
|
||||||
setTimeout(() => {
|
...initialOptions,
|
||||||
if (gridApi && !gridApi.isDestroyed()) {
|
theme: getTheme(isDark()),
|
||||||
const allColumns = gridApi.getColumns();
|
rowData: initialData || [],
|
||||||
if (allColumns?.length) {
|
onGridReady: (params) => {
|
||||||
gridApi.autoSizeColumns(allColumns);
|
gridApi = params.api;
|
||||||
}
|
if (api) api.current = gridApi;
|
||||||
}
|
if (on?.onGridReady) on.onGridReady(params);
|
||||||
}, 100);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
...eventHandlers
|
|
||||||
};
|
|
||||||
|
|
||||||
gridApi = createGrid(container, gridOptions);
|
if (initialOptions?.autoSizeColumns) {
|
||||||
|
setTimeout(() => {
|
||||||
const stopData = $watch([data], () => {
|
if (gridApi && !gridApi.isDestroyed()) {
|
||||||
if (!gridApi || gridApi.isDestroyed()) return;
|
const allColumns = gridApi.getColumns();
|
||||||
const newData = typeof data === "function" ? data() : data;
|
if (allColumns?.length) {
|
||||||
if (Array.isArray(newData)) {
|
gridApi.autoSizeColumns(allColumns);
|
||||||
const currentData = gridApi.getGridOption("rowData");
|
|
||||||
if (newData !== currentData) {
|
|
||||||
gridApi.setGridOption("rowData", newData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const stopTheme = $watch([isDark], () => {
|
|
||||||
if (gridApi && !gridApi.isDestroyed()) {
|
|
||||||
const dark = isDark();
|
|
||||||
const newTheme = getTheme(dark);
|
|
||||||
const currentTheme = gridApi.getGridOption("theme");
|
|
||||||
if (newTheme !== currentTheme) {
|
|
||||||
gridApi.setGridOption("theme", newTheme);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const safeOptions = ['pagination', 'paginationPageSize', 'suppressRowClickSelection',
|
|
||||||
'rowSelection', 'enableCellTextSelection', 'ensureDomOrder',
|
|
||||||
'stopEditingWhenCellsLoseFocus', 'enterMovesDown', 'enterMovesDownAfterEdit'];
|
|
||||||
|
|
||||||
const stopOptions = $watch([options], () => {
|
|
||||||
if (!gridApi || gridApi.isDestroyed() || !options) return;
|
|
||||||
const newOptions = typeof options === "function" ? options() : options;
|
|
||||||
safeOptions.forEach(key => {
|
|
||||||
if (newOptions[key] !== undefined) {
|
|
||||||
try {
|
|
||||||
gridApi.setGridOption(key, newOptions[key]);
|
|
||||||
} catch (e) {
|
|
||||||
console.warn(`Could not set grid option ${key}:`, e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}, 100);
|
||||||
});
|
}
|
||||||
|
},
|
||||||
|
...eventHandlers
|
||||||
|
};
|
||||||
|
|
||||||
container._cleanups.add(stopData);
|
gridApi = createGrid(container, gridOptions);
|
||||||
container._cleanups.add(stopTheme);
|
|
||||||
container._cleanups.add(stopOptions);
|
|
||||||
container._cleanups.add(() => {
|
|
||||||
if (gridApi && !gridApi.isDestroyed()) {
|
|
||||||
gridApi.destroy();
|
|
||||||
if (api) api.current = null;
|
|
||||||
gridApi = null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
const stopData = Watch(data, () => {
|
||||||
console.error("Failed to initialize AG Grid:", error);
|
if (!gridApi || gridApi.isDestroyed()) return;
|
||||||
container.innerHTML = `<div class="text-error p-4">Error loading grid: ${error.message}</div>`;
|
const newData = typeof data === "function" ? data() : data;
|
||||||
|
if (Array.isArray(newData)) {
|
||||||
|
const currentData = gridApi.getGridOption("rowData");
|
||||||
|
if (newData !== currentData) {
|
||||||
|
gridApi.setGridOption("rowData", newData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}, true);
|
||||||
|
|
||||||
|
const stopTheme = Watch(isDark, () => {
|
||||||
|
if (gridApi && !gridApi.isDestroyed()) {
|
||||||
|
const dark = isDark();
|
||||||
|
const newTheme = getTheme(dark);
|
||||||
|
const currentTheme = gridApi.getGridOption("theme");
|
||||||
|
if (newTheme !== currentTheme) {
|
||||||
|
gridApi.setGridOption("theme", newTheme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
const safeOptions = [
|
||||||
|
'pagination', 'paginationPageSize', 'suppressRowClickSelection',
|
||||||
|
'rowSelection', 'enableCellTextSelection', 'ensureDomOrder',
|
||||||
|
'stopEditingWhenCellsLoseFocus', 'enterMovesDown', 'enterMovesDownAfterEdit'
|
||||||
|
];
|
||||||
|
|
||||||
|
const stopOptions = Watch(options, () => {
|
||||||
|
if (!gridApi || gridApi.isDestroyed() || !options) return;
|
||||||
|
const newOptions = typeof options === "function" ? options() : options;
|
||||||
|
safeOptions.forEach(key => {
|
||||||
|
if (newOptions[key] !== undefined) {
|
||||||
|
try {
|
||||||
|
gridApi.setGridOption(key, newOptions[key]);
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
onUnmount(() => {
|
||||||
|
stopData();
|
||||||
|
stopTheme();
|
||||||
|
stopOptions();
|
||||||
|
if (gridApi && !gridApi.isDestroyed()) {
|
||||||
|
gridApi.destroy();
|
||||||
|
if (api) api.current = null;
|
||||||
|
gridApi = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return Tag("div", {
|
||||||
|
class: className,
|
||||||
|
style: style,
|
||||||
|
ref: initGrid
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user