2018-10-30 16:31:16 +01:00
|
|
|
/**
|
|
|
|
* Created by stefania on 7/13/17.
|
|
|
|
*/
|
2018-12-03 12:33:03 +01:00
|
|
|
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
|
2018-11-13 16:26:49 +01:00
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
2018-10-30 16:31:16 +01:00
|
|
|
import { HelpContentService } from "../../services/help-content.service";
|
|
|
|
import { FormGroup } from "@angular/forms";
|
|
|
|
import { PageFormComponent } from "./page-form.component";
|
|
|
|
import { CheckPage, Page } from "../../domain/page";
|
|
|
|
import { Community } from "../../domain/community";
|
|
|
|
import { Entity } from "../../domain/entity";
|
|
|
|
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
2018-11-13 16:26:49 +01:00
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
2019-04-24 13:44:46 +02:00
|
|
|
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'pages',
|
|
|
|
templateUrl: './pages.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class PagesComponent implements OnInit {
|
|
|
|
|
|
|
|
@ViewChild('AlertModalSavePage') alertModalSavePage;
|
|
|
|
@ViewChild('AlertModalUpdatePage') alertModalUpdatePage;
|
|
|
|
@ViewChild('AlertModalDeletePages') alertModalDeletePages;
|
|
|
|
private selectedPages: string[] = [];
|
|
|
|
|
|
|
|
@ViewChild(PageFormComponent)
|
|
|
|
public formComponent : PageFormComponent;
|
|
|
|
|
|
|
|
public checkboxes : CheckPage[] = [];
|
|
|
|
|
|
|
|
public pages : Page[] = [];
|
|
|
|
public pageWithDivIds: string[] = [];
|
|
|
|
|
|
|
|
//public errorMessage: string;
|
|
|
|
|
|
|
|
public formGroup : FormGroup;
|
|
|
|
|
|
|
|
private searchText : RegExp = new RegExp('');
|
2018-11-27 13:28:20 +01:00
|
|
|
public keyword: string = "";
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
public communities: Community[] = [];
|
|
|
|
|
|
|
|
public selectedCommunityPid: string;
|
|
|
|
|
|
|
|
public pagesType: string;
|
|
|
|
public properties:EnvProperties = null;
|
|
|
|
|
|
|
|
public showLoading: boolean = true;
|
|
|
|
public errorMessage: string = '';
|
|
|
|
public updateErrorMessage: string = '';
|
|
|
|
public modalErrorMessage: string = '';
|
|
|
|
public isPortalAdministrator = null;
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.formGroup = this.formComponent.form;
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
|
|
|
|
this.route.queryParams.subscribe(params => {
|
2019-04-24 13:44:46 +02:00
|
|
|
HelperFunctions.scroll();
|
2018-12-03 12:33:03 +01:00
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
this.pagesType = "";
|
|
|
|
if(params['type']) {
|
|
|
|
this.pagesType = params['type'];
|
|
|
|
}
|
|
|
|
|
2018-11-27 13:28:20 +01:00
|
|
|
this.keyword = "";
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
this.selectedCommunityPid = params['communityId'];
|
|
|
|
this.applyCommunityFilter(this.selectedCommunityPid);
|
|
|
|
this.isPortalAdministrator = Session.isPortalAdministrator();
|
|
|
|
|
|
|
|
//this.getCommunities();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:33:03 +01:00
|
|
|
constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router, private _helpContentService: HelpContentService) {}
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
getPages(community_pid: string) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.errorMessage = "";
|
|
|
|
|
|
|
|
this.pageWithDivIds = [];
|
|
|
|
|
|
|
|
let parameters = "";
|
|
|
|
if(this.pagesType) {
|
|
|
|
parameters = "?page_type="+this.pagesType;
|
|
|
|
}
|
|
|
|
this._helpContentService.getCommunityPages(community_pid, parameters, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
pages => {
|
|
|
|
this.pagesReturned(pages);
|
|
|
|
if(!this.pagesType || this.pagesType == "link") {
|
|
|
|
this.getPagesWithDivIds(community_pid);
|
|
|
|
} else {
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving pages', error)
|
|
|
|
);
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getPagesWithDivIds(community_pid: string) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
this._helpContentService.getPagesWithDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
pages => {
|
|
|
|
this.pageWithDivIds = pages;
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving information about pages\' classes', error));
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pagesReturned(pages: Page[]) {
|
|
|
|
this.pages = pages;
|
|
|
|
this.checkboxes = [];
|
|
|
|
|
|
|
|
if(pages) {
|
|
|
|
pages.forEach(_ => {
|
|
|
|
this.checkboxes.push(<CheckPage>{page : _, checked : false});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
getCommunities() {
|
|
|
|
this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
communities => {
|
|
|
|
this.communities = communities;
|
|
|
|
this.selectedCommunityPid = this.communities[0].pid;
|
|
|
|
this.getPages(this.selectedCommunityPid);
|
|
|
|
this.getPagesWithDivIds(this.selectedCommunityPid);
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving communities', error));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
public toggleCheckBoxes(event) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
|
|
|
}
|
|
|
|
|
|
|
|
public applyCheck(flag : boolean) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getSelectedPages() : string[] {
|
|
|
|
return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private deletePagesFromArray(ids : string[]) : void {
|
|
|
|
for(let id of ids) {
|
|
|
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
|
|
|
this.checkboxes.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDeletePage(id : string) {
|
|
|
|
//this.deleteConfirmationModal.ids = [id];
|
|
|
|
//this.deleteConfirmationModal.showModal();
|
|
|
|
this.selectedPages = [id];
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDeleteSelectedPages() {
|
|
|
|
//this.deleteConfirmationModal.ids = this.getSelectedPages();
|
|
|
|
//this.deleteConfirmationModal.showModal();
|
|
|
|
this.selectedPages = this.getSelectedPages();
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
private confirmModalOpen() {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
this.alertModalDeletePages.cancelButton = true;
|
|
|
|
this.alertModalDeletePages.okButton = true;
|
|
|
|
this.alertModalDeletePages.alertTitle = "Delete Confirmation";
|
|
|
|
this.alertModalDeletePages.message = "Are you sure you want to delete the selected page(s)?";
|
|
|
|
this.alertModalDeletePages.okButtonText = "Yes";
|
|
|
|
this.alertModalDeletePages.open();
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public confirmedDeletePages(data: any) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
|
|
|
|
this._helpContentService.deletePages(this.selectedPages, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
_ => {
|
|
|
|
this.deletePagesFromArray(this.selectedPages);
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error deleting the selected pages', error)
|
|
|
|
);
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public editPage(i : number) {
|
|
|
|
let page : Page = this.checkboxes[i].page;
|
|
|
|
this.formGroup.patchValue(page);
|
|
|
|
this.formComponent.setEntities(page.entities as Entity[]);
|
|
|
|
|
2019-02-14 13:20:00 +01:00
|
|
|
//console.info(this.formGroup.value);
|
2018-10-30 16:31:16 +01:00
|
|
|
//this.updateModal.showModal();
|
|
|
|
this.modalErrorMessage = "";
|
|
|
|
this.pagesModalOpen(this.alertModalUpdatePage, "Update", "Update Page");
|
|
|
|
}
|
|
|
|
|
|
|
|
public newPage() {
|
|
|
|
this.formComponent.reset();
|
|
|
|
this.modalErrorMessage = "";
|
|
|
|
this.pagesModalOpen(this.alertModalSavePage, "Save", "Add a new Page");
|
|
|
|
}
|
|
|
|
|
|
|
|
private pagesModalOpen(modal: any, title: string, yesBtn: string) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
modal.cancelButton = true;
|
|
|
|
modal.okButton = true;
|
|
|
|
modal.alertTitle = title;
|
|
|
|
modal.okButtonText = yesBtn;
|
|
|
|
modal.open();
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public pageSaveConfirmed(data: any) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
2018-10-30 16:31:16 +01:00
|
|
|
} else {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!this.formGroup.valid) {
|
|
|
|
this.pagesModalOpen(this.alertModalSavePage, "Save", "Add a new Page");
|
|
|
|
this.modalErrorMessage = "Please fill in all required fields marked with *";
|
|
|
|
} else {
|
|
|
|
this.modalErrorMessage = "";
|
|
|
|
this._helpContentService.savePage(<Page> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
page => {
|
|
|
|
this.pageSavedSuccessfully(page);
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError("System error creating page", error)
|
|
|
|
);
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public pageUpdateConfirmed(data: any) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
2018-10-30 16:31:16 +01:00
|
|
|
} else {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!this.formGroup.valid) {
|
|
|
|
this.pagesModalOpen(this.alertModalUpdatePage, "Update", "Update Page");
|
|
|
|
this.modalErrorMessage = "Please fill in all required fields marked with *";
|
|
|
|
} else {
|
|
|
|
this._helpContentService.updatePage(<Page> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
page => {
|
|
|
|
this.pageUpdatedSuccessfully(page);
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError("System error updating page", error)
|
|
|
|
);
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public pageSavedSuccessfully(page: Page) {
|
|
|
|
this.checkboxes.push(<CheckPage>{page : page, checked : false});
|
|
|
|
this.applyCheck(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public pageUpdatedSuccessfully(page : Page) {
|
|
|
|
this.checkboxes.find(checkItem => checkItem.page._id==page._id).page = page;
|
|
|
|
this.applyCheck(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public filterBySearch(text : string) {
|
|
|
|
this.searchText = new RegExp(text,'i');
|
|
|
|
this.applyFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public applyFilter() {
|
|
|
|
this.checkboxes = [];
|
|
|
|
this.pages.filter(item => this.filterPages(item)).forEach(
|
|
|
|
_ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public filterPages(page : Page) : boolean {
|
|
|
|
let textFlag = this.searchText.toString() == '' || (page.route + ' ' +page.name).match(this.searchText) != null;
|
|
|
|
return textFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
// if(error == null) {
|
|
|
|
// this.formComponent.reset();
|
|
|
|
// } else {
|
|
|
|
this.errorMessage = message;// + ' (Server responded: ' + error + ')';
|
|
|
|
console.log('Server responded: ' +error);
|
|
|
|
//}
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
if(error == null) {
|
|
|
|
this.formComponent.reset();
|
|
|
|
} else {
|
|
|
|
this.updateErrorMessage = message;// + ' (Server responded: ' + error + ')';
|
|
|
|
console.log('Server responded: ' +error);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// public filterByCommunity(event: any) {
|
|
|
|
// this.selectedCommunityPid = event.target.value;
|
|
|
|
// this.applyCommunityFilter(this.selectedCommunityPid);
|
|
|
|
// }
|
|
|
|
|
|
|
|
public applyCommunityFilter(community_pid: string) {
|
|
|
|
this.getPages(community_pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public togglePages(status : boolean, ids : string[]) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
|
|
|
|
this._helpContentService.togglePages(this.selectedCommunityPid,ids,status, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
() => {
|
|
|
|
for(let id of ids) {
|
|
|
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
|
|
|
this.checkboxes[i].page.isEnabled=status;
|
|
|
|
}
|
|
|
|
this.applyCheck(false);
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error changing the status of the selected page(s)', error)
|
|
|
|
);
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public capitalizeFirstLetter(str: string) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
|
}
|
|
|
|
}
|