blob: e5da9f4bf146978e2cb5e64e4c008226c8f18620 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
import { Component, ViewChild, OnInit, EventEmitter} from '@angular/core';
import { PreferenceView, View, ViewService} from './tableview.service';
import { EditViewComponent } from './editview.component';
import {classToClass} from 'class-transformer';
import { ModalDirective } from 'ngx-bootstrap';
import { OverwriteDialogComponent } from '../core/overwrite-dialog.component';
import { MDMNotificationService } from '../core/mdm-notification.service';
import { Scope } from '../core/preference.service';
import { TranslateService } from '@ngx-translate/core';
import { TRANSLATE } from '../core/mdm-core.module';
@Component({
selector: 'mdm-view',
templateUrl: 'view.component.html'
})
export class ViewComponent implements OnInit {
public selectedView: View;
public groupedViews: { scope: string, view: View[], label: string }[] = [];
public viewChanged$ = new EventEmitter();
public viewName = '';
public userViewNames: string[];
public selectedRow: string;
public lazySelectedRow: string;
@ViewChild(EditViewComponent)
editViewComponent: EditViewComponent;
@ViewChild('lgSaveModal')
childSaveModal: ModalDirective;
@ViewChild(OverwriteDialogComponent)
overwriteDialogComponent: OverwriteDialogComponent;
constructor(private viewService: ViewService,
private notificationService: MDMNotificationService,
private translateService: TranslateService) {
}
ngOnInit() {
this.viewService.getViews().subscribe(
views => this.setViews(views),
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-load-views'), error)
);
this.viewService.viewSaved$.subscribe(
view => this.onViewSaved(view),
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-saving-view'), error)
);
this.viewService.viewDeleted$.subscribe(
() => this.onDeleteView(),
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-deleting-view'), error)
);
}
selectView(view: View) {
this.selectedView = view;
this.viewChanged$.emit();
}
public editSelectedView(e: Event) {
e.stopPropagation();
this.editViewComponent.showDialog(this.selectedView).subscribe(
v => this.selectedView = v,
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-select-view'), error)
);
}
public newView(e: Event) {
e.stopPropagation();
this.editViewComponent.showDialog(new View()).subscribe(
v => this.selectedView = v,
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-display-view-editor'), error)
);
}
private onDeleteView() {
this.viewService.getViews().subscribe(
prefViews => {
this.getGroupedView(prefViews);
if (prefViews.find(pv => pv.view.name === this.selectedView.name) == undefined
&& this.viewService.defaultPrefViews.find(pv => pv.view.name === this.selectedView.name) == undefined) {
this.selectView(prefViews[0].view);
}
},
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-update-views'), error)
);
}
private onViewSaved(view: View) {
this.viewService.getViews().subscribe(
prefViews => this.getGroupedView(prefViews),
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-update-views'), error)
);
}
private setViews(prefViews: PreferenceView[]) {
this.getGroupedView(prefViews);
this.selectView(prefViews[0].view);
}
private getGroupedView(prefViews: PreferenceView[]) {
this.groupedViews = [];
for (let i = 0; i < prefViews.length; i++) {
let pushed = false;
for (let j = 0; j < this.groupedViews.length; j++) {
if (prefViews[i].scope === this.groupedViews[j].scope) {
this.groupedViews[j].view.push(prefViews[i].view);
pushed = true;
}
}
if (pushed === false) { this.groupedViews.push({
scope: prefViews[i].scope,
view: [prefViews[i].view],
label: Scope.toLabel(prefViews[i].scope)
}); }
}
this.updateUserViewNames();
}
private updateUserViewNames() {
this.viewService.getViews().subscribe(
prefViews => this.userViewNames = prefViews.filter(pv => pv.scope === Scope.USER)
.map(pv => pv.view.name),
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-update-views'), error)
);
}
saveView(e: Event) {
e.stopPropagation();
if (this.groupedViews.find(gv => gv.view.find(v => v.name === this.viewName) != undefined)) {
this.childSaveModal.hide();
this.overwriteDialogComponent.showOverwriteModal(this.translateService.instant('tableview.view.a-view')).subscribe(
needSave => this.saveView2(needSave),
error => {
this.saveView2(false);
this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-save-view'), error);
}
);
} else {
this.saveView2(true);
}
}
saveView2(save: boolean) {
if (save) {
let view = classToClass(this.selectedView);
view.name = this.viewName;
this.viewService.saveView(view);
this.childSaveModal.hide();
this.selectView(classToClass(view));
} else {
this.childSaveModal.show();
}
}
showSaveModal(e: Event) {
e.stopPropagation();
this.viewName = this.selectedView.name === this.translateService.instant('tableview.view.new-view') ? '' : this.selectedView.name;
this.childSaveModal.show();
}
deleteView(e: Event) {
e.stopPropagation();
let userGroup = this.groupedViews.find(gv => gv.scope === Scope.USER);
if (userGroup && userGroup.view.length > 0) {
this.viewService.deleteView(this.selectedView.name).subscribe(() =>
this.viewService.getViews().subscribe(
views => {
this.setViews(views);
this.viewService.viewDeleted$.emit();
},
error => this.notificationService.notifyError(this.translateService.instant('tableview.view.err-cannot-delete-view'), error))
);
} else {
this.notificationService.notifyError('Forbidden.',
this.translateService.instant('tableview.view.err-cannot-modify-system-settings'));
}
}
getSaveBtnTitle() {
return this.viewName ? TRANSLATE('tableview.view.tooltip-save-view') : TRANSLATE('tableview.view.tooltip-no-name-set');
}
onRowSelect(e: any) {
if (this.lazySelectedRow !== e.data) {
this.selectedRow = e.data;
this.viewName = e.data;
} else {
this.selectedRow = undefined;
this.viewName = '';
}
this.lazySelectedRow = this.selectedRow;
}
}