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