2019-07-19 18:14:13 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-25 15:28:06 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2019-07-22 09:56:25 +02:00
|
|
|
const storeUtils = require('./utils/store');
|
2019-07-19 18:14:13 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
const uidToStoreKey = uid => `components::${uid}`;
|
2019-07-22 12:15:54 +02:00
|
|
|
|
2019-07-19 18:14:13 +02:00
|
|
|
module.exports = {
|
2019-10-25 15:28:06 +02:00
|
|
|
async getComponentInformations(uid) {
|
|
|
|
const ctService = strapi.plugins['content-manager'].services.contenttypes;
|
|
|
|
|
|
|
|
const component = strapi.components[uid];
|
|
|
|
|
|
|
|
const configurations = await this.getConfiguration(uid);
|
|
|
|
|
|
|
|
return {
|
|
|
|
component: {
|
|
|
|
uid,
|
2019-12-11 12:19:48 +01:00
|
|
|
category: component.category,
|
2019-10-25 15:28:06 +02:00
|
|
|
schema: ctService.formatContentTypeSchema(component),
|
|
|
|
...configurations,
|
|
|
|
},
|
|
|
|
components: await this.getComponentsSchemas(component),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
async getComponentsSchemas(model) {
|
|
|
|
const componentsMap = {};
|
|
|
|
|
|
|
|
for (const key in model.attributes) {
|
|
|
|
const attr = model.attributes[key];
|
|
|
|
|
|
|
|
if (!['component', 'dynamiczone'].includes(attr.type)) continue;
|
|
|
|
|
|
|
|
if (attr.type === 'component') {
|
|
|
|
const compo = strapi.components[attr.component];
|
|
|
|
|
|
|
|
if (_.has(componentsMap, compo.uid)) continue;
|
|
|
|
|
|
|
|
const { component, components } = await this.getComponentInformations(
|
|
|
|
compo.uid
|
|
|
|
);
|
|
|
|
|
|
|
|
Object.assign(componentsMap, {
|
|
|
|
[compo.uid]: component,
|
|
|
|
...components,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.type === 'dynamiczone') {
|
|
|
|
const componentKeys = attr.components;
|
|
|
|
|
|
|
|
for (const componentKey of componentKeys) {
|
|
|
|
const compo = strapi.components[componentKey];
|
|
|
|
|
|
|
|
if (_.has(componentsMap, compo.uid)) continue;
|
|
|
|
|
|
|
|
const { component, components } = await this.getComponentInformations(
|
|
|
|
compo.uid
|
|
|
|
);
|
|
|
|
|
|
|
|
Object.assign(componentsMap, {
|
|
|
|
[compo.uid]: component,
|
|
|
|
...components,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return componentsMap;
|
|
|
|
},
|
|
|
|
|
2019-07-22 12:15:54 +02:00
|
|
|
getConfiguration(uid) {
|
|
|
|
const storeKey = uidToStoreKey(uid);
|
2019-07-22 09:56:25 +02:00
|
|
|
return storeUtils.getModelConfiguration(storeKey);
|
2019-07-19 18:14:13 +02:00
|
|
|
},
|
|
|
|
|
2019-07-22 12:15:54 +02:00
|
|
|
setConfiguration(uid, input) {
|
2019-07-19 18:14:13 +02:00
|
|
|
const { settings, metadatas, layouts } = input;
|
|
|
|
|
2019-07-22 12:15:54 +02:00
|
|
|
const storeKey = uidToStoreKey(uid);
|
2019-07-22 09:56:25 +02:00
|
|
|
return storeUtils.setModelConfiguration(storeKey, {
|
|
|
|
uid,
|
2019-10-22 18:01:03 +02:00
|
|
|
isComponent: true,
|
2019-07-22 09:56:25 +02:00
|
|
|
settings,
|
|
|
|
metadatas,
|
|
|
|
layouts,
|
|
|
|
});
|
2019-07-19 18:14:13 +02:00
|
|
|
},
|
|
|
|
|
2019-07-22 12:15:54 +02:00
|
|
|
deleteConfiguration(uid) {
|
|
|
|
const storeKey = uidToStoreKey(uid);
|
|
|
|
return storeUtils.deleteKey(storeKey);
|
|
|
|
},
|
2019-07-19 18:14:13 +02:00
|
|
|
};
|