blob: 247b78df501415aaf08b119da887ba242073ad5d [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 { Injectable, EventEmitter } from '@angular/core';
import { defaultIfEmpty, filter, map} from 'rxjs/operators';
import { TranslateService } from '@ngx-translate/core';
import { Type, serialize, deserialize } from 'class-transformer';
import { PreferenceService, Preference, Scope } from '../core/preference.service';
import { MDMNotificationService } from '../core/mdm-notification.service';
export class ViewColumn {
type: string;
name: string;
hidden = false;
sortOrder: number;
width: number;
constructor(type: string, name: string, width?: number, sortOrder?: number) {
this.type = type;
this.name = name;
this.width = width;
this.sortOrder = sortOrder;
}
equals(vc: ViewColumn) {
return this.type === vc.type && this.name === vc.name;
}
}
export class View {
name: string;
@Type(() => ViewColumn)
columns: ViewColumn[] = [];
sortOrder: number;
sortField: string;
constructor(name?: string, cols?: ViewColumn[]) {
this.name = name || ''; // TODO TRANSLATION: find a way to name a new view "New view" in current language
this.columns = cols || [];
}
setSortOrder(type: string, name: string, order: any) {
this.columns.forEach(c => {
if (c.type === type && c.name === name ) {
c.sortOrder = order;
} else {
c.sortOrder = null;
}
});
}
getSortOrder() {
let col = this.columns.find(c => c.sortOrder !== null);
if (col) {
return col.sortOrder;
}
}
getSortField() {
let col = this.columns.find(c => c.sortOrder !== null);
if (col) {
return col.type + '.' + col.name;
}
}
}
export class PreferenceView {
scope: string;
@Type(() => View)
view: View;
constructor(scope: string, view?: View) {
this.scope = scope;
this.view = view || new View();
}
}
export class Style {
[field: string]: string
}
@Injectable()
export class ViewService {
public viewSaved$ = new EventEmitter<View>();
public viewDeleted$ = new EventEmitter();
readonly preferencePrefix = 'tableview.view.';
private views: View[] = [];
defaultPrefViews = [ new PreferenceView(Scope.SYSTEM, new View('Standard', [new ViewColumn('Test', 'Name')])) ];
constructor(private prefService: PreferenceService,
private notificationService: MDMNotificationService,
private translateService: TranslateService) {
}
getViews() {
return this.prefService.getPreference(this.preferencePrefix).pipe(
map(preferences => preferences.map(p => new PreferenceView(p.scope, deserialize(View, p.value)))),
filter(prefViews => !(prefViews == undefined || prefViews.length === 0)),
defaultIfEmpty(this.defaultPrefViews));
}
saveView(view: View) {
const pref = new Preference();
pref.value = serialize(view);
pref.key = this.preferencePrefix + view.name;
pref.scope = Scope.USER;
this.prefService.savePreference(pref).subscribe(
saved => this.viewSaved$.emit(view),
error => this.notificationService.notifyError(this.translateService.instant('tableview.tableview.err-cannot-save-view'), error)
);
}
deleteView(name: string) {
return this.prefService.deletePreferenceByScopeAndKey(Scope.USER, 'tableview.view.' + name);
}
}