2023-09-25 10:36:05 +02:00
|
|
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
|
|
|
import {HelpContentService} from "../../../services/help-content.service";
|
|
|
|
import {
|
|
|
|
FormArray,
|
|
|
|
UntypedFormArray,
|
|
|
|
UntypedFormBuilder,
|
|
|
|
UntypedFormGroup,
|
|
|
|
ValidatorFn,
|
|
|
|
Validators
|
|
|
|
} from "@angular/forms";
|
|
|
|
import {Page} from "../../../utils/entities/adminTool/page";
|
|
|
|
import {EnvProperties} from '../../../utils/properties/env-properties';
|
|
|
|
import {HelperFunctions} from "../../../utils/HelperFunctions.class";
|
|
|
|
import {Subscriber} from "rxjs";
|
|
|
|
import {properties} from "../../../../../environments/environment";
|
|
|
|
import {PortalUtils} from "../../portal/portalHelper";
|
|
|
|
import {AlertModal} from "../../../utils/modal/alert";
|
|
|
|
import {Option} from "../../../sharedComponents/input/input.component";
|
|
|
|
import {Title} from "@angular/platform-browser";
|
|
|
|
import {ClearCacheService} from "../../../services/clear-cache.service";
|
|
|
|
import {NotificationHandler} from "../../../utils/notification-handler";
|
|
|
|
import {PluginsService} from "../../../services/plugins.service";
|
|
|
|
import {PluginTemplate} from "../../../utils/entities/adminTool/pluginTemplate";
|
|
|
|
import {Entity} from "../../../utils/entities/adminTool/entity";
|
|
|
|
import {StringUtils} from "../../../utils/string-utils.class";
|
2024-01-19 09:53:17 +01:00
|
|
|
import {PluginEditEvent} from "../utils/base-plugin.component";
|
|
|
|
import {StakeholderEntities} from "../../../monitor/entities/stakeholder";
|
2023-09-25 10:36:05 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'plugin-templates',
|
|
|
|
templateUrl: './pluginTemplates.component.html',
|
|
|
|
})
|
|
|
|
export class PluginTemplatesComponent implements OnInit {
|
|
|
|
@ViewChild('editModal') editModal: AlertModal;
|
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
|
|
private selectedId: string;
|
|
|
|
public checkboxes: {
|
|
|
|
template: PluginTemplate;
|
|
|
|
checked: boolean;
|
|
|
|
}[] = [];
|
|
|
|
public templates: PluginTemplate[] = [];
|
|
|
|
public templateForm: UntypedFormGroup;
|
|
|
|
public pagesCtrl: UntypedFormArray;
|
|
|
|
urlValidator: ValidatorFn = StringUtils.urlValidator;
|
|
|
|
private searchText: RegExp = new RegExp('');
|
|
|
|
public keyword: string = "";
|
|
|
|
public properties: EnvProperties = properties;
|
|
|
|
public formPages: Page[] = [];
|
|
|
|
public showLoading: boolean = true;
|
|
|
|
public filterForm: UntypedFormGroup;
|
|
|
|
private subscriptions: any[] = [];
|
|
|
|
public allPages: Option[] = [];
|
|
|
|
public attrTypeOptions: Option[] = [
|
|
|
|
{label:"Text", value:"text"},
|
|
|
|
{label:"HTML", value:"HTML"},
|
|
|
|
{label:"Boolean", value:"boolean"},
|
|
|
|
{label:"URL", value:"URL"},
|
|
|
|
];
|
2023-09-26 16:32:27 +02:00
|
|
|
public placementsOptions: Option[] = [
|
|
|
|
{label:"Top", value:"top"},
|
|
|
|
{label:"Bottom", value:"bottom"},
|
|
|
|
{label:"Top Right", value:"top-right"},
|
|
|
|
{label:"Center", value:"center"},
|
|
|
|
{label:"Right", value:"right"},
|
|
|
|
{label:"Left", value:"left"},
|
|
|
|
];
|
2024-01-19 09:53:17 +01:00
|
|
|
plans: Option[] = [
|
|
|
|
{value: 'starter', label: 'Starter'},
|
|
|
|
{value: 'extended', label: 'Extended'}
|
|
|
|
];
|
2023-09-25 10:36:05 +02:00
|
|
|
selectedCommunityPid = null;
|
|
|
|
public portalUtils: PortalUtils = new PortalUtils();
|
|
|
|
private index: number;
|
2024-01-19 09:53:17 +01:00
|
|
|
// pluginObject:any = {};
|
2023-10-17 08:20:16 +02:00
|
|
|
pluginsCount = {};
|
2023-09-25 10:36:05 +02:00
|
|
|
constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router,
|
|
|
|
private title: Title,private _helpContentService: HelpContentService,
|
|
|
|
private _pluginsService: PluginsService, private _fb: UntypedFormBuilder,
|
|
|
|
private _clearCacheService: ClearCacheService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.title.setTitle('Administrator Dashboard | Classes');
|
|
|
|
this.filterForm = this._fb.group({
|
|
|
|
keyword: [''],
|
|
|
|
type: ['all', Validators.required]
|
|
|
|
});
|
|
|
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
|
|
|
this.searchText = new RegExp(value, 'i');
|
|
|
|
this.applyFilters();
|
|
|
|
}));
|
|
|
|
this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => {
|
|
|
|
this.applyFilters();
|
|
|
|
}));
|
|
|
|
this.getTemplates();
|
2023-10-17 08:20:16 +02:00
|
|
|
|
2023-09-25 10:36:05 +02:00
|
|
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
this.selectedCommunityPid = params['communityId'];
|
|
|
|
this.getPages();
|
2023-10-17 08:20:16 +02:00
|
|
|
this.getPluginsCounts();
|
2023-09-25 10:36:05 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.subscriptions.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
} else if (value instanceof Function) {
|
|
|
|
value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getTemplates() {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this._pluginsService.getAllPluginTemplates(this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
templates => {
|
|
|
|
this.templates = templates;
|
|
|
|
this.checkboxes = [];
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
templates.forEach(_ => {
|
|
|
|
self.checkboxes.push( {template: _, checked: false});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving classes', error)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// public showModal():void {
|
|
|
|
// this.modal.showModal();
|
|
|
|
// }
|
|
|
|
|
|
|
|
public toggleCheckBoxes(event) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
|
|
|
}
|
|
|
|
|
|
|
|
public applyCheck(flag: boolean) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
private deleteFromArray(id: string): void {
|
|
|
|
|
|
|
|
let i = this.templates.findIndex(_ => _._id == id);
|
|
|
|
this.templates.splice(i, 1);
|
|
|
|
|
|
|
|
this.applyFilters();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDelete(id: string) {
|
|
|
|
this.selectedId = id;
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private confirmModalOpen() {
|
|
|
|
this.deleteModal.alertTitle = "Delete Confirmation";
|
|
|
|
this.deleteModal.message = "Are you sure you want to delete the selected template(s)?";
|
|
|
|
this.deleteModal.okButtonText = "Yes";
|
|
|
|
this.deleteModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmedDelete() {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this._pluginsService.deletePluginTemplate(this.selectedId, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
_ => {
|
|
|
|
this.deleteFromArray(this.selectedId);
|
|
|
|
NotificationHandler.rise('Template have been <b>successfully deleted</b>');
|
|
|
|
this.showLoading = false;
|
|
|
|
this._clearCacheService.clearCache("Template id deleted");
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error deleting the selected Template', error)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public edit(i: number) {
|
|
|
|
let pluginTemplate: PluginTemplate = this.checkboxes[i].template;
|
2024-01-19 09:53:17 +01:00
|
|
|
console.log(pluginTemplate.object)
|
2023-09-25 10:36:05 +02:00
|
|
|
this.index = this.templates.findIndex(value => value._id === pluginTemplate._id);
|
2024-01-19 09:53:17 +01:00
|
|
|
// this.pluginObject = Object.assign(this.templates[this.index].object);
|
2023-09-25 10:36:05 +02:00
|
|
|
// this.formPages = <Page[]>pluginTemplate.pages;
|
|
|
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
|
|
|
this.templateForm = this._fb.group({
|
|
|
|
_id: this._fb.control(pluginTemplate._id),
|
|
|
|
name: this._fb.control(pluginTemplate.name, Validators.required),
|
|
|
|
pages: this.pagesCtrl,
|
|
|
|
portalType: this._fb.control(pluginTemplate.portalType, Validators.required),
|
|
|
|
|
|
|
|
code: this._fb.control(pluginTemplate.code, Validators.required),
|
|
|
|
description: this._fb.control(pluginTemplate.description),
|
2024-01-19 09:53:17 +01:00
|
|
|
plan: this._fb.control('', Validators.required),
|
2023-09-25 10:36:05 +02:00
|
|
|
placements: this._fb.array(pluginTemplate.placements),
|
2024-01-19 09:53:17 +01:00
|
|
|
settings:this._fb.array([]),
|
|
|
|
object: this._fb.group(pluginTemplate.object)
|
2023-09-25 10:36:05 +02:00
|
|
|
});
|
|
|
|
this.templateForm.get('portalType').disable();
|
|
|
|
for (let i = 0; i < pluginTemplate.pages.length; i++) {
|
|
|
|
this.pagesCtrl.push(this._fb.control(this.getPageById(pluginTemplate.pages[i])));
|
|
|
|
}
|
2024-01-19 09:53:17 +01:00
|
|
|
if(pluginTemplate.settings) {
|
|
|
|
for (let attrKey of Object.keys(pluginTemplate.settings)) {
|
|
|
|
(this.templateForm.get("settings") as FormArray).push(this._fb.group({
|
2023-09-25 10:36:05 +02:00
|
|
|
key: this._fb.control(attrKey, Validators.required),
|
2024-01-19 09:53:17 +01:00
|
|
|
name: this._fb.control(pluginTemplate.settings[attrKey].name, Validators.required),
|
|
|
|
type: this._fb.control(pluginTemplate.settings[attrKey].type, Validators.required),
|
|
|
|
value: this._fb.control(pluginTemplate.settings[attrKey].value)
|
2023-09-25 10:36:05 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2024-01-19 09:53:17 +01:00
|
|
|
// console.log(this.templateForm.getRawValue(),this.templateForm.get("object").value)
|
2023-09-25 10:36:05 +02:00
|
|
|
this.modalOpen("Edit Template", "Save Changes");
|
|
|
|
}
|
|
|
|
|
|
|
|
public newPlugin() {
|
2024-01-19 09:53:17 +01:00
|
|
|
this.index = -1;
|
2023-09-25 10:36:05 +02:00
|
|
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
|
|
|
if (this.templateForm) {
|
|
|
|
this.templateForm.get('portalType').enable();
|
|
|
|
}
|
|
|
|
this.templateForm = this._fb.group({
|
|
|
|
_id: this._fb.control(null),
|
2023-09-26 16:32:27 +02:00
|
|
|
name: this._fb.control('', Validators.required),
|
|
|
|
code: this._fb.control('', Validators.required),
|
2024-01-19 09:53:17 +01:00
|
|
|
plan: this._fb.control('', Validators.required),
|
2023-09-26 16:32:27 +02:00
|
|
|
description: this._fb.control(''),
|
2023-09-25 10:36:05 +02:00
|
|
|
pages: this.pagesCtrl,
|
|
|
|
portalType: this._fb.control('community', Validators.required),
|
|
|
|
placements: this._fb.array([]),
|
2024-01-19 09:53:17 +01:00
|
|
|
settings:this._fb.array([]),
|
|
|
|
object: this._fb.control(null)
|
2023-09-25 10:36:05 +02:00
|
|
|
});
|
2024-01-19 09:53:17 +01:00
|
|
|
// this.addNewAttr();
|
2023-09-25 10:36:05 +02:00
|
|
|
this.modalOpen("Create template", "Create");
|
|
|
|
}
|
|
|
|
|
|
|
|
private modalOpen(title: string, yesBtn: string) {
|
|
|
|
this.editModal.okButtonLeft = false;
|
|
|
|
this.editModal.alertTitle = title;
|
|
|
|
this.editModal.okButtonText = yesBtn;
|
|
|
|
this.editModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveConfirmed(data: any) {
|
|
|
|
this.showLoading = true;
|
|
|
|
let template:PluginTemplate = <PluginTemplate>this.templateForm.getRawValue();
|
|
|
|
template.pages = this.pagesCtrl.getRawValue().map(page => page._id?page._id:page);
|
2024-01-19 09:53:17 +01:00
|
|
|
template.settings = new Map<string, {name: string; type: string; value: string}>();
|
|
|
|
for (let attr of this.templateForm.getRawValue().settings) {
|
|
|
|
template.settings[attr.key]={name: attr.name, type: attr.type, value: attr.value};
|
2023-09-25 10:36:05 +02:00
|
|
|
}
|
|
|
|
let update = template._id?true:false;
|
|
|
|
this.subscriptions.push(this._pluginsService.savePluginTemplate(template, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
saved => {
|
|
|
|
this.savedSuccessfully(saved, update );
|
|
|
|
NotificationHandler.rise('Template <b>' + saved.name + '</b> has been <b>successfully' + (update?' updated ':' created ') + '</b>');
|
|
|
|
this._clearCacheService.clearCache("Template id saved");
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError("System error creating template", error)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public savedSuccessfully(template: PluginTemplate, update:boolean) {
|
|
|
|
if(update){
|
|
|
|
this.templates[this.index] = template;
|
|
|
|
}else{
|
|
|
|
this.templates.push(template);
|
|
|
|
}
|
|
|
|
this.applyFilters();
|
|
|
|
this.applyCheck(false);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public applyFilters() {
|
|
|
|
this.checkboxes = [];
|
|
|
|
this.templates.filter(item => this.filterByType(item)).forEach(
|
|
|
|
item => this.checkboxes.push({template: item, checked: false})
|
|
|
|
);
|
|
|
|
this.checkboxes = this.checkboxes.filter(item => this.filter(item.template));
|
|
|
|
}
|
|
|
|
|
|
|
|
public filterByType(template: PluginTemplate): boolean {
|
|
|
|
let type = this.filterForm.get("type").value;
|
|
|
|
return type == "all" || (type == template.portalType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public filter(plugin: PluginTemplate): boolean {
|
|
|
|
return this.searchText.toString() == '' || (plugin.name + ' ' + plugin.portalType).match(this.searchText) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error = null) {
|
|
|
|
if (error) {
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
NotificationHandler.rise(message,'danger');
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error = null) {
|
|
|
|
if (error) {
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
NotificationHandler.rise(message,'danger');
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPages() {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this._helpContentService.getAllPages(this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
pages => {
|
|
|
|
this.allPages = [];
|
|
|
|
pages.forEach(page => {
|
|
|
|
this.allPages.push({
|
|
|
|
label: page.name + " [" + page.portalType + "]",
|
|
|
|
value: page
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving pages', error)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
addNewAttr(){
|
2024-01-19 09:53:17 +01:00
|
|
|
(this.templateForm.get("settings") as FormArray).push(this._fb.group({
|
2023-09-25 10:36:05 +02:00
|
|
|
key:this._fb.control("", Validators.required),
|
|
|
|
name: this._fb.control("", Validators.required),
|
|
|
|
type: this._fb.control("text", Validators.required),
|
|
|
|
value: this._fb.control("")
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAttr(index){
|
|
|
|
this.attrFormArray.removeAt(index);
|
|
|
|
this.attrFormArray.markAsDirty();
|
|
|
|
}
|
|
|
|
get attrFormArray(){
|
|
|
|
|
2024-01-19 09:53:17 +01:00
|
|
|
return this.templateForm.get("settings") as FormArray;
|
2023-09-25 10:36:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
attributeTypeChanged(form){
|
|
|
|
let type = form.get("value").get("type");
|
|
|
|
form.get("value").setValue("");
|
|
|
|
if(type == "boolean"){
|
|
|
|
form.get("value").setValue(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public getPagesAsString(pageIds): string {
|
|
|
|
|
|
|
|
let pages = [];
|
|
|
|
for(let id of pageIds) {
|
2023-10-02 09:20:36 +02:00
|
|
|
pages.push(this.allPages.filter(option => option.value._id == id).map((option => option.value.name + " [" + option.value.portalType + "]")));
|
2023-09-25 10:36:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
return pages.join(", ");
|
|
|
|
}
|
|
|
|
public getPageById(pageId) {
|
|
|
|
for(let option of this.allPages) {
|
|
|
|
if(option.value._id == pageId){
|
|
|
|
return option.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pageId;
|
|
|
|
}
|
2023-10-17 08:20:16 +02:00
|
|
|
getPluginsCounts() {
|
|
|
|
this.subscriptions.push(this._pluginsService.countPluginPerTemplate( this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
countPlugins => {
|
|
|
|
this.pluginsCount = countPlugins;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page contents', error)));
|
|
|
|
}
|
2024-01-19 09:53:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
pluginFieldChanged($event:PluginEditEvent){
|
|
|
|
let object = this.templateForm.get("object").getRawValue();
|
|
|
|
object[$event.field]=$event.value;
|
|
|
|
this.templateForm.get("object").setValue(object);
|
|
|
|
this.templateForm.markAsDirty();
|
|
|
|
}
|
2023-09-25 10:36:05 +02:00
|
|
|
}
|