356 lines
12 KiB
TypeScript
356 lines
12 KiB
TypeScript
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";
|
|
|
|
@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"},
|
|
];
|
|
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"},
|
|
];
|
|
selectedCommunityPid = null;
|
|
public portalUtils: PortalUtils = new PortalUtils();
|
|
private index: number;
|
|
|
|
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();
|
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
|
HelperFunctions.scroll();
|
|
this.selectedCommunityPid = params['communityId'];
|
|
this.getPages();
|
|
}));
|
|
|
|
}
|
|
|
|
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;
|
|
this.index = this.templates.findIndex(value => value._id === pluginTemplate._id);
|
|
// 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),
|
|
placements: this._fb.array(pluginTemplate.placements),
|
|
attributes:this._fb.array([])
|
|
});
|
|
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])));
|
|
}
|
|
if(pluginTemplate.attributes) {
|
|
for (let attrKey of Object.keys(pluginTemplate.attributes)) {
|
|
(this.templateForm.get("attributes") as FormArray).push(this._fb.group({
|
|
key: this._fb.control(attrKey, Validators.required),
|
|
name: this._fb.control(pluginTemplate.attributes[attrKey].name, Validators.required),
|
|
type: this._fb.control(pluginTemplate.attributes[attrKey].type, Validators.required),
|
|
value: this._fb.control(pluginTemplate.attributes[attrKey].value)
|
|
}));
|
|
}
|
|
}
|
|
this.modalOpen("Edit Template", "Save Changes");
|
|
}
|
|
|
|
public newPlugin() {
|
|
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),
|
|
name: this._fb.control('', Validators.required),
|
|
code: this._fb.control('', Validators.required),
|
|
description: this._fb.control(''),
|
|
pages: this.pagesCtrl,
|
|
portalType: this._fb.control('community', Validators.required),
|
|
placements: this._fb.array([]),
|
|
attributes:this._fb.array([])
|
|
});
|
|
this.addNewAttr();
|
|
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);
|
|
template.attributes = new Map<string, {name: string; type: string; value: string}>();
|
|
for (let attr of this.templateForm.getRawValue().attributes) {
|
|
template.attributes[attr.key]={name: attr.name, type: attr.type, value: attr.value};
|
|
}
|
|
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(){
|
|
(this.templateForm.get("attributes") as FormArray).push(this._fb.group({
|
|
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(){
|
|
|
|
return this.templateForm.get("attributes") as FormArray;
|
|
|
|
}
|
|
|
|
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) {
|
|
pages.push(this.allPages.filter(option => option.value._id == id).map((option => option.value.name)));
|
|
|
|
}
|
|
return pages.join(", ");
|
|
}
|
|
public getPageById(pageId) {
|
|
for(let option of this.allPages) {
|
|
if(option.value._id == pageId){
|
|
return option.value;
|
|
}
|
|
}
|
|
return pageId;
|
|
}
|
|
|
|
}
|