1
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
2
|
import { ActivatedRoute } from '@angular/router';
|
3
|
import { AdvQueryObject, EventsPage, NotificationFrequency, NotificationMode } from '../../domain/typeScriptClasses';
|
4
|
import { BrokerService } from '../../services/broker.service';
|
5
|
import { loadingEvents, noEventsForTopic, noEventsWithParams, noServiceMessage,
|
6
|
subscribingChooseFrequency, subscribingToEvents, subscribingToEventsError,
|
7
|
subscribingToeventsSuccess } from '../../domain/shared-messages';
|
8
|
import { AbstractControl, FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
9
|
import { AuthenticationService } from '../../services/authentication.service';
|
10
|
import { ConfirmationDialogComponent } from '../../shared/reusablecomponents/confirmation-dialog.component';
|
11
|
|
12
|
@Component ({
|
13
|
selector: 'app-content-events-of-repo-eventslist',
|
14
|
templateUrl: 'content-events-of-repo-eventslist.component.html'
|
15
|
})
|
16
|
|
17
|
export class ContentEventsOfRepoEventslistComponent implements OnInit {
|
18
|
errorMessage: string;
|
19
|
loadingMessage: string;
|
20
|
successMessage: string;
|
21
|
noEvents: string;
|
22
|
eventsPageInitialized = false;
|
23
|
|
24
|
topic = '';
|
25
|
lastTopicEntry = '';
|
26
|
correctTopic = '';
|
27
|
repoName = '';
|
28
|
|
29
|
advanceSearch: AdvQueryObject;
|
30
|
eventsPage: EventsPage;
|
31
|
currentPage: number; /* DELETE WHEN ADVANCED SHOW EVENTS IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
|
32
|
|
33
|
selectedItemIndex: number;
|
34
|
|
35
|
group: FormGroup;
|
36
|
readonly titleDefinition = { eventTitle: [''] };
|
37
|
readonly authorDefinition = { eventAuthor: [''] };
|
38
|
readonly subjectDefinition = { eventSubject: [''] };
|
39
|
readonly dateRangeDefinition = { dateFrom: '', dateTo: '' };
|
40
|
readonly groupDefinition = {
|
41
|
trustMin: [+''],
|
42
|
trustMax: [+''],
|
43
|
eventTitles: this.fb.array([this.initControl(this.titleDefinition)]),
|
44
|
eventAuthors: this.fb.array([this.initControl(this.authorDefinition)]),
|
45
|
eventSubjects: this.fb.array([this.initControl(this.subjectDefinition)]),
|
46
|
eventDateRanges: this.fb.array([this.initControl(this.dateRangeDefinition)])
|
47
|
};
|
48
|
|
49
|
eventTitleFormArray: any;
|
50
|
eventAuthorFormArray: any;
|
51
|
eventSubjectsFormArray: any;
|
52
|
eventDateRangesFormArray: any;
|
53
|
|
54
|
frequencyChoice: string;
|
55
|
userEmail: string;
|
56
|
modalErrorMessage: string;
|
57
|
|
58
|
isModalShown: boolean;
|
59
|
@ViewChild('subscribeToEventsModal')
|
60
|
public subscribeToEventsModal: ConfirmationDialogComponent;
|
61
|
|
62
|
constructor (private route: ActivatedRoute,
|
63
|
private fb: FormBuilder,
|
64
|
private brokerService: BrokerService,
|
65
|
private authService: AuthenticationService) {}
|
66
|
|
67
|
ngOnInit () {
|
68
|
this.userEmail = this.authService.getUserEmail();
|
69
|
this.getParams();
|
70
|
this.initQuery();
|
71
|
this.initForm();
|
72
|
this.currentPage = 0; /* DELETE WHEN ADVANCED SHOW EVENTS IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
|
73
|
this.getEventsPage(0);
|
74
|
let body = document.getElementsByTagName('body')[0];
|
75
|
body.classList.remove("top_bar_active"); //remove the class
|
76
|
body.classList.add("page_heading_active");
|
77
|
body.classList.remove("landing");
|
78
|
body.classList.add("dashboard");
|
79
|
}
|
80
|
|
81
|
getParams() {
|
82
|
this.topic = this.route.snapshot.paramMap.get('topic');
|
83
|
console.log(`my topic is: ${this.topic}`);
|
84
|
this.lastTopicEntry = this.topic.substring(this.topic.lastIndexOf('|') + 1).toLowerCase();
|
85
|
this.lastTopicEntry = this.replaceAll(this.lastTopicEntry, '_', ' ');
|
86
|
this.getCorrectTopic();
|
87
|
this.repoName = this.route.snapshot.paramMap.get('name');
|
88
|
}
|
89
|
|
90
|
replaceAll(str, find, replace) {
|
91
|
return str.replace(new RegExp(find, 'g'), replace);
|
92
|
}
|
93
|
|
94
|
initQuery() {
|
95
|
this.advanceSearch = {
|
96
|
datasource: this.repoName,
|
97
|
topic: this.correctTopic,
|
98
|
titles: [],
|
99
|
subjects: [],
|
100
|
authors: [],
|
101
|
dates: [],
|
102
|
trust: {min: '0', max: '1'},
|
103
|
page: 0
|
104
|
};
|
105
|
}
|
106
|
|
107
|
initForm() {
|
108
|
this.group = this.fb.group( this.groupDefinition, { validator: checkMinMax } );
|
109
|
this.group.get('trustMin').setValue(0);
|
110
|
this.group.get('trustMax').setValue(1);
|
111
|
}
|
112
|
|
113
|
initControl(definition: any) {
|
114
|
return this.fb.group(definition);
|
115
|
}
|
116
|
|
117
|
removeControl(controlName: string, i: number) {
|
118
|
const controlArray = <FormArray>this.group.controls[controlName];
|
119
|
controlArray.removeAt(i);
|
120
|
}
|
121
|
|
122
|
addControl(controlName: string, definition: any) {
|
123
|
const controlArray = <FormArray>this.group.controls[controlName];
|
124
|
controlArray.push(this.initControl(definition));
|
125
|
}
|
126
|
|
127
|
clearForm() {
|
128
|
let controlArray: FormArray;
|
129
|
controlArray = <FormArray>this.group.controls['eventTitles'];
|
130
|
controlArray.controls = [];
|
131
|
controlArray.push(this.initControl(this.titleDefinition));
|
132
|
|
133
|
controlArray = <FormArray>this.group.controls['eventAuthors'];
|
134
|
controlArray.controls = [];
|
135
|
controlArray.push(this.initControl(this.authorDefinition));
|
136
|
|
137
|
controlArray = <FormArray>this.group.controls['eventSubjects'];
|
138
|
controlArray.controls = [];
|
139
|
controlArray.push(this.initControl(this.subjectDefinition));
|
140
|
|
141
|
controlArray = <FormArray>this.group.controls['eventDateRanges'];
|
142
|
controlArray.controls = [];
|
143
|
controlArray.push(this.initControl(this.dateRangeDefinition));
|
144
|
|
145
|
this.group.get('trustMin').setValue(0);
|
146
|
this.group.get('trustMax').setValue(1);
|
147
|
|
148
|
this.initQuery();
|
149
|
this.getEventsPage(0);
|
150
|
}
|
151
|
|
152
|
updateQuery() {
|
153
|
let i: number;
|
154
|
let controlArray: FormArray;
|
155
|
|
156
|
if ( this.group.valid ) {
|
157
|
this.initQuery();
|
158
|
this.advanceSearch.trust.min = this.group.get('trustMin').value;
|
159
|
this.advanceSearch.trust.max = this.group.get('trustMax').value;
|
160
|
|
161
|
controlArray = <FormArray>this.group.controls['eventTitles'];
|
162
|
for (i = 0; i < controlArray.length; i++) {
|
163
|
if (controlArray.at(i).get('eventTitle').value) {
|
164
|
this.advanceSearch.titles.push(controlArray.at(i).get('eventTitle').value);
|
165
|
}
|
166
|
}
|
167
|
controlArray = <FormArray>this.group.controls['eventAuthors'];
|
168
|
for (i = 0; i < controlArray.length; i++) {
|
169
|
if (controlArray.at(i).get('eventAuthor').value) {
|
170
|
this.advanceSearch.authors.push(controlArray.at(i).get('eventAuthor').value);
|
171
|
}
|
172
|
}
|
173
|
controlArray = <FormArray>this.group.controls['eventSubjects'];
|
174
|
for (i = 0; i < controlArray.length; i++) {
|
175
|
if (controlArray.at(i).get('eventSubject').value) {
|
176
|
this.advanceSearch.subjects.push(controlArray.at(i).get('eventSubject').value);
|
177
|
}
|
178
|
}
|
179
|
controlArray = <FormArray>this.group.controls['eventDateRanges'];
|
180
|
for (i = 0; i < controlArray.length; i++) {
|
181
|
if (controlArray.at(i).get('dateFrom').value) {
|
182
|
let toDate;
|
183
|
if (controlArray.at(i).get('dateTo').value ||
|
184
|
(controlArray.at(i).get('dateFrom').value > controlArray.at(i).get('dateTo').value) ) {
|
185
|
toDate = controlArray.at(i).get('dateTo').value;
|
186
|
} else {
|
187
|
toDate = Date.now();
|
188
|
}
|
189
|
this.advanceSearch.dates.push({
|
190
|
min: controlArray.at(i).get('dateFrom').value,
|
191
|
max: toDate
|
192
|
});
|
193
|
}
|
194
|
}
|
195
|
console.log(this.advanceSearch);
|
196
|
this.currentPage = 0; /* DELETE WHEN ADVANCED SHOW EVENTS IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
|
197
|
this.getEventsPage(0);
|
198
|
}
|
199
|
}
|
200
|
|
201
|
getEventsPage(page: number) {
|
202
|
this.noEvents = '';
|
203
|
this.errorMessage = '';
|
204
|
this.successMessage = '';
|
205
|
this. loadingMessage = loadingEvents;
|
206
|
this.brokerService.advancedShowEvents(page, 10, this.advanceSearch).subscribe(
|
207
|
events => this.eventsPage = events,
|
208
|
error => {
|
209
|
this.loadingMessage = '';
|
210
|
this.errorMessage = noServiceMessage;
|
211
|
console.log(error);
|
212
|
},
|
213
|
() => {
|
214
|
this.loadingMessage = '';
|
215
|
console.log(this.eventsPage);
|
216
|
if (!this.eventsPage.total) {
|
217
|
if (!this.eventsPageInitialized) {
|
218
|
this.noEvents = noEventsForTopic;
|
219
|
} else {
|
220
|
this.noEvents = noEventsWithParams;
|
221
|
}
|
222
|
}
|
223
|
let tempArray = <FormArray>this.group.controls['eventTitles'];
|
224
|
this.eventTitleFormArray = tempArray.controls;
|
225
|
tempArray = <FormArray>this.group.controls['eventAuthors'];
|
226
|
this.eventAuthorFormArray = tempArray.controls;
|
227
|
tempArray = <FormArray>this.group.controls['eventSubjects'];
|
228
|
this.eventSubjectsFormArray = tempArray.controls;
|
229
|
tempArray = <FormArray>this.group.controls['eventDateRanges'];
|
230
|
this.eventDateRangesFormArray = tempArray.controls;
|
231
|
console.log(`total pages is ${this.eventsPage.totalPages}`);
|
232
|
this.eventsPageInitialized = true;
|
233
|
}
|
234
|
);
|
235
|
}
|
236
|
|
237
|
isHighlighted(item: any, itemList: any[]) {
|
238
|
return itemList.some(x => x === item);
|
239
|
}
|
240
|
|
241
|
getCorrectTopic() {
|
242
|
const temp = this.topic.split('|');
|
243
|
this.correctTopic = temp[0];
|
244
|
this.topic = temp[0];
|
245
|
for (let i = 1; i < temp.length; i++) {
|
246
|
this.correctTopic += `/${temp[i]}`;
|
247
|
this.topic += ` | ${temp[i]}`;
|
248
|
}
|
249
|
}
|
250
|
|
251
|
goToNextPage() {
|
252
|
/* RESTORE WHEN ADVANCED SHOW EVENTS IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
|
253
|
/*if(this.eventsPage.currPage < this.eventsPage.totalPages) {
|
254
|
console.log(`Get me page ${this.eventsPage.currPage+1}!`);
|
255
|
this.getEventsPage(this.eventsPage.currPage+1);
|
256
|
}*/
|
257
|
|
258
|
/* DELETE WHEN ADVANCED SHOW EVENTS IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
|
259
|
if ( (this.currentPage + 1) < this.eventsPage.totalPages) {
|
260
|
this.currentPage = this.currentPage + 1;
|
261
|
console.log(`Get me page ${this.currentPage}!`);
|
262
|
this.getEventsPage(this.currentPage);
|
263
|
}
|
264
|
|
265
|
}
|
266
|
|
267
|
goToPreviousPage() {
|
268
|
/* RESTORE WHEN ADVANCED SHOW EVENTS IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
|
269
|
/*if(this.eventsPage.currPage > 0) {
|
270
|
console.log(`Get me page ${this.eventsPage.currPage-1}!`);
|
271
|
this.getEventsPage(this.eventsPage.currPage-1);
|
272
|
}*/
|
273
|
|
274
|
/* DELETE WHEN ADVANCED SHOW EVENTS IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
|
275
|
if (this.currentPage > 0) {
|
276
|
this.currentPage = this.currentPage - 1;
|
277
|
console.log(`Get me page ${this.currentPage}!`);
|
278
|
this.getEventsPage(this.currentPage);
|
279
|
}
|
280
|
}
|
281
|
|
282
|
showSubscriptionModal() {
|
283
|
if (this.advanceSearch && this.eventsPage) {
|
284
|
this.subscribeToEventsModal.confirmed = false;
|
285
|
this.subscribeToEventsModal.showModal();
|
286
|
}
|
287
|
}
|
288
|
|
289
|
choseFrequency(freq: string) {
|
290
|
this.frequencyChoice = freq;
|
291
|
}
|
292
|
|
293
|
subscribeToEvents(event: any) {
|
294
|
this.modalErrorMessage = '';
|
295
|
if (this.frequencyChoice) {
|
296
|
this.subscribeToEventsModal.confirmed = true;
|
297
|
const freq = <NotificationFrequency>this.frequencyChoice;
|
298
|
const mod: NotificationMode = 'EMAIL';
|
299
|
const sub = {
|
300
|
subscriber: this.userEmail,
|
301
|
frequency: freq,
|
302
|
mode: mod,
|
303
|
query: this.advanceSearch
|
304
|
};
|
305
|
this.errorMessage = '';
|
306
|
this.successMessage = '';
|
307
|
console.log(JSON.stringify(sub));
|
308
|
this.loadingMessage = subscribingToEvents;
|
309
|
this.brokerService.subscribeToEvent(sub).subscribe(
|
310
|
response => console.log(`subscribeToEvents responded ${JSON.stringify(response)}`),
|
311
|
error => {
|
312
|
this.errorMessage = subscribingToEventsError;
|
313
|
this.loadingMessage = '';
|
314
|
},
|
315
|
() => {
|
316
|
this.loadingMessage = '';
|
317
|
this.successMessage = subscribingToeventsSuccess;
|
318
|
}
|
319
|
);
|
320
|
} else {
|
321
|
this.modalErrorMessage = subscribingChooseFrequency;
|
322
|
}
|
323
|
}
|
324
|
|
325
|
displayFullResultInfo(i: number) {
|
326
|
if (this.selectedItemIndex === i) {
|
327
|
this.selectedItemIndex = null;
|
328
|
} else {
|
329
|
this.selectedItemIndex = i;
|
330
|
}
|
331
|
}
|
332
|
|
333
|
}
|
334
|
|
335
|
export function checkMinMax(c: AbstractControl) {
|
336
|
if ( c.get('trustMin').value > c.get('trustMax').value ) {
|
337
|
return 'invalid';
|
338
|
}
|
339
|
return null;
|
340
|
}
|
341
|
|
342
|
export function checkDates(c: AbstractControl) {
|
343
|
if ( c.get('dateFrom').value > c.get('dateTo').value ) {
|
344
|
return 'invalid';
|
345
|
}
|
346
|
return null;
|
347
|
}
|