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

181 lines
5.8 KiB
TypeScript

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 { EntityFormComponent } from "./entity-form.component";
import { CheckEntity, Entity } from "../../domain/entity";
import { Community } from "../../domain/community";
@Component({
selector: 'entities',
templateUrl: './entities.component.html',
})
export class EntitiesComponent implements OnInit {
// @ViewChild(ModalFormComponent)
@ViewChild('saveModal')
public modal:ModalFormComponent;
@ViewChild('updateModal')
public updateModal:ModalFormComponent;
@ViewChild('deleteConfirmationModal')
public deleteConfirmationModal : DeleteConfirmationDialogComponent;
@ViewChild(EntityFormComponent)
public formComponent : EntityFormComponent;
public entitiesCheckboxes : CheckEntity[] = [];
public entities : Entity[] = [];
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) {}
getCommunities() {
let self = this;
this._helpContentService.getCommunities().subscribe(
communities => {
self.communities = communities;
self.getEntities(self.communities[0]._id);
self.selectedCommunityId = self.communities[0]._id;
},
error => this.handleError('System error retrieving communities', error));
}
getEntities(community_id: string) {
let self = this;
this._helpContentService.getCommunityEntities(community_id).subscribe(
entities => {
self.entities = entities;
self.entitiesCheckboxes = [];
entities.forEach(_ => {
self.entitiesCheckboxes.push(<CheckEntity>{entity : _, checked : false});
});
},
error => this.handleError('System error retrieving entities', error));
}
public showModal():void {
this.modal.showModal();
}
public toggleCheckBoxes(event) {
this.entitiesCheckboxes.forEach(_ => _.checked = event.target.checked);
}
public applyCheck(flag : boolean) {
console.info("applyCheck "+flag);
this.entitiesCheckboxes.forEach(_ => _.checked = flag);
}
public getSelectedEntities() : string[] {
return this.entitiesCheckboxes.filter(entity => entity.checked == true).map(checkedEntity => checkedEntity.entity).map(res => res._id);
}
private deleteEntitiesFromArray(ids : string[]) : void {
for(let id of ids) {
let i = this.entitiesCheckboxes.findIndex(_ => _.entity._id == id);
this.entitiesCheckboxes.splice(i, 1);
}
}
public confirmDeleteEntity(id : string) {
this.deleteConfirmationModal.ids = [id];
this.deleteConfirmationModal.showModal();
}
public confirmDeleteSelectedEntities() {
this.deleteConfirmationModal.ids = this.getSelectedEntities();
this.deleteConfirmationModal.showModal();
}
public confirmedDeleteEntities(ids : string[]) {
this._helpContentService.deleteEntities(ids).subscribe(
_ => this.deleteEntitiesFromArray(ids),
error => this.handleError('System error deleting the selected entities', error)
);
}
public editEntity(i : number) {
let entity : Entity = this.entitiesCheckboxes[i].entity;
this.formGroup.patchValue(entity);
this.updateModal.showModal();
}
public entitySavedSuccessfully(entity: Entity) {
this.entitiesCheckboxes.push(<CheckEntity>{entity : entity, checked : false});
this.applyCheck(false);
}
public entityUpdatedSuccessfully(entity : Entity) {
this.entitiesCheckboxes.find(checkItem => checkItem.entity._id==entity._id).entity = entity;
this.applyCheck(false);
}
public filterBySearch(text : string) {
this.searchText = new RegExp(text,'i');
this.applyFilter();
}
public applyFilter() {
this.entitiesCheckboxes = [];
this.entities.filter(item => this.filterEntities(item)).forEach(
_ => this.entitiesCheckboxes.push(<CheckEntity>{entity: _, checked: false})
);
}
public filterEntities(entity : Entity) : boolean {
let textFlag = this.searchText.toString() == '' || (entity.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.getEntities(community_id);
}
public toggleEntity(status : boolean, id : string) {
this._helpContentService.toggleEntity(this.selectedCommunityId,id,status).subscribe(
() => {
let i = this.entitiesCheckboxes.findIndex(_ => _.entity._id == id);
this.entitiesCheckboxes[i].entity.isEnabled=status;
this.applyCheck(false);
},
error => this.handleError('System error changing the status of the selected entity(-ies)', error)
);
}
}