Release version 1.13.7
[sdc.git] / catalog-ui / src / app / ng2 / pages / attributes-outputs / services / hierarchy-nav.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021 Nordix Foundation. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 import {Injectable} from '@angular/core';
22 import {AttributeFEModel} from "../../../../models/attributes-outputs/attribute-fe-model";
23 import {SimpleFlatAttribute} from "app/models/attributes-outputs/simple-flat-attribute";
24 import {DerivedFEAttribute} from "../../../../models/attributes-outputs/derived-fe-attribute";
25
26 @Injectable()
27 export class HierarchyNavService {
28   /**
29    * Build hierarchy structure for the tree when user selects on table row.
30    * First create Array<SimpleFlatAttribute> and insert also the parent (AttributeFEModel) to this array.
31    * The Array is flat and contains SimpleFlatAttribute that has parentName and uniqueId.
32    * Now we build hierarchy from this Array (that includes children) and return it for the tree
33    *
34    * @argument attribute: AttributeFEModel - attribute contains flattenedChildren array of DerivedFEAttribute
35    * @returns  Array<SimpleFlatAttribute> - containing children Array<SimpleFlatAttribute>, augmantin children to SimpleFlatAttribute.
36    */
37   public getSimpleAttributesTree(attribute: AttributeFEModel, instanceName: string): Array<SimpleFlatAttribute> {
38     // Build Array of SimpleFlatAttribute before unflatten function
39     let flatAttributes: Array<SimpleFlatAttribute> = [];
40     flatAttributes.push(this.createSimpleFlatAttribute(attribute, instanceName)); // Push the root attribute
41     attribute.flattenedChildren.forEach((child: DerivedFEAttribute): void => {
42       if (child.isChildOfListOrMap && child.schema.property.isSimpleType) return; //do not display non-complex children of list or map
43       flatAttributes.push(this.createSimpleFlatAttribute(child, instanceName));
44     });
45
46     let tree = this.unflatten(flatAttributes, '', []);
47     return tree[0].childrens; // Return the childrens without the root.
48   }
49
50   public createSimpleFlatAttribute = (attribute: AttributeFEModel | DerivedFEAttribute, instanceName: string): SimpleFlatAttribute => {
51     if (attribute instanceof AttributeFEModel) {
52       return new SimpleFlatAttribute(attribute.uniqueId, attribute.name, attribute.name, '', instanceName);
53     } else {
54       let attribName: string = (attribute.isChildOfListOrMap) ? attribute.mapKey : attribute.name;
55       return new SimpleFlatAttribute(attribute.uniqueId, attribute.attributesName, attribName, attribute.parentName, instanceName);
56     }
57
58   }
59
60   /**
61    * Unflatten Array<SimpleFlatAttribute> and build hierarchy.
62    * The result will be Array<SimpleFlatAttribute> that augmantin with children for each SimpleFlatAttribute.
63    */
64   private unflatten(array: Array<SimpleFlatAttribute>, parent: any, tree?: any): any {
65     tree = typeof tree !== 'undefined' ? tree : [];
66     parent = typeof parent !== 'undefined' && parent !== '' ? parent : {path: ''};
67
68     var children = array.filter((child: SimpleFlatAttribute): boolean => {
69       return child.parentName == parent.path;
70     });
71
72     if (children && children.length) {
73       if (parent.path == '') {
74         tree = children;
75       } else {
76         parent['children'] = children;
77       }
78       children.forEach((child): void => {
79         this.unflatten(array, child);
80       });
81     }
82     return tree;
83   }
84 }