2023-03-29 14:43:56 +02:00
|
|
|
import {ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
|
2021-03-31 13:23:24 +02:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
2022-09-23 12:25:08 +02:00
|
|
|
import {UntypedFormBuilder, UntypedFormGroup, Validators} from '@angular/forms';
|
2021-03-31 13:23:24 +02:00
|
|
|
import {Page} from '../../utils/entities/adminTool/page';
|
|
|
|
import {HelpContentService} from '../../services/help-content.service';
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
|
|
|
import {properties} from '../../../../environments/environment';
|
2021-07-14 13:19:57 +02:00
|
|
|
import {Subscriber, Subscription, zip} from 'rxjs';
|
2021-03-31 13:23:24 +02:00
|
|
|
import {DivHelpContent} from '../../utils/entities/adminTool/div-help-content';
|
2022-06-30 17:22:35 +02:00
|
|
|
import {NotificationHandler} from "../../utils/notification-handler";
|
2022-07-12 15:50:56 +02:00
|
|
|
import {ClearCacheService} from "../../services/clear-cache.service";
|
2023-03-29 14:43:56 +02:00
|
|
|
import {CKEditorComponent} from "ng2-ckeditor";
|
2021-07-14 13:19:57 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'class-content-form',
|
|
|
|
templateUrl: './class-help-content-form.component.html',
|
|
|
|
})
|
|
|
|
export class ClassContentFormComponent implements OnInit {
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2022-09-23 12:25:08 +02:00
|
|
|
myForm: UntypedFormGroup;
|
2021-03-31 13:23:24 +02:00
|
|
|
portal: string;
|
2022-07-27 11:34:10 +02:00
|
|
|
parentClass: string;
|
2021-03-31 13:23:24 +02:00
|
|
|
pageId: string;
|
|
|
|
pageContentId: string;
|
|
|
|
page: Page;
|
|
|
|
classOptions = [];
|
2021-07-14 13:19:57 +02:00
|
|
|
public properties: EnvProperties = properties;
|
2021-03-31 13:23:24 +02:00
|
|
|
public showLoading: boolean = true;
|
|
|
|
private subs: Subscription[] = [];
|
|
|
|
public pageHelpContent: DivHelpContent;
|
2023-03-29 14:43:56 +02:00
|
|
|
@ViewChild('ckEditor') ckEditor: CKEditorComponent;
|
|
|
|
|
2022-09-23 12:25:08 +02:00
|
|
|
constructor(private route: ActivatedRoute, private _router: Router, private _fb: UntypedFormBuilder,
|
2023-03-29 14:43:56 +02:00
|
|
|
private cdr: ChangeDetectorRef, private _helpContentService: HelpContentService,
|
|
|
|
private _clearCacheService: ClearCacheService) {
|
2021-03-31 13:23:24 +02:00
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
ngOnInit() {
|
2022-07-27 11:34:10 +02:00
|
|
|
this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
|
|
|
|
this.parentClass = this.route.snapshot.data.parentClass;
|
|
|
|
this.subs.push(this.route.queryParams.subscribe(params => {
|
2023-03-29 14:43:56 +02:00
|
|
|
this.pageId = params['pageId'];
|
|
|
|
this.myForm = this.form;
|
|
|
|
this.pageContentId = params['pageContentId'];
|
|
|
|
if (!this.pageId) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
this.getInfo(this.pageId);
|
|
|
|
}));
|
2021-03-31 13:23:24 +02:00
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.subs.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
getInfo(pageId: string) {
|
|
|
|
this.showLoading = true;
|
2024-09-12 09:49:03 +02:00
|
|
|
let obs = zip(this._helpContentService.getPageByPortal(pageId, this.portal),
|
|
|
|
this._helpContentService.getDivIdsFullByPortal(pageId, this.portal));
|
2021-03-31 13:23:24 +02:00
|
|
|
this.subs.push(obs.subscribe(
|
|
|
|
results => {
|
|
|
|
this.page = results[0];
|
|
|
|
if (this.properties.adminToolsPortalType != this.page.portalType) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
this.setOptions(results[1]);
|
|
|
|
if (!this.pageContentId) {
|
2023-03-29 14:43:56 +02:00
|
|
|
this.updateForm(null);
|
2021-03-31 13:23:24 +02:00
|
|
|
this.showLoading = false;
|
2023-03-29 14:43:56 +02:00
|
|
|
this.initCKEditor();
|
2021-03-31 13:23:24 +02:00
|
|
|
} else {
|
2024-09-12 09:49:03 +02:00
|
|
|
this.subs.push(this._helpContentService.getDivHelpContent(this.pageContentId, this.portal).subscribe(pageHelpContent => {
|
2023-03-29 14:43:56 +02:00
|
|
|
this.pageHelpContent = pageHelpContent;
|
|
|
|
if (this.properties.adminToolsPortalType != this.page.portalType) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
this.updateForm(this.pageHelpContent);
|
|
|
|
this.showLoading = false;
|
|
|
|
this.initCKEditor();
|
|
|
|
},
|
2021-03-31 13:23:24 +02:00
|
|
|
error => {
|
2023-03-29 14:43:56 +02:00
|
|
|
this.handleError('System error retrieving content by id ' + this.pageContentId, error)
|
|
|
|
}));
|
2021-03-31 13:23:24 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-29 14:43:56 +02:00
|
|
|
private initCKEditor() {
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
if (this.ckEditor) {
|
|
|
|
this.ckEditor.instance.on('mode', () => {
|
|
|
|
let editor = this.ckEditor.instance;
|
|
|
|
if (editor.mode === 'source') {
|
|
|
|
let editable = editor.editable();
|
|
|
|
editable.attachListener(editable, 'input', () => {
|
|
|
|
this.myForm.get('content').setValue(editor.getData());
|
|
|
|
this.myForm.get('content').markAsDirty();
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
private updateForm(pageHelpContent: DivHelpContent) {
|
|
|
|
this.pageHelpContent = pageHelpContent;
|
|
|
|
this.myForm = this.form;
|
|
|
|
if (this.pageHelpContent) {
|
|
|
|
this.myForm.patchValue((pageHelpContent));
|
|
|
|
this.myForm.get('divId').disable();
|
|
|
|
}
|
|
|
|
this.myForm.markAsPristine();
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
public setOptions(divIds) {
|
|
|
|
this.classOptions = [];
|
2023-03-29 14:43:56 +02:00
|
|
|
for (let divid of divIds) {
|
|
|
|
this.classOptions.push({label: divid.name, value: divid._id});
|
2021-03-31 13:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
public get form() {
|
|
|
|
return this._fb.group({
|
|
|
|
divId: ['', Validators.required],
|
|
|
|
content: ['', Validators.required],
|
|
|
|
isActive: true,
|
|
|
|
portal: this.portal,
|
2023-03-29 14:43:56 +02:00
|
|
|
_id: '',
|
2021-03-31 13:23:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
public reset() {
|
|
|
|
this.myForm.patchValue({
|
|
|
|
divId: ['', Validators.required],
|
|
|
|
content: ['', Validators.required],
|
|
|
|
isActive: true,
|
|
|
|
portal: '',
|
2023-03-29 14:43:56 +02:00
|
|
|
_id: '',
|
2021-03-31 13:23:24 +02:00
|
|
|
});
|
|
|
|
this.myForm.markAsPristine();
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2022-06-30 17:22:35 +02:00
|
|
|
handleError(message: string, error = null) {
|
2023-03-29 14:43:56 +02:00
|
|
|
if (error) {
|
2022-06-30 17:22:35 +02:00
|
|
|
console.error('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
NotificationHandler.rise(message, 'danger');
|
2021-03-31 13:23:24 +02:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
public saveCustom() {
|
2022-06-30 17:22:35 +02:00
|
|
|
if (this.myForm.valid) {
|
|
|
|
this.showLoading = true;
|
2022-07-13 19:25:19 +02:00
|
|
|
let pageHelpContent: DivHelpContent = this.myForm.getRawValue();
|
2024-09-12 09:49:03 +02:00
|
|
|
this.subs.push(this._helpContentService.insertOrUpdateDivHelpContent(pageHelpContent, this.portal).subscribe(
|
2022-06-30 17:22:35 +02:00
|
|
|
_ => {
|
|
|
|
this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Page content has been <b>successfully updated</b>');
|
2022-06-30 17:22:35 +02:00
|
|
|
this.showLoading = false;
|
2024-07-01 15:48:10 +02:00
|
|
|
this._clearCacheService.purgeBrowserCache("Class help text saved or updated",this.portal);
|
2022-06-30 17:22:35 +02:00
|
|
|
},
|
|
|
|
err => this.handleUpdateError('System error saving page content', err)
|
|
|
|
));
|
2021-03-31 13:23:24 +02:00
|
|
|
} else {
|
2022-06-30 17:22:35 +02:00
|
|
|
this.showLoading = false;
|
|
|
|
this.handleError('Please fill all required fields');
|
2021-03-31 13:23:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public resetCustom() {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateForm(this.pageHelpContent);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
console.error('Server responded: ' + error);
|
2022-06-30 17:22:35 +02:00
|
|
|
NotificationHandler.rise(message, 'danger');
|
2021-03-31 13:23:24 +02:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
changeStatus() {
|
|
|
|
this.myForm.get('isActive').setValue(!this.myForm.get('isActive').value);
|
|
|
|
if (this.pageHelpContent && this.myForm.get('isActive').value != this.pageHelpContent.isActive || !this.pageHelpContent && !this.myForm.get('isActive').value) {
|
|
|
|
this.myForm.get('isActive').markAsDirty();
|
|
|
|
} else {
|
|
|
|
this.myForm.get('isActive').markAsPristine()
|
|
|
|
}
|
|
|
|
}
|
2023-03-29 14:43:56 +02:00
|
|
|
|
2021-03-31 13:23:24 +02:00
|
|
|
contentChanged() {
|
|
|
|
if (this.pageHelpContent && this.myForm.get('content').value != this.pageHelpContent.content || !this.pageHelpContent && this.myForm.get('content').value != '') {
|
|
|
|
this.myForm.get('content').markAsDirty();
|
|
|
|
} else {
|
|
|
|
this.myForm.get('content').markAsPristine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|