gy-foo
Version:
A library that contains all models for the gy-web-project.
1,174 lines (1,134 loc) • 51.1 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define('gy-foo', ['exports'], factory) :
(global = global || self, factory(global['gy-foo'] = {}));
}(this, (function (exports) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function __param(paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
}
function __metadata(metadataKey, metadataValue) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
}
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __createBinding(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}
function __exportStar(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) exports[p] = m[p];
}
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
}
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
function __await(v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
}
function __asyncGenerator(thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
}
function __asyncDelegator(o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
}
function __asyncValues(o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
}
function __makeTemplateObject(cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
function __importStar(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result.default = mod;
return result;
}
function __importDefault(mod) {
return (mod && mod.__esModule) ? mod : { default: mod };
}
function __classPrivateFieldGet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
}
function __classPrivateFieldSet(receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}
var IButton = /** @class */ (function () {
function IButton(obj) {
this.icon = obj.icon ? obj.icon : '';
this.label = obj.label ? obj.label : '';
this.class = obj.class ? obj.class : '';
this.disabled = obj.disabled ? obj.disabled : false;
this.tooltip = obj.tooltip ? obj.tooltip : '';
this.tooltipPosition = obj.tooltipPosition ? obj.tooltipPosition : 'bottom';
}
return IButton;
}());
/**
* Save modes for Occurrences
*/
(function (ModeFlags) {
/**
* Add this new Occurrence to the Entity
*/
ModeFlags["ADD"] = "add";
/**
* This Occurrence is a Copy of another Occurrence
*/
ModeFlags["CPY"] = "cpy";
/**
* Delete this Occurrence from the Entity
*/
ModeFlags["DEL"] = "del";
})(exports.ModeFlags || (exports.ModeFlags = {}));
var FieldRight;
(function (FieldRight) {
FieldRight["ALL"] = "ALL";
FieldRight["RDO"] = "RDO";
FieldRight["DNY"] = "DNY";
})(FieldRight || (FieldRight = {}));
(function (FieldDisplay) {
FieldDisplay["INPUT"] = "INP";
FieldDisplay["CALENDAR"] = "CAL";
FieldDisplay["CHECKBOX"] = "CHK";
FieldDisplay["DROPDOWN"] = "DDL";
FieldDisplay["IOV"] = "IOV";
FieldDisplay["RADIOGROUP"] = "RAD";
FieldDisplay["TEXTAREA"] = "TXT";
})(exports.FieldDisplay || (exports.FieldDisplay = {}));
var Field = /** @class */ (function () {
function Field(field) {
var _this = this;
if (field) {
Object.keys(field).forEach(function (key) { return (_this[key] = field[key]); });
}
}
Field.prototype.canRead = function (checkType, meta) {
var readRights = [FieldRight.RDO, FieldRight.ALL];
return meta && meta[checkType] ? readRights.includes(meta[checkType]) : readRights.includes(this[checkType]);
};
Field.prototype.canReadWrite = function (checkType, meta) {
return meta && meta[checkType] ? meta[checkType] === FieldRight.ALL : this[checkType] === FieldRight.ALL;
};
Field.prototype.displayIOVBez = function () {
return this.display === exports.FieldDisplay.IOV && this.iov;
};
Field.prototype.isIOVId = function () {
return this.iov && this.display !== exports.FieldDisplay.IOV;
};
return Field;
}());
var Occurrence = /** @class */ (function () {
/**
* Creates a new Occrruence
* @param occ An Occrrence object or an object that looks like an Occurence
*/
function Occurrence(occ, modeFlag) {
if (occ) {
if (occ instanceof Occurrence) {
this.pk = occ.getPk();
this.structure = occ.getStructure();
this.data = occ.getData() ? this.convertValues(occ.getData(), occ.getStructure()) : {};
this.meta = occ.getMeta() || {};
this.modified = occ.isModified();
this.locked = occ.isLocked();
}
else {
this.pk = occ.pk;
this.structure = occ.structure;
this.data = occ.data ? this.convertValues(occ.data, occ.structure) : {};
this.meta = occ.meta || {};
this.modified = occ.modified ? occ.modified : false;
this.locked = occ.locked ? occ.locked : false;
}
}
else {
this.pk = null;
this.data = {};
this.structure = {};
this.meta = {};
this.modified = false;
this.locked = false;
}
this.setMode(modeFlag);
}
/**
* Converts the SubEntities in data to Entities
* @param data data to convert
* @param structure structure of current occ
* @returns new data object
*/
Occurrence.prototype.convertValues = function (data, structure) {
var e_1, _a;
try {
for (var _b = __values(Object.keys(structure)), _c = _b.next(); !_c.done; _c = _b.next()) {
var key = _c.value;
var value = data[key.toUpperCase()];
if (value) {
data[key.toUpperCase()] = new Entity(value.occs ? value : { occs: value, structure: structure[key.toUpperCase()] });
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return data;
};
// ------------------------------------------------------------------------------------------------------
// Get Functions
// ------------------------------------------------------------------------------------------------------
/**
* Gets the pk of this Occurrence
*/
Occurrence.prototype.getPk = function () {
return this.pk;
};
/**
* Gets the structure of this Occurrence
*/
Occurrence.prototype.getStructure = function () {
return this.structure;
};
/**
* Gets the value of the given field
* @param field the name of the field
* @returns the value of the field
*/
Occurrence.prototype.getValue = function (field) {
return this.data[field.toUpperCase()];
};
/**
* Returns the Mode of this Occurrence. Possible values are: 'add', 'cpy', 'del' and '';
* @returns returns the mode of the occ (ModeFlags)
*/
Occurrence.prototype.getMode = function () {
return this.data._mode || '';
};
/**
* Gets the data of this Occurence
* @param col get data with or without collections. Default is true (all data).
*/
Occurrence.prototype.getData = function (col) {
var _this = this;
return col === false
? Object.keys(this.data)
.filter(function (field) { return !_this.structure[field.toUpperCase()]; })
.reduce(function (prev, field) {
var _a;
return Object.assign(prev, (_a = {}, _a[field] = _this.data[field], _a));
}, {})
: this.data;
};
/**
* Gets the Subentity from this Occurrence
* @param name the name of the Subentity
* @returns returns the subEntity
*/
Occurrence.prototype.getSubEnt = function (name) {
if (!this.hasCollection(name)) {
return (this.data[name.toUpperCase()] = new Entity());
}
return this.data[name.toUpperCase()];
};
/**
* Gets the Occurences of the given SubEntity
* @param name the name of the Subentity
* @returns data from SubEntity
*/
Occurrence.prototype.getCollection = function (name) {
return this.data[name.toUpperCase()] ? this.data[name.toUpperCase()].occs : [];
};
/**
* Gets a list of all fields of this Occurrence
* @param col if true all Subentitys will be included in this list, default false
* @returns returns an array of all field Names
*/
Occurrence.prototype.getFields = function (col) {
var _this = this;
return Object.keys(this.data).filter(function (value) { return col || !_this.structure[value.toUpperCase()]; });
};
/**
* Has the Occurrence been modified?
* @param checkSubEnt check Subentities (default: true)
*/
Occurrence.prototype.isModified = function (checkSubEnt) {
if (checkSubEnt === void 0) { checkSubEnt = true; }
return this.modified || (!checkSubEnt || !this.hasAnyCollection() ? this.modified : this.checkSubEntityModified());
};
/**
* is this occurence locked and can not be edited?
*/
Occurrence.prototype.isLocked = function () {
return this.locked;
};
/**
* Gets the structure of the given subentity
* @param name the name of the subentity
*/
Occurrence.prototype.getSubEntityStructure = function (name) {
return this.structure[name.toUpperCase()];
};
/**
* Gets the Meta informations of this occurence
* @param entity the name of a subentity, whose meta information should be read
*/
Occurrence.prototype.getMeta = function () {
return this.meta;
};
/**
* Gets the Meta informations of this occurence for the given entityname
* @param entity the name of a subentity, whose meta information should be read
*/
Occurrence.prototype.getMetaFieldList = function (entity) {
return this.meta[entity.toUpperCase()]
? this.meta[entity.toUpperCase()].reduce(function (acc, val) { return Object.assign(acc, val); }, {}) || {}
: {};
};
/**
* Gets the Meta information for one field, if given
* @param field the name of the field
* @param entity the name of a subentity, where the field should be looked up
*/
Occurrence.prototype.getMetaField = function (field, entity) {
var fieldname = field.startsWith('_') ? field : field.toUpperCase();
var metaList = this.meta[entity.toUpperCase()]
? this.meta[entity.toUpperCase()].find(function (value) {
return fieldname in value;
})
: undefined;
return metaList ? metaList[fieldname] : {};
};
// ------------------------------------------------------------------------------------------------------
// Set Functions
// ------------------------------------------------------------------------------------------------------
/**
* Sets the _mode value of this Occurrence
* @param mode boolean, that defines the mode. Add if this Occurrence should be added to the Database,
* cpy if this is a Copy of a Occurrrence and del if this Occurrence should be deleted in the DB.
* If missing, the _mode flag will be deleted.
* @param col set the mode for all subentitys.
*/
Occurrence.prototype.setMode = function (mode, col) {
var _this = this;
if (mode) {
if (mode.del) {
this.data._mode = exports.ModeFlags.DEL;
}
else if (mode.cpy) {
this.data._mode = exports.ModeFlags.CPY;
}
else if (mode.add) {
this.data._mode = exports.ModeFlags.ADD;
}
else if (this.data._mode) {
delete this.data._mode;
}
}
else if (this.data._mode) {
delete this.data._mode;
}
if (col) {
Object.keys(this.structure).forEach(function (subEnt) {
if (_this.data.hasOwnProperty(subEnt)) {
_this.getCollection(subEnt).forEach(function (occ) { return occ.setMode(mode, col); });
}
});
}
};
/**
* sets the pk property
* @param pk pk that should be set
*/
Occurrence.prototype.addPk = function (pk) {
if (this.pk) {
return;
}
this.pk = pk;
};
/**
* Sets the given field on the given value
* @param field the name of the field
* @param value the new value
* @param isUserChange is this change a userChange and should set the modified Flag? (default: false)
*/
Occurrence.prototype.setValue = function (field, value, isUserChange) {
if (isUserChange === void 0) { isUserChange = false; }
if (isUserChange && this.isLocked()) {
return;
}
if (!field.startsWith('_')) {
field = field.toUpperCase();
}
var undovalue = this.data[field];
this.data[field] = value;
if (isUserChange && undovalue != value) {
this.modified = true;
}
};
/**
* Sets the given list of fields on the given values
* @param values list of fields and values in the form {fieldname1: value1, fieldname2: value2, ...}
* @param isUserChange is this change a userChange and should set the modified Flag? (default: false)
*/
Occurrence.prototype.setValues = function (values, isUserChange) {
var _this = this;
if (isUserChange === void 0) { isUserChange = false; }
if (!values || (isUserChange && this.isLocked())) {
return;
}
Object.entries(values).forEach(function (_a) {
var _b = __read(_a, 2), field = _b[0], value = _b[1];
return _this.setValue(field, value, isUserChange && !_this.modified);
});
this.data = this.convertValues(this.data, this.structure);
};
/**
* Changes all Occurrences of the given Subentity
* @param name name ot the subentity
* @param occs list of the new Occurences
*/
Occurrence.prototype.setCollection = function (name, occs) {
if (this.hasCollection(name.toUpperCase())) {
this.data[name.toUpperCase()].occs = occs;
}
else if (this.getSubEntityStructure(name)) {
this.data[name.toUpperCase()] = new Entity({ occs: occs, structure: this.getSubEntityStructure(name) });
}
};
/**
* Sets the structure of this Occurrence
* @param value the new value of the structure
*/
Occurrence.prototype.setStructure = function (value) {
this.structure = Object.assign(this.structure, value);
};
/**
* Sets the modified flag
* @param modified occ is modified?
* @param setSubEntites should the modified flag be set also in the subentites?
*/
Occurrence.prototype.setModified = function (modified, setSubEntites) {
var _this = this;
if (setSubEntites === void 0) { setSubEntites = false; }
this.modified = modified;
if (setSubEntites) {
Object.keys(this.structure).forEach(function (entName) {
_this.getSubEnt(entName).setModified(modified, setSubEntites);
});
}
};
/**
* Sets the Meta information for one field
* @param field the name of the field
* @param entity the name of a subentity, where the field should be looked up
* @param value the new value of the definition
*/
Occurrence.prototype.setMetaField = function (field, entity, value) {
var _a, _b, _c, _d;
if (value) {
var fldvalue = new Field(value);
var fldname_1 = field.toUpperCase();
var entityname = entity.toUpperCase();
if (!this.meta) {
this.meta = (_a = {}, _a[entityname] = [(_b = {}, _b[fldname_1] = fldvalue, _b)], _a);
}
else if (!this.meta[entityname]) {
this.meta[entityname] = [(_c = {}, _c[fldname_1] = fldvalue, _c)];
}
else {
var metaList = this.meta[entityname].find(function (fldlst) { return fldname_1 in fldlst; });
if (!metaList) {
this.meta[entityname].push((_d = {}, _d[fldname_1] = fldvalue, _d));
}
else {
Object.assign(metaList[fldname_1], fldvalue);
}
}
}
};
/**
* Sets the Meta information for one entity
* @param entity the name of a subentity, whose meta information should be set
* @param values the new values of the definition
*/
Occurrence.prototype.setMetaFieldList = function (entity, values) {
var _a;
var _this = this;
if (values) {
var fldlsts = Array.isArray(values) ? values : [values];
var entityname = entity.toUpperCase();
if (!this.meta) {
this.meta = (_a = {}, _a[entityname] = fldlsts, _a);
}
else if (!this.meta[entityname]) {
this.meta[entityname] = fldlsts;
}
else {
fldlsts.forEach(function (fldlst) {
return Object.entries(fldlst).forEach(function (_a) {
var _b = __read(_a, 2), field = _b[0], value = _b[1];
return _this.setMetaField(field, entity, value);
});
});
}
}
};
/**
* Sets the Meta information
* @param metaValue the new values of the definition
*/
Occurrence.prototype.setMeta = function (metaValue) {
var _this = this;
if (metaValue) {
if (!this.meta) {
this.meta = metaValue;
}
else {
Object.entries(metaValue).forEach(function (_a) {
var _b = __read(_a, 2), entity = _b[0], fieldlsts = _b[1];
_this.setMetaFieldList(entity, fieldlsts);
});
}
}
};
// ------------------------------------------------------------------------------------------------------
// Copy Functions
// ------------------------------------------------------------------------------------------------------
/**
* Copies the given Occurrence into this Occurrence
* @param occ the Occurrence that should be copied
* @param cpy the Copy mode should be set
* @param isUserChange is this change a userChange and should set the modified Flag? (default: false)
*/
Occurrence.prototype.copy = function (occ, cpy, isUserChange) {
if (isUserChange === void 0) { isUserChange = false; }
this.pk = occ.pk;
this.data = occ.data;
this.structure = occ.structure;
this.meta = occ.meta;
this.modified = isUserChange;
this.locked = false;
this.setMode({ cpy: cpy });
};
/**
* Makes a copy of this Occurrence into a new instance.
* @returns new Occurrence
*/
Occurrence.prototype.copyOccFlat = function () {
return new Occurrence({
pk: this.pk,
structure: this.structure,
meta: this.meta,
data: { _pk: this.data._pk, _hash: this.data._hash },
modified: this.modified,
});
};
// ------------------------------------------------------------------------------------------------------
// SubEntity/Structure Functions
// ------------------------------------------------------------------------------------------------------
/**
* Checks if this Occurrence has the given Subentity
* @param name the name of the Subentity
* @returns true or false
*/
Occurrence.prototype.hasCollection = function (name) {
return this.data[name.toUpperCase()];
};
/**
* Checks if the Occ is type OCC or COL
* @returns true or false
*/
Occurrence.prototype.hasAnyCollection = function () {
var e_2, _a;
try {
for (var _b = __values(Object.keys(this.structure)), _c = _b.next(); !_c.done; _c = _b.next()) {
var subEnt = _c.value;
if (this.data.hasOwnProperty(subEnt)) {
return true;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return false;
};
/**
* Changes the given Subentity
* @param name the name of the Subentity
* @param SubEnt the new Entity
* @param isUserChange is this a users change (should modified flag been set) (default: false)
*/
Occurrence.prototype.changeSubEnt = function (name, SubEnt, isUserChange) {
if (isUserChange === void 0) { isUserChange = false; }
this.data[name.toUpperCase()] = SubEnt;
if (isUserChange) {
this.getSubEnt(name).setModified(true, false);
}
};
// ------------------------------------------------------------------------------------------------------
// Delete Functions
// ------------------------------------------------------------------------------------------------------
/**
* Deletes a Subenetity from this Occurrence
* @param name name of the subentity
*/
Occurrence.prototype.deleteSubEnt = function (name) {
delete this.data[name.toUpperCase()];
};
/**
* Deletes a field from this Occurrence
* @param name name of field
*/
Occurrence.prototype.deleteValue = function (name) {
delete this.data[name.toUpperCase()];
};
// ------------------------------------------------------------------------------------------------------
// Check Functions
// ------------------------------------------------------------------------------------------------------
/**
* checks if some subentity is modified
*/
Occurrence.prototype.checkSubEntityModified = function () {
var e_3, _a;
try {
for (var _b = __values(Object.keys(this.structure)), _c = _b.next(); !_c.done; _c = _b.next()) {
var entName = _c.value;
if (this.hasCollection(entName) && this.getSubEnt(entName).isModified()) {
return true;
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_3) throw e_3.error; }
}
return false;
};
// ------------------------------------------------------------------------------------------------------
// Additional
// ------------------------------------------------------------------------------------------------------
/**
* Compares this Occurrence with the given Occ. First the PK will be compared and then the hash value.
* @param occ the second occ
*/
Occurrence.prototype.equals = function (occ) {
return this.getPk() === occ.getPk() && this.getValue('_hash') === occ.getValue('_hash');
};
/**
* Gets a string representation of this Occurrence. This is mainly for debug purpose.
*/
Occurrence.prototype.toString = function () {
return JSON.stringify({ pk: this.pk, _hash: this.data._hash });
};
return Occurrence;
}());
var Entity = /** @class */ (function () {
function Entity(entity, keepModes) {
if (keepModes === void 0) { keepModes = true; }
if (entity) {
if (entity instanceof Entity) {
this.structure = entity.getStructure();
this.occs = this.convertOccurences(entity.getOccs(), keepModes) || [];
}
else {
this.structure = entity.structure;
this.occs = this.convertOccurences(entity.occs, keepModes) || [];
}
}
else {
this.structure = {};
this.occs = [];
}
}
/**
* Checks if entity has the structure of an entity
* @param entity entity to check
*/
Entity.isEntity = function (entity) {
return entity && entity.occs ? true : false;
};
/**
* Gets the number of the Occurrences of this Entity
*/
Entity.prototype.getOccsLength = function () {
return this.occs.length;
};
/**
* Get all Occurences of this entity.
*/
Entity.prototype.getOccs = function () {
return this.occs;
};
/**
* Sets all Occurrences of the Entity to the given array.
* @param occs array of the new Occurrences
*/
Entity.prototype.setOccs = function (occs) {
this.occs = occs;
};
/**
* Checks if the given occurence is in this Entity
* @param pk the pk of the occurrence
*/
Entity.prototype.hasOcc = function (pk) {
return this.occs.find(function (occ) { return occ.getPk() === pk; }) != null;
};
/**
* Sets the given Occurence on the given value. If pk is missing, the first Occurrence is set.
* @param occ the new Occurrence
* @param pk the pk of the Occurrence
*/
Entity.prototype.setOcc = function (occ, pk) {
if (pk) {
this.occs = this.occs.map(function (oldOcc) {
if (oldOcc.getPk() === pk) {
return occ;
}
else {
return oldOcc;
}
});
}
else {
this.occs[0] = occ;
}
};
/**
* Gets the Occurrence with the given pk, if no pk is given it gets the first Occurence
* @param pk PK of the Occurrence
*/
Entity.prototype.getOcc = function (pk) {
if (pk) {
return this.occs.find(function (occ) { return occ.getPk() === pk; });
}
else {
var occ = void 0;
if (this.occs && this.occs.length > 0) {
occ = this.occs[0];
}
else {
occ = new Occurrence();
this.addOcc(occ);
}
return occ;
}
};
/**
* Gets the structure of the Entity
*/
Entity.prototype.getStructure = function () {
return this.structure;
};
/**
* Adds an Occurrence to the Entity
* @param occ the new Occurrence
* @param modeFlag decides, if the add mode should be set for occ
* @param isUserChange is that a User Change
* @returns the pk of the new Occ
*/
Entity.prototype.addOcc = function (occ, add, isUserChange) {
var addFlag = add !== undefined && add !== null ? add : true;
if (!occ) {
occ = new Occurrence();
}
occ.setMode({ add: addFlag });
if (isUserChange) {
occ.setModified(true, false);
}
if (!occ.getPk()) {
var pk = 'new_';
var index_1 = 1;
this.occs.forEach(function (occ) {
if (occ.getPk().substr(0, 4) === 'new_' && Number(occ.getPk().substr(4)) >= index_1) {
index_1 = Number(occ.getPk().substr(4)) + 1;
}
});
pk += index_1;
occ.addPk(pk);
}
if (!occ.getStructure() || !Object.keys(occ.getStructure()).length) {
occ.setStructure(this.structure);
}
var occIndex = this.occs.findIndex(function (o) { return o.getPk() === occ.getPk(); });
if (occIndex >= 0) {
this.occs[occIndex] = occ;
}
else {
this.occs.push(occ);
}
return occ.getPk();
};
/**
* Copys an Occurrence to the Entity
* @param pk pk of the occurrence that should be copied
* @param modeFlag decides, if the copy mode should be set for the new occ
* @param isUserChange is this a User Change?
* @returns the pk of the new Occ
*/
Entity.prototype.copyOcc = function (pk, cpy, isUserChange) {
var cpyFlag = cpy !== undefined && cpy !== null ? cpy : true;
var occ = this.getOcc(pk);
var addFlag = occ.getMode() === 'add';
if (addFlag) {
cpyFlag = false;
}
var copyCount = this.occs.filter(function (o) { return o.getPk().search(new RegExp('^(?:copy_)?' + occ.getPk())) >= 0; }).length;
var copyOcc = new Occurrence({
pk: 'copy_' + occ.getPk() + '#' + copyCount,
data: __assign({}, occ.getData()),
structure: occ.getStructure(),
meta: occ.getMeta(),
}, { add: addFlag, cpy: cpyFlag });
if (isUserChange) {
occ.setModified(true, false);
}
this.occs.push(copyOcc);
return copyOcc.getPk();
};
/**
* Adds the Occurrences of an Entity to this Occurrences
* @param entity the Entity that should be added
*/
Entity.prototype.addEntity = function (entity) {
var _a;
var _this = this;
(_a = this.occs).push.apply(_a, __spread(this.convertOccurences(entity.occs).filter(function (occ) { return !_this.hasOcc(occ.getPk()); })));
};
Entity.prototype.convertOccurences = function (occs, keepModes) {
var _this = this;
if (keepModes === void 0) { keepModes = true; }
return occs.map(function (occ) {
return occ instanceof Occurrence
? occ
: new Occurrence(occ.pk ? occ : { pk: occ._pk, data: occ, structure: _this.structure || {} }, keepModes
? {
add: (occ.data != null ? occ.data._mode : occ._mode) === exports.ModeFlags.ADD,
cpy: (occ.data != null ? occ.data._mode : occ._mode) === exports.ModeFlags.CPY,
del: (occ.data != null ? occ.data._mode : occ._mode) === exports.ModeFlags.DEL,
}
: null);
});
};
/**
* Sets the modified flag for all occs in the entity
* @param modified occ is modified?
* @param setSubEntites should the modified flag be set also in the subentites? (default: false)
*/
Entity.prototype.setModified = function (modified, setSubEntites) {
var e_1, _a;
if (setSubEntites === void 0) { setSubEntites = false; }
try {
for (var _b = __values(this.getOccs()), _c = _b.next(); !_c.done; _c = _b.next()) {
var occ = _c.value;
occ.setModified(modified, setSubEntites);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* Has the Entity been modified?
* @param checkSubEnt check Subentities (default: true)
*/
Entity.prototype.isModified = function (checkSubEnt) {
var e_2, _a;
if (checkSubEnt === void 0) { checkSubEnt = true; }
try {
for (var _b = __values(this.getOccs()), _c = _b.next(); !_c.done; _c = _b.next()) {
var occ = _c.value;
if (occ.isModified(checkSubEnt)) {
return true;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return false;
};
return Entity;
}());
var EntityDef = /** @class */ (function () {
function EntityDef(entityDef) {
if (entityDef instanceof EntityDef) {
this.fields = entityDef.getFields();
this.altdef = entityDef.getAltdef();
this.init = entityDef.getInitMode();
}
else {
this.fields = entityDef
? Object.entries(entityDef.fields)
.map(function (_a) {
var _b = __read(_a, 2), fldname = _b[0], fld = _b[1];
return [fldname, new Field(fld)];
})
.reduce(function (fields, _a) {
var _b;
var _c = __read(_a, 2), fldname = _c[0], fld = _c[1];
return Object.assign(fields, (_b = {}, _b[fldname] = fld, _b));
}, {})
: {};
this.altdef = entityDef ? entityDef.altdef || 0 : 0;
this.init = entityDef ? entityDef.init || 0 : 0;
}
}
/**
* Gets the fields of this Entity Definition
*/
EntityDef.prototype.getFields = function () {
return this.fields;
};
/**
* Get the given field from this Entity Definition
* @param fld the name of the field
*/
EntityDef.prototype.getField = function (fld) {
if (typeof fld === 'string') {
return this.fields ? this.fields[fld.toUpperCase()] : undefined;
}
else {
return undefined;
}
};
/**
* Get a list of all fields, that can be seen.
* @param type 'lst' for list views (table), 'occ' for detail views, empty for reading without filter
* @param write read and write rights are needed
* @returns an array with all fields that fits the rigths
*/
EntityDef.prototype.getFieldList = function (options) {
var _this = this;
if (this.fields) {
var result = Object.entries(this.fields);
if (options.type) {
var checkType_1 = 'ar_' + options.type;
if (options.write) {
result = result.filter(function (_a) {
var _b = __read(_a, 2), fld = _b[0], field = _b[1];
return _this.canReadWrite(fld, checkType_1, options.meta ? options.meta[fld] : undefined);
});
}
else {
result = result.filter(function (_a) {
var _b = __read(_a, 2), fld = _b[0], field = _b[1];
return _this.canRead(fld, checkType_1, options.meta ? options.meta[fld] : undefined);
});
}
}
return result.map(function (_a) {
var _b = __read(_a, 2), fld = _b[0], field = _b[1];
var bez = field.bez;
if (options.meta && options.meta[fld] && options.meta[fld].bez) {
bez = options.meta[fld].bez;
}
return { fld: fld, bez: bez };
});
}
else {
return [];
}
};
/**
* Checks if the altdef flag is set.
*/
EntityDef.prototype.getAltdef = function () {
return this.altdef != null ? this.altdef : 0;
};
/**
* Checks if the init flag is set.
*/
EntityDef.prototype.getInitMode = function () {
return this.init != null ? this.init : 0;
};
/**
* Checks if the given field can be seen in a list
* @param fld the name of the field
* @param field an alternative field definition
*/
EntityDef.prototype.listCanRead = function (fld, field) {
return this.canRead(fld, 'ar_lst', field);
};
EntityDef.prototype.canRead = function (fld, checkType, field) {
var defaultField = this.getField(fld);
return defaultField && defaultField.canRead(checkType, field);
};
EntityDef.prototype.canReadWrite = function (fld, checkType, field) {
var defaultField = this.getField(fld);
return defaultField && defaultField.canReadWrite(checkType, field);
};
return EntityDef;
}());
var ModelsMap = {
ENTITY: Entity,
OCCURRENCE: Occurrence,
ENTITYDEF: EntityDef
};
/**
* Different types for sending request to the backend
*/
(function (AccessTypes) {
/**
* List of occurence