@silexlabs/grapesjs-data-source
Version:
Grapesjs Data Source
108 lines • 3.27 kB
JavaScript
;
/*
* Silex website builder, free/libre no-code tool for makers.
* Copyright (c) 2023 lexoyo and Silex Labs foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeDataSourceRegistry = initializeDataSourceRegistry;
exports.getAllDataSources = getAllDataSources;
exports.addDataSource = addDataSource;
exports.removeDataSource = removeDataSource;
exports.getDataSource = getDataSource;
exports.setDataSources = setDataSources;
exports.dataSourcesToJSON = dataSourcesToJSON;
const types_1 = require("../types");
// Global registry instance
let globalRegistry = null;
/**
* Initialize the data source registry
*/
function initializeDataSourceRegistry(editor) {
globalRegistry = {
dataSources: [],
editor,
};
}
/**
* Get the global registry (throws if not initialized)
*/
function getRegistry() {
if (!globalRegistry) {
throw new Error('DataSourceRegistry not initialized. Call initializeDataSourceRegistry first.');
}
return globalRegistry;
}
/**
* Get all data sources
*/
function getAllDataSources() {
return [...getRegistry().dataSources];
}
/**
* Add a data source
*/
function addDataSource(dataSource) {
const registry = getRegistry();
registry.dataSources.push(dataSource);
dataSource.connect()
.then(() => {
registry.editor.trigger(types_1.DATA_SOURCE_CHANGED);
})
.catch((error) => {
console.error('Failed to connect data source:', error);
registry.editor.trigger(types_1.DATA_SOURCE_CHANGED);
});
}
/**
* Remove a data source
*/
function removeDataSource(dataSource) {
const registry = getRegistry();
const index = registry.dataSources.indexOf(dataSource);
if (index > -1) {
registry.dataSources.splice(index, 1);
registry.editor.trigger(types_1.DATA_SOURCE_CHANGED);
}
}
/**
* Get a data source by ID
*/
function getDataSource(id) {
return getRegistry().dataSources.find(ds => ds.id === id);
}
/**
* Set all data sources (replaces existing)
*/
function setDataSources(dataSources) {
const registry = getRegistry();
registry.dataSources = [...dataSources];
registry.editor.trigger(types_1.DATA_SOURCE_CHANGED);
}
/**
* Convert to JSON for storage
*/
function dataSourcesToJSON() {
return getRegistry().dataSources.map(ds => ({
id: ds.id,
label: ds.label,
url: ds.url,
type: ds.type,
method: ds.method,
headers: ds.headers,
readonly: ds.readonly,
hidden: ds.hidden,
}));
}
//# sourceMappingURL=dataSourceRegistry.js.map