UNPKG

idea-toolbox

Version:
195 lines (194 loc) 7.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PDFTemplateComplexField = exports.PDFTemplateSimpleField = exports.PDFTemplateSectionTypes = exports.PDFTemplateSection = exports.PDFEntity = void 0; const label_model_1 = require("./label.model"); const resource_model_1 = require("./resource.model"); /** * The PDF-printable version of an entity, that contains variables and inner contextes as defined by either a * `PDFTemplateBlueprint` or `PDFTemplateSectionBlueprint`. */ class PDFEntity extends resource_model_1.Resource { } exports.PDFEntity = PDFEntity; /** * The section of a multi-sections template for PDF printing of entities. */ class PDFTemplateSection extends resource_model_1.Resource { load(x, languages) { super.load(x); this.type = this.clean(x.type, Number, 0); if (x.description) this.description = this.clean(x.description, String); if (x.border) this.border = true; switch (this.type) { case PDFTemplateSectionTypes.ROW: this.columns = new Array(); for (let k = 0; k < 12; k++) this.columns.push(null); if (x.columns) for (let i = 0; i < this.columns.length; i++) { if (!x.columns[i]) this.columns[i] = null; else if (x.columns[i] === '-') this.columns[i] = '-'; else this.columns[i] = x.columns[i].code ? new PDFTemplateSimpleField(x.columns[i], languages) : new PDFTemplateComplexField(x.columns[i], languages); } break; case PDFTemplateSectionTypes.HEADER: this.title = new label_model_1.Label(x.title, languages); break; case PDFTemplateSectionTypes.INNER_SECTION: case PDFTemplateSectionTypes.REPEATED_INNER_SECTION: if (x.title) this.title = new label_model_1.Label(x.title, languages); else { // init the title equal to the section description this.title = new label_model_1.Label(null, languages); this.title[languages.default] = this.description; } this.context = this.clean(x.context, String); this.innerTemplate = this.cleanArray(x.innerTemplate, t => new PDFTemplateSection(t, languages)); break; } } validate(languages, variables) { const e = super.validate(); const ST = PDFTemplateSectionTypes; if (!(this.type in ST)) e.push('type'); if (this.type === ST.ROW) { this.columns.forEach((content, i) => { if (this.doesColumnContainAField(i)) { const field = content; if (field.isComplex()) { const complexField = field; if (complexField.content.validate(languages).length) e.push(`columns[${i}]`); } else { const simpleField = field; if (variables && !(variables || []).some(v => v.code === simpleField.code)) e.push(`columns[${i}]`); } } }); } if (this.type === ST.HEADER && this.title.validate(languages).length) e.push('title'); if (this.isEither(ST.INNER_SECTION, ST.REPEATED_INNER_SECTION)) { if (!this.context) e.push('context'); this.innerTemplate.forEach((s, i) => { if (s.validate(languages).length) e.push(`innerTemplate[${i}]`); }); } return e; } /** * Check whether the section is among one of the types selected. */ isEither(...types) { return types.some(t => this.type === t); } /** * Whether the column identified by the index is empty or not. */ isColumnEmpty(indexInColumns) { // skip in case the section isn't of type ROW if (this.type !== PDFTemplateSectionTypes.ROW) return false; else return !this.columns[indexInColumns]; } /** * Whether the column identified by the index contains or not a field. * It returns false in case the section isn't of type ROW. */ doesColumnContainAField(indexInColumns) { // skip in case the section isn't of type ROW if (this.type !== PDFTemplateSectionTypes.ROW) return false; // if '-', it means that in the previous columns there's a field spanning over the current column return this.columns[indexInColumns] && this.columns[indexInColumns] !== '-'; } /** * Given the index of a column containing a field, return on how many columns the field spans. */ getColumnFieldSize(indexInColumns) { let size = 1; if (this.doesColumnContainAField(indexInColumns)) while (++indexInColumns < 12 && this.columns[indexInColumns] === '-') size++; return size; } /** * Remove a field from the columns that it occupied before. */ removeFieldFromOccupiedColumns(colIndex) { // skip if the column doesn't contain a field if (!this.doesColumnContainAField(colIndex)) return; // remove it from the starting column this.columns[colIndex] = null; // remove it from all the other columns on which it span this.columns.slice(colIndex + 1).some((c, index) => { if (c === '-') { this.columns[colIndex + 1 + index] = null; return false; } else return true; }); } } exports.PDFTemplateSection = PDFTemplateSection; /** * The type of sections for a PDF template. */ var PDFTemplateSectionTypes; (function (PDFTemplateSectionTypes) { PDFTemplateSectionTypes[PDFTemplateSectionTypes["PAGE_BREAK"] = 0] = "PAGE_BREAK"; PDFTemplateSectionTypes[PDFTemplateSectionTypes["BLANK_ROW"] = 1] = "BLANK_ROW"; PDFTemplateSectionTypes[PDFTemplateSectionTypes["ROW"] = 2] = "ROW"; PDFTemplateSectionTypes[PDFTemplateSectionTypes["HEADER"] = 3] = "HEADER"; PDFTemplateSectionTypes[PDFTemplateSectionTypes["INNER_SECTION"] = 4] = "INNER_SECTION"; PDFTemplateSectionTypes[PDFTemplateSectionTypes["REPEATED_INNER_SECTION"] = 5] = "REPEATED_INNER_SECTION"; })(PDFTemplateSectionTypes || (exports.PDFTemplateSectionTypes = PDFTemplateSectionTypes = {})); /** * A simple field, with a direct reference to a variable, depending on the current section's context. */ class PDFTemplateSimpleField extends resource_model_1.Resource { load(x, languages) { super.load(x); this.label = new label_model_1.Label(x.label, languages); this.code = this.clean(x.code, String); } /** * Quickly recognize the nature of the field (simple/complex). */ isComplex() { return false; } } exports.PDFTemplateSimpleField = PDFTemplateSimpleField; /** * A complex field, with a content depending on the current section's context. */ class PDFTemplateComplexField extends resource_model_1.Resource { load(x, languages) { super.load(x); this.content = new label_model_1.Label(x.content, languages); } /** * Quickly recognize the nature of the field (simple/complex). */ isComplex() { return true; } } exports.PDFTemplateComplexField = PDFTemplateComplexField;