-
Notifications
You must be signed in to change notification settings - Fork 6
/
list-view.component.ts
368 lines (319 loc) · 11.4 KB
/
list-view.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
EventEmitter,
Inject,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import {
ApiResponseError,
CountQueryResponse,
IFulltextSearchParams,
KnoraApiConnection,
ReadResourceSequence,
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken, RouteConstants } from '@dasch-swiss/vre/shared/app-config';
import { AppErrorHandler } from '@dasch-swiss/vre/shared/app-error-handler';
import { NotificationService } from '@dasch-swiss/vre/shared/app-notification';
import { OntologyClassSelectors } from '@dasch-swiss/vre/shared/app-state';
import { Store } from '@ngxs/store';
import { Subject, Subscription, combineLatest, of } from 'rxjs';
import { map, take, takeUntil } from 'rxjs/operators';
import {
ComponentCommunicationEventService,
EmitEvent,
Events,
} from '../../../main/services/component-communication-event.service';
/**
* query: search query. It can be gravserch query or fulltext string query.
* The query value is expected to have at least length of 3 characters.
*
* mode: search mode "fulltext" OR "gravsearch"
*
* filter: Optional fulltext search parameter with following (optional) properties:
* - limitToResourceClass: string; Iri of resource class the fulltext search is restricted to, if any.
* - limitToProject: string; Iri of the project the fulltext search is restricted to, if any.
* - limitToStandoffClass: string; Iri of standoff class the fulltext search is restricted to, if any.
*/
export interface SearchParams {
query: string;
mode: 'fulltext' | 'gravsearch';
filter?: IFulltextSearchParams;
projectUuid?: string;
classId?: string;
}
export interface ShortResInfo {
id: string;
label: string;
}
/* return the selected resources in below format
*
* count: total number of resources selected
* selectedIds: list of selected resource's ids
*/
export interface FilteredResources {
count: number;
resListIndex: number[];
resInfo: ShortResInfo[];
selectionType: 'multiple' | 'single';
}
/* return the checkbox value
*
* checked: checkbox value
* resIndex: resource index from the list
*/
export interface CheckboxUpdate {
checked: boolean;
resIndex: number;
resId: string;
resLabel: string;
isCheckbox: boolean;
}
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'app-list-view',
templateUrl: './list-view.component.html',
styleUrls: ['./list-view.component.scss'],
})
export class ListViewComponent implements OnChanges, OnInit, OnDestroy {
private ngUnsubscribe: Subject<void> = new Subject<void>();
@Input() search: SearchParams;
currentSearch: SearchParams;
/**
* set to true if multiple resources can be selected for comparison
*/
@Input() withMultipleSelection?: boolean = false;
/**
* emits the selected resources 1-n
*/
@Output() selectedResources: EventEmitter<FilteredResources> = new EventEmitter<FilteredResources>();
resources: ReadResourceSequence;
selectedResourceIdx: number[] = [];
componentCommsSubscriptions: Subscription[] = [];
resetCheckBoxes = false;
// number of all results including the ones not included as resources in the response bc. the user does not have the permissions to see them
numberOfAllResults: number;
// progress status
loading = true;
currentIndex = 0;
currentRangeStart = 1;
currentRangeEnd = 0;
pageSize = 25;
previousDisabled = false;
nextDisabled = false;
constructor(
@Inject(DspApiConnectionToken)
private _dspApiConnection: KnoraApiConnection,
private _componentCommsService: ComponentCommunicationEventService,
private _errorHandler: AppErrorHandler,
private _notification: NotificationService,
private _route: ActivatedRoute,
private _router: Router,
private _cd: ChangeDetectorRef,
private _store: Store
) {}
ngOnInit(): void {
this.componentCommsSubscriptions.push(
this._componentCommsService.on(Events.resourceChanged, () => this._doSearch()),
this._componentCommsService.on(Events.resourceDeleted, () => this._doSearch())
);
}
ngOnChanges(): void {
if (this.isCurrentSearch()) {
this.currentSearch = this.search;
this.initSearch();
}
}
isCurrentSearch = (): boolean =>
this.search.query !== this.currentSearch?.query || this.currentSearch.query === undefined;
initSearch(): void {
// reset
this.currentIndex = 0;
this.currentRangeStart = 1;
this.currentRangeEnd = 0;
this.nextDisabled = false;
this.resources = undefined;
this.emitSelectedResources();
this._doSearch();
}
ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
// the child component send the selected resources to the parent of this component directly;
// but when this component is initialized, it should select the first item in the list and
// emit this selected resource to the parent.
emitSelectedResources(res?: FilteredResources) {
if (!res || res.count === 0) {
// no resource is selected: In case of an error or no search results
this.selectedResources.emit({
count: 0,
resListIndex: [],
resInfo: [],
selectionType: 'single',
});
} else if (res.count > 0) {
this.selectedResourceIdx = res.resListIndex;
this.selectedResources.emit(res);
}
}
goToPage(direction: 'previous' | 'next') {
if (direction === 'previous') {
if (this.currentIndex > 0) {
this.nextDisabled = false;
this.currentIndex -= 1;
}
}
if (direction === 'next') {
this.currentIndex += 1;
// if end range for the next page of results is greater than the total number of results
if (this.pageSize * (this.currentIndex + 1) >= this.numberOfAllResults) {
this.nextDisabled = true;
}
}
this._calculateRange(this.currentIndex);
this._doSearch(this.currentIndex);
}
handleBackButtonClicked() {
const projectUuid = this._route.parent.snapshot.paramMap.get('uuid');
if (projectUuid) {
this._router.navigate([RouteConstants.project, projectUuid, RouteConstants.advancedSearch]);
}
}
/**
* given the index number, calculate the range of results shown
* @param index offset of gravsearch query
*/
private _calculateRange(index: number) {
this.currentRangeStart = this.pageSize * index + 1;
if (this.pageSize * (index + 1) > this.numberOfAllResults) {
this.currentRangeEnd = this.numberOfAllResults;
} else {
this.currentRangeEnd = this.pageSize * (index + 1);
}
}
/**
* do the search and send the resources to the child components
* like resource-list, resource-grid or resource-table
* @param index offset of gravsearch query
*/
private _doSearch(index = 0) {
this.loading = true;
this._cd.markForCheck();
if (this.search.mode === 'fulltext') {
// search mode: fulltext
if (index === 0) {
// perform count query
this._dspApiConnection.v2.search
.doFulltextSearchCountQuery(this.search.query, index, this.search.filter)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(
(count: CountQueryResponse) => {
this.numberOfAllResults = count.numberOfResults;
this.currentRangeEnd = this.numberOfAllResults > 25 ? 25 : this.numberOfAllResults;
if (this.numberOfAllResults === 0) {
this.emitSelectedResources();
this.resources = undefined;
this.loading = false;
this._cd.markForCheck();
}
},
(countError: ApiResponseError) => {
if (countError.status === 400) {
this.numberOfAllResults = 0;
}
this.loading = false;
this._cd.markForCheck();
this._errorHandler.showMessage(countError);
}
);
}
// perform full text search
this._dspApiConnection.v2.search
.doFulltextSearch(this.search.query, index, this.search.filter)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(
(response: ReadResourceSequence) => {
// if the response does not contain any resources even though the search count is greater than 0,
// it means that the user does not have the permissions to see anything: emit an empty result
if (response.resources.length === 0) {
this.emitSelectedResources();
}
this.resources = response;
this.loading = false;
this._cd.markForCheck();
},
(error: ApiResponseError) => {
this.loading = false;
this.resources = undefined;
this._errorHandler.showMessage(error);
}
);
} else if (this.search.mode === 'gravsearch') {
this.performGravSearch(index);
}
}
performGravSearch(index: number) {
// emit 'gravSearchExecuted' event to the fulltext-search component in order to clear the input field
this._componentCommsService.emit(new EmitEvent(Events.gravSearchExecuted, true));
if (this.search.query === undefined) {
this._notification.openSnackBar('The gravsearch query is not set correctly');
this.resources = undefined;
this.loading = false;
this._cd.markForCheck();
return;
}
// request the count query if the page index is zero otherwise it is already stored in the numberOfAllResults
const numberOfAllResults$ =
index !== 0
? of(this.numberOfAllResults)
: this._store.select(OntologyClassSelectors.classItems).pipe(
take(1),
map(classItems => {
this.numberOfAllResults = classItems[this.search.classId]
? classItems[this.search.classId].classItemsCount
: 0;
this.currentRangeEnd = this.numberOfAllResults > 25 ? 25 : this.numberOfAllResults;
if (this.numberOfAllResults === 0) {
this._notification.openSnackBar('No resources to display.');
this.emitSelectedResources();
this.resources = undefined;
this.loading = false;
this._cd.markForCheck();
}
return this.numberOfAllResults;
})
);
let gravsearch = this.search.query;
gravsearch = gravsearch.substring(0, gravsearch.search('OFFSET'));
gravsearch = `${gravsearch}OFFSET ${index}`;
const graveSearchQuery$ = this._dspApiConnection.v2.search
.doExtendedSearch(gravsearch)
.pipe(takeUntil(this.ngUnsubscribe));
combineLatest([graveSearchQuery$, numberOfAllResults$]).subscribe(
([response, numberOfAllResults]) => {
response = response as ReadResourceSequence;
// if the response does not contain any resources even the search count is greater than 0,
// it means that the user does not have the permissions to see anything: emit an empty result
if (response.resources.length === 0 && this.numberOfAllResults > 0) {
this._notification.openSnackBar('No permission to display the resources.');
this.emitSelectedResources();
}
this.resources = response;
this.loading = false;
this._cd.markForCheck();
},
(error: ApiResponseError) => {
this.loading = false;
this.resources = undefined;
this._errorHandler.showMessage(error);
}
);
}
}