2020-07-13 10:41:36 +02:00
|
|
|
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
2020-01-28 09:13:26 +01:00
|
|
|
import { LanguageService } from '@app/core/services/language/language.service';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { FormGroup, FormBuilder } from '@angular/forms';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { UiNotificationService, SnackBarNotificationLevel } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { Router } from '@angular/router';
|
2020-07-13 10:41:36 +02:00
|
|
|
import { CdkTextareaAutosize } from '@angular/cdk/text-field';
|
2020-01-28 09:13:26 +01:00
|
|
|
|
|
|
|
@Component({
|
2020-07-10 17:41:20 +02:00
|
|
|
selector: 'app-language-editor',
|
|
|
|
templateUrl: './language-editor.component.html',
|
|
|
|
styleUrls: ['./language-editor.component.scss']
|
2020-01-28 09:13:26 +01:00
|
|
|
})
|
2020-02-26 16:23:48 +01:00
|
|
|
export class LanguageEditorComponent extends BaseComponent implements OnInit, OnDestroy {
|
2020-07-13 10:41:36 +02:00
|
|
|
|
|
|
|
@ViewChild('autosize', {static: false}) autosize: CdkTextareaAutosize;
|
|
|
|
|
|
|
|
|
2020-02-26 16:23:48 +01:00
|
|
|
readonly rowHeight = 100;
|
|
|
|
readonly maxElements = 12;
|
|
|
|
|
2020-02-28 11:58:42 +01:00
|
|
|
allkeys = [];
|
2020-01-28 09:13:26 +01:00
|
|
|
keys = [];
|
2020-02-26 16:23:48 +01:00
|
|
|
visibleKeys = [];
|
|
|
|
startIndex = 0;
|
|
|
|
endIndex: number;
|
2020-01-28 09:13:26 +01:00
|
|
|
parseFinished = false;
|
|
|
|
currentLang: string;
|
|
|
|
formGroup: FormGroup;
|
|
|
|
formBuilder: FormBuilder;
|
|
|
|
|
2020-07-10 17:41:20 +02:00
|
|
|
constructor(
|
|
|
|
private language: LanguageService,
|
|
|
|
private uiNotificationService: UiNotificationService,
|
|
|
|
private translate: TranslateService,
|
|
|
|
private router: Router,
|
|
|
|
) { super(); }
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.formBuilder = new FormBuilder();
|
|
|
|
this.formGroup = this.formBuilder.group({});
|
|
|
|
this.endIndex = this.maxElements;
|
|
|
|
window.addEventListener('scroll', this.refreshFn, true);
|
|
|
|
this.language.getCurrentLanguageJSON()
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(response => {
|
|
|
|
const blob = new Blob([response.body], { type: 'application/json' });
|
|
|
|
this.convertBlobToJSON(blob);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2020-01-28 09:13:26 +01:00
|
|
|
|
2020-07-10 17:41:20 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
window.removeEventListener('scroll', this.refreshFn, true);
|
|
|
|
}
|
2020-01-28 09:13:26 +01:00
|
|
|
|
2020-07-10 17:41:20 +02:00
|
|
|
convertBlobToJSON(blob: Blob) {
|
|
|
|
const fr = new FileReader();
|
|
|
|
fr.onload = ev => {
|
|
|
|
const langObj = JSON.parse(fr.result as string);
|
|
|
|
this.convertObjectToForm(langObj, '', this.formGroup);
|
|
|
|
this.currentLang = this.language.getCurrentLanguageName();
|
|
|
|
this.keys.length = 0;
|
|
|
|
for (const key of this.allkeys) {
|
|
|
|
this.keys.push(key);
|
|
|
|
}
|
|
|
|
this.visibleKeys = this.keys.slice(this.startIndex, this.endIndex);
|
|
|
|
this.parseFinished = true;
|
2020-07-13 10:41:36 +02:00
|
|
|
this.setupAutosize();
|
2020-07-10 17:41:20 +02:00
|
|
|
};
|
|
|
|
fr.readAsText(blob);
|
|
|
|
}
|
2020-02-26 16:23:48 +01:00
|
|
|
|
2020-07-10 17:41:20 +02:00
|
|
|
convertObjectToForm(obj: any, parentKey: string, form: FormGroup) {
|
|
|
|
for (let prop in obj) {
|
|
|
|
const key = parentKey !== '' ? `${parentKey}.${prop}` : prop;
|
|
|
|
if (typeof obj[prop] === 'object') {
|
|
|
|
form.addControl(prop, this.formBuilder.group({}));
|
|
|
|
this.convertObjectToForm(obj[prop], key, form.get(prop) as FormGroup);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
form.addControl(prop, this.formBuilder.control(obj[prop]));
|
|
|
|
this.allkeys.push(key);
|
|
|
|
}
|
2020-02-28 11:58:42 +01:00
|
|
|
}
|
2020-07-10 17:41:20 +02:00
|
|
|
return;
|
2020-02-28 11:58:42 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 17:41:20 +02:00
|
|
|
updateLang() {
|
|
|
|
const json = JSON.stringify(this.formGroup.value, null, " ");
|
|
|
|
this.language.updateLanguage(json).pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
complete => {
|
|
|
|
this.onCallbackSuccess(complete);
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.onCallbackError(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-28 11:58:42 +01:00
|
|
|
}
|
2020-07-10 17:41:20 +02:00
|
|
|
|
|
|
|
onCallbackSuccess(id?: String): void {
|
|
|
|
this.uiNotificationService.snackBarNotification(this.translate.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
|
|
|
this.router.navigate(['/reload']).then(() => this.router.navigate(['/language-editor']));
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(error: any) {
|
|
|
|
this.uiNotificationService.snackBarNotification(error, SnackBarNotificationLevel.Error);
|
|
|
|
//this.validateAllFormFields(this.formGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshFn = (ev: Event) => {
|
2020-07-13 10:41:36 +02:00
|
|
|
const evDoc = (<HTMLBaseElement>ev.target);
|
2020-07-10 17:41:20 +02:00
|
|
|
if (document.scrollingElement !== undefined) {
|
2020-07-13 10:41:36 +02:00
|
|
|
this.startIndex = Math.floor(evDoc.scrollTop / this.rowHeight);
|
2020-07-10 17:41:20 +02:00
|
|
|
this.endIndex = this.startIndex + this.maxElements;
|
|
|
|
const tempKeys = this.keys.slice(this.startIndex, this.endIndex);
|
|
|
|
this.visibleKeys.length = 0;
|
|
|
|
for (const key of tempKeys) {
|
|
|
|
this.visibleKeys.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
findKeys(ev: any) {
|
|
|
|
let tempKeys = [];
|
|
|
|
if (ev.value === "") {
|
|
|
|
tempKeys = this.allkeys;
|
|
|
|
} else {
|
|
|
|
tempKeys = this.allkeys.filter((key) => (this.formGroup.get(key).value as string).toLowerCase().includes(ev.value.toLowerCase()));
|
|
|
|
window.scrollTo({ top: 0 });
|
|
|
|
}
|
|
|
|
this.keys.length = 0;
|
|
|
|
for (const key of tempKeys) {
|
|
|
|
this.keys.push(key);
|
|
|
|
}
|
|
|
|
this.visibleKeys = this.keys.slice(this.startIndex, this.endIndex);
|
2020-02-26 16:23:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 10:41:36 +02:00
|
|
|
private setupAutosize() {
|
|
|
|
this.autosize.minRows = 1;
|
|
|
|
this.autosize.maxRows = 5;
|
|
|
|
this.autosize.enabled = true;
|
|
|
|
}
|
|
|
|
|
2020-01-28 09:13:26 +01:00
|
|
|
}
|