UNPKG

@cuba-platform/front-generator

Version:
111 lines 4.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromStudioProperties = exports.fromStudioProperty = void 0; const cuba_model_1 = require("./model/cuba-model"); const cuba_model_utils_1 = require("./model/cuba-model-utils"); const matching = { ["BOOLEAN" /* BOOLEAN */]: "confirm" /* confirm */, ["ENTITY" /* ENTITY */]: "autocomplete" /* autocomplete */, ["VIEW" /* VIEW */]: "autocomplete" /* autocomplete */, ["NESTED_ENTITY_VIEW" /* NESTED_ENTITY_VIEW */]: "autocomplete" /* autocomplete */, ["STRING" /* STRING */]: "input" /* input */, ["INTEGER" /* INTEGER */]: "input" /* input */, ["OPTION" /* OPTION */]: "autocomplete" /* autocomplete */, ["MULTI_OPTION" /* MULTI_OPTION */]: "list" /* list */, ["REST_QUERY" /* REST_QUERY */]: "list" /* list */, ["REST_SERVICE_METHOD" /* REST_SERVICE_METHOD */]: "list" /* list */, ["POLYMER_COMPONENT_NAME" /* POLYMER_COMPONENT_NAME */]: "input" /* input */, ["PASSWORD" /* PASSWORD */]: "password" /* password */ }; function fromStudioProperty(prop, projectModel) { const question = { type: matching[prop.propertyType] || "input" /* input */, name: prop.code, message: prop.caption }; switch (prop.propertyType) { case "ENTITY" /* ENTITY */: if (!projectModel) { throw new Error('Project model is required to determine choices for property of type ' + "ENTITY" /* ENTITY */); } question.choices = (0, cuba_model_1.getEntitiesArray)(projectModel.entities) .filter(entity => entity.name) .map(entity => { const entityInfo = { name: entity.name }; return { name: entity.name, value: entityInfo }; }); break; case "VIEW" /* VIEW */: if (!projectModel) { throw new Error('Project model is required to determine choices for property of type ' + "VIEW" /* VIEW */); } question.choices = (previousAnswers) => { return (0, cuba_model_utils_1.findViewsForEntity)(projectModel, previousAnswers.entity.name) .map(view => ({ name: view.name, value: view })); }; break; case "NESTED_ENTITY_VIEW" /* NESTED_ENTITY_VIEW */: if (!projectModel) { throw new Error('Project model is required to determine choices for property of type ' + "NESTED_ENTITY_VIEW" /* NESTED_ENTITY_VIEW */); } if (!prop.options) { throw new Error('Property name and entity name are required to determine choices for property of type ' + "NESTED_ENTITY_VIEW" /* NESTED_ENTITY_VIEW */); } const propertyName = prop.options[0]; const entityName = prop.options[1]; question.choices = (0, cuba_model_utils_1.findViewsForEntity)(projectModel, entityName) .map(view => ({ name: view.name, value: { [propertyName]: view.name } })); break; case "OPTION" /* OPTION */: if (!prop.options) { throw new Error('Options are missing'); } question.choices = prop.options; break; } if (question.type === "autocomplete" /* autocomplete */) { if (!question.choices) { throw new Error('Question choices are not defined'); } question.source = (answers, input) => { let choices; if (typeof (question.choices) === 'function') { choices = question.choices(answers); } else { choices = [...question.choices]; } if (input) { return Promise.resolve(choices.filter(choice => { if (typeof (choice) === 'string') { return choice.indexOf(input) > -1; } else { return choice.name.indexOf(input) > -1; } })); } else { return Promise.resolve(choices); } }; } return question; } exports.fromStudioProperty = fromStudioProperty; function fromStudioProperties(props, projectModel) { return props.map(prop => fromStudioProperty(prop, projectModel)); } exports.fromStudioProperties = fromStudioProperties; //# sourceMappingURL=questions.js.map