Sync Integ to Master
[sdc.git] / catalog-ui / src / app / directives / capabilities-and-requirements / requirement / requirements-list-directive.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 ob0695 on 5/9/2017.
23  */
24 /**
25  * Created by ob0695 on 5/9/2017.
26  */
27 /**
28  * Created by obarda on 1/8/2017.
29  */
30 'use strict';
31 import * as _ from "lodash";
32 import {RequirementsGroup, Component, Relationship, RelationshipModel} from "app/models";
33
34 export interface IRequirementsListScope extends ng.IScope {
35
36     requirements:RequirementsGroup;
37     currentComponent: Component;
38 }
39
40
41 export class RequirementsListDirective implements ng.IDirective {
42
43     constructor(private $filter: ng.IFilterService) {
44
45     }
46
47     scope = {
48         requirements: '=',
49         component: '='
50     };
51
52     restrict = 'E';
53     replace = true;
54     template = ():string => {
55         return require('./requirements-list-view.html');
56     };
57
58     link = (scope:IRequirementsListScope) => {
59
60         scope.isInstanceSelected = () : boolean => {
61             return  scope.component && scope.component.selectedInstance != undefined && scope.component.selectedInstance != null;
62         }
63
64         scope.getRelation = (requirement:any):any => {
65             if (scope.isInstanceSelected() && scope.component.componentInstancesRelations) {
66                 let relationItem:Array<RelationshipModel> = _.filter((<Component>scope.component).componentInstancesRelations, (relation:RelationshipModel) => {
67                     return relation.fromNode === scope.component.selectedInstance.uniqueId &&
68                         _.filter(relation.relationships, (relationship:Relationship) => {
69                             return relationship.relation.requirement == requirement.name && relationship.relation.requirementOwnerId == requirement.ownerId;
70                         }).length;
71                 });
72
73                 if (relationItem && relationItem.length) {
74                     return {
75                         type: requirement.relationship.split('.').pop(),
76                         requirementName: this.$filter('resourceName')(scope.component.componentInstances[_.map
77                         (scope.component.componentInstances, "uniqueId").indexOf(relationItem[0].toNode)].name)
78                     };
79                 }
80             }
81             return null;
82         };
83
84     };
85
86     public static factory = ($filter: ng.IFilterService)=> {
87         return new RequirementsListDirective($filter);
88     };
89 }
90
91 RequirementsListDirective.factory.$inject = ['$filter'];