398 lines
13 KiB
TypeScript
398 lines
13 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 {Plugin} from "../../utils/entities/adminTool/plugin";
|
|
import {StringUtils} from "../../utils/string-utils.class";
|
|
import {Portal} from "../../utils/entities/adminTool/portal";
|
|
import {PluginTemplate} from "../../utils/entities/adminTool/pluginTemplate";
|
|
|
|
@Component({
|
|
selector: 'plugins',
|
|
templateUrl: './plugins.component.html',
|
|
})
|
|
export class PluginsComponent implements OnInit {
|
|
@ViewChild('editModal') editModal: AlertModal;
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
private selectedId: string;
|
|
public checkboxes: {
|
|
plugin: Plugin;
|
|
checked: boolean;
|
|
template:PluginTemplate
|
|
}[] = [];
|
|
public plugins: Plugin[] = [];
|
|
public pluginTemplates: PluginTemplate[] = [];
|
|
public selectedTemplate: PluginTemplate = null;
|
|
public selectedPlugin: Plugin = null;
|
|
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;
|
|
public portal: string;
|
|
public selectedPageId: string;
|
|
public community: Portal;
|
|
public page: Page;
|
|
|
|
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.subscriptions.push(this.route.params.subscribe(params => {
|
|
console.log(params)
|
|
this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
|
|
this.selectedCommunityPid = params.community;
|
|
|
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
|
HelperFunctions.scroll();
|
|
console.log(params)
|
|
// this.selectedCommunityPid = params['communityId'];
|
|
this.selectedPageId = params['pageId'];
|
|
if (this.portal && this.selectedPageId) {
|
|
this.getPage(this.selectedPageId);
|
|
}
|
|
if (!this.selectedPageId) {
|
|
this._router.navigate(['../pages'], {relativeTo: this.route});
|
|
}
|
|
}));
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.subscriptions.forEach(value => {
|
|
if (value instanceof Subscriber) {
|
|
value.unsubscribe();
|
|
} else if (value instanceof Function) {
|
|
value();
|
|
}
|
|
});
|
|
}
|
|
getTemplateByCode(code){
|
|
for(let template of this.pluginTemplates){
|
|
if(template.code == code){
|
|
return template;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
getPage(pageId: string) {
|
|
this.showLoading = true;
|
|
this.subscriptions.push(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal).subscribe(
|
|
page => {
|
|
if (this.properties.adminToolsPortalType != page.portalType) {
|
|
this._router.navigate(['./pageContents']);
|
|
} else {
|
|
this.page = page;
|
|
this.getPlugins();
|
|
}
|
|
},
|
|
error => this.handleError('System error retrieving page', error)));
|
|
}
|
|
getPlugins() {
|
|
this.showLoading = true;
|
|
this.subscriptions.push(this._pluginsService.getPluginTemplatesByPage( this.properties.adminToolsAPIURL,this.selectedCommunityPid, this.selectedPageId).subscribe(
|
|
templates => {
|
|
this.pluginTemplates = templates;
|
|
|
|
this.subscriptions.push(this._pluginsService.getPluginsByPage( this.properties.adminToolsAPIURL, this.selectedCommunityPid, this.selectedPageId).subscribe(
|
|
plugins => {
|
|
this.plugins = plugins;
|
|
this.checkboxes = [];
|
|
|
|
let self = this;
|
|
this.plugins.forEach(_ => {
|
|
self.checkboxes.push( {plugin: _, checked: false, template:this.getTemplateByCode(_.code)});
|
|
});
|
|
|
|
this.showLoading = false;
|
|
},
|
|
error => this.handleError('System error retrieving plugins', error)));
|
|
|
|
},
|
|
error => this.handleError('System error retrieving templates', 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.plugins.findIndex(_ => _._id == id);
|
|
this.plugins.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 plugin:Plugin = this.checkboxes[i].plugin;
|
|
this.selectedPlugin = plugin;
|
|
this.selectedTemplate = this.checkboxes[i].template;
|
|
this.index = this.plugins.findIndex(value => value._id === plugin._id);
|
|
// this.formPages = <Page[]>plugin.pages;
|
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
|
this.templateForm = this._fb.group({
|
|
_id: this._fb.control(plugin._id),
|
|
page: this._fb.control(plugin.page),
|
|
code: this._fb.control(plugin.code, Validators.required),
|
|
placement: this._fb.control(plugin.placement),
|
|
order: this._fb.control(plugin.order),
|
|
isActive: this._fb.control(plugin.isActive),
|
|
isPriorTo: this._fb.control(plugin.isPriorTo),
|
|
values:this._fb.array([])
|
|
});
|
|
if(plugin.values) {
|
|
for (let attrKey of Object.keys(plugin.values)) {
|
|
(this.templateForm.get("values") as FormArray).push(this._fb.group({
|
|
'key': this._fb.control(attrKey),
|
|
'value': this._fb.control(plugin.values[attrKey])}
|
|
));
|
|
}
|
|
}
|
|
this.modalOpen("Edit Plugin", "Save Changes");
|
|
}
|
|
|
|
public newPlugin(template=null) {
|
|
this.selectedPlugin = null;
|
|
this.selectedTemplate = template;
|
|
if(this.selectedTemplate) {
|
|
if (this.templateForm) {
|
|
this.templateForm.get('portalType').enable();
|
|
}
|
|
this.templateForm = this._fb.group({
|
|
_id: this._fb.control(null),
|
|
page: this._fb.control(this.selectedPageId),
|
|
code: this._fb.control(this.selectedTemplate.code, Validators.required),
|
|
placement: this._fb.control(this.selectedTemplate.placements[0]),
|
|
order: this._fb.control(""),
|
|
isActive: this._fb.control(false),
|
|
isPriorTo: this._fb.control(false),
|
|
values: this._fb.array([])
|
|
});
|
|
// if(plugin.values) {
|
|
for (let attrKey of Object.keys(this.selectedTemplate.attributes)) {
|
|
(this.templateForm.get("values") as FormArray).push(this._fb.group({
|
|
key: this._fb.control(attrKey),
|
|
value: this._fb.control(this.selectedTemplate.attributes[attrKey].value?this.selectedTemplate.attributes[attrKey].value:""),
|
|
}));
|
|
|
|
}
|
|
// }
|
|
}else {
|
|
// this.addNewAttr();
|
|
this.modalOpen("Create plugin", "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 plugin:Plugin = <Plugin>this.templateForm.getRawValue();
|
|
// template.pages = this.pagesCtrl.getRawValue().map(page => page._id?page._id:page);
|
|
plugin.values = new Map<string, string>();
|
|
for (let fields of this.templateForm.getRawValue().values) {
|
|
plugin.values[fields.key]= fields.value;
|
|
}
|
|
let update = plugin._id?true:false;
|
|
this.subscriptions.push(this._pluginsService.savePlugin(plugin, this.properties.adminToolsAPIURL).subscribe(
|
|
saved => {
|
|
this.savedSuccessfully(saved, update );
|
|
NotificationHandler.rise('Plugin <b>' + this.selectedTemplate.name + '</b> has been <b>successfully' + (update?' updated ':' created ') + '</b>');
|
|
this._clearCacheService.clearCache("Plugin id saved");
|
|
},
|
|
error => this.handleUpdateError("System error creating template", error)
|
|
));
|
|
}
|
|
|
|
public savedSuccessfully(template: Plugin, update:boolean) {
|
|
if(update){
|
|
this.plugins[this.index] = template;
|
|
}else{
|
|
this.plugins.push(template);
|
|
}
|
|
this.applyFilters();
|
|
this.applyCheck(false);
|
|
this.showLoading = false;
|
|
}
|
|
|
|
|
|
public applyFilters() {
|
|
/* this.checkboxes = [];
|
|
this.plugins.filter(item => this.filterByType(item)).forEach(
|
|
item => this.checkboxes.push({plugin: item, checked: false})
|
|
);
|
|
this.checkboxes = this.checkboxes.filter(item => this.filter(item.plugin));*/
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
));
|
|
}
|
|
|
|
get attrFormArray(){
|
|
|
|
return this.templateForm.get("values") 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;
|
|
}
|
|
|
|
getKeys(obj){
|
|
return Object.keys(obj);
|
|
}
|
|
|
|
}
|