2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 import * as _ from "lodash";
22 import { Injectable } from '@angular/core';
23 import { SimpleFlatProperty, PropertyFEModel, DerivedFEProperty } from 'app/models';
27 export class HierarchyNavService {
29 * Build hirarchy structure for the tree when user selects on table row.
30 * First create Array<SimpleFlatProperty> and insert also the parent (PropertyFEModel) to this array.
31 * The Array is flat and contains SimpleFlatProperty that has parentName and uniqueId.
32 * Now we build hirarchy from this Array (that includes childrens) and return it for the tree
34 * @argument property: PropertyFEModel - property contains flattenedChildren array of DerivedFEProperty
35 * @returns Array<SimpleFlatProperty> - containing childrens Array<SimpleFlatProperty>, augmantin childrens to SimpleFlatProperty.
37 public getSimplePropertiesTree(property: PropertyFEModel, instanceName: string): Array<SimpleFlatProperty> {
38 // Build Array of SimpleFlatProperty before unflatten function
39 let flattenProperties: Array<SimpleFlatProperty> = [];
40 flattenProperties.push(this.createSimpleFlatProperty(property, instanceName)); // Push the root property
41 _.each(property.flattenedChildren, (child: DerivedFEProperty): void => {
42 if (child.isChildOfListOrMap && child.schema.property.isSimpleType) return; //do not display non-complex children of list or map
43 flattenProperties.push(this.createSimpleFlatProperty(child, instanceName));
46 let tree = this.unflatten(flattenProperties, '', []);
47 return tree[0].childrens; // Return the childrens without the root.
50 public createSimpleFlatProperty = (property: PropertyFEModel | DerivedFEProperty, instanceName:string): SimpleFlatProperty => {
51 if (property instanceof PropertyFEModel) {
52 return new SimpleFlatProperty(property.uniqueId, property.name, property.name, '', instanceName);
54 let propName: string = (property.isChildOfListOrMap) ? property.mapKey : property.name;
55 return new SimpleFlatProperty(property.uniqueId, property.propertiesName, propName, property.parentName, instanceName);
61 * Unflatten Array<SimpleFlatProperty> and build hirarchy.
62 * The result will be Array<SimpleFlatProperty> that augmantin with childrens for each SimpleFlatProperty.
64 private unflatten(array: Array<SimpleFlatProperty>, parent: any, tree?: any): any {
65 tree = typeof tree !== 'undefined' ? tree : [];
66 parent = typeof parent !== 'undefined' && parent !== '' ? parent : { path: '' };
68 var childrens = _.filter(array, (child: SimpleFlatProperty): boolean => {
69 return child.parentName == parent.path;
72 if (!_.isEmpty(childrens)) {
73 if (parent.path == '') {
76 parent['childrens'] = childrens;
78 _.each(childrens, (child): void => {
79 this.unflatten(array, child);