[SDC-29] rebase continue work to align source
[sdc.git] / catalog-ui / src / app / ng2 / services / hierarchy-nav.service.ts
1 import { Injectable } from '@angular/core';
2 import { SimpleFlatProperty, PropertyFEModel, DerivedFEProperty } from 'app/models';
3
4
5 @Injectable()
6 export class HierarchyNavService {
7     /**
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
12      *
13      * @argument property: PropertyFEModel - property contains flattenedChildren array of DerivedFEProperty
14      * @returns  Array<SimpleFlatProperty> - containing childrens Array<SimpleFlatProperty>, augmantin childrens to SimpleFlatProperty.
15      */
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));
23         });
24
25         let tree = this.unflatten(flattenProperties, '', []);
26         return tree[0].childrens; // Return the childrens without the root.
27     }
28
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);
32         } else {
33             let propName: string = (property.isChildOfListOrMap) ? property.mapKey : property.name;
34             return new SimpleFlatProperty(property.uniqueId, property.propertiesName, propName, property.parentName, instanceName);
35         }
36         
37     }
38
39     /**
40      * Unflatten Array<SimpleFlatProperty> and build hirarchy.
41      * The result will be Array<SimpleFlatProperty> that augmantin with childrens for each SimpleFlatProperty.
42      */
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: '' };
46
47         var childrens = _.filter(array, (child: SimpleFlatProperty): boolean => {
48             return child.parentName == parent.path;
49         });
50
51         if (!_.isEmpty(childrens)) {
52             if (parent.path == '') {
53                 tree = childrens;
54             } else {
55                 parent['childrens'] = childrens;
56             }
57             _.each(childrens, (child): void => {
58                 this.unflatten(array, child);
59             });
60         }
61         return tree;
62     }
63 }