UNPKG

@pilotlab/lux-attributes

Version:

A luxurious user experience framework, developed by your friends at Pilot.

103 lines 5.39 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var 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 function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var lux_is_1 = require("@pilotlab/lux-is"); var lux_debug_1 = require("@pilotlab/lux-debug"); var lux_nodes_1 = require("@pilotlab/lux-nodes"); var attributesBase_1 = require("./attributesBase"); var attributeEnums_1 = require("./attributeEnums"); var attributeChangeOptions_1 = require("./attributeChangeOptions"); var attributeCreateOptions_1 = require("./attributeCreateOptions"); var attributeTools_1 = require("./attributeTools"); var AttributesFactoryBase = (function (_super) { __extends(AttributesFactoryBase, _super); function AttributesFactoryBase(nodeFactory) { return _super.call(this, nodeFactory) || this; } Object.defineProperty(AttributesFactoryBase.prototype, "node", { get: function () { return this.p_node; }, enumerable: true, configurable: true }); AttributesFactoryBase.prototype.instance = function (parent) { return null; }; AttributesFactoryBase.prototype.fromObject = function (obj, parent, map) { try { if (obj instanceof attributesBase_1.default) return obj; var collection = this.instance(); if (lux_is_1.default.empty(obj)) return collection; for (var key in obj) { var attributeNew = void 0; if (!obj.hasOwnProperty(key)) continue; var value = obj[key]; if (lux_is_1.default.empty(value)) { attributeNew = collection.get(key, '', attributeEnums_1.DataType.STRING); if (lux_is_1.default.notEmpty(map)) map(attributeNew); continue; } if (lux_is_1.default.notEmpty(value['dataType']) && typeof value['dataType'] === 'string') { attributeNew = this.node.fromStrings(key, value['returnValue'], value['dataType'].toLowerCase()); } else { var dataType = attributeTools_1.default.getDataType(value); dataType = typeof dataType === 'string' ? dataType.toLowerCase() : 'object'; if (dataType === 'object' && lux_is_1.default.notEmpty(value)) { dataType = 'collection'; var attributes = this.fromObject(value); attributeNew = collection.get(key, attributes, attributeEnums_1.DataType.COLLECTION, null, attributeChangeOptions_1.default.zero); attributes.internalParent = attributeNew; if (lux_is_1.default.notEmpty(map)) map(attributeNew); continue; } else if (dataType === 'collection') { var attributes = value; attributeNew = collection.get(key, attributes, attributeEnums_1.DataType.COLLECTION, null, attributeChangeOptions_1.default.zero); value.internalParent = attributeNew; if (lux_is_1.default.notEmpty(map)) map(attributeNew); continue; } else attributeNew = this.node.fromStrings(key, value, dataType); } if (lux_is_1.default.notEmpty(attributeNew) && lux_is_1.default.notEmpty(attributeNew.value)) { var createOptions = new attributeCreateOptions_1.default(attributeNew.value, attributeNew.dataType, attributeNew.label, attributeChangeOptions_1.default.zero); createOptions.validate = attributeNew.validate; createOptions.omit = attributeNew.omit; createOptions.index = attributeNew.index; collection.getOrCreate(key, createOptions); if (lux_is_1.default.notEmpty(map)) map(attributeNew); } } return collection; } catch (e) { lux_debug_1.default.error(e + ', object: ' + JSON.stringify(obj), 'AttributesFactoryBase.create.fromObject(...)'); } }; AttributesFactoryBase.prototype.fromAttributes = function (collectionIn) { var collection = this.instance(); if (lux_is_1.default.empty(collectionIn) || !(collectionIn instanceof attributesBase_1.default)) return collection; collection.update(collectionIn, attributeChangeOptions_1.default.noSaveOrSignal.durationZero); return collection; }; return AttributesFactoryBase; }(lux_nodes_1.NodesFactoryBase)); exports.AttributesFactoryBase = AttributesFactoryBase; exports.default = AttributesFactoryBase; //# sourceMappingURL=attributesFactoryBase.js.map