2019-12-18 18:18:53 +00:00
|
|
|
const cloneDeep = require('lodash/cloneDeep');
|
2018-04-26 10:20:46 +00:00
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
module.exports = {
|
2019-12-18 18:18:53 +00:00
|
|
|
extend: (base, target, options = {}) => {
|
|
|
|
base = cloneDeep(base);
|
|
|
|
const fieldsToMerge = (target.fields || []).map(df => df.fieldname);
|
|
|
|
const fieldsToRemove = options.skipFields || [];
|
|
|
|
const overrideProps = options.overrideProps || [];
|
|
|
|
for (let prop of overrideProps) {
|
|
|
|
if (base.hasOwnProperty(prop)) {
|
|
|
|
delete base[prop];
|
|
|
|
}
|
|
|
|
}
|
2018-04-26 10:20:46 +00:00
|
|
|
|
2019-12-18 18:18:53 +00:00
|
|
|
let mergeFields = (baseFields, targetFields) => {
|
|
|
|
let fields = cloneDeep(baseFields);
|
|
|
|
fields = fields
|
|
|
|
.filter(df => !fieldsToRemove.includes(df.fieldname))
|
|
|
|
.map(df => {
|
|
|
|
if (fieldsToMerge.includes(df.fieldname)) {
|
|
|
|
let copy = cloneDeep(df);
|
|
|
|
return Object.assign(
|
|
|
|
copy,
|
|
|
|
targetFields.find(tdf => tdf.fieldname === df.fieldname)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return df;
|
|
|
|
});
|
|
|
|
let fieldsAdded = fields.map(df => df.fieldname);
|
|
|
|
let fieldsToAdd = targetFields.filter(
|
|
|
|
df => !fieldsAdded.includes(df.fieldname)
|
|
|
|
);
|
|
|
|
return fields.concat(fieldsToAdd);
|
|
|
|
};
|
2018-04-26 10:20:46 +00:00
|
|
|
|
2019-12-18 18:18:53 +00:00
|
|
|
let fields = mergeFields(base.fields, target.fields || []);
|
|
|
|
let out = Object.assign(base, target);
|
|
|
|
out.fields = fields;
|
2018-04-26 10:20:46 +00:00
|
|
|
|
2019-12-18 18:18:53 +00:00
|
|
|
return out;
|
|
|
|
},
|
|
|
|
commonFields: [
|
|
|
|
{
|
|
|
|
fieldname: 'name',
|
|
|
|
fieldtype: 'Data',
|
|
|
|
required: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
submittableFields: [
|
|
|
|
{
|
|
|
|
fieldname: 'submitted',
|
|
|
|
fieldtype: 'Check',
|
|
|
|
required: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
parentFields: [
|
|
|
|
{
|
|
|
|
fieldname: 'owner',
|
|
|
|
fieldtype: 'Data',
|
|
|
|
required: 1
|
2018-04-26 10:20:46 +00:00
|
|
|
},
|
2019-12-18 18:18:53 +00:00
|
|
|
{
|
|
|
|
fieldname: 'modifiedBy',
|
|
|
|
fieldtype: 'Data',
|
|
|
|
required: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'creation',
|
|
|
|
fieldtype: 'Datetime',
|
|
|
|
required: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'modified',
|
|
|
|
fieldtype: 'Datetime',
|
|
|
|
required: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'keywords',
|
|
|
|
fieldtype: 'Text'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
childFields: [
|
|
|
|
{
|
|
|
|
fieldname: 'idx',
|
|
|
|
fieldtype: 'Int',
|
|
|
|
required: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'parent',
|
|
|
|
fieldtype: 'Data',
|
|
|
|
required: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'parenttype',
|
|
|
|
fieldtype: 'Data',
|
|
|
|
required: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'parentfield',
|
|
|
|
fieldtype: 'Data',
|
|
|
|
required: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
treeFields: [
|
|
|
|
{
|
|
|
|
fieldname: 'lft',
|
|
|
|
fieldtype: 'Int'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'rgt',
|
|
|
|
fieldtype: 'Int'
|
|
|
|
}
|
|
|
|
]
|
2018-04-18 06:48:24 +00:00
|
|
|
};
|