2019-09-05 16:59:02 +02:00

107 lines
2.0 KiB
JavaScript

'use strict';
const _ = require('lodash');
const NON_SORTABLES = ['group', 'json', 'relation', 'media'];
const NON_LISTABLES = ['group', 'json', 'relation', 'password'];
const isListable = (schema, name) => {
if (!_.has(schema.attributes, name)) {
return false;
}
const attribute = schema.attributes[name];
if (NON_LISTABLES.includes(attribute.type)) {
return false;
}
return true;
};
const isSortable = (schema, name) => {
if (!_.has(schema.attributes, name)) {
return false;
}
if (schema.modelType === 'group' && name === 'id') return false;
const attribute = schema.attributes[name];
if (NON_SORTABLES.includes(attribute.type)) {
return false;
}
return true;
};
const isSearchable = (schema, name) => {
return isSortable(schema, name);
};
const isVisible = (schema, name) => {
if (!_.has(schema.attributes, name)) {
return false;
}
if (isTimestamp(schema, name) || name === 'id') {
return false;
}
return true;
};
const isTimestamp = (schema, name) => {
if (!_.has(schema.attributes, name)) {
return false;
}
const timestampsOpt = _.get(schema, ['options', 'timestamps']);
if (!timestampsOpt || !Array.isArray(timestampsOpt)) {
return false;
}
if (timestampsOpt.includes(name)) {
return true;
}
};
const isRelation = attribute => attribute.type === 'relation';
const hasRelationAttribute = (schema, name) => {
if (!_.has(schema.attributes, name)) {
return false;
}
if (!isVisible(schema, name)) {
return false;
}
return isRelation(schema.attributes[name]);
};
const hasEditableAttribute = (schema, name) => {
if (!_.has(schema.attributes, name)) {
return false;
}
if (!isVisible(schema, name)) {
return false;
}
if (isRelation(schema.attributes[name])) {
if (schema.modelType === 'group') return true;
return false;
}
return true;
};
module.exports = {
isSortable,
isVisible,
isSearchable,
isRelation,
isListable,
hasEditableAttribute,
hasRelationAttribute,
};