[SDC-29] rebase continue work to align source
[sdc.git] / catalog-ui / src / app / ng2 / services / properties.service.ts
1 import { Injectable } from '@angular/core';
2 import { DataTypeModel, PropertyFEModel, PropertyBEModel, SchemaProperty, DerivedFEProperty, DerivedFEPropertyMap, DerivedPropertyType, InputFEModel} from "app/models";
3 import { DataTypeService } from "./data-type.service";
4 import { PROPERTY_TYPES } from "app/utils";
5 import { ContentAfterLastDotPipe } from "../pipes/contentAfterLastDot.pipe";
6 import { UUID } from "angular2-uuid";
7
8 @Injectable()
9 export class PropertiesService {
10
11     constructor(private dataTypeService: DataTypeService, private contentAfterLastDotPipe: ContentAfterLastDotPipe) {
12     }
13
14     public getParentPropertyFEModelFromPath = (properties: Array<PropertyFEModel>, path: string) => {
15         let parent: PropertyFEModel = _.find(properties, (property: PropertyFEModel): boolean => {
16             return property.name === path.substring(0, path.indexOf('#'));
17         });
18         return parent;
19     }
20
21     //undo disabling of parent and child props=
22     public undoDisableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
23         property.isDisabled = false;
24         if (!childPath) {
25             property.isSelected = false;
26             property.flattenedChildren && property.flattenedChildren.map(child => child.isDisabled = false);
27         } 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
28             property.flattenedChildren.filter(child => child.isDisabled && !child.isDeclared).map(child => child.isDisabled = false);
29             property.flattenedChildren.filter(child => child.isDeclared || child.isSelected).forEach((childProp) => { //handle brothers who are selected - redo their disabled relatives as well
30                 this.disableRelatedProperties(property, childProp.propertiesName);
31             });
32         }
33     }
34
35     //disable parents and children of prop
36     public disableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
37         if (!childPath) { //selecting the parent property
38             property.isSelected = true;
39             property.flattenedChildren && property.flattenedChildren.map(child => { child.isSelected = false; child.isDisabled = true; });
40         } else {
41             property.isSelected = false;
42             property.isDisabled = true;
43             property.flattenedChildren.filter((childProp: DerivedFEProperty) => {
44                 return (childProp.propertiesName.indexOf(childPath + "#") === 0 //is child of prop to disable
45                     || childPath.indexOf(childProp.propertiesName + "#") === 0); //is parent of prop to disable
46             }).map((child: DerivedFEProperty) => { child.isSelected = false; child.isDisabled = true; });
47         }
48     }
49
50     public getCheckedProperties = (properties: Array<PropertyFEModel>): Array<PropertyBEModel> => {
51         let selectedProps: Array<PropertyBEModel> = [];
52         properties.forEach(prop => {
53             if (prop.isSelected && !prop.isDeclared && !prop.isDisabled) {
54                 selectedProps.push(new PropertyBEModel(prop));
55             } else if (prop.flattenedChildren) {
56                 prop.flattenedChildren.forEach((child) => {
57                     if (child.isSelected && !child.isDeclared && !child.isDisabled) {
58                         let childProp = new PropertyBEModel(prop, child); //create it from the parent
59                         selectedProps.push(childProp);
60                     }
61                 })
62             }
63         });
64         return selectedProps;
65     }
66
67
68 }