2023-03-29 14:43:56 +02:00
|
|
|
import {ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
|
2021-02-25 16:55:56 +01:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
2022-09-23 12:25:08 +02:00
|
|
|
import {UntypedFormBuilder, UntypedFormGroup, Validators} from '@angular/forms';
|
2021-02-25 16:55:56 +01: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-02-25 16:55:56 +01:00
|
|
|
import {HelperFunctions} from '../../utils/HelperFunctions.class';
|
|
|
|
import {PageHelpContent} from '../../utils/entities/adminTool/page-help-content';
|
2022-07-12 15:50:56 +02:00
|
|
|
import {ClearCacheService} from "../../services/clear-cache.service";
|
2022-07-13 19:25:19 +02:00
|
|
|
import {NotificationHandler} from "../../utils/notification-handler";
|
2023-03-29 14:43:56 +02:00
|
|
|
import {CKEditorComponent} from "ng2-ckeditor";
|
2021-03-18 15:11:41 +01:00
|
|
|
|
2019-12-20 12:38:13 +01:00
|
|
|
@Component({
|
2021-03-02 16:52:09 +01:00
|
|
|
selector: 'page-content-form',
|
|
|
|
templateUrl: './page-help-content-form.component.html',
|
2019-12-20 12:38:13 +01:00
|
|
|
})
|
2021-03-02 16:52:09 +01:00
|
|
|
export class PageContentFormComponent implements OnInit {
|
|
|
|
|
2022-09-23 12:25:08 +02:00
|
|
|
myForm: UntypedFormGroup;
|
2021-03-02 16:52:09 +01:00
|
|
|
portal: string;
|
2022-07-27 11:34:10 +02:00
|
|
|
parentClass: string;
|
2021-03-02 16:52:09 +01:00
|
|
|
pageId: string;
|
|
|
|
pageContentId: string;
|
|
|
|
page: Page;
|
|
|
|
placementsOptions = [];
|
|
|
|
orderOptions = [];
|
2021-03-19 17:51:02 +01:00
|
|
|
public properties: EnvProperties = properties;
|
2021-03-02 16:52:09 +01:00
|
|
|
public showLoading: boolean = true;
|
|
|
|
private subs: Subscription[] = [];
|
2021-03-04 12:32:57 +01:00
|
|
|
public pageHelpContent: PageHelpContent;
|
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-02 16:52:09 +01: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 => {
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
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-02 16:52:09 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subs.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getInfo(pageId: string) {
|
|
|
|
this.showLoading = true;
|
2021-07-14 13:19:57 +02:00
|
|
|
let obs = zip(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal), this._helpContentService.getCommunityPageHelpContents(this.portal, this.properties.adminToolsAPIURL, pageId));
|
2021-03-02 16:52:09 +01:00
|
|
|
this.subs.push(obs.subscribe(
|
|
|
|
results => {
|
|
|
|
this.page = results[0];
|
|
|
|
if (this.properties.adminToolsPortalType != this.page.portalType) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
let countPageContents = results[1] ? results[1].length : 0;
|
|
|
|
for (let content of (results[1] as Array<PageHelpContent>)) {
|
|
|
|
if (this.pageContentId && this.pageContentId == content._id) {
|
|
|
|
this.pageHelpContent = content;
|
|
|
|
}
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.setOptions(this.page, countPageContents + (this.pageHelpContent ? 0 : 1));
|
|
|
|
if (!this.pageContentId) {
|
|
|
|
this.myForm = this.form;
|
|
|
|
this.initFormWithSelectOptions();
|
|
|
|
} else {
|
|
|
|
this.updateForm(this.pageHelpContent);
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.showLoading = false;
|
2023-03-29 14:43:56 +02:00
|
|
|
this.initCKEditor();
|
2021-03-02 16:52:09 +01:00
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page with id: ' + pageId, error)
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2021-02-25 16:55:56 +01: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-02 16:52:09 +01:00
|
|
|
private updateForm(pageHelpContent: PageHelpContent) {
|
|
|
|
this.pageHelpContent = pageHelpContent;
|
|
|
|
this.myForm = this.form;
|
|
|
|
if (this.pageHelpContent) {
|
|
|
|
this.myForm.patchValue((pageHelpContent));
|
|
|
|
} else {
|
|
|
|
this.initFormWithSelectOptions();
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (this.orderOptions.length == 1) {
|
|
|
|
this.myForm.get('order').disable()
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (this.placementsOptions.length == 1) {
|
|
|
|
this.myForm.get('placement').disable()
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.myForm.markAsPristine();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
initFormWithSelectOptions() {
|
|
|
|
if (this.placementsOptions.length == 1) {
|
|
|
|
this.myForm.get("placement").setValue(this.placementsOptions[0].value);
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.myForm.get("order").setValue(this.orderOptions[this.orderOptions.length - 1].value);
|
|
|
|
if (this.orderOptions.length == 1) {
|
|
|
|
this.myForm.get('order').disable()
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (this.placementsOptions.length == 1) {
|
|
|
|
this.myForm.get('placement').disable()
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public setOptions(page: Page, countContents: number) {
|
|
|
|
this.placementsOptions = [];
|
|
|
|
if (page.top) {
|
|
|
|
this.placementsOptions.push({label: "top", value: "top"});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (page.bottom) {
|
|
|
|
this.placementsOptions.push({label: "bottom", value: "bottom"});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (page.left) {
|
|
|
|
this.placementsOptions.push({label: "left", value: "left"});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.orderOptions = [];
|
|
|
|
for (let i = 1; i < countContents + 1; i++) {
|
|
|
|
this.orderOptions.push({label: "" + i, value: i});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public get form() {
|
|
|
|
return this._fb.group({
|
|
|
|
page: [this.pageId, Validators.required],
|
|
|
|
portal: this.portal,
|
|
|
|
placement: ['', Validators.required],
|
|
|
|
content: ['', Validators.required],
|
|
|
|
order: [1, Validators.required],
|
|
|
|
isActive: true,
|
|
|
|
//isPriorTo : false,
|
|
|
|
_id: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.myForm.patchValue({
|
|
|
|
page: '',
|
|
|
|
portal: this.portal,
|
|
|
|
placement: '',
|
|
|
|
content: [''],
|
|
|
|
order: 1,
|
|
|
|
isActive: true,
|
|
|
|
//isPriorTo : false,
|
|
|
|
_id: ''
|
|
|
|
});
|
|
|
|
this.myForm.markAsPristine();
|
|
|
|
}
|
|
|
|
|
2022-07-13 19:25:19 +02:00
|
|
|
handleError(message: string, error = null) {
|
|
|
|
if(error) {
|
|
|
|
console.error('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
NotificationHandler.rise(message, 'danger');
|
2021-03-02 16:52:09 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveCustom() {
|
2021-03-19 17:51:02 +01:00
|
|
|
if (this.myForm.valid) {
|
|
|
|
this.showLoading = true;
|
2022-07-13 19:25:19 +02:00
|
|
|
let pageHelpContent: PageHelpContent = this.myForm.getRawValue();
|
2021-03-19 17:51:02 +01:00
|
|
|
this.subs.push(this._helpContentService.savePageHelpContent(pageHelpContent, this.properties.adminToolsAPIURL, this.portal).subscribe(
|
|
|
|
_ => {
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Page content has been <b>successfully ' + (this.pageContentId ? 'updated' : 'created') + '</b>');
|
2021-03-19 17:51:02 +01:00
|
|
|
this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
|
|
|
|
this.showLoading = false;
|
2024-04-03 09:26:29 +02:00
|
|
|
this._clearCacheService.clearCacheInRoute("page help content saved",this.portal, this.page.route);
|
2021-03-19 17:51:02 +01:00
|
|
|
},
|
|
|
|
err => this.handleUpdateError('System error saving page content', err)
|
|
|
|
));
|
2021-03-02 16:52:09 +01:00
|
|
|
} else {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = false;
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public resetCustom() {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateForm(this.pageHelpContent);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
2022-07-13 19:25:19 +02:00
|
|
|
handleUpdateError(message: string, error = null) {
|
|
|
|
if(error) {
|
|
|
|
console.error('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
NotificationHandler.rise(message, 'danger');
|
2021-03-02 16:52:09 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|