@cuba-platform/front-generator
Version:
CUBA Platform front-end clients generator
111 lines • 4.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cuba_model_1 = require("../../../../common/cuba-model");
const ejs_1 = require("ejs");
const fs = require("fs");
const path = require("path");
exports.fieldDependencies = {
["boolean" /* BOOLEAN */]: [
'vaadin-checkbox/vaadin-checkbox.html'
],
["date" /* DATE */]: [
'vaadin-text-field/vaadin-text-field.html',
'vaadin-date-picker/vaadin-date-picker-light.html',
'iron-icons/iron-icons.html'
],
["dateTime" /* DATE_TIME */]: [
'vaadin-text-field/vaadin-text-field.html'
],
["enum" /* ENUM */]: [
'vaadin-combo-box/vaadin-combo-box.html'
],
["file" /* FILE */]: [
'cuba-file-field/cuba-file-field.html'
],
["fpNumber" /* FP_NUMBER */]: [
'vaadin-text-field/vaadin-text-field.html'
],
["number" /* NUMBER */]: [
'vaadin-text-field/vaadin-text-field.html'
],
["lookup" /* LOOKUP */]: [
'cuba-ui/cuba-lookup.html',
'cuba-data/cuba-entities.html',
'vaadin-combo-box/vaadin-combo-box.html'
],
["text" /* TEXT */]: [
'vaadin-text-field/vaadin-text-field.html'
],
["time" /* TIME */]: [
'vaadin-text-field/vaadin-text-field.html'
]
};
exports.getFieldType = (attribute) => {
if (attribute.mappingType === "ENUM" /* ENUM */) {
return "enum" /* ENUM */;
}
if (isFile(attribute)) {
return "file" /* FILE */;
}
if (isToOneRelation(attribute)) {
return "lookup" /* LOOKUP */;
}
if (attribute.mappingType === "DATATYPE" /* DATATYPE */) {
if (isAttributeTypeOf(attribute, 'java.lang.Boolean')) {
return "boolean" /* BOOLEAN */;
}
if (isAttributeTypeOf(attribute, 'java.lang.Integer') || isAttributeTypeOf(attribute, 'java.lang.Long')) {
return "number" /* NUMBER */;
}
if (isAttributeTypeOf(attribute, 'java.math.BigDecimal') || isAttributeTypeOf(attribute, 'java.lang.Double')) {
return "fpNumber" /* FP_NUMBER */;
}
if (isAttributeTypeOf(attribute, 'java.lang.String')) {
return "text" /* TEXT */;
}
if (isAttributeTypeOf(attribute, 'java.util.Date') && attribute.temporalType != null) {
if (attribute.temporalType === "DATE" /* DATE */)
return "date" /* DATE */;
if (attribute.temporalType == "TIMESTAMP" /* TIMESTAMP */)
return "dateTime" /* DATE_TIME */;
if (attribute.temporalType == "TIME" /* TIME */)
return "time" /* TIME */;
}
}
return "text" /* TEXT */;
};
exports.getFieldModel = (attribute) => {
const fieldType = exports.getFieldType(attribute);
let model = {
type: fieldType,
bindPath: 'entity.' + attribute.name,
errorBindPath: 'serverErrors.' + attribute.name,
label: `[[msg('${attribute.name}')]]`,
required: attribute.mandatory,
maxLength: attribute.length
};
if (fieldType === "enum" /* ENUM */) {
model.enumItems = `[[enumValues('${attribute.type.fqn}')]]`;
}
if (fieldType === "lookup" /* LOOKUP */ && attribute.type.entityName) {
model.lookupEntityName = attribute.type.entityName;
model.itemsBindPath = `entity_${attribute.name}Options`;
}
return model;
};
exports.getFieldHtml = (model) => {
const html = ejs_1.render(fs.readFileSync(path.join(__dirname, 'template', model.type + 'Field.html'), 'utf8'), model);
return html;
};
function isFile(attribute) {
return isToOneRelation(attribute) && isAttributeTypeOf(attribute, cuba_model_1.FILE_DESCRIPTOR_FQN);
}
function isToOneRelation(attribute) {
return (attribute.mappingType === "ASSOCIATION" /* ASSOCIATION */ || attribute.mappingType === "COMPOSITION" /* COMPOSITION */)
&& (attribute.cardinality === "MANY_TO_ONE" /* MANY_TO_ONE */ || attribute.cardinality === "ONE_TO_ONE" /* ONE_TO_ONE */);
}
// todo extensions inheritance
function isAttributeTypeOf(attribute, fqn) {
return attribute.type != null && attribute.type.fqn === fqn;
}
//# sourceMappingURL=index.js.map