1
|
/**
|
2
|
* Created by stefania on 7/13/17.
|
3
|
*/
|
4
|
import { Component, ViewChild, OnInit } from '@angular/core';
|
5
|
import { ActivatedRoute } from "@angular/router";
|
6
|
import { HelpContentService } from "../../services/help-content.service";
|
7
|
import { FormGroup } from "@angular/forms";
|
8
|
import { ModalFormComponent } from "../modal-form.component";
|
9
|
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
|
10
|
import { PageFormComponent } from "./page-form.component";
|
11
|
import { CheckPage, Page } from "../../domain/page";
|
12
|
import { Community } from "../../domain/community";
|
13
|
import { Entity } from "../../domain/entity";
|
14
|
|
15
|
@Component({
|
16
|
selector: 'pages',
|
17
|
templateUrl: './pages.component.html',
|
18
|
})
|
19
|
|
20
|
export class PagesComponent implements OnInit {
|
21
|
|
22
|
// @ViewChild(ModalFormComponent)
|
23
|
@ViewChild('saveModal')
|
24
|
public modal:ModalFormComponent;
|
25
|
|
26
|
@ViewChild('updateModal')
|
27
|
public updateModal:ModalFormComponent;
|
28
|
|
29
|
@ViewChild('deleteConfirmationModal')
|
30
|
public deleteConfirmationModal : DeleteConfirmationDialogComponent;
|
31
|
|
32
|
@ViewChild(PageFormComponent)
|
33
|
public formComponent : PageFormComponent;
|
34
|
|
35
|
public checkboxes : CheckPage[] = [];
|
36
|
|
37
|
public pages : Page[] = [];
|
38
|
|
39
|
public errorMessage: string;
|
40
|
|
41
|
public formGroup : FormGroup;
|
42
|
|
43
|
private searchText : RegExp = new RegExp('');
|
44
|
|
45
|
public communities: Community[] = [];
|
46
|
|
47
|
public selectedCommunityId: string;
|
48
|
|
49
|
public pagesType: string;
|
50
|
|
51
|
ngOnInit() {
|
52
|
this.formGroup = this.formComponent.form;
|
53
|
// this.getCommunities();
|
54
|
|
55
|
this.route.queryParams.subscribe(params => {
|
56
|
this.pagesType = "";
|
57
|
if(params['type']) {
|
58
|
this.pagesType = params['type'];
|
59
|
}
|
60
|
this.getCommunities();
|
61
|
});
|
62
|
}
|
63
|
|
64
|
constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
|
65
|
|
66
|
getPages(community_id: string) {
|
67
|
let self = this;
|
68
|
|
69
|
if(this.pagesType) {
|
70
|
this._helpContentService.getCommunityPagesByType(community_id, "?page_type="+this.pagesType).subscribe(
|
71
|
pages => {
|
72
|
self.pagesReturned(pages);
|
73
|
},
|
74
|
error => this.handleError('System error retrieving pages', error));
|
75
|
} else {
|
76
|
this._helpContentService.getCommunityPages(community_id).subscribe(
|
77
|
pages => {
|
78
|
self.pagesReturned(pages);
|
79
|
},
|
80
|
error => this.handleError('System error retrieving pages', error));
|
81
|
}
|
82
|
}
|
83
|
|
84
|
pagesReturned(pages: Page[]) {
|
85
|
this.pages = pages;
|
86
|
this.checkboxes = [];
|
87
|
|
88
|
pages.forEach(_ => {
|
89
|
this.checkboxes.push(<CheckPage>{page : _, checked : false});
|
90
|
});
|
91
|
}
|
92
|
|
93
|
getCommunities() {
|
94
|
let self = this;
|
95
|
this._helpContentService.getCommunities().subscribe(
|
96
|
communities => {
|
97
|
self.communities = communities;
|
98
|
self.getPages(self.communities[0]._id);
|
99
|
self.selectedCommunityId = self.communities[0]._id;
|
100
|
},
|
101
|
error => this.handleError('System error retrieving communities', error));
|
102
|
}
|
103
|
|
104
|
public showModal():void {
|
105
|
this.modal.showModal();
|
106
|
}
|
107
|
|
108
|
public toggleCheckBoxes(event) {
|
109
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
110
|
}
|
111
|
|
112
|
public applyCheck(flag : boolean) {
|
113
|
console.info("applyCheck "+flag);
|
114
|
this.checkboxes.forEach(_ => _.checked = flag);
|
115
|
}
|
116
|
|
117
|
public getSelectedPages() : string[] {
|
118
|
return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
|
119
|
}
|
120
|
|
121
|
private deletePagesFromArray(ids : string[]) : void {
|
122
|
for(let id of ids) {
|
123
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
124
|
this.checkboxes.splice(i, 1);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
public confirmDeletePage(id : string) {
|
129
|
this.deleteConfirmationModal.ids = [id];
|
130
|
this.deleteConfirmationModal.showModal();
|
131
|
}
|
132
|
|
133
|
public confirmDeleteSelectedPages() {
|
134
|
this.deleteConfirmationModal.ids = this.getSelectedPages();
|
135
|
this.deleteConfirmationModal.showModal();
|
136
|
}
|
137
|
|
138
|
public confirmedDeletePages(ids : string[]) {
|
139
|
this._helpContentService.deletePages(ids).subscribe(
|
140
|
_ => this.deletePagesFromArray(ids),
|
141
|
error => this.handleError('System error deleting the selected pages', error)
|
142
|
);
|
143
|
}
|
144
|
|
145
|
public editPage(i : number) {
|
146
|
let page : Page = this.checkboxes[i].page;
|
147
|
this.formGroup.patchValue(page);
|
148
|
const entityFGs = (page.entities as Entity[]).map(entity => this.formComponent._fb.group(entity));
|
149
|
const entityFormArray = this.formComponent._fb.array(entityFGs);
|
150
|
this.formGroup.setControl('entities', entityFormArray);
|
151
|
console.info(this.formGroup.value);
|
152
|
this.updateModal.showModal();
|
153
|
}
|
154
|
|
155
|
public pageSavedSuccessfully(page: Page) {
|
156
|
this.checkboxes.push(<CheckPage>{page : page, checked : false});
|
157
|
console.info("checkboxes length: "+this.checkboxes.length);
|
158
|
this.applyCheck(false);
|
159
|
}
|
160
|
|
161
|
public pageUpdatedSuccessfully(page : Page) {
|
162
|
console.info(page._id);
|
163
|
console.info(this.checkboxes.find(checkItem => (checkItem.page._id == page._id)));
|
164
|
console.info(page.entities);
|
165
|
this.checkboxes.find(checkItem => checkItem.page._id==page._id).page = page;
|
166
|
this.applyCheck(false);
|
167
|
}
|
168
|
|
169
|
public filterBySearch(text : string) {
|
170
|
this.searchText = new RegExp(text,'i');
|
171
|
this.applyFilter();
|
172
|
}
|
173
|
|
174
|
public applyFilter() {
|
175
|
this.checkboxes = [];
|
176
|
this.pages.filter(item => this.filterPages(item)).forEach(
|
177
|
_ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
|
178
|
);
|
179
|
}
|
180
|
|
181
|
public filterPages(page : Page) : boolean {
|
182
|
let textFlag = this.searchText.toString() == '' || (page.route + ' ' +page.name).match(this.searchText) != null;
|
183
|
return textFlag;
|
184
|
}
|
185
|
|
186
|
handleError(message: string, error) {
|
187
|
if(error == null) {
|
188
|
this.formComponent.reset();
|
189
|
}
|
190
|
this.errorMessage = message + ' (Server responded: ' + error + ')';
|
191
|
}
|
192
|
|
193
|
|
194
|
|
195
|
public filterByCommunity(event: any) {
|
196
|
this.selectedCommunityId = event.target.value;
|
197
|
this.applyCommunityFilter(this.selectedCommunityId);
|
198
|
}
|
199
|
|
200
|
public applyCommunityFilter(community_id: string) {
|
201
|
this.getPages(community_id);
|
202
|
}
|
203
|
|
204
|
public togglePage(status : boolean, id : string) {
|
205
|
this._helpContentService.togglePage(this.selectedCommunityId,id,status).subscribe(
|
206
|
() => {
|
207
|
// for(let id of ret) {
|
208
|
// let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
209
|
// this.checkboxes[i].page.isEnabled=status;
|
210
|
// }
|
211
|
//this.countPageHelpContents();
|
212
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
213
|
this.checkboxes[i].page.isEnabled=status;
|
214
|
this.applyCheck(false);
|
215
|
},
|
216
|
error => this.handleError('System error changing the status of the selected page(s)', error)
|
217
|
);
|
218
|
}
|
219
|
|
220
|
|
221
|
}
|