budibase/packages/bootstrap-components/dist/generators.js

446 lines
44 KiB
JavaScript
Raw Normal View History

2019-10-18 18:32:03 +02:00
const buttons = () => [
{
name: "common/Primary Button",
description: "Bootstrap primary button ",
inherits: "@budibase/standard-components/button",
props: {
className: "btn btn-primary",
2019-10-18 18:32:03 +02:00
},
},
{
name: "common/Default Button",
description: "Bootstrap default button",
inherits: "@budibase/standard-components/button",
props: {
className: "btn btn-secondary",
},
},
];
2019-10-14 09:32:20 +02:00
const forms = ({ records, indexes, helpers }) => [
...records.map(root),
...buttons(),
];
2019-10-18 18:32:03 +02:00
const formName = record => `${record.name}/${record.name} Form`;
2019-10-18 18:32:03 +02:00
const root = record => ({
name: formName(record),
description: `Control for creating/updating '${record.nodeKey()}' `,
inherits: "@budibase/standard-components/div",
props: {
className: "p-1",
children: [
{
component: {
_component: "@budibase/standard-components/h3",
text: `Edit ${record.name}`,
},
},
form(record),
saveCancelButtons(record),
],
},
});
2019-10-18 18:32:03 +02:00
const form = record => ({
component: {
_component: "@budibase/standard-components/form",
formControls: record.fields.map(f => formControl(record, f)),
},
});
2019-10-18 18:32:03 +02:00
const formControl = (record, field) => {
if (
field.type === "string" &&
field.typeOptions.values &&
field.typeOptions.values.length > 0
) {
return {
control: {
_component: "@budibase/standard-components/select",
options: field.typeOptions.values.map(v => ({ id: v, value: v })),
value: {
"##bbstate": `${record.name}.${field.name}`,
"##bbsource": "store",
},
className: "form-control",
},
label: field.label,
2019-10-18 18:32:03 +02:00
}
} else {
return {
control: {
_component: "@budibase/standard-components/input",
value: {
"##bbstate": `${record.name}.${field.name}`,
"##bbsource": "store",
},
className: "form-control",
type:
field.type === "string"
? "text"
: field.type === "datetime"
? "date"
: field.type === "number"
? "number"
: "text",
},
label: field.label,
2019-10-18 18:32:03 +02:00
}
}
};
const saveCancelButtons = record => ({
component: {
_component: "@budibase/standard-components/stackpanel",
direction: "horizontal",
children: [
paddedPanelForButton({
_component: "common/Primary Button",
contentText: `Save ${record.name}`,
onClick: [
{
"##eventHandlerType": "Save Record",
parameters: {
statePath: `${record.name}`,
},
},
{
"##eventHandlerType": "Set State",
parameters: {
path: `isEditing${record.name}`,
value: "",
},
},
],
}),
paddedPanelForButton({
_component: "common/Default Button",
contentText: `Cancel`,
onClick: [
{
2019-10-18 18:32:03 +02:00
"##eventHandlerType": "Set State",
parameters: {
path: `isEditing${record.name}`,
value: "",
2019-10-18 18:32:03 +02:00
},
},
],
}),
],
},
});
const paddedPanelForButton = button => ({
control: {
_component: "@budibase/standard-components/div",
className: "btn-group",
children: [
{
component: button,
},
],
},
});
const getRecordPath = () => {
const parts = [];
return parts.reverse().join("/")
};
const indexTables = ({ indexes, helpers }) =>
indexes.map(i => indexTable(i, helpers));
const excludedColumns = ["id", "isNew", "key", "type", "sortKey"];
const indexTableProps = (index, helpers) => ({
data: {
"##bbstate": index.nodeKey(),
"##bbsource": "store",
},
tableClass: "table table-hover",
theadClass: "thead-dark",
columns: helpers
.indexSchema(index)
.filter(c => !excludedColumns.includes(c.name))
.map(column),
onRowClick: [
{
"##eventHandlerType": "Set State",
parameters: {
path: `selectedrow_${index.name}`,
value: {
"##bbstate": "key",
"##bbsource": "event",
},
},
},
],
});
2019-10-18 18:32:03 +02:00
const getIndexTableName = (index, record) => {
record = record || index.parent().type === "record" ? index.parent() : null;
return record
? `${getRecordPath()}/${index.name} Table`
: `${index.name} Table`
};
2019-10-18 18:32:03 +02:00
const indexTable = (index, helpers) => ({
name: getIndexTableName(index),
inherits: "@budibase/standard-components/table",
props: indexTableProps(index, helpers),
});
const column = col => ({
title: col.name,
value: {
"##bbstate": col.name,
"##bbsource": "context",
},
});
2019-10-18 18:32:03 +02:00
const recordHomePageComponents = ({ indexes, records, helpers }) => [
...recordHomepages({ indexes, records }).map(component),
2019-10-18 18:32:03 +02:00
...recordHomepages({ indexes, records }).map(homePageButtons),
2019-10-18 18:32:03 +02:00
...indexTables({ indexes, records, helpers }),
2019-10-18 18:32:03 +02:00
...buttons(),
];
2019-10-18 18:32:03 +02:00
const findIndexForRecord = (indexes, record) => {
const forRecord = indexes.filter(i =>
i.allowedRecordNodeIds.includes(record.nodeId)
);
if (forRecord.length === 0) return
if (forRecord.length === 1) return forRecord[0]
const noMap = forRecord.filter(i => !i.filter || !i.filter.trim());
if (noMap.length === 0) forRecord[0];
return noMap[0]
};
const recordHomepages = ({ indexes, records }) =>
records
.filter(r => r.parent().type === "root")
.map(r => ({
record: r,
index: findIndexForRecord(indexes, r),
}))
.filter(r => r.index);
const homepageComponentName = record =>
`${record.name}/${record.name} homepage`;
const component = ({ record, index }) => ({
inherits: "@budibase/standard-components/div",
name: homepageComponentName(record),
props: {
className: "d-flex flex-column h-100",
children: [
{
component: {
_component: `${record.name}/homepage buttons`,
},
},
{
component: {
_component: getIndexTableName(index),
},
className: "flex-gow-1 overflow-auto",
},
],
onLoad: [
{
"##eventHandlerType": "Set State",
parameters: {
path: `isEditing${record.name}`,
value: "",
},
},
{
"##eventHandlerType": "List Records",
parameters: {
statePath: index.nodeKey(),
indexKey: index.nodeKey(),
},
},
],
},
});
const homePageButtons = ({ index, record }) => ({
inherits: "@budibase/standard-components/div",
name: `${record.name}/homepage buttons`,
props: {
className: "btn-toolbar mt-4 mb-2",
children: [
{
component: {
_component: "@budibase/standard-components/div",
className: "btn-group mr-3",
children: [
2019-10-18 18:32:03 +02:00
{
component: {
_component: "common/Default Button",
contentText: `Create ${record.name}`,
onClick: [
{
"##eventHandlerType": "Get New Record",
parameters: {
statePath: record.name,
collectionKey: `/${record.collectionName}`,
childRecordType: record.name,
},
},
{
"##eventHandlerType": "Set State",
parameters: {
path: `isEditing${record.name}`,
value: "true",
},
},
],
},
2019-10-18 18:32:03 +02:00
},
{
component: {
_component: "common/Default Button",
contentText: `Refresh`,
onClick: [
{
"##eventHandlerType": "List Records",
parameters: {
statePath: index.nodeKey(),
indexKey: index.nodeKey(),
},
},
],
},
2019-10-18 18:32:03 +02:00
},
],
},
},
{
component: {
_component: "@budibase/standard-components/if",
condition: `$store.selectedrow_${index.name} && $store.selectedrow_${index.name}.length > 0`,
thenComponent: {
_component: "@budibase/standard-components/div",
className: "btn-group",
children: [
{
2019-10-18 18:32:03 +02:00
component: {
_component: "common/Default Button",
contentText: `Edit ${record.name}`,
onClick: [
{
"##eventHandlerType": "Load Record",
parameters: {
statePath: record.name,
recordKey: {
"##bbstate": `selectedrow_${index.name}`,
"##source": "store",
2019-10-19 08:24:20 +02:00
},
},
},
{
"##eventHandlerType": "Set State",
parameters: {
path: `isEditing${record.name}`,
value: "true",
},
},
],
},
},
{
2019-10-18 18:32:03 +02:00
component: {
_component: "common/Default Button",
contentText: `Delete ${record.name}`,
onClick: [
{
"##eventHandlerType": "Delete Record",
parameters: {
recordKey: {
"##bbstate": `selectedrow_${index.name}`,
"##source": "store",
},
},
},
],
},
},
],
},
},
},
],
},
});
2019-10-18 18:32:03 +02:00
const selectNavContent = ({ indexes, records, helpers }) => [
...recordHomepages({ indexes, records }).map(component$1),
2019-10-18 18:32:03 +02:00
...recordHomePageComponents({ indexes, records, helpers }),
2019-10-18 18:32:03 +02:00
...forms({ indexes, records, helpers }),
];
2019-10-18 18:32:03 +02:00
const navContentComponentName = record =>
`${record.name}/${record.name} Nav Content`;
const component$1 = ({ record }) => ({
inherits: "@budibase/standard-components/if",
description: `the component that gets displayed when the ${record.collectionName} nav is selected`,
name: navContentComponentName(record),
props: {
condition: `$store.isEditing${record.name}`,
thenComponent: {
_component: formName(record),
2019-10-18 18:32:03 +02:00
},
elseComponent: {
_component: homepageComponentName(record),
2019-10-18 18:32:03 +02:00
},
},
});
2019-10-18 18:32:03 +02:00
const app = ({ records, indexes, helpers }) => [
{
name: "Application Root",
inherits: "@budibase/bootstrap-components/nav",
props: {
items: recordHomepages({ indexes, records }).map(navItem),
orientation: "horizontal",
alignment: "start",
fill: false,
pills: true,
selectedItem: {
"##bbstate": "selectedNav",
"##bbstatefallback": `${records[0].name}`,
"##bbsource": "store",
},
className: "p-3",
},
},
{
name: "Login",
inherits: "@budibase/standard-components/login",
props: {},
},
...selectNavContent({ records, indexes, helpers }),
];
const navItem = ({ record }) => ({
title: record.collectionName,
component: {
_component: navContentComponentName(record),
},
});
export { app, forms, indexTables, recordHomePageComponents as recordHomepages };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhdG9ycy5qcyIsInNvdXJjZXMiOlsiLi4vc3JjL2dlbmVyYXRvcnMvYnV0dG9uR2VuZXJhdG9ycy5qcyIsIi4uL3NyYy9nZW5lcmF0b3JzL2Zvcm1zR2VuZXJhdG9yLmpzIiwiLi4vc3JjL2dlbmVyYXRvcnMvZ2V0UmVjb3JkUGF0aC5qcyIsIi4uL3NyYy9nZW5lcmF0b3JzL2luZGV4VGFibGVzR2VuZXJhdG9yLmpzIiwiLi4vc3JjL2dlbmVyYXRvcnMvcmVjb3JkSG9tZVBhZ2VHZW5lcmF0b3IuanMiLCIuLi9zcmMvZ2VuZXJhdG9ycy9zZWxlY3RlZE5hdkNvbnRlbnRHZW5lcmF0b3IuanMiLCIuLi9zcmMvZ2VuZXJhdG9ycy9hcHBHZW5lcmF0b3IuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGNvbnN0IGJ1dHRvbnMgPSAoKSA9PiBbXG4gIHtcbiAgICBuYW1lOiBcImNvbW1vbi9QcmltYXJ5IEJ1dHRvblwiLFxuICAgIGRlc2NyaXB0aW9uOiBcIkJvb3RzdHJhcCBwcmltYXJ5IGJ1dHRvbiBcIixcbiAgICBpbmhlcml0czogXCJAYnVkaWJhc2Uvc3RhbmRhcmQtY29tcG9uZW50cy9idXR0b25cIixcbiAgICBwcm9wczoge1xuICAgICAgY2xhc3NOYW1lOiBcImJ0biBidG4tcHJpbWFyeVwiLFxuICAgIH0sXG4gIH0sXG4gIHtcbiAgICBuYW1lOiBcImNvbW1vbi9EZWZhdWx0IEJ1dHRvblwiLFxuICAgIGRlc2NyaXB0aW9uOiBcIkJvb3RzdHJhcCB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nQ6IFwiQGJ1ZGliYXNlL3N0YW5kYXJkLWNvbXBvbmVudHMvaW5wdXRcIixcbiAgICAgICAgdmFsdWU6IHtcbiAgICAgICAgICBcIiMjYmJzdGF0ZVwiOiBgJHtyZWNvcmQubmFtZX0uJHtmaWVsZC5uYW1lfWAsXG4gICAgICAgICAgXCIjI2Jic291cmNlXCI6IFwic3RvcmVcIixcbiAgICAgICAgfSxcbiAgICAgICAgY2xhc3NOYW1lOiBcImZvcm0tY29udHJvbFwiLFxuICAgICAgICB0eXBlOlxuICAgICAgICAgIGZpZWxkLnR5cGUgPT09IFwic3RyaW5nXCJcbiAgICAgICAgICAgID8gXCJ0ZXh0XCJcbiAgICAgICAgICAgIDogZmllbGQudHlwZSA9PT0gXCJkYXRldGltZVwiXG4gICAgICAgICAgICA/IFwiZGF0ZVwiXG4gICAgICAgICAgICA6IGZpZWxkLnR5cGUgPT09IFwibnVtYmVyXCJcbiAgICAgICAgICAgID8gXCJudW1iZXJcIlxuICAgICAgICAgICAgOiBcInRleHRcIixcbiAgICAgIH0sXG4gICAgICBsYWJlbDogZmllbGQubGFiZWwsXG4gICAgfVxuICB9XG59XG5cbmNvbnN0IHNhdmVDYW5jZWxCdXR0b25zID0gcmVjb3JkID0+ICh7XG4gIGNvbXBvbmVudDoge1xuICAgIF9jb21wb25lbnQ6IFwiQGJ1ZGliYXNlL3N0YW5kYXJkLWNvbXBvbmVudHMvc3RhY2twYW5lbFwiLFxuICAgIGRpcmVjdGlvbjogXCJob3Jpem9udGFsXCIsXG4gICAgY2hpbGRyZW46IFtcbiAgICAgIHBhZGRlZFBhbmVsRm9yQnV