Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / policies-table / policies-table.component.ts
1 /*!
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 import { Component, EventEmitter, Input, Output } from '@angular/core';
18 import { PolicyInstance } from 'app/models';
19 import { InstanceFeDetails } from 'app/models/instance-fe-details';
20 import { TranslateService } from 'app/ng2/shared/translator/translate.service';
21 import { ModalService } from '../../../services/modal.service';
22
23 @Component({
24     selector: 'policies-table',
25     templateUrl: 'policies-table.component.html',
26     styleUrls: ['policies-table.component.less'],
27 })
28 export class PoliciesTableComponent {
29
30     @Input() policies: PolicyInstance[];
31     @Input() instanceNamesMap: Map<string, InstanceFeDetails>;
32     @Input() readonly: boolean;
33     @Input() isLoading: boolean;
34     @Output() deletePolicy: EventEmitter<any> = new EventEmitter<any>();
35
36     sortBy: string;
37     reverse: boolean;
38     selectedPolicyToDelete: PolicyInstance;
39     deleteMsgTitle: string;
40     deleteMsgBodyTxt: string;
41     modalDeleteBtn: string;
42     modalCancelBtn: string;
43
44     constructor(private modalService: ModalService, private translateService: TranslateService) {
45     }
46
47     sort = (sortBy) => {
48         this.reverse = (this.sortBy === sortBy) ? !this.reverse : true;
49         const reverse = this.reverse ? 1 : -1;
50         this.sortBy = sortBy;
51         const instanceNameMapTemp = this.instanceNamesMap;
52         let itemIdx1Val = '';
53         let itemIdx2Val = '';
54         this.policies.sort((itemIdx1, itemIdx2) => {
55             if (sortBy === 'instanceUniqueId') {
56                 itemIdx1Val = (itemIdx1[sortBy] && instanceNameMapTemp[itemIdx1[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx1[sortBy]].name : '';
57                 itemIdx2Val = (itemIdx2[sortBy] && instanceNameMapTemp[itemIdx2[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx2[sortBy]].name : '';
58             } else {
59                 itemIdx1Val = itemIdx1[sortBy];
60                 itemIdx2Val = itemIdx2[sortBy];
61             }
62             if (itemIdx1Val < itemIdx2Val) {
63                 return -1 * reverse;
64             } else if (itemIdx1Val > itemIdx2Val) {
65                 return 1 * reverse;
66             } else {
67                 return 0;
68             }
69         });
70     }
71
72     ngOnInit() {
73         this.translateService.languageChangedObservable.subscribe((lang) => {
74             this.deleteMsgTitle = this.translateService.translate('DELETE_POLICY_TITLE');
75             this.modalDeleteBtn = this.translateService.translate('MODAL_DELETE');
76             this.modalCancelBtn = this.translateService.translate('MODAL_CANCEL');
77
78         });
79     }
80
81     onDeletePolicy = () => {
82         this.deletePolicy.emit(this.selectedPolicyToDelete);
83         this.modalService.closeCurrentModal();
84     }
85
86     openDeleteModal = (policy: PolicyInstance) => {
87         this.selectedPolicyToDelete = policy;
88         this.translateService.languageChangedObservable.subscribe((lang) => {
89             this.deleteMsgBodyTxt = this.translateService.translate('DELETE_POLICY_MSG', {policyName: policy.name});
90             this.modalService.createActionModal(this.deleteMsgTitle, this.deleteMsgBodyTxt, this.modalDeleteBtn, this.onDeletePolicy, this.modalCancelBtn).instance.open();
91         });
92     }
93 }