/
list-view.component.ts
264 lines (224 loc) · 9.7 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
import { Component, EventEmitter, Inject, Input, OnChanges, OnInit, Output } from '@angular/core';
import { PageEvent } from '@angular/material/paginator';
import { ApiResponseError, CountQueryResponse, IFulltextSearchParams, KnoraApiConnection, ReadResourceSequence } from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { ErrorHandlerService } from 'src/app/main/services/error-handler.service';
import { ComponentCommunicationEventService, EmitEvent, Events } from 'src/app/main/services/component-communication-event.service';
import { NotificationService } from 'src/app/main/services/notification.service';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';
/**
* 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;
}
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({
selector: 'app-list-view',
templateUrl: './list-view.component.html',
styleUrls: ['./list-view.component.scss']
})
export class ListViewComponent implements OnChanges, OnInit {
@Input() search: SearchParams;
@Input() view?: 'list' | 'grid' = 'list'; // todo: will be expanded with 'table' as soon as resource-table component is done
@Input() displayViewSwitch?: boolean = true;
/**
* 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;
// matPaginator Output
pageEvent: PageEvent;
// number of all results
numberOfAllResults: number;
// progress status
loading = true;
// feature toggle for new concept
beta = false;
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _componentCommsService: ComponentCommunicationEventService,
private _errorHandler: ErrorHandlerService,
private _notification: NotificationService,
private _route: ActivatedRoute
) {
// get feature toggle information if url contains beta
this.beta = (this._route.parent.snapshot.url[0].path === 'beta');
if (this.beta) {
console.warn('This is a pre-released (beta) search results view');
}
}
ngOnInit(): void {
this.componentCommsSubscriptions.push(this._componentCommsService.on(
Events.resourceDeleted, () => {
this._doSearch();
}
));
}
ngOnChanges(): void {
// reset
this.pageEvent = new PageEvent();
this.pageEvent.pageIndex = 0;
this.resources = undefined;
this.emitSelectedResources();
this._doSearch();
}
/**
*
* @param view 'list' | ' grid'; TODO: will be expanded with 'table' as soon as resource-table component is done
*/
toggleView(view: 'list' | 'grid') {
this.view = view;
}
// the child component send the selected resources to the parent of this component directly;
// but when this component is intialized, 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(page: PageEvent) {
this.pageEvent = page;
this._doSearch();
}
/**
* do the search and send the resources to the child components
* like resource-list, resource-grid or resource-table
*/
private _doSearch() {
this.loading = true;
if (this.search.mode === 'fulltext') {
// search mode: fulltext
if (this.pageEvent.pageIndex === 0) {
// perform count query
this._dspApiConnection.v2.search.doFulltextSearchCountQuery(this.search.query, this.pageEvent.pageIndex, this.search.filter).subscribe(
(count: CountQueryResponse) => {
this.numberOfAllResults = count.numberOfResults;
if (this.numberOfAllResults === 0) {
this.emitSelectedResources();
this.resources = undefined;
this.loading = false;
}
},
(countError: ApiResponseError) => {
this.loading = countError.status !== 504;
this._errorHandler.showMessage(countError);
}
);
}
// perform full text search
this._dspApiConnection.v2.search.doFulltextSearch(this.search.query, this.pageEvent.pageIndex, this.search.filter).subscribe(
(response: 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.emitSelectedResources();
}
this.resources = response;
this.loading = false;
},
(error: ApiResponseError) => {
this.loading = false;
this.resources = undefined;
this._errorHandler.showMessage(error);
}
);
} else if (this.search.mode === 'gravsearch') {
// emit 'gravSearchExecuted' event to the fulltext-search component in order to clear the input field
this._componentCommsService.emit(new EmitEvent(Events.gravSearchExecuted, true));
// search mode: gravsearch
if (this.pageEvent.pageIndex === 0) {
// perform count query
this._dspApiConnection.v2.search.doExtendedSearchCountQuery(this.search.query).subscribe(
(count: CountQueryResponse) => {
this.numberOfAllResults = count.numberOfResults;
if (this.numberOfAllResults === 0) {
this.emitSelectedResources();
this.resources = undefined;
this.loading = false;
}
},
(countError: ApiResponseError) => {
this.loading = countError.status !== 504;
this._errorHandler.showMessage(countError);
}
);
}
let gravsearch: string;
if (this.search.query !== undefined) {
gravsearch = this.search.query;
gravsearch = gravsearch.substring(0, gravsearch.search('OFFSET'));
gravsearch = gravsearch + 'OFFSET ' + this.pageEvent.pageIndex;
this._dspApiConnection.v2.search.doExtendedSearch(gravsearch).subscribe(
(response: 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.emitSelectedResources();
}
this.resources = response;
this.loading = false;
},
(error: ApiResponseError) => {
this.loading = false;
this.resources = undefined;
this._errorHandler.showMessage(error);
}
);
} else {
this._notification.openSnackBar('The gravsearch query is not set correctly');
this.resources = undefined;
this.loading = false;
}
}
}
}