2017-09-22 12:26:08 +02:00
|
|
|
/**
|
|
|
|
* Created by stefania on 7/13/17.
|
|
|
|
*/
|
|
|
|
import { Component, ViewChild, OnInit } from '@angular/core';
|
2017-12-20 15:26:30 +01:00
|
|
|
import { ActivatedRoute } from "@angular/router";
|
2017-09-22 12:26:08 +02:00
|
|
|
import { HelpContentService } from "../../services/help-content.service";
|
|
|
|
import { FormGroup } from "@angular/forms";
|
|
|
|
import { ModalFormComponent } from "../modal-form.component";
|
|
|
|
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
|
|
|
|
import { PageFormComponent } from "./page-form.component";
|
|
|
|
import { CheckPage, Page } from "../../domain/page";
|
2017-12-13 12:15:19 +01:00
|
|
|
import { Community } from "../../domain/community";
|
2017-12-20 15:26:30 +01:00
|
|
|
import { Entity } from "../../domain/entity";
|
2017-09-22 12:26:08 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'pages',
|
|
|
|
templateUrl: './pages.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class PagesComponent implements OnInit {
|
|
|
|
|
|
|
|
// @ViewChild(ModalFormComponent)
|
|
|
|
@ViewChild('saveModal')
|
|
|
|
public modal:ModalFormComponent;
|
|
|
|
|
|
|
|
@ViewChild('updateModal')
|
|
|
|
public updateModal:ModalFormComponent;
|
|
|
|
|
|
|
|
@ViewChild('deleteConfirmationModal')
|
|
|
|
public deleteConfirmationModal : DeleteConfirmationDialogComponent;
|
|
|
|
|
|
|
|
@ViewChild(PageFormComponent)
|
|
|
|
public formComponent : PageFormComponent;
|
|
|
|
|
2018-01-04 16:56:22 +01:00
|
|
|
public checkboxes : CheckPage[] = [];
|
2017-09-22 12:26:08 +02:00
|
|
|
|
|
|
|
public pages : Page[] = [];
|
|
|
|
|
|
|
|
public errorMessage: string;
|
|
|
|
|
|
|
|
public formGroup : FormGroup;
|
|
|
|
|
|
|
|
private searchText : RegExp = new RegExp('');
|
|
|
|
|
2017-12-13 12:15:19 +01:00
|
|
|
public communities: Community[] = [];
|
|
|
|
|
|
|
|
public selectedCommunityId: string;
|
|
|
|
|
2017-12-20 15:26:30 +01:00
|
|
|
public pagesType: string;
|
2017-12-13 12:15:19 +01:00
|
|
|
|
2017-12-20 15:26:30 +01:00
|
|
|
ngOnInit() {
|
2017-09-22 12:26:08 +02:00
|
|
|
this.formGroup = this.formComponent.form;
|
2017-12-20 15:26:30 +01:00
|
|
|
// this.getCommunities();
|
|
|
|
|
|
|
|
this.route.queryParams.subscribe(params => {
|
|
|
|
this.pagesType = "";
|
|
|
|
if(params['type']) {
|
|
|
|
this.pagesType = params['type'];
|
|
|
|
}
|
|
|
|
this.getCommunities();
|
|
|
|
});
|
2017-09-22 12:26:08 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 15:26:30 +01:00
|
|
|
constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
|
2017-09-22 12:26:08 +02:00
|
|
|
|
2017-12-13 12:15:19 +01:00
|
|
|
getPages(community_id: string) {
|
2017-09-22 12:26:08 +02:00
|
|
|
let self = this;
|
2017-12-20 15:26:30 +01:00
|
|
|
|
|
|
|
if(this.pagesType) {
|
|
|
|
this._helpContentService.getCommunityPagesByType(community_id, "?page_type="+this.pagesType).subscribe(
|
|
|
|
pages => {
|
|
|
|
self.pagesReturned(pages);
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving pages', error));
|
|
|
|
} else {
|
|
|
|
this._helpContentService.getCommunityPages(community_id).subscribe(
|
|
|
|
pages => {
|
|
|
|
self.pagesReturned(pages);
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving pages', error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pagesReturned(pages: Page[]) {
|
|
|
|
this.pages = pages;
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes = [];
|
2017-12-20 15:26:30 +01:00
|
|
|
|
|
|
|
pages.forEach(_ => {
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.push(<CheckPage>{page : _, checked : false});
|
2017-12-20 15:26:30 +01:00
|
|
|
});
|
2017-09-22 12:26:08 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 12:15:19 +01:00
|
|
|
getCommunities() {
|
|
|
|
let self = this;
|
|
|
|
this._helpContentService.getCommunities().subscribe(
|
|
|
|
communities => {
|
|
|
|
self.communities = communities;
|
|
|
|
self.getPages(self.communities[0]._id);
|
|
|
|
self.selectedCommunityId = self.communities[0]._id;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving communities', error));
|
|
|
|
}
|
|
|
|
|
2017-09-22 12:26:08 +02:00
|
|
|
public showModal():void {
|
|
|
|
this.modal.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggleCheckBoxes(event) {
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
2017-09-22 12:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public applyCheck(flag : boolean) {
|
2017-12-13 12:15:19 +01:00
|
|
|
console.info("applyCheck "+flag);
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
2017-09-22 12:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getSelectedPages() : string[] {
|
2018-01-04 16:56:22 +01:00
|
|
|
return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
|
2017-09-22 12:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private deletePagesFromArray(ids : string[]) : void {
|
|
|
|
for(let id of ids) {
|
2018-01-04 16:56:22 +01:00
|
|
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
|
|
|
this.checkboxes.splice(i, 1);
|
2017-09-22 12:26:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDeletePage(id : string) {
|
|
|
|
this.deleteConfirmationModal.ids = [id];
|
|
|
|
this.deleteConfirmationModal.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDeleteSelectedPages() {
|
|
|
|
this.deleteConfirmationModal.ids = this.getSelectedPages();
|
|
|
|
this.deleteConfirmationModal.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmedDeletePages(ids : string[]) {
|
|
|
|
this._helpContentService.deletePages(ids).subscribe(
|
|
|
|
_ => this.deletePagesFromArray(ids),
|
|
|
|
error => this.handleError('System error deleting the selected pages', error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public editPage(i : number) {
|
2018-01-04 16:56:22 +01:00
|
|
|
let page : Page = this.checkboxes[i].page;
|
2017-09-22 12:26:08 +02:00
|
|
|
this.formGroup.patchValue(page);
|
2017-12-20 15:26:30 +01:00
|
|
|
const entityFGs = (page.entities as Entity[]).map(entity => this.formComponent._fb.group(entity));
|
2017-12-13 12:15:19 +01:00
|
|
|
const entityFormArray = this.formComponent._fb.array(entityFGs);
|
|
|
|
this.formGroup.setControl('entities', entityFormArray);
|
|
|
|
console.info(this.formGroup.value);
|
2017-09-22 12:26:08 +02:00
|
|
|
this.updateModal.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public pageSavedSuccessfully(page: Page) {
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.push(<CheckPage>{page : page, checked : false});
|
|
|
|
console.info("checkboxes length: "+this.checkboxes.length);
|
2017-09-22 12:26:08 +02:00
|
|
|
this.applyCheck(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public pageUpdatedSuccessfully(page : Page) {
|
2017-12-13 12:15:19 +01:00
|
|
|
console.info(page._id);
|
2018-01-04 16:56:22 +01:00
|
|
|
console.info(this.checkboxes.find(checkItem => (checkItem.page._id == page._id)));
|
2017-12-13 12:15:19 +01:00
|
|
|
console.info(page.entities);
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.find(checkItem => checkItem.page._id==page._id).page = page;
|
2017-09-22 12:26:08 +02:00
|
|
|
this.applyCheck(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public filterBySearch(text : string) {
|
|
|
|
this.searchText = new RegExp(text,'i');
|
|
|
|
this.applyFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public applyFilter() {
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes = [];
|
2017-09-22 12:26:08 +02:00
|
|
|
this.pages.filter(item => this.filterPages(item)).forEach(
|
2018-01-04 16:56:22 +01:00
|
|
|
_ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
|
2017-09-22 12:26:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
this.errorMessage = message + ' (Server responded: ' + error + ')';
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public filterByCommunity(event: any) {
|
|
|
|
this.selectedCommunityId = event.target.value;
|
|
|
|
this.applyCommunityFilter(this.selectedCommunityId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public applyCommunityFilter(community_id: string) {
|
|
|
|
this.getPages(community_id);
|
|
|
|
}
|
|
|
|
|
2018-01-08 10:29:40 +01:00
|
|
|
public togglePages(status : boolean, ids : string[]) {
|
|
|
|
this._helpContentService.togglePages(this.selectedCommunityId,ids,status).subscribe(
|
2017-12-13 12:15:19 +01:00
|
|
|
() => {
|
2018-01-08 10:29:40 +01:00
|
|
|
for(let id of ids) {
|
2018-01-04 16:56:22 +01:00
|
|
|
// let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
|
|
|
// this.checkboxes[i].page.isEnabled=status;
|
2017-12-13 12:15:19 +01:00
|
|
|
// }
|
2018-01-08 10:29:40 +01:00
|
|
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
|
|
|
this.checkboxes[i].page.isEnabled=status;
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
this.applyCheck(false);
|
|
|
|
},
|
|
|
|
error => this.handleError('System error changing the status of the selected page(s)', error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|