UNPKG

@easyquery/core

Version:

EasyQuery.JS core modules

684 lines 26.4 kB
import { eqi18n } from "../i18n/i18n"; import { equtils } from "../utils/utils"; import { AggrFunction } from "./aggr_function"; import { Operator, Operand } from "./operator"; import { Entity, EntityAttr } from "./entity"; import { ValueEditor } from "./value_editor"; import { DataKind } from "../types/data_kind"; import { DataType } from "../types/data_type"; import { EditorTag } from "../types/editor_tag"; import { Query } from "../query/query"; var DataModel = /** @class */ (function () { function DataModel() { this.id = ""; this.name = ""; this.eqlcns = new EqLcns(); this.operators = new Array(); this.rootEntity = new Entity(); this.aggrFunctions = new Array(); this.dateMacros = ["${Today}", "${Yesterday}", "${Tomorrow}", "${FirstDayOfMonth}", "${LastDayOfMonth}", "${FirstDayOfWeek}", "${FirstDayOfYear}", "${FirstDayOfNextWeek}", "${FirstDayOfNextMonth}", "${FirstDayOfNextYear}"]; this.timeMacros = ["${Now}", "${HourStart}", "${Midnight}", "${Noon}"]; this.rgdlceval = false; } ///<summary> /// ///</summary> DataModel.prototype.loadFromJSON = function (stringJson) { var model = JSON.parse(stringJson); this.loadFromData(model); }; DataModel.prototype.loadFromData = function (data) { this.id = data.id; this.name = data.name; this.version = data.vers; //licensing info if (data.easyQuery) { this.eqlcns.loadFromData(data.easyQuery); this.rgdlceval = this.eqlcns.chkrgd(); } //Editors this.editors = new Array(); if (data.editors) { for (var i = 0; i < data.editors.length; i++) { var newEditor = new ValueEditor(); newEditor.loadFromData(data.editors[i]); this.editors.push(newEditor); } } //Operators this.operators = new Array(); if (data.operators) { for (var i = 0; i < data.operators.length; i++) { var newOperator = new Operator(); newOperator.loadFromData(this, data.operators[i]); this.operators.push(newOperator); } } //rootEntity this.rootEntity.loadFromData(this, data.entroot); //Aggr Functions this.aggrFunctions = new Array(); if (data.aggrfuncs) { for (var i = 0; i < data.aggrfuncs.length; i++) { var newAggrFunc = new AggrFunction(); newAggrFunc.loadFromData(data.aggrfuncs[i]); this.aggrFunctions.push(newAggrFunc); } } }; ///<summary> /// ///</summary> DataModel.prototype.getObject = function () { return this; }; ///<summary> /// ///</summary> DataModel.prototype.setData = function (model) { if (typeof model === "string") { this.loadFromJSON(model); } else { this.loadFromData(model); } }; DataModel.prototype.createQuery = function () { return new Query(this); }; DataModel.prototype.isEmpty = function () { return this.rootEntity.subEntities.length === 0 && this.rootEntity.attributes.length === 0; }; DataModel.prototype.getId = function () { return this.id; }; DataModel.prototype.getName = function () { return this.name; }; DataModel.prototype.getRootEntity = function () { return this.rootEntity; }; DataModel.prototype.rgdlce = function () { return this.rgdlceval; }; DataModel.prototype.getEditorById = function (editorId) { for (var _i = 0, _a = this.editors; _i < _a.length; _i++) { var editor = _a[_i]; if (editor.id === editorId) { return editor; } } return null; }; /// <method name="getAttributById" version="1.0.0"> /// <summary> /// Gets entity attribute by its ID. /// This function runs through all attributes inside specified model (it's root entity and all its sub-entities). /// Returns null if attribute is not found. /// </summary> /// <returns type="EntityAttr"> /// An attribute. /// </returns> /// <param name="attrId" type="String"> /// Attribute ID /// </param> /// </method> DataModel.prototype.getAttributeById = function (attrId) { var attr = this.getEntityAttrById(this.getRootEntity(), attrId); if (!attr) { attr = new EntityAttr(); } return attr; }; DataModel.prototype.checkAttrProperty = function (attrId, propName) { var attribute = this.getAttributeById(attrId); if (attribute) { if (attribute[propName]) { return true; } else if (attribute.lookupAttr) { attrId = attribute.lookupAttr; attribute = this.getAttributeById(attrId); return attribute && attribute[propName]; } else { return false; } } else return false; }; /// <method name="getEntityAttrById" version="1.0.0"> /// <summary> /// Gets entity attribute by its ID. /// This function runs through all attributes inside specified entity and all its sub-entities. /// Returns null if attribute is not found. /// </summary> /// <returns type="EntityAttr"> /// An attribute. /// </returns> /// <param name="entity" type="EntityAttr"> /// Entity object to search in /// </param> /// <param name="attrId" type="String"> /// Attribute ID /// </param> /// </method> DataModel.prototype.getEntityAttrById = function (entity, attrId) { var idx; if (entity.attributes) { var attrCount = entity.attributes.length; for (idx = 0; idx < attrCount; idx++) { if (entity.attributes[idx].id == attrId) { return entity.attributes[idx]; } } } var res; if (entity.subEntities) { var subEntityCount = entity.subEntities.length; for (idx = 0; idx < subEntityCount; idx++) { res = this.getEntityAttrById(entity.subEntities[idx], attrId); if (res) return res; } } return null; }; DataModel.prototype.listByEntityWithFilter = function (entity, filterCallback) { var result = new Array(); var caption; var ent = null; if (entity.subEntities) { var subEntityCount = entity.subEntities.length; for (var entIdx = 0; entIdx < subEntityCount; entIdx++) { ent = entity.subEntities[entIdx]; if (!filterCallback || filterCallback(ent)) { caption = eqi18n.getText('Entities', ent.name); if (!caption) { caption = ent.caption; } var newEnt = equtils.assign(new Entity(), { id: ent.name, text: caption, items: [], isEntity: true }); newEnt.items = this.listByEntityWithFilter(ent, filterCallback); if (newEnt.items.length > 0) result.push(newEnt); } } } var attr = null; if (entity.attributes) { var attrCount = entity.attributes.length; for (var attrIdx = 0; attrIdx < attrCount; attrIdx++) { attr = entity.attributes[attrIdx]; if (!filterCallback || filterCallback(attr)) { caption = eqi18n.getText('Attributes', attr.id); if (!caption) caption = attr.caption; var newEnt = equtils.assign(new Entity(), { id: attr.id, text: caption, dataType: attr.dataType }); result.push(newEnt); } } } return result; }; DataModel.prototype.listByEntity = function (entity, opts) { opts = opts || {}; var resultEnt = []; var resultAttr = []; var caption; var ent = null; if (entity.subEntities) { var subEntityCount = entity.subEntities.length; for (var entIdx = 0; entIdx < subEntityCount; entIdx++) { ent = entity.subEntities[entIdx]; if (opts.addUIC !== false && ent.useInConditions !== false || opts.addUIR !== false && ent.useInResult !== false || opts.addUIS !== false && ent.useInSorting !== false) { caption = eqi18n.getText('Entities', ent.name); if (!caption) caption = ent.caption; var newEnt = equtils.assign(new Entity(), { id: ent.name, text: caption, items: [], isEntity: true, description: ent.description }); var newOpts = equtils.assign({}, opts); newOpts.includeRootData = false; newEnt.items = this.listByEntity(ent, newOpts); resultEnt.push(newEnt); } } } var attr = null; if (entity.attributes) { var attrCount = entity.attributes.length; for (var attrIdx = 0; attrIdx < attrCount; attrIdx++) { attr = entity.attributes[attrIdx]; if (opts.addUIC !== false && attr.usedInCondition !== false || opts.addUIR !== false && attr.usedInResult !== false || opts.addUIS !== false && attr.usedInSorting !== false) { caption = eqi18n.getText('Attributes', attr.id); if (!caption) caption = attr.caption; resultAttr.push(equtils.assign(new EntityAttr(), { id: attr.id, text: caption, dataType: attr.dataType, lookupAttr: attr.lookupAttr, description: attr.description })); } } } var sortCheck = function (a, b) { if (a.text.toLowerCase() == b.text.toLowerCase()) { return 0; } if (a.text.toLowerCase() > b.text.toLowerCase()) { return 1; } return -1; }; if (opts.sortEntities) { resultEnt.sort(sortCheck); resultAttr.sort(sortCheck); } var result; if (!opts.attrPlacement || opts.attrPlacement == 0) { result = resultEnt.concat(resultAttr); } else { result = resultAttr.concat(resultEnt); } if (opts.attrPlacement == 2) { result.sort(sortCheck); } if (opts.includeRootData) { caption = eqi18n.getText('Entities', entity.name); if (!caption) caption = entity.caption; return { id: entity.name, text: caption, items: result }; } else { return result; } }; DataModel.prototype.getEntitiesTree = function (opts) { return this.listByEntity(this.getRootEntity(), opts); }; DataModel.prototype.getEntitiesTreeWithFilter = function (filterCallback) { return this.listByEntityWithFilter(this.getRootEntity(), filterCallback); }; DataModel.prototype.getFullEntityPathByAttr = function (attrId, sep) { sep = sep || " "; return this.getEntityPathByAttr(this.getRootEntity(), attrId, sep, true); }; DataModel.prototype.getEntityPathByAttr = function (entity, attrId, sep, root) { if (!entity) return ""; sep = sep || " "; var entityCaption = ""; if (entity.caption && !root) { var entityText = eqi18n.getText('Entities', entity.caption); entityCaption = entityText ? entityText : entity.caption; } if (entity.attributes) { var attrCount = entity.attributes.length; for (var i = 0; i < attrCount; i++) { if (entity.attributes[i].id == attrId) { return entityCaption; } } } if (entity.subEntities) { var subEntityCount = entity.subEntities.length; for (var i = 0; i < subEntityCount; i++) { var ent = entity.subEntities[i]; var res = this.getEntityPathByAttr(ent, attrId, sep, false); if (res !== "") { if (entityCaption !== "") res = entityCaption + sep + res; return res; } } } return ""; }; DataModel.prototype.getAttributeText = function (attr, format) { var attrText = eqi18n.getText('Attributes', attr.id); if (!attrText) { attrText = attr.caption; } if (!format) { return attrText; } var result = ""; var entityPath = this.getFullEntityPathByAttr(attr.id, ' '); if (entityPath) { result = format.replace(new RegExp("{attr}", 'g'), attrText); result = result.replace(new RegExp("{entity}", 'g'), entityPath); } else { result = attrText; } return result.trim(); }; DataModel.prototype.getFirstUICAttr = function () { return this.getFirstUICAttrInEntity(this.getRootEntity()); }; /// <method name="getFirstUICAttrInEntity" version="1.0.0"> /// <summary> /// Gets first "UIC" attribute in specified entity /// (UIC stands for "use in conditions" - so such attribute can be used in conditions) /// Returns null if attribute is not found. /// </summary> /// <returns type="EntityAttr"> /// An attribute. /// </returns> /// <param name="entity" type="Entity"> /// Entity object to search our attribute in. /// </param> /// </method> DataModel.prototype.getFirstUICAttrInEntity = function (entity) { if (entity.useInConditions !== false) { if (entity.attributes) { var attrCount = entity.attributes.length; for (var i = 0; i < attrCount; i++) { if (entity.attributes[i].usedInCondition) { return entity.attributes[i]; } } } if (entity.subEntities) { var subEntityCount = entity.subEntities.length; for (var i = 0; i < subEntityCount; i++) { var result = this.getFirstUICAttrInEntity(entity.subEntities[i]); if (result) { return result; } } } } return null; }; /// <method version="1.0.0"> /// <summary> /// Scans model's entity tree and calls the callback functions for each attribute and entity. /// </summary> /// <param name="processAttribute" type="Function"> /// The callback function which is called for each attribute in model's entity tree. /// The processed attribute is passed in the first function parameter. /// </param> /// <param name="processEntity" type="Function"> /// The callback function which is called for each entity in tree. /// The processed entity is passed in the first function parameter. /// </param> /// </method> DataModel.prototype.runThroughEntities = function (processAttribute, processEntity) { var opts = { stop: false }; var internalProcessEntity = function (entity) { if (processEntity) processEntity(entity, opts); if (entity.attributes) { var attrCount = entity.attributes.length; for (var i = 0; (i < attrCount) && !opts.stop; i++) { var attr = entity.attributes[i]; if (processAttribute) { processAttribute(attr, opts); } if (opts.stop) return; } } if (entity.subEntities) { var subEntityCount = entity.subEntities.length; for (var i = 0; (i < subEntityCount) && !opts.stop; i++) { internalProcessEntity(entity.subEntities[i]); } } }; internalProcessEntity(this.getRootEntity()); }; DataModel.prototype.getFirstAttributeByFilter = function (filterCallback) { var res = null; this.runThroughEntities(function (attr, opts) { if (filterCallback(attr)) { opts.stop = true; res = attr; } }, null); return res; }; /// <method name="findOperatorById" version="1.0.0"> /// <summary> /// Finds operator in model by its ID. /// This function runs through all operators inside specified model and returns the one with specified ID. /// Returns null if operator is not found. /// </summary> /// <returns type="Operator"> /// An operator. /// </returns> /// <param name="operatorId" type="String"> /// Operator ID /// </param> /// </method> DataModel.prototype.findOperatorById = function (operatorId) { if (this.operators.length > 0) { var opCount = this.operators.length; for (var idx = 0; idx < opCount; idx++) { if (this.operators[idx].id == operatorId) { return this.operators[idx]; } } } return null; }; /// <method name="getOperatorById" version="1.0.0"> /// <summary> /// Finds operator in model by its ID. /// This function runs through all operators inside specified model and returns the one with specified ID. /// Returns special NullOperator object if operator is not found. /// </summary> /// <returns type="Operator"> /// An operator. /// </returns> /// <param name="operatorId" type="String"> /// Operator ID /// </param> /// </method> DataModel.prototype.getOperatorById = function (operatorId) { var op = this.findOperatorById(operatorId); if (!op) op = new Operator(); return op; }; DataModel.prototype.getDefaultOperatorIdForAttr = function (attr) { if (attr.defaultOperator) { return attr.defaultOperator; } else if (attr.operators.length > 0) { return attr.operators[0]; } else { return (new Operator()).id; } }; DataModel.prototype.getAggrFunctionCaption = function (funcId) { var funcTexts = eqi18n.getText("AggregateFunctions", funcId); var funcCaption = funcTexts ? funcTexts.caption : eqi18n.getText('Aggr' + funcId.replace(' ', '') + '_Caption'); if (funcCaption) { return funcCaption; } var func = equtils.findItemById(this.aggrFunctions, funcId); if (!func || !func.caption) return funcId; return func.caption; }; DataModel.prototype.getAggrFunctionFormat = function (funcId) { var funcTexts = eqi18n.getText("AggregateFunctions", funcId); var funcFormat = funcTexts ? funcTexts.displayFormat : eqi18n.getText('Aggr' + funcId.replace(' ', '') + '_Format'); if (funcFormat) { return funcFormat; } var func = equtils.findItemById(this.aggrFunctions, funcId); if (!func || !func.displayFormat) return ''; return func.displayFormat; }; DataModel.prototype.getDefaultOperatorForAttr = function (attr) { var operatorId = this.getDefaultOperatorIdForAttr(attr); return this.getOperatorById(operatorId); }; DataModel.prototype.getOperand = function (attr, operator, index) { var defOperand = new Operand(); if (operator && operator.defaultOperand) { defOperand.copyFrom(operator.defaultOperand); if (!defOperand.defValue) { defOperand.defValue = ""; } if (!defOperand.defText) { defOperand.defText = ""; } } else { defOperand.kind = DataKind.Scalar; defOperand.dataType = DataType.Unknown; defOperand.defValue = ""; defOperand.defText = ""; defOperand.editor = null; } var result = defOperand; if (result.dataType === DataType.Unknown && attr) { result.dataType = attr.dataType; } if (operator && (index >= 0)) { if (operator.operands && operator.operands[index - 1]) { result = equtils.assign(result, operator.operands[index - 1]); } } ; if (!result.editor) { if (defOperand.editor) { result.editor = defOperand.editor; } else if (defOperand.kind === DataKind.Query) { result.editor.tag = EditorTag.SubQuery; } else if (attr && attr.defaultEditor) { result.editor = attr.defaultEditor; } else if (result.dataType === DataType.Date || result.dataType === DataType.DateTime || result.dataType === DataType.Time) { result.editor.tag = EditorTag.DateTime; } else { result.editor.tag = EditorTag.Edit; } } return result; }; DataModel.prototype.getAggrFunctions = function () { return this.aggrFunctions; }; DataModel.prototype.getDateMacroValue = function (macro) { var d = new Date(); if (this.isDateMacro(macro)) { switch (macro) { case "${Today}": break; case "${Yesterday}": d.setDate(d.getDate() - 1); break; case "${Tomorrow}": d.setDate(d.getDate() + 1); break; case "${FirstDayOfMonth}": d.setDate(1); break; case "${LastDayOfMonth}": d.setMonth(d.getMonth() + 1, 0); break; case "${FirstDayOfWeek}": var day = d.getDay(); day = (day == 0) ? 6 : day - 1; //We start week from Monday, but js - from Sunday d.setDate(d.getDate() - day); break; case "${FirstDayOfYear}": d.setMonth(0, 1); break; case "${FirstDayOfNextWeek}": var day = d.getDay(); day = (day == 0) ? 1 : 8 - day; //We start week from Monday, but js - from Sunday d.setDate(d.getDate() + day); break; case "${FirstDayOfNextMonth}": d.setMonth(d.getMonth() + 1, 1); break; case "${FirstDayOfNextYear}": d.setFullYear(d.getFullYear() + 1, 0, 1); break; } ; return d; } return null; }; DataModel.prototype.isDateMacro = function (macro) { return equtils.indexOfArrayItem(this.dateMacros, macro) >= 0; }; DataModel.prototype.getDateOrMacroValue = function (macroValue) { var d = this.getDateMacroValue(macroValue); return d ? d : macroValue; }; DataModel.prototype.getTimeMacroValue = function (macro) { var d = new Date(); if (this.isTimeMacro(macro)) { switch (macro) { case "${Now}": break; case "${HourStart}": d.setMinutes(0, 0, 0); break; case "${Midnight}": d.setHours(0, 0, 0, 0); break; case "${Noon}": d.setHours(12, 0, 0, 0); break; } ; return d; } return null; }; DataModel.prototype.isTimeMacro = function (macro) { return equtils.indexOfArrayItem(this.timeMacros, macro) >= 0; }; DataModel.prototype.getTimeOrMacroValue = function (time) { var t = this.getTimeMacroValue(time); return t ? t : time; }; return DataModel; }()); export { DataModel }; var EqLcns = /** @class */ (function () { function EqLcns() { this.version = "5.0.0-beta1"; this.token = " "; } ; EqLcns.prototype.loadFromData = function (data) { if (data.version) { this.version = data.version; } if (data.token) { this.token = data.token; } }; EqLcns.prototype.chkrgd = function () { var t = this.token; var v = this.version; var vns = v.split("."); var maj = (vns.length > 0) ? parseInt(vns[0]) : 0; var min = (vns.length > 1) ? parseInt(vns[1]) : 0; var sym = t.charCodeAt(min % 14 + 2); var calc = (maj % 10) + (min % 10) + 65; return calc == sym; }; return EqLcns; }()); export { EqLcns }; //# sourceMappingURL=data_model.js.map