f6b77320dfd4d480ab6d73e5fcebd91b1ceb66b4
[sdc.git] / catalog-ui / src / app / ng2 / services / properties.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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 * as _ from "lodash";
22 import { Injectable } from '@angular/core';
23 import { PropertyFEModel, PropertyBEModel, PropertyDeclareAPIModel, DerivedFEProperty} from "app/models";
24
25 @Injectable()
26 export class PropertiesService {
27
28     checkedPropertyType: string;
29
30     constructor() {
31     }
32
33     public getParentPropertyFEModelFromPath = (properties: Array<PropertyFEModel>, path: string) => {
34         let parent: PropertyFEModel = _.find(properties, (property: PropertyFEModel): boolean => {
35             return property.name === path.substring(0, path.indexOf('#'));
36         });
37         return parent;
38     }
39
40     //undo disabling of parent and child props=
41     public undoDisableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
42         property.isDisabled = false;
43         if (!childPath) {
44             property.isSelected = false;
45             property.flattenedChildren && property.flattenedChildren.map(child => child.isDisabled = false);
46         } else { //QND - unselect everything and then re-do the disabling of declared props. TODO: put a flag on propertyFEModel instead to indicate who's causing them to be disabled instead
47             property.flattenedChildren.filter(child => child.isDisabled && !child.isDeclared).map(child => child.isDisabled = false);
48             property.flattenedChildren.filter(child => child.isDeclared || child.isSelected).forEach((childProp) => { //handle brothers who are selected - redo their disabled relatives as well
49                 this.disableRelatedProperties(property, childProp.propertiesName);
50             });
51         }
52     }
53
54     //disable parents and children of prop
55     public disableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
56         if (!childPath) { //selecting the parent property
57             property.isSelected = true;
58             property.flattenedChildren && property.flattenedChildren.map(child => { child.isSelected = false; child.isDisabled = true; });
59         } else {
60             property.isSelected = false;
61             property.isDisabled = true;
62             property.flattenedChildren.filter((childProp: DerivedFEProperty) => {
63                 return (childProp.propertiesName.indexOf(childPath + "#") === 0 //is child of prop to disable
64                     || childPath.indexOf(childProp.propertiesName + "#") === 0); //is parent of prop to disable
65             }).map((child: DerivedFEProperty) => { child.isSelected = false; child.isDisabled = true; });
66         }
67     }
68
69     public getCheckedProperties = (properties: Array<PropertyFEModel>): Array<PropertyBEModel> => {
70         let selectedProps: Array<PropertyDeclareAPIModel> = [];
71         properties.forEach(prop => {
72             if (prop.isSelected && !prop.isDeclared && !prop.isDisabled) {
73                 selectedProps.push(new PropertyDeclareAPIModel(prop));
74             } else if (prop.flattenedChildren) {
75                 prop.flattenedChildren.forEach((child) => {
76                     if (child.isSelected && !child.isDeclared && !child.isDisabled) {
77                         let childProp = new PropertyDeclareAPIModel(prop, child); //create it from the parent
78                         selectedProps.push(childProp);
79                     }
80                 })
81             }
82         });
83         return selectedProps;
84     }
85
86     setCheckedPropertyType(type: string){
87         this.checkedPropertyType = type;
88     }
89
90     getCheckedPropertyType(){
91         return this.checkedPropertyType;
92     }
93
94
95 }