119a59d5aff2dff425ec759e9e030b3cdb6bb198
[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 /// <reference path="../../../../../../references"/>>
21 module Sdc.ViewModels {
22     'use strict';
23
24     interface IRelationsViewModelScope extends ICompositionViewModelScope {
25         isLoading: boolean;
26         $parent: ICompositionViewModelScope;
27         getRelation(requirement:any): any;
28     }
29
30     export class RelationsViewModel {
31
32         static '$inject' = [
33             '$scope',
34             '$filter'
35         ];
36
37         constructor(private $scope:IRelationsViewModelScope,
38                     private $filter:ng.IFilterService) {
39             this.initScope();
40         }
41
42
43         private updateRC = ():void =>{
44             if(this.$scope.currentComponent) {
45                 this.$scope.currentComponent.updateRequirementsCapabilities();
46             }
47         };
48
49         private initScope = ():void => {
50
51             this.$scope.isLoading = this.$scope.$parent.isLoading;
52
53             this.$scope.getRelation = (requirement:any):any => {
54
55                 if(this.$scope.isComponentInstanceSelected() && this.$scope.currentComponent.componentInstancesRelations ) {
56                     let relationItem = _.filter(this.$scope.currentComponent.componentInstancesRelations, (relation:any) => {
57                         return relation.fromNode === this.$scope.currentComponent.selectedInstance.uniqueId &&
58                                 _.some(relation.relationships, {'requirement': requirement.name,
59                                                                 'requirementOwnerId': requirement.ownerId});
60                     });
61
62                     if (relationItem && relationItem.length) {
63                         return {
64                             type: requirement.relationship.split('.').pop(),
65                             requirementName: this.$filter('resourceName')(this.$scope.currentComponent.componentInstances[_.map
66                             (this.$scope.currentComponent.componentInstances, "uniqueId").indexOf(relationItem[0].toNode)].name)
67                         };
68                     }
69                 }
70                 return null;
71             };
72
73             if(!this.$scope.isComponentInstanceSelected()) {
74                 this.$scope.$watch('currentComponent.componentInstances + currentComponent.componentInstancesRelations', ():void => {
75                     this.updateRC();
76                 });
77                 
78             }
79         }
80     }
81 }