1 import { Injectable } from '@angular/core';
2 import { SimpleFlatProperty, PropertyFEModel, DerivedFEProperty } from 'app/models';
6 export class HierarchyNavService {
8 * Build hirarchy structure for the tree when user selects on table row.
9 * First create Array<SimpleFlatProperty> and insert also the parent (PropertyFEModel) to this array.
10 * The Array is flat and contains SimpleFlatProperty that has parentName and uniqueId.
11 * Now we build hirarchy from this Array (that includes childrens) and return it for the tree
13 * @argument property: PropertyFEModel - property contains flattenedChildren array of DerivedFEProperty
14 * @returns Array<SimpleFlatProperty> - containing childrens Array<SimpleFlatProperty>, augmantin childrens to SimpleFlatProperty.
16 public getSimplePropertiesTree(property: PropertyFEModel, instanceName: string): Array<SimpleFlatProperty> {
17 // Build Array of SimpleFlatProperty before unflatten function
18 let flattenProperties: Array<SimpleFlatProperty> = [];
19 flattenProperties.push(this.createSimpleFlatProperty(property, instanceName)); // Push the root property
20 _.each(property.flattenedChildren, (child: DerivedFEProperty): void => {
21 if (child.isChildOfListOrMap && child.schema.property.isSimpleType) return; //do not display non-complex children of list or map
22 flattenProperties.push(this.createSimpleFlatProperty(child, instanceName));
25 let tree = this.unflatten(flattenProperties, '', []);
26 return tree[0].childrens; // Return the childrens without the root.
29 public createSimpleFlatProperty = (property: PropertyFEModel | DerivedFEProperty, instanceName:string): SimpleFlatProperty => {
30 if (property instanceof PropertyFEModel) {
31 return new SimpleFlatProperty(property.uniqueId, property.name, property.name, '', instanceName);
33 let propName: string = (property.isChildOfListOrMap) ? property.mapKey : property.name;
34 return new SimpleFlatProperty(property.uniqueId, property.propertiesName, propName, property.parentName, instanceName);
40 * Unflatten Array<SimpleFlatProperty> and build hirarchy.
41 * The result will be Array<SimpleFlatProperty> that augmantin with childrens for each SimpleFlatProperty.
43 private unflatten(array: Array<SimpleFlatProperty>, parent: any, tree?: any): any {
44 tree = typeof tree !== 'undefined' ? tree : [];
45 parent = typeof parent !== 'undefined' && parent !== '' ? parent : { path: '' };
47 var childrens = _.filter(array, (child: SimpleFlatProperty): boolean => {
48 return child.parentName == parent.path;
51 if (!_.isEmpty(childrens)) {
52 if (parent.path == '') {
55 parent['childrens'] = childrens;
57 _.each(childrens, (child): void => {
58 this.unflatten(array, child);