Support of get_property in property assignment
[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     constructor() {
29     }
30
31     public getParentPropertyFEModelFromPath = (properties: Array<PropertyFEModel>, path: string) => {
32         let parent: PropertyFEModel = _.find(properties, (property: PropertyFEModel): boolean => {
33             return property.name === path.substring(0, path.indexOf('#'));
34         });
35         return parent;
36     }
37
38     //undo disabling of parent and child props=
39     public undoDisableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
40         property.isDisabled = false;
41         if (!childPath) {
42             property.isSelected = false;
43             property.flattenedChildren && property.flattenedChildren.map(child => child.isDisabled = false);
44         } 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
45             property.flattenedChildren.filter(child => child.isDisabled && !child.isDeclared).map(child => child.isDisabled = false);
46             property.flattenedChildren.filter(child => child.isDeclared || child.isSelected).forEach((childProp) => { //handle brothers who are selected - redo their disabled relatives as well
47                 this.disableRelatedProperties(property, childProp.propertiesName);
48             });
49         }
50     }
51
52     //disable parents and children of prop
53     public disableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
54         if (!childPath) { //selecting the parent property
55             property.isSelected = true;
56             property.flattenedChildren && property.flattenedChildren.map(child => { child.isSelected = false; child.isDisabled = true; });
57         } else {
58             property.isSelected = false;
59             property.isDisabled = true;
60             property.flattenedChildren.filter((childProp: DerivedFEProperty) => {
61                 return (childProp.propertiesName.indexOf(childPath + "#") === 0 //is child of prop to disable
62                     || childPath.indexOf(childProp.propertiesName + "#") === 0); //is parent of prop to disable
63             }).map((child: DerivedFEProperty) => { child.isSelected = false; child.isDisabled = true; });
64         }
65     }
66
67     public getCheckedProperties = (properties: Array<PropertyFEModel>): Array<PropertyBEModel> => {
68         let selectedProps: Array<PropertyDeclareAPIModel> = [];
69         properties.forEach(prop => {
70             if (prop.isSelected && !prop.isDeclared && !prop.isDisabled) {
71                 selectedProps.push(new PropertyDeclareAPIModel(prop));
72             } else if (prop.flattenedChildren) {
73                 prop.flattenedChildren.forEach((child) => {
74                     if (child.isSelected && !child.isDeclared && !child.isDisabled) {
75                         let childProp = new PropertyDeclareAPIModel(prop, child); //create it from the parent
76                         selectedProps.push(childProp);
77                     }
78                 })
79             }
80         });
81         return selectedProps;
82     }
83
84
85 }