primevue-mirror/layouts/doc/DocApiSection.vue

352 lines
12 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<div className="doc-main">
<div className="doc-intro">
<h1>{{ header }} API</h1>
<p>{{ description }}</p>
2023-02-28 08:29:30 +00:00
</div>
2023-03-06 10:59:56 +00:00
<DocSections :docs="docs" />
2023-02-28 08:29:30 +00:00
</div>
<DocSectionNav :docs="docs" />
</template>
<script>
import APIDocs from '@/doc/common/apidoc/index.json';
import DocApiTable from './DocApiTable.vue';
export default {
props: {
header: {
type: String,
default: ''
},
doc: {
type: Array,
default: () => []
}
},
data() {
return {
docs: []
};
},
mounted() {
this.docs = this.createDocs();
},
methods: {
createDocs() {
const newDocs = [];
for (const docName of this.doc) {
const moduleName = docName.toLowerCase();
const regex = /\[Live Demo][\s\S]*/g;
let newDoc = {
id: `api.${moduleName}`,
label: docName,
description: APIDocs[moduleName]?.description.replace(regex, '') || null,
2023-03-06 10:59:56 +00:00
children: [],
docName: docName
2023-02-28 08:29:30 +00:00
};
2023-03-07 13:37:45 +00:00
const values = APIDocs[moduleName]?.interfaces?.values;
const componentValues = APIDocs[moduleName]?.components;
2023-03-07 13:37:45 +00:00
const modelValues = APIDocs[moduleName]?.model;
let props = null;
let emits = null;
let slots = null;
let events = null;
let options = null;
let interfaces = null;
if (values) {
props = values[`${docName}Props`];
emits = values[`${docName}Emits`];
slots = values[`${docName}Slots`];
events = this.findEvents(values);
options = this.findOptions(values, docName); // MenuItem && ConfirmationOptions
interfaces = this.findOtherInterfaces(values, docName);
}
2023-02-28 08:29:30 +00:00
2023-03-01 11:08:29 +00:00
const methods = componentValues ? componentValues['default'].methods : null;
2023-02-28 08:29:30 +00:00
const types = APIDocs[moduleName]['types'];
2023-03-07 13:37:45 +00:00
const services = modelValues; // (TerminalService && ConfirmationService && ToastService)
2023-03-03 07:05:18 +00:00
2023-02-28 08:29:30 +00:00
if (props && props.props.length) {
newDoc.children.push({
id: `api.${moduleName}.props`,
label: 'Props',
component: DocApiTable,
data: this.setPropsData(props.props),
description: props.description
});
}
if (emits && emits.methods.length) {
newDoc.children.push({
id: `api.${moduleName}.emits`,
label: 'Emits',
component: DocApiTable,
data: this.setEmitData(emits.methods),
description: emits.description
});
}
if (slots && slots.methods.length > 0) {
2023-02-28 08:29:30 +00:00
newDoc.children.push({
id: `api.${moduleName}.slots`,
label: 'Slots',
component: DocApiTable,
data: this.setEmitData(slots.methods),
description: slots.description
});
}
if (methods && methods.values.length > 0) {
2023-02-28 08:29:30 +00:00
newDoc.children.push({
id: `api.${moduleName}.methods`,
label: 'Methods',
component: DocApiTable,
data: this.setEmitData(methods.values),
2023-02-28 08:29:30 +00:00
description: APIDocs[moduleName].interfaces.methodDescription
});
}
2023-03-07 13:37:45 +00:00
if (events && events.length > 0) {
2023-02-28 08:29:30 +00:00
newDoc.children.push({
id: `api.${moduleName}.events`,
label: 'Events',
component: DocApiTable,
data: this.setEventsData(moduleName, events),
description: APIDocs[moduleName].interfaces.eventDescription
});
}
if (interfaces && interfaces.length > 0) {
2023-03-03 07:05:18 +00:00
const isValidDirective = this.checkDirectiveInterfaces(interfaces);
if (!isValidDirective) return;
2023-02-28 08:29:30 +00:00
newDoc.children.push({
id: `api.${moduleName}.interfaces`,
label: 'Interfaces',
component: DocApiTable,
data: this.setInterfacesData(moduleName, interfaces),
description: APIDocs[moduleName].interfaces.description || null
});
}
if (types) {
newDoc.children.push({
id: `api.${moduleName}.types`,
label: 'Types',
component: DocApiTable,
data: this.setTypesData(moduleName, types.values),
description: APIDocs[moduleName].interfaces.typeDescription || null
});
}
2023-03-03 07:05:18 +00:00
if (options) {
newDoc.children.push({
2023-03-12 11:06:00 +00:00
id: `api.options.${options[0].key}`,
2023-03-03 07:05:18 +00:00
label: 'Options',
component: DocApiTable,
data: this.setPropsData(options[0].values.props)
});
}
2023-03-07 13:37:45 +00:00
if (services) {
newDoc.children.push({
id: `api.${moduleName}.services`,
label: 'Services',
component: DocApiTable,
data: this.setServicesData(moduleName, services),
description: Object.values(services)[0].description || null
});
}
2023-02-28 08:29:30 +00:00
newDocs.push(newDoc);
}
return newDocs;
},
setEmitData(emits) {
const data = [];
for (const emit of emits) {
data.push({
name: emit.name,
parameters: {
name: emit.parameters[0]?.name,
type: emit.parameters[0]?.type
},
2023-02-28 08:29:30 +00:00
returnType: emit.returnType,
description: emit.description
});
}
return data;
},
setPropsData(props) {
const data = [];
for (const prop of props) {
data.push({
name: prop.name,
type: prop.type,
default: prop.default,
description: prop.description
2023-02-28 08:29:30 +00:00
});
}
return data;
},
setEventsData(moduleName, events) {
const data = [];
for (const event of events) {
const eventData = {
id: `api.${moduleName}.events.${event.key}`,
label: event.key,
component: DocApiTable,
description: event.values.description,
relatedProp: event.values.relatedProp,
data: []
};
if (event.values.relatedProp) {
eventData.relatedProp = event.values.relatedProp;
}
event.values.props.forEach((prop) => {
eventData.data.push({
name: prop.name,
type: prop.type,
description: prop.description
});
});
data.push(eventData);
}
return data;
},
setInterfacesData(moduleName, interfaces) {
const data = [];
for (const interfaceData of interfaces) {
const interfaceDatas = {
id: `api.${moduleName}.interfaces.${interfaceData.key}`,
label: interfaceData.key,
component: DocApiTable,
description: interfaceData.values.description,
data: []
};
interfaceData.values.props.forEach((prop) => {
interfaceDatas.data.push({
name: prop.name,
type: prop.type,
default: prop.default,
2023-02-28 08:29:30 +00:00
description: prop.description
});
});
data.push(interfaceDatas);
}
return data;
},
setTypesData(moduleName, types) {
const data = [];
for (const key of Object.keys(types)) {
const value = types[key];
const typeData = {
id: `api.${moduleName}.types.${key}`,
label: key,
component: DocApiTable,
data: []
};
typeData.data.push({
values: value.values
});
data.push(typeData);
}
return data;
},
2023-03-07 13:37:45 +00:00
setServicesData(moduleName, services) {
const data = [];
for (const key of Object.keys(services)) {
const value = services[key];
value?.methods.values.forEach((method) => {
data.push({
name: method.name,
parameters: {
name: method.parameters[0]?.name,
type: method.parameters[0]?.type
},
returnType: method.returnType,
description: method.description
});
});
}
return data;
},
2023-02-28 08:29:30 +00:00
findEvents(values) {
const events = [];
for (const key of Object.keys(values)) {
if (key.includes('Event')) {
events.push({ key, values: values[key] });
}
}
return events;
},
findOtherInterfaces(values, docName) {
const interfaces = [];
for (const key of Object.keys(values)) {
if (!key.includes('Event') && !key.includes('Props') && !key.includes('Emits') && !key.includes('Slots') && key !== docName) {
interfaces.push({ key, values: values[key] });
}
}
return interfaces;
},
2023-03-03 07:05:18 +00:00
findOptions(values, docName) {
2023-03-06 10:59:56 +00:00
const validOptionsDocNames = ['MenuItem', 'ConfirmationOptions'];
if (!validOptionsDocNames.includes(docName)) return;
2023-03-03 07:05:18 +00:00
const options = [];
for (const key of Object.keys(values)) {
2023-03-06 10:59:56 +00:00
if (key === 'MenuItem' || key === 'ConfirmationOptions') {
2023-03-03 07:05:18 +00:00
options.push({ key, values: values[key] });
}
}
return options;
},
checkDirectiveInterfaces(interfaces) {
const findMainInterface = interfaces.find((interfaceData) => interfaceData.key.includes('DirectiveBinding'));
return !findMainInterface || findMainInterface.values.props.length > 0;
2023-02-28 08:29:30 +00:00
}
},
computed: {
description() {
return this.doc && this.doc.length > 0 ? `API defines helper props, events and others for the PrimeVue ${this.header} module.` : `${this.header} is a CSS feature so does not provide a Javascript API`;
}
2023-02-28 08:29:30 +00:00
}
};
</script>