2019-07-23 10:54:44 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2019-10-24 14:07:41 +02:00
|
|
|
const NON_SORTABLES = [
|
|
|
|
'component',
|
|
|
|
'json',
|
|
|
|
'relation',
|
|
|
|
'media',
|
|
|
|
'richtext',
|
|
|
|
'dynamiczone',
|
|
|
|
];
|
|
|
|
|
|
|
|
const NON_LISTABLES = [
|
|
|
|
'component',
|
|
|
|
'json',
|
|
|
|
'relation',
|
|
|
|
'password',
|
|
|
|
'richtext',
|
|
|
|
'dynamiczone',
|
|
|
|
];
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isHidden = _.get(
|
|
|
|
schema,
|
|
|
|
['config', 'attributes', name, 'hidden'],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
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,
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|