UNPKG

@pilotlab/lux-attributes

Version:

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

107 lines (88 loc) 4.83 kB
import is from '@pilotlab/lux-is'; import Debug from '@pilotlab/lux-debug'; import { NodesFactoryBase } from '@pilotlab/lux-nodes'; import AttributesBase from './attributesBase'; import { DataType } from './attributeEnums'; import Attribute from './attribute'; import AttributeChangeOptions from './attributeChangeOptions'; import AttributeCreateOptions from './attributeCreateOptions'; import AttributeFactoryBase from './attributeFactoryBase'; import AttributeTools from './attributeTools'; import IAttributeFactory from './interfaces/iAttributeFactory'; import IAttributesFactory from './interfaces/iAttributesFactory'; import IAttribute from './interfaces/iAttribute'; import IAttributes from './interfaces/iAttributes'; export abstract class AttributesFactoryBase< TAttribute extends IAttribute, TAttributes extends IAttributes> extends NodesFactoryBase<TAttribute, TAttributes> implements IAttributesFactory { constructor(nodeFactory:IAttributeFactory) { super(nodeFactory); } get node():IAttributeFactory { return this.p_node; } protected p_node:IAttributeFactory; instance(parent?:TAttribute):TAttributes { return null; } fromObject(obj:any, parent?:IAttribute, map?:(node:TAttribute) => void):TAttributes { try { if (obj instanceof AttributesBase) return <TAttributes><IAttributes>obj; let collection:TAttributes = this.instance(); if (is.empty(obj)) return collection; for (let key in obj) { let attributeNew:TAttribute; if (!obj.hasOwnProperty(key)) continue; let value:any = obj[key]; if (is.empty(value)) { /// We got a key with no associated value, so just create a new empty string node. attributeNew = <TAttribute>collection.get(key, '', DataType.STRING); if (is.notEmpty(map)) map(attributeNew); continue; } if (is.notEmpty(value['dataType']) && typeof value['dataType'] === 'string') { attributeNew = <TAttribute>this.node.fromStrings(key, value['returnValue'], value['dataType'].toLowerCase()); } else { let dataType:string = AttributeTools.getDataType(value); dataType = typeof dataType === 'string' ? dataType.toLowerCase() : 'object'; if (dataType === 'object' && is.notEmpty(value)) { dataType = 'collection'; let attributes:TAttributes = <TAttributes>this.fromObject(value); attributeNew = <TAttribute>collection.get(key, attributes, DataType.COLLECTION, null, AttributeChangeOptions.zero); attributes.internalParent = attributeNew; if (is.notEmpty(map)) map(attributeNew); continue } else if (dataType === 'collection') { let attributes:TAttributes = value; attributeNew = <TAttribute>collection.get(key, attributes, DataType.COLLECTION, null, AttributeChangeOptions.zero); value.internalParent = attributeNew; if (is.notEmpty(map)) map(attributeNew); continue } else attributeNew = <TAttribute>this.node.fromStrings(key, value, dataType); } if (is.notEmpty(attributeNew) && is.notEmpty(attributeNew.value)) { const createOptions:AttributeCreateOptions = new AttributeCreateOptions( attributeNew.value, attributeNew.dataType, attributeNew.label, AttributeChangeOptions.zero ); createOptions.validate = attributeNew.validate; createOptions.omit = attributeNew.omit; createOptions.index = attributeNew.index; collection.getOrCreate( key, createOptions ); if (is.notEmpty(map)) map(attributeNew); } } return collection; } catch(e) { Debug.error(e + ', object: ' + JSON.stringify(obj), 'AttributesFactoryBase.create.fromObject(...)') } } fromAttributes(collectionIn:TAttributes):TAttributes { let collection:TAttributes = this.instance(); if (is.empty(collectionIn) || !(collectionIn instanceof AttributesBase)) return collection; collection.update(collectionIn, AttributeChangeOptions.noSaveOrSignal.durationZero); return collection; } } // End class export default AttributesFactoryBase;