// (C) 2007-2021 GoodData Corporation import { getProjectId, IProject } from "../../model/Project"; import { objectUri, elementsUri } from "../../route/routes"; import { ensureIdentifier } from "../helpers/objects"; import { webalize } from "../../utils/string"; import { IAttribute } from "../../model/Attribute"; import { INestedAttributeDisplayForm } from "../../model/NestedAttributeDisplayForm"; import { ISchemaAttribute, ISchemaAttributeDisplayForm, SchemaAttributeDisplayFormType, } from "../model/SchemaAttribute"; export const createAttributeIdentifier = (attribute: ISchemaAttribute) => `attr.${webalize(attribute.title)}`; function getDisplayFormType(type: SchemaAttributeDisplayFormType) { switch (type) { case "hyperlink": return "GDC.link"; case "geoPin": return "GDC.geo.pin"; case "text": default: return undefined; } } function createDefaultDisplayForm( project: IProject, attributeId: string, attribute: ISchemaAttribute, ): INestedAttributeDisplayForm { const projectId = project.meta.identifier; const displayFormId = `${attributeId}.df`; const type = attribute.defaultDisplayFormType; return createDisplayForm(projectId, type, displayFormId, attributeId, attribute.title); } function createAdditionalDisplayForms(projectId: string, attributeId: string, attribute: ISchemaAttribute) { const additionalDisplayForms = attribute.additionalDisplayForms || []; return additionalDisplayForms.map((displayForm: ISchemaAttributeDisplayForm) => { const title = `${attribute.title} (${displayForm.identifier})`; const type = getDisplayFormType(displayForm.type); return createDisplayForm(projectId, type, displayForm.identifier, attributeId, title); }); } function createDisplayForm( projectId: string, type: string, displayFormId: string, attributeId: string, title: string, ): INestedAttributeDisplayForm { return { content: { expression: `[${objectUri(projectId, "123")}]`, formOf: objectUri(projectId, attributeId), type, }, links: { elements: elementsUri(projectId, attributeId), }, meta: { category: "attributeDisplayForm", identifier: displayFormId, title, uri: objectUri(projectId, displayFormId), }, }; } export function createAttribute(project: IProject, attribute: ISchemaAttribute): IAttribute { const identifier = ensureIdentifier(attribute, createAttributeIdentifier(attribute)); const projectId = getProjectId(project); const drillDown = attribute.drillDownStepAttributeDF ? { drillDownStepAttributeDF: objectUri(projectId, attribute.drillDownStepAttributeDF), } : {}; return { content: { displayForms: [ createDefaultDisplayForm(project, identifier, attribute), ...createAdditionalDisplayForms(projectId, identifier, attribute), ], ...drillDown, }, meta: { category: "attribute", title: attribute.title, identifier, uri: objectUri(projectId, identifier), tags: attribute.tags, }, }; }