2019-07-23 10:54:44 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2020-08-18 17:09:21 +02:00
|
|
|
const { contentTypes: contentTypesUtils } = require('strapi-utils');
|
2020-10-27 11:27:17 +01:00
|
|
|
|
2020-08-18 17:09:21 +02:00
|
|
|
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
2019-07-23 10:54:44 +02:00
|
|
|
|
2021-01-14 12:16:47 +01:00
|
|
|
const NON_SORTABLES = ['component', 'json', 'media', 'richtext', 'dynamiczone'];
|
2020-12-02 15:28:58 +01:00
|
|
|
const SORTABLE_RELATIONS = ['oneWay', 'oneToOne', 'manyToOne'];
|
|
|
|
|
|
|
|
const NON_LISTABLES = ['component', 'json', 'password', 'richtext', 'dynamiczone'];
|
|
|
|
const LISTABLE_RELATIONS = [
|
|
|
|
'oneWay',
|
|
|
|
'oneToOne',
|
|
|
|
'oneToMany',
|
|
|
|
'manyToOne',
|
|
|
|
'manyToMany',
|
|
|
|
'manyWay',
|
|
|
|
];
|
2019-07-25 08:02:10 +02:00
|
|
|
|
2019-12-17 12:33:56 +01:00
|
|
|
// hidden fields are fields that are configured to be hidden from list, and edit views
|
|
|
|
const isHidden = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:09:21 +02:00
|
|
|
const isHidden = _.get(schema, ['config', 'attributes', name, 'hidden'], false);
|
2019-12-17 12:33:56 +01:00
|
|
|
if (isHidden === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-08-05 16:39:14 +02:00
|
|
|
const isListable = (schema, name) => {
|
2019-09-05 16:59:02 +02:00
|
|
|
if (!_.has(schema.attributes, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:33:56 +01:00
|
|
|
if (isHidden(schema, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:59:02 +02:00
|
|
|
const attribute = schema.attributes[name];
|
|
|
|
if (NON_LISTABLES.includes(attribute.type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:28:58 +01:00
|
|
|
if (isRelation(attribute) && !LISTABLE_RELATIONS.includes(attribute.relationType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:59:02 +02:00
|
|
|
return true;
|
2019-08-05 16:39:14 +02:00
|
|
|
};
|
2019-07-25 08:02:10 +02:00
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isSortable = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
if (schema.modelType === 'component' && name === 'id') return false;
|
2019-07-30 16:12:14 +02:00
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const attribute = schema.attributes[name];
|
2019-07-23 10:54:44 +02:00
|
|
|
if (NON_SORTABLES.includes(attribute.type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:28:58 +01:00
|
|
|
if (isRelation(attribute) && !SORTABLE_RELATIONS.includes(attribute.relationType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isSearchable = (schema, name) => {
|
|
|
|
return isSortable(schema, name);
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isVisible = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:33:56 +01:00
|
|
|
if (isHidden(schema, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
if (isTimestamp(schema, name) || name === 'id') {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:09:21 +02:00
|
|
|
if (isPublicationField(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-08-18 17:09:21 +02:00
|
|
|
const isPublicationField = name => {
|
|
|
|
return PUBLISHED_AT_ATTRIBUTE === name;
|
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isTimestamp = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const timestampsOpt = _.get(schema, ['options', 'timestamps']);
|
|
|
|
if (!timestampsOpt || !Array.isArray(timestampsOpt)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
if (timestampsOpt.includes(name)) {
|
|
|
|
return true;
|
2019-07-23 10:54:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isRelation = attribute => attribute.type === 'relation';
|
|
|
|
|
2019-07-25 08:02:10 +02:00
|
|
|
const hasRelationAttribute = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:33:56 +01:00
|
|
|
if (isHidden(schema, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-05 16:39:14 +02:00
|
|
|
if (!isVisible(schema, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-25 08:02:10 +02:00
|
|
|
return isRelation(schema.attributes[name]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasEditableAttribute = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:33:56 +01:00
|
|
|
if (isHidden(schema, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-25 08:02:10 +02:00
|
|
|
if (!isVisible(schema, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isRelation(schema.attributes[name])) {
|
2019-10-22 18:01:03 +02:00
|
|
|
if (schema.modelType === 'component') return true;
|
2019-07-25 08:02:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-10-22 16:43:51 +02:00
|
|
|
const findFirstStringAttribute = schema => {
|
|
|
|
return Object.keys(schema.attributes || {}).find(key => {
|
|
|
|
const { type } = schema.attributes[key];
|
|
|
|
return type === 'string' && key !== 'id';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDefaultMainField = schema => findFirstStringAttribute(schema) || 'id';
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
module.exports = {
|
|
|
|
isSortable,
|
2019-07-24 11:51:35 +02:00
|
|
|
isVisible,
|
|
|
|
isSearchable,
|
|
|
|
isRelation,
|
2019-07-25 08:02:10 +02:00
|
|
|
isListable,
|
|
|
|
hasEditableAttribute,
|
|
|
|
hasRelationAttribute,
|
2020-10-22 16:43:51 +02:00
|
|
|
getDefaultMainField,
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|