@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
81 lines (80 loc) • 2.84 kB
JavaScript
import { ApiBase } from '../Implementation/ApiBase';
export class DataSetInternalApi extends ApiBase {
handleDataSetSelected(dataSet) {
if (!dataSet.form) {
this.getEventApi().internalApi.fireDataSetSelectedEvent(dataSet);
const dataSetSelectedInfo = {
...this.getAdaptableInternalApi().buildBaseContext(),
dataSet,
};
this.runDataSetHandler(async () => {
if (dataSet.loadData) {
const rowData = await dataSet.loadData(dataSetSelectedInfo);
if (rowData != null) {
this.getGridApi().loadGridData(rowData);
}
}
if (dataSet.onSelect) {
await dataSet.onSelect(dataSetSelectedInfo);
}
}, 'selection', dataSet.name);
}
}
showDataSetForm(dataSet) {
if (!dataSet.form) {
return;
}
this.getAdaptableInternalApi().showPopupForm({
Id: 'data-set-form',
Form: this.buildDataSetForm(dataSet),
prepareContext: (context) => {
return new Promise((resolve) => {
const preparedContext = {
...context,
dataSet,
};
resolve(preparedContext);
});
},
});
}
buildDataSetForm(dataSet) {
const sourceForm = dataSet.form;
if (!dataSet.onFormSubmit) {
return sourceForm;
}
const invokeOnFormSubmit = (context) => {
this.runDataSetHandler(() => dataSet.onFormSubmit(context), 'onFormSubmit', dataSet.name);
};
const onSubmit = (formData, context) => {
sourceForm.onSubmit?.(formData, context);
invokeOnFormSubmit({ ...context, formData, dataSet });
};
const buttons = sourceForm.buttons?.length > 0
? sourceForm.buttons
: [
{
label: 'OK',
onClick: (_, context) => invokeOnFormSubmit({ ...context, dataSet }),
},
];
return {
...sourceForm,
onSubmit,
buttons,
};
}
runDataSetHandler(handler, handlerName, dataSetName) {
try {
const result = handler();
if (result != null && typeof result.then === 'function') {
result.catch((error) => {
this.logError(`DataSet '${dataSetName}' ${handlerName} handler failed`, error);
});
}
}
catch (error) {
this.logError(`DataSet '${dataSetName}' ${handlerName} handler failed`, error);
}
}
}