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 { PropertyFEModel, PropertyBEModel, PropertyDeclareAPIModel, DerivedFEProperty} from "app/models";
26 export class PropertiesService {
28 checkedPropertyType: string;
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('#'));
40 //undo disabling of parent and child props=
41 public undoDisableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
42 property.isDisabled = false;
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);
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; });
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; });
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);
86 setCheckedPropertyType(type: string){
87 this.checkedPropertyType = type;
90 getCheckedPropertyType(){
91 return this.checkedPropertyType;