2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
24 selector: 'policies-table',
25 templateUrl: 'policies-table.component.html',
26 styleUrls: ['policies-table.component.less'],
28 export class PoliciesTableComponent {
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>();
38 selectedPolicyToDelete: PolicyInstance;
39 deleteMsgTitle: string;
40 deleteMsgBodyTxt: string;
41 modalDeleteBtn: string;
42 modalCancelBtn: string;
44 constructor(private modalService: ModalService, private translateService: TranslateService) {
48 this.reverse = (this.sortBy === sortBy) ? !this.reverse : true;
49 const reverse = this.reverse ? 1 : -1;
51 const instanceNameMapTemp = this.instanceNamesMap;
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 : '';
59 itemIdx1Val = itemIdx1[sortBy];
60 itemIdx2Val = itemIdx2[sortBy];
62 if (itemIdx1Val < itemIdx2Val) {
64 } else if (itemIdx1Val > itemIdx2Val) {
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');
81 onDeletePolicy = () => {
82 this.deletePolicy.emit(this.selectedPolicyToDelete);
83 this.modalService.closeCurrentModal();
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();