connect-admin/app/pages/helpcontent/pages.component.ts

198 lines
6.4 KiB
TypeScript

/**
* Created by stefania on 7/13/17.
*/
import { Component, ViewChild, OnInit } from '@angular/core';
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";
import { Community } from "../../domain/community";
@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;
public pagesCheckboxes : CheckPage[] = [];
public pages : Page[] = [];
public errorMessage: string;
public formGroup : FormGroup;
private searchText : RegExp = new RegExp('');
public communities: Community[] = [];
public selectedCommunityId: string;
ngOnInit() {
this.getCommunities();
this.formGroup = this.formComponent.form;
}
constructor(private _helpContentService: HelpContentService) {}
getPages(community_id: string) {
let self = this;
this._helpContentService.getCommunityPages(community_id).subscribe(
pages => {
self.pages = pages;
self.pagesCheckboxes = [];
pages.forEach(_ => {
self.pagesCheckboxes.push(<CheckPage>{page : _, checked : false});
});
},
error => this.handleError('System error retrieving pages', error));
}
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));
}
public showModal():void {
this.modal.showModal();
}
public toggleCheckBoxes(event) {
this.pagesCheckboxes.forEach(_ => _.checked = event.target.checked);
}
public applyCheck(flag : boolean) {
console.info("applyCheck "+flag);
this.pagesCheckboxes.forEach(_ => _.checked = flag);
}
public getSelectedPages() : string[] {
return this.pagesCheckboxes.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.pagesCheckboxes.findIndex(_ => _.page._id == id);
this.pagesCheckboxes.splice(i, 1);
}
}
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) {
let page : Page = this.pagesCheckboxes[i].page;
this.formGroup.patchValue(page);
const entityFGs = page.entities.map(entity => this.formComponent._fb.group(entity));
const entityFormArray = this.formComponent._fb.array(entityFGs);
this.formGroup.setControl('entities', entityFormArray);
console.info(this.formGroup.value);
this.updateModal.showModal();
}
public pageSavedSuccessfully(page: Page) {
this.pagesCheckboxes.push(<CheckPage>{page : page, checked : false});
console.info("checkboxes length: "+this.pagesCheckboxes.length);
this.applyCheck(false);
}
public pageUpdatedSuccessfully(page : Page) {
console.info(page._id);
console.info(this.pagesCheckboxes.find(checkItem => (checkItem.page._id == page._id)));
console.info(page.entities);
this.pagesCheckboxes.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.pagesCheckboxes = [];
this.pages.filter(item => this.filterPages(item)).forEach(
_ => this.pagesCheckboxes.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();
}
this.errorMessage = message + ' (Server responded: ' + error + ')';
}
public filterByCommunity(event: any) {
this.selectedCommunityId = event.target.value;
this.applyCommunityFilter(this.selectedCommunityId);
}
public applyCommunityFilter(community_id: string) {
this.getPages(community_id);
}
public togglePage(status : boolean, id : string) {
this._helpContentService.togglePage(this.selectedCommunityId,id,status).subscribe(
() => {
// for(let id of ret) {
// let i = this.pagesCheckboxes.findIndex(_ => _.page._id == id);
// this.pagesCheckboxes[i].page.isEnabled=status;
// }
//this.countPageHelpContents();
let i = this.pagesCheckboxes.findIndex(_ => _.page._id == id);
this.pagesCheckboxes[i].page.isEnabled=status;
this.applyCheck(false);
},
error => this.handleError('System error changing the status of the selected page(s)', error)
);
}
}