2017-12-20 15:26:30 +01:00
|
|
|
/**
|
|
|
|
* Created by stefania on 7/13/17.
|
|
|
|
*/
|
|
|
|
import { Component, ViewChild, OnInit } from '@angular/core';
|
|
|
|
import { ActivatedRoute } from "@angular/router";
|
|
|
|
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 { CommunityFormComponent } from "./community-form.component";
|
|
|
|
import { CheckCommunity, Community } from "../../domain/community";
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'communities',
|
|
|
|
templateUrl: './communities.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class CommunitiesComponent implements OnInit {
|
|
|
|
|
|
|
|
@ViewChild(ModalFormComponent)
|
|
|
|
@ViewChild('saveModal')
|
|
|
|
public modal:ModalFormComponent;
|
|
|
|
|
|
|
|
@ViewChild('updateModal')
|
|
|
|
public updateModal:ModalFormComponent;
|
|
|
|
|
|
|
|
@ViewChild('deleteConfirmationModal')
|
|
|
|
public deleteConfirmationModal : DeleteConfirmationDialogComponent;
|
|
|
|
|
|
|
|
@ViewChild(CommunityFormComponent)
|
|
|
|
public formComponent : CommunityFormComponent;
|
|
|
|
|
2018-01-04 16:56:22 +01:00
|
|
|
public checkboxes : CheckCommunity[] = [];
|
2017-12-20 15:26:30 +01:00
|
|
|
|
|
|
|
public communities : Community[] = [];
|
|
|
|
|
|
|
|
public errorMessage: string;
|
|
|
|
|
|
|
|
public formGroup : FormGroup;
|
|
|
|
|
|
|
|
private searchText : RegExp = new RegExp('');
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.formGroup = this.formComponent.form;
|
|
|
|
this.route.queryParams.subscribe(params => {
|
|
|
|
if(params['community']) {
|
|
|
|
this.getCommunity(params['community']);
|
|
|
|
} else {
|
|
|
|
this.getCommunities();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
|
|
|
|
|
2018-01-29 23:45:42 +01:00
|
|
|
getCommunity(community_pid: string) {
|
2017-12-20 15:26:30 +01:00
|
|
|
let self = this;
|
2018-01-29 23:45:42 +01:00
|
|
|
this._helpContentService.getCommunityFull(community_pid).subscribe(
|
2017-12-20 15:26:30 +01:00
|
|
|
community => {
|
|
|
|
self.communities = [community];
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.push(<CheckCommunity>{community : community, checked : false});
|
2017-12-20 15:26:30 +01:00
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving communities', error));
|
|
|
|
}
|
|
|
|
|
|
|
|
getCommunities() {
|
|
|
|
let self = this;
|
|
|
|
this._helpContentService.getCommunitiesFull().subscribe(
|
|
|
|
communities => {
|
|
|
|
self.communities = communities;
|
|
|
|
|
|
|
|
communities.forEach(_ => {
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.push(<CheckCommunity>{community : _, checked : false});
|
2017-12-20 15:26:30 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving communities', error));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-20 15:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public applyCheck(flag : boolean) {
|
|
|
|
console.info("applyCheck "+flag);
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
2017-12-20 15:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getSelectedCommunities() : string[] {
|
2018-01-04 16:56:22 +01:00
|
|
|
return this.checkboxes.filter(community => community.checked == true).map(checkedCommunity => checkedCommunity.community).map(res => res._id);
|
2017-12-20 15:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private deleteCommunitiesFromArray(ids : string[]) : void {
|
|
|
|
for(let id of ids) {
|
2018-01-04 16:56:22 +01:00
|
|
|
let i = this.checkboxes.findIndex(_ => _.community._id == id);
|
|
|
|
this.checkboxes.splice(i, 1);
|
2017-12-20 15:26:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDeleteCommunity(id : string) {
|
|
|
|
this.deleteConfirmationModal.ids = [id];
|
|
|
|
this.deleteConfirmationModal.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDeleteSelectedCommunities() {
|
|
|
|
this.deleteConfirmationModal.ids = this.getSelectedCommunities();
|
|
|
|
this.deleteConfirmationModal.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmedDeleteCommunities(ids : string[]) {
|
|
|
|
this._helpContentService.deleteCommunities(ids).subscribe(
|
|
|
|
_ => this.deleteCommunitiesFromArray(ids),
|
|
|
|
error => this.handleError('System error deleting the selected communities', error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public editCommunity(i : number) {
|
2018-01-04 16:56:22 +01:00
|
|
|
let community : Community = this.checkboxes[i].community;
|
2017-12-20 15:26:30 +01:00
|
|
|
this.formGroup.patchValue(community);
|
|
|
|
this.updateModal.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public communitySavedSuccessfully(community: Community) {
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.push(<CheckCommunity>{community : community, checked : false});
|
|
|
|
console.info("checkboxes length: "+this.checkboxes.length);
|
2017-12-20 15:26:30 +01:00
|
|
|
this.applyCheck(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public communityUpdatedSuccessfully(community : Community) {
|
2018-01-04 16:56:22 +01:00
|
|
|
this.checkboxes.find(checkItem => checkItem.community._id==community._id).community = community;
|
2017-12-20 15:26:30 +01: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-12-20 15:26:30 +01:00
|
|
|
this.communities.filter(item => this.filterCommunities(item)).forEach(
|
2018-01-04 16:56:22 +01:00
|
|
|
_ => this.checkboxes.push(<CheckCommunity>{community: _, checked: false})
|
2017-12-20 15:26:30 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public filterCommunities(community : Community) : boolean {
|
|
|
|
let textFlag = this.searchText.toString() == '' || (/*community.route + ' ' +*/community.name).match(this.searchText) != null;
|
|
|
|
return textFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
if(error == null) {
|
|
|
|
this.formComponent.reset();
|
|
|
|
}
|
|
|
|
this.errorMessage = message + ' (Server responded: ' + error + ')';
|
|
|
|
}
|
|
|
|
}
|