blob: 65ef991abef0d68e5b14bd73b3cb1d2d61d74800 [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, OnInit, ViewChild, Input, OnDestroy } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, FormArray, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { PreferenceService, Preference, Scope } from '../core/preference.service';
import { EditPreferenceComponent } from './edit-preference.component';
import {MDMNotificationService} from '../core/mdm-notification.service';
import { TranslateService } from '@ngx-translate/core';
@Component( {
selector: 'mdm-preference',
templateUrl: './preference.component.html',
styleUrls: ['./preference.component.css']
})
export class PreferenceComponent implements OnInit, OnDestroy {
@Input() preferences: Preference[];
scope: string;
subscription: any;
sub: any;
@ViewChild( EditPreferenceComponent )
editPreferenceComponent: EditPreferenceComponent;
constructor( private formBuilder: FormBuilder,
private preferenceService: PreferenceService,
private route: ActivatedRoute,
private notificationService: MDMNotificationService,
private translateService: TranslateService) { }
ngOnInit() {
this.sub = this.route.params.subscribe(
params => this.onScopeChange(params),
error => this.notificationService.notifyError(
this.translateService.instant('administration.edit-preference.err-cannot-load-scope'), error)
);
}
ngOnDestroy() {
this.sub.unsubscribe();
}
onScopeChange(params: any) {
this.scope = params['scope'].toUpperCase();
this.preferenceService.getPreferenceForScope(this.scope)
.subscribe(pref => this.preferences = pref);
}
editPreference( preference?: Preference ) {
this.subscription = this.editPreferenceComponent.childModal.onHide.subscribe(() => this.handleDialogResult() );
this.editPreferenceComponent.showDialog( preference );
}
removePreference( preference: Preference ) {
if (preference.id) {
this.preferenceService.deletePreference( preference.id )
.subscribe();
}
let index = this.preferences.findIndex( p => p === preference );
this.preferences.splice( index, 1 );
}
handleDialogResult() {
if ( !this.editPreferenceComponent.needSave ) {
return;
}
let preference = this.editPreferenceComponent.preferenceForm.value;
let index = this.preferences.findIndex( p => this.preferenceEqualsWithoutId( p, preference ) );
this.preferenceService.savePreference( preference )
.subscribe(
() => index !== -1 ? this.preferences[index] = preference : this.reloadPreference( preference ),
error => this.notificationService.notifyError(
this.translateService.instant('administration.preference.err-cannot-save-preference'), error)
);
this.subscription.unsubscribe();
}
reloadPreference( preference: Preference ) {
this.preferenceService.getPreferenceForScope( preference.scope, preference.key )
.subscribe(
p => this.preferences.push(p[0]),
error => this.notificationService.notifyError(
this.translateService.instant('administration.preference.err-cannot-update-preference'), error)
);
}
preferenceEqualsWithoutId( pref1: Preference, pref2: Preference ) {
return pref1.key === pref2.key && pref1.source === pref2.source && pref1.user === pref2.user;
}
}