2020-10-22 16:43:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-07-29 14:39:28 +02:00
|
|
|
const { upperFirst, prop, pick, getOr } = require('lodash/fp');
|
2020-10-22 16:43:51 +02:00
|
|
|
const pluralize = require('pluralize');
|
2021-04-29 13:51:12 +02:00
|
|
|
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
|
2020-10-22 16:43:51 +02:00
|
|
|
|
|
|
|
const dtoFields = [
|
|
|
|
'uid',
|
|
|
|
'isDisplayed',
|
|
|
|
'apiID',
|
|
|
|
'kind',
|
|
|
|
'category',
|
|
|
|
'info',
|
|
|
|
'options',
|
2021-03-04 17:13:08 +01:00
|
|
|
'pluginOptions',
|
2020-10-22 16:43:51 +02:00
|
|
|
'attributes',
|
2021-03-08 11:02:01 +01:00
|
|
|
'pluginOptions',
|
2020-10-22 16:43:51 +02:00
|
|
|
];
|
|
|
|
|
2021-07-13 18:46:36 +02:00
|
|
|
module.exports = () => ({
|
2020-10-22 16:43:51 +02:00
|
|
|
toContentManagerModel(contentType) {
|
|
|
|
return {
|
|
|
|
...contentType,
|
2021-06-30 22:52:12 +02:00
|
|
|
options: {
|
|
|
|
...contentType.options,
|
|
|
|
timestamps: [],
|
|
|
|
},
|
2020-10-22 16:43:51 +02:00
|
|
|
apiID: contentType.modelName,
|
2021-02-10 14:29:54 +01:00
|
|
|
isDisplayed: isVisible(contentType),
|
2020-10-22 16:43:51 +02:00
|
|
|
info: {
|
|
|
|
...contentType.info,
|
|
|
|
label: formatContentTypeLabel(contentType),
|
|
|
|
},
|
|
|
|
attributes: {
|
|
|
|
id: {
|
2021-06-30 22:52:12 +02:00
|
|
|
type: 'integer',
|
2020-10-22 16:43:51 +02:00
|
|
|
},
|
|
|
|
...formatAttributes(contentType),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
toDto: pick(dtoFields),
|
2021-07-13 18:46:36 +02:00
|
|
|
});
|
2020-10-22 16:43:51 +02:00
|
|
|
|
|
|
|
const formatContentTypeLabel = contentType => {
|
|
|
|
const name = prop('info.name', contentType) || contentType.modelName;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return contentTypesUtils.isSingleType(contentType)
|
|
|
|
? upperFirst(name)
|
|
|
|
: upperFirst(pluralize(name));
|
|
|
|
} catch (error) {
|
|
|
|
// in case pluralize throws cyrillic characters
|
|
|
|
return upperFirst(name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-29 14:39:28 +02:00
|
|
|
const formatAttributes = contentType => {
|
2021-03-12 14:34:04 +01:00
|
|
|
const { getVisibleAttributes } = contentTypesUtils;
|
2020-10-22 16:43:51 +02:00
|
|
|
|
2021-03-12 14:34:04 +01:00
|
|
|
// only get attributes that can be seen in the auto generated Edit view or List view
|
2021-07-29 14:39:28 +02:00
|
|
|
return getVisibleAttributes(contentType).reduce((acc, key) => {
|
|
|
|
const attribute = contentType.attributes[key];
|
|
|
|
|
|
|
|
// ignore morph until they are handled in the front
|
|
|
|
if (attribute.type === 'relation' && attribute.relation.toLowerCase().includes('morph')) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[key] = formatAttribute(key, attribute);
|
2020-10-22 16:43:51 +02:00
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
2021-06-22 17:13:11 +02:00
|
|
|
// FIXME: not needed
|
2021-07-29 14:39:28 +02:00
|
|
|
const formatAttribute = (key, attribute) => {
|
2021-06-30 22:52:12 +02:00
|
|
|
if (attribute.type === 'relation') {
|
|
|
|
return toRelation(attribute);
|
2020-10-22 16:43:51 +02:00
|
|
|
}
|
|
|
|
|
2021-06-30 22:52:12 +02:00
|
|
|
return attribute;
|
2020-10-22 16:43:51 +02:00
|
|
|
};
|
|
|
|
|
2021-06-22 17:13:11 +02:00
|
|
|
// FIXME: not needed
|
2021-06-30 22:52:12 +02:00
|
|
|
const toRelation = attribute => {
|
2020-10-22 16:43:51 +02:00
|
|
|
return {
|
|
|
|
...attribute,
|
|
|
|
type: 'relation',
|
2021-06-30 22:52:12 +02:00
|
|
|
targetModel: attribute.target,
|
|
|
|
relationType: attribute.relation,
|
2020-10-22 16:43:51 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-10 14:29:54 +01:00
|
|
|
const isVisible = model => getOr(true, 'pluginOptions.content-manager.visible', model) === true;
|