re base code
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / req-and-capabilities / req-and-capabilities-view-model.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 /**
22  * Created by rcohen on 9/22/2016.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
27 import {ModalsHandler} from "app/utils";
28 import {Capability, PropertyModel, Requirement} from "app/models";
29 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
30 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
31
32 export class SortTableDefined {
33     reverse:boolean;
34     sortByField:string;
35 }
36
37 interface IReqAndCapabilitiesViewModelScope extends IWorkspaceViewModelScope {
38     requirementsTableHeadersList:Array<any>;
39     capabilitiesTableHeadersList:Array<any>;
40     capabilityPropertiesTableHeadersList:Array<any>;
41     requirementsSortTableDefined:SortTableDefined;
42     capabilitiesSortTableDefined:SortTableDefined;
43     propertiesSortTableDefined:SortTableDefined;
44     requirements:Array<Requirement>;
45     capabilities:Array<Capability>;
46     mode:string;
47     filteredProperties:Array<Array<PropertyModel>>;
48     searchText:string;
49
50     sort(sortBy:string, sortByTableDefined:SortTableDefined):void;
51     updateProperty(property:PropertyModel, indexInFilteredProperties:number):void;
52     allCapabilitiesSelected(selected:boolean):void;
53 }
54
55 export class ReqAndCapabilitiesViewModel {
56
57     static '$inject' = [
58         '$scope',
59         '$filter',
60         'ModalsHandler',
61         'ComponentServiceNg2'
62     ];
63
64
65     constructor(private $scope:IReqAndCapabilitiesViewModelScope,
66                 private $filter:ng.IFilterService,
67                 private ModalsHandler:ModalsHandler,
68                 private ComponentServiceNg2: ComponentServiceNg2) {
69
70         this.initCapabilitiesAndRequirements();
71     }
72
73     private initCapabilitiesAndRequirements = (): void => {
74
75         if(!this.$scope.component.capabilities || !this.$scope.component.requirements) {
76             this.$scope.isLoading = true;
77             this.ComponentServiceNg2.getCapabilitiesAndRequirements(this.$scope.component.componentType, this.$scope.component.uniqueId).subscribe((response:ComponentGenericResponse) => {
78                 this.$scope.component.capabilities = response.capabilities;
79                 this.$scope.component.requirements = response.requirements;
80                 this.initScope();
81                 this.$scope.isLoading = false;
82             }, () => {
83                 this.$scope.isLoading = false;
84             });
85         } else {
86             this.initScope();
87         }
88
89     }
90
91     private openEditPropertyModal = (property:PropertyModel, indexInFilteredProperties:number):void => {
92         //...because there is not be api
93         _.forEach(this.$scope.filteredProperties[indexInFilteredProperties], (prop:PropertyModel)=> {
94             prop.readonly = true;
95         });
96         this.ModalsHandler.openEditPropertyModal(property, this.$scope.component, this.$scope.filteredProperties[indexInFilteredProperties], false, "component", this.$scope.component.uniqueId).then(() => {
97
98         });
99     };
100
101     private initScope = ():void => {
102
103         this.$scope.requirementsSortTableDefined = {
104             reverse: false,
105             sortByField: 'name'
106         };
107         this.$scope.capabilitiesSortTableDefined = {
108             reverse: false,
109             sortByField: 'name'
110         };
111         this.$scope.propertiesSortTableDefined = {
112             reverse: false,
113             sortByField: 'name'
114         };
115
116         this.$scope.setValidState(true);
117         this.$scope.requirementsTableHeadersList = [
118             {title: 'Name', property: 'name'},
119             {title: 'Capability', property: 'capability'},
120             {title: 'Node', property: 'node'},
121             {title: 'Relationship', property: 'relationship'},
122             {title: 'Connected To', property: ''},
123             {title: 'Occurrences', property: ''}
124         ];
125         this.$scope.capabilitiesTableHeadersList = [
126             {title: 'Name', property: 'name'},
127             {title: 'Type', property: 'type'},
128             {title: 'Description', property: ''},
129             {title: 'Valid Source', property: ''},
130             {title: 'Occurrences', property: ''}
131         ];
132         this.$scope.capabilityPropertiesTableHeadersList = [
133             {title: 'Name', property: 'name'},
134             {title: 'Type', property: 'type'},
135             {title: 'Schema', property: 'schema.property.type'},
136             {title: 'Description', property: 'description'},
137         ];
138         this.$scope.filteredProperties = [];
139
140         this.$scope.mode = 'requirements';
141         this.$scope.requirements = [];
142         _.forEach(this.$scope.component.requirements, (req:Array<Requirement>, capName)=> {
143             this.$scope.requirements = this.$scope.requirements.concat(req);
144         });
145
146         this.$scope.capabilities = [];
147         _.forEach(this.$scope.component.capabilities, (cap:Array<Capability>, capName)=> {
148             this.$scope.capabilities = this.$scope.capabilities.concat(cap);
149         });
150
151         this.$scope.sort = (sortBy:string, sortByTableDefined:SortTableDefined):void => {
152             sortByTableDefined.reverse = (sortByTableDefined.sortByField === sortBy) ? !sortByTableDefined.reverse : false;
153             sortByTableDefined.sortByField = sortBy;
154         };
155
156         this.$scope.updateProperty = (property:PropertyModel, indexInFilteredProperties:number):void => {
157             this.openEditPropertyModal(property, indexInFilteredProperties);
158         };
159
160         this.$scope.allCapabilitiesSelected = (selected:boolean):void => {
161             _.forEach(this.$scope.capabilities, (cap:Capability)=> {
162                 cap.selected = selected;
163             });
164         };
165     }
166 }
167