144 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const {
isSortable,
2019-07-24 11:51:35 +02:00
isSearchable,
isVisible,
isRelation,
} = require('./attributes');
2019-07-24 11:51:35 +02:00
const { formatContentTypeSchema } = require('../../ContentTypes');
2019-07-24 11:51:35 +02:00
function createDefaultMetadatas(schema) {
return {
2019-07-24 11:51:35 +02:00
...Object.keys(schema.attributes).reduce((acc, name) => {
acc[name] = createDefaultMetadata(schema, name);
return acc;
}, {}),
id: {
edit: {},
list: {
label: 'Id',
searchable: true,
sortable: true,
},
},
};
}
2019-07-24 11:51:35 +02:00
function createDefaultMetadata(schema, name) {
const edit = {
2019-07-24 11:51:35 +02:00
label: _.upperFirst(name),
description: '',
placeholder: '',
2019-07-24 11:51:35 +02:00
visible: isVisible(schema, name),
editable: true,
};
2019-07-24 11:51:35 +02:00
if (isRelation(schema.attributes[name])) {
edit.mainField = 'id';
}
2019-08-06 14:04:16 +02:00
_.assign(
edit,
_.pick(_.get(schema, ['config', 'metadatas', name, 'edit'], {}), [
'label',
'description',
'placeholder',
'visible',
'editable',
'mainField',
])
);
const list = {
2019-07-24 11:51:35 +02:00
label: _.upperFirst(name),
searchable: isSearchable(schema, name),
sortable: isSortable(schema, name),
..._.pick(_.get(schema, ['config', 'metadatas', name, 'list'], {}), [
'label',
'searchable',
'sortable',
]),
};
return { edit, list };
}
/** Synchronisation functions */
2019-07-24 11:51:35 +02:00
async function syncMetadatas(configuration, schema) {
// clear all keys that do not exist anymore
2019-07-24 11:51:35 +02:00
if (_.isEmpty(configuration.metadatas)) return createDefaultMetadatas(schema);
// remove old keys
const metasWithValidKeys = _.pick(
configuration.metadatas,
2019-07-24 11:51:35 +02:00
Object.keys(schema.attributes)
);
// add new keys and missing fields
const metasWithDefaults = _.merge(
{},
2019-07-24 11:51:35 +02:00
createDefaultMetadatas(schema),
metasWithValidKeys
);
// clear the invalid mainFields
const updatedMetas = Object.keys(metasWithDefaults).reduce((acc, key) => {
2019-07-24 11:51:35 +02:00
const { edit, list } = metasWithDefaults[key];
const attr = schema.attributes[key];
let updatedMeta = { edit, list };
// update sortable attr
2019-07-24 11:51:35 +02:00
if (list.sortable && !isSortable(schema, key)) {
_.set(updatedMeta, ['list', 'sortable'], false);
_.set(acc, [key], updatedMeta);
}
2019-07-24 11:51:35 +02:00
if (list.searchable && !isSearchable(schema, key)) {
_.set(updatedMeta, ['list', 'searchable'], false);
_.set(acc, [key], updatedMeta);
}
if (!_.has(edit, 'mainField')) return acc;
// remove mainField if the attribute is not a relation anymore
2019-07-24 11:51:35 +02:00
if (!isRelation(attr)) {
_.set(updatedMeta, 'edit', _.omit(edit, ['mainField']));
_.set(acc, [key], updatedMeta);
return acc;
}
// if the mainField is id you can keep it
if (edit.mainField === 'id') return acc;
// check the mainField in the targetModel
2019-07-24 11:51:35 +02:00
const targetSchema = getTargetSchema(attr.targetModel, attr.plugin);
if (!targetSchema) return acc;
2019-07-24 11:51:35 +02:00
if (!isSortable(targetSchema, edit.mainField)) {
_.set(updatedMeta, ['edit', 'mainField'], 'id');
_.set(acc, [key], updatedMeta);
return acc;
}
return acc;
}, {});
return _.assign(metasWithDefaults, updatedMetas);
}
2019-07-24 11:51:35 +02:00
const getTargetSchema = (name, plugin) => {
const model = strapi.getModel(name, plugin);
if (!model) return null;
return formatContentTypeSchema(model);
};
module.exports = {
createDefaultMetadatas,
syncMetadatas,
};