2020-10-22 16:43:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-02-10 14:29:54 +01:00
|
|
|
const { upperFirst, has, prop, pick, getOr } = require('lodash/fp');
|
2020-10-22 16:43:51 +02:00
|
|
|
const pluralize = require('pluralize');
|
|
|
|
const { contentTypes: contentTypesUtils } = require('strapi-utils');
|
2021-04-02 10:24:44 +02:00
|
|
|
const { isMediaAttribute } = require('strapi-utils').contentTypes;
|
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
|
|
|
];
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
toContentManagerModel(contentType) {
|
|
|
|
return {
|
|
|
|
...contentType,
|
|
|
|
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: {
|
|
|
|
type: contentType.primaryKeyType,
|
|
|
|
},
|
|
|
|
...formatAttributes(contentType),
|
|
|
|
...contentTypesUtils.getTimestampsAttributes(contentType),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
toDto: pick(dtoFields),
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatAttributes = model => {
|
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
|
|
|
|
return getVisibleAttributes(model).reduce((acc, key) => {
|
2020-10-22 16:43:51 +02:00
|
|
|
acc[key] = formatAttribute(key, model.attributes[key], { model });
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatAttribute = (key, attribute, { model }) => {
|
|
|
|
if (has('type', attribute)) return attribute;
|
|
|
|
|
2021-04-02 10:24:44 +02:00
|
|
|
if (isMediaAttribute(attribute)) {
|
2020-10-22 16:43:51 +02:00
|
|
|
return toMedia(attribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
const relation = (model.associations || []).find(assoc => assoc.alias === key);
|
|
|
|
return toRelation(attribute, relation);
|
|
|
|
};
|
|
|
|
|
|
|
|
const toMedia = attribute => {
|
|
|
|
return {
|
|
|
|
type: 'media',
|
|
|
|
multiple: attribute.collection ? true : false,
|
|
|
|
required: attribute.required ? true : false,
|
|
|
|
allowedTypes: attribute.allowedTypes,
|
2021-04-02 10:24:44 +02:00
|
|
|
pluginOptions: attribute.pluginOptions,
|
2020-10-22 16:43:51 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const toRelation = (attribute, relation) => {
|
|
|
|
return {
|
|
|
|
...attribute,
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: relation.targetUid,
|
|
|
|
relationType: relation.nature,
|
2021-04-02 10:24:44 +02:00
|
|
|
pluginOptions: attribute.pluginOptions,
|
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;
|