1
|
import { Component, ViewChild, OnInit } from '@angular/core';
|
2
|
import { HelpContentService } from "../../services/help-content.service";
|
3
|
import { FormGroup } from "@angular/forms";
|
4
|
import { ModalFormComponent } from "../modal-form.component";
|
5
|
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
|
6
|
import { EntityFormComponent } from "./entity-form.component";
|
7
|
import { CheckEntity, Entity } from "../../domain/entity";
|
8
|
import { Community } from "../../domain/community";
|
9
|
|
10
|
@Component({
|
11
|
selector: 'entities',
|
12
|
templateUrl: './entities.component.html',
|
13
|
})
|
14
|
|
15
|
export class EntitiesComponent implements OnInit {
|
16
|
|
17
|
// @ViewChild(ModalFormComponent)
|
18
|
@ViewChild('saveModal')
|
19
|
public modal:ModalFormComponent;
|
20
|
|
21
|
@ViewChild('updateModal')
|
22
|
public updateModal:ModalFormComponent;
|
23
|
|
24
|
@ViewChild('deleteConfirmationModal')
|
25
|
public deleteConfirmationModal : DeleteConfirmationDialogComponent;
|
26
|
|
27
|
@ViewChild(EntityFormComponent)
|
28
|
public formComponent : EntityFormComponent;
|
29
|
|
30
|
public checkboxes : CheckEntity[] = [];
|
31
|
|
32
|
public entities : Entity[] = [];
|
33
|
|
34
|
public errorMessage: string;
|
35
|
|
36
|
public formGroup : FormGroup;
|
37
|
|
38
|
private searchText : RegExp = new RegExp('');
|
39
|
|
40
|
public communities: Community[] = [];
|
41
|
|
42
|
public selectedCommunityId: string;
|
43
|
|
44
|
ngOnInit() {
|
45
|
this.getCommunities();
|
46
|
|
47
|
this.formGroup = this.formComponent.form;
|
48
|
}
|
49
|
|
50
|
constructor(private _helpContentService: HelpContentService) {}
|
51
|
|
52
|
getCommunities() {
|
53
|
let self = this;
|
54
|
this._helpContentService.getCommunities().subscribe(
|
55
|
communities => {
|
56
|
self.communities = communities;
|
57
|
self.getEntities(self.communities[0]._id);
|
58
|
self.selectedCommunityId = self.communities[0]._id;
|
59
|
},
|
60
|
error => this.handleError('System error retrieving communities', error));
|
61
|
}
|
62
|
|
63
|
getEntities(community_id: string) {
|
64
|
let self = this;
|
65
|
this._helpContentService.getCommunityEntities(community_id).subscribe(
|
66
|
entities => {
|
67
|
self.entities = entities;
|
68
|
self.checkboxes = [];
|
69
|
|
70
|
entities.forEach(_ => {
|
71
|
self.checkboxes.push(<CheckEntity>{entity : _, checked : false});
|
72
|
});
|
73
|
},
|
74
|
error => this.handleError('System error retrieving entities', error));
|
75
|
}
|
76
|
|
77
|
public showModal():void {
|
78
|
this.modal.showModal();
|
79
|
}
|
80
|
|
81
|
public toggleCheckBoxes(event) {
|
82
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
83
|
}
|
84
|
|
85
|
public applyCheck(flag : boolean) {
|
86
|
console.info("applyCheck "+flag);
|
87
|
this.checkboxes.forEach(_ => _.checked = flag);
|
88
|
}
|
89
|
|
90
|
public getSelectedEntities() : string[] {
|
91
|
return this.checkboxes.filter(entity => entity.checked == true).map(checkedEntity => checkedEntity.entity).map(res => res._id);
|
92
|
}
|
93
|
|
94
|
private deleteEntitiesFromArray(ids : string[]) : void {
|
95
|
for(let id of ids) {
|
96
|
let i = this.checkboxes.findIndex(_ => _.entity._id == id);
|
97
|
this.checkboxes.splice(i, 1);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
public confirmDeleteEntity(id : string) {
|
102
|
this.deleteConfirmationModal.ids = [id];
|
103
|
this.deleteConfirmationModal.showModal();
|
104
|
}
|
105
|
|
106
|
public confirmDeleteSelectedEntities() {
|
107
|
this.deleteConfirmationModal.ids = this.getSelectedEntities();
|
108
|
this.deleteConfirmationModal.showModal();
|
109
|
}
|
110
|
|
111
|
public confirmedDeleteEntities(ids : string[]) {
|
112
|
this._helpContentService.deleteEntities(ids).subscribe(
|
113
|
_ => this.deleteEntitiesFromArray(ids),
|
114
|
error => this.handleError('System error deleting the selected entities', error)
|
115
|
);
|
116
|
}
|
117
|
|
118
|
public editEntity(i : number) {
|
119
|
let entity : Entity = this.checkboxes[i].entity;
|
120
|
this.formGroup.patchValue(entity);
|
121
|
this.updateModal.showModal();
|
122
|
}
|
123
|
|
124
|
public entitySavedSuccessfully(entity: Entity) {
|
125
|
this.checkboxes.push(<CheckEntity>{entity : entity, checked : false});
|
126
|
this.applyCheck(false);
|
127
|
}
|
128
|
|
129
|
public entityUpdatedSuccessfully(entity : Entity) {
|
130
|
this.checkboxes.find(checkItem => checkItem.entity._id==entity._id).entity = entity;
|
131
|
this.applyCheck(false);
|
132
|
}
|
133
|
|
134
|
public filterBySearch(text : string) {
|
135
|
this.searchText = new RegExp(text,'i');
|
136
|
this.applyFilter();
|
137
|
}
|
138
|
|
139
|
public applyFilter() {
|
140
|
this.checkboxes = [];
|
141
|
this.entities.filter(item => this.filterEntities(item)).forEach(
|
142
|
_ => this.checkboxes.push(<CheckEntity>{entity: _, checked: false})
|
143
|
);
|
144
|
}
|
145
|
|
146
|
public filterEntities(entity : Entity) : boolean {
|
147
|
let textFlag = this.searchText.toString() == '' || (entity.name).match(this.searchText) != null;
|
148
|
return textFlag;
|
149
|
}
|
150
|
|
151
|
handleError(message: string, error) {
|
152
|
if(error == null) {
|
153
|
this.formComponent.reset();
|
154
|
}
|
155
|
this.errorMessage = message + ' (Server responded: ' + error + ')';
|
156
|
}
|
157
|
|
158
|
|
159
|
|
160
|
public filterByCommunity(event: any) {
|
161
|
this.selectedCommunityId = event.target.value;
|
162
|
this.applyCommunityFilter(this.selectedCommunityId);
|
163
|
}
|
164
|
|
165
|
public applyCommunityFilter(community_id: string) {
|
166
|
this.getEntities(community_id);
|
167
|
}
|
168
|
|
169
|
public toggleEntity(status : boolean, id : string) {
|
170
|
this._helpContentService.toggleEntity(this.selectedCommunityId,id,status).subscribe(
|
171
|
() => {
|
172
|
let i = this.checkboxes.findIndex(_ => _.entity._id == id);
|
173
|
this.checkboxes[i].entity.isEnabled=status;
|
174
|
this.applyCheck(false);
|
175
|
},
|
176
|
error => this.handleError('System error changing the status of the selected entity(-ies)', error)
|
177
|
);
|
178
|
}
|
179
|
|
180
|
}
|