forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathentity-dropdown.component.ts
More file actions
248 lines (226 loc) · 7.33 KB
/
entity-dropdown.component.ts
File metadata and controls
248 lines (226 loc) · 7.33 KB
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
import {
ChangeDetectorRef,
Component,
EventEmitter,
HostListener,
Input,
OnDestroy,
OnInit,
Output
} from '@angular/core';
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
import { hasValue, isNotNull } from '../empty.util';
import { map, reduce, startWith, switchMap, take, tap } from 'rxjs/operators';
import { RemoteData } from '../../core/data/remote-data';
import { buildPaginatedList, PaginatedList } from '../../core/data/paginated-list.model';
import { EntityTypeDataService } from '../../core/data/entity-type-data.service';
import { ItemType } from '../../core/shared/item-relationships/item-type.model';
import { getFirstSucceededRemoteWithNotEmptyData } from '../../core/shared/operators';
import {
ItemExportFormatMolteplicity,
ItemExportFormatService
} from '../../core/itemexportformat/item-export-format.service';
import { createSuccessfulRemoteDataObject } from '../remote-data.utils';
import { FindListOptions } from '../../core/data/find-list-options.model';
@Component({
selector: 'ds-entity-dropdown',
templateUrl: './entity-dropdown.component.html',
styleUrls: ['./entity-dropdown.component.scss']
})
export class EntityDropdownComponent implements OnInit, OnDestroy {
/**
* The entity list obtained from a search
* @type {Observable<ItemType[]>}
*/
public searchListEntity$: Observable<ItemType[]>;
/**
* A boolean representing if dropdown list is scrollable to the bottom
* @type {boolean}
*/
private scrollableBottom = false;
/**
* A boolean representing if dropdown list is scrollable to the top
* @type {boolean}
*/
private scrollableTop = false;
/**
* The list of entity to render
*/
public searchListEntity: ItemType[] = [];
/**
* TRUE if the parent operation is a 'new submission' operation, FALSE otherwise (eg.: is an 'Import metadata from an external source' operation).
*/
@Input() isSubmission: boolean;
/**
* TRUE if the parent operation is a 'Import metadata from an external source' operation, FALSE otherwise (eg.: is an 'Export Item' operation).
*/
@Input() isExternalImport = false;
/**
* The entity to output to the parent component
*/
@Output() selectionChange = new EventEmitter<ItemType>();
/**
* A boolean representing if the loader is visible or not
*/
public isLoadingList: BehaviorSubject<boolean> = new BehaviorSubject(false);
/**
* A numeric representig current page
*/
public currentPage: number;
/**
* A boolean representing if exist another page to render
*/
public hasNextPage: boolean;
/**
* Array to track all subscriptions and unsubscribe them onDestroy
* @type {Array}
*/
public subs: Subscription[] = [];
/**
* Initialize instance variables
*
* @param {ChangeDetectorRef} changeDetectorRef
* @param {EntityTypeDataService} entityTypeService
* @param {ItemExportFormatService} itemExportFormatService
*/
constructor(
private changeDetectorRef: ChangeDetectorRef,
private entityTypeService: EntityTypeDataService,
private itemExportFormatService: ItemExportFormatService,
) { }
/**
* Method called on mousewheel event, it prevent the page scroll
* when arriving at the top/bottom of dropdown menu
*
* @param event
* mousewheel event
*/
@HostListener('mousewheel', ['$event']) onMousewheel(event) {
if (event.wheelDelta > 0 && this.scrollableTop) {
event.preventDefault();
}
if (event.wheelDelta < 0 && this.scrollableBottom) {
event.preventDefault();
}
}
/**
* Initialize entity list
*/
ngOnInit() {
this.resetPagination();
this.populateEntityList(this.currentPage);
}
/**
* Check if dropdown scrollbar is at the top or bottom of the dropdown list
*
* @param event
*/
public onScroll(event) {
this.scrollableBottom = ((event.target.scrollTop + event.target.clientHeight) === event.target.scrollHeight);
this.scrollableTop = (event.target.scrollTop === 0);
}
/**
* Method used from infitity scroll for retrive more data on scroll down
*/
public onScrollDown() {
if ( this.hasNextPage ) {
this.populateEntityList(++this.currentPage);
}
}
/**
* Emit a [selectionChange] event when a new entity is selected from list
*
* @param event
* the selected [ItemType]
*/
public onSelect(event: ItemType) {
this.selectionChange.emit(event);
}
/**
* Method called for populate the entity list
* @param page page number
*/
public populateEntityList(page: number) {
this.isLoadingList.next(true);
let searchListEntity$;
if (this.isSubmission) {
// Set the pagination info
const findOptions: FindListOptions = {
elementsPerPage: 10,
currentPage: page
};
searchListEntity$ =
this.entityTypeService.getAllAuthorizedRelationshipType(findOptions)
.pipe(
getFirstSucceededRemoteWithNotEmptyData(),
tap(entityType => {
if ((this.searchListEntity.length + findOptions.elementsPerPage) >= entityType.payload.totalElements) {
this.hasNextPage = false;
}
})
);
} else if (this.isExternalImport) {
const findOptions: FindListOptions = {
elementsPerPage: 10,
currentPage: page
};
searchListEntity$ = this.entityTypeService.getAllAuthorizedRelationshipTypeImport(findOptions)
.pipe(
getFirstSucceededRemoteWithNotEmptyData(),
tap(entityType => {
if ((this.searchListEntity.length + findOptions.elementsPerPage) >= entityType.payload.totalElements) {
this.hasNextPage = false;
}
})
);
} else {
searchListEntity$ =
this.itemExportFormatService.byEntityTypeAndMolteplicity(null, ItemExportFormatMolteplicity.MULTIPLE)
.pipe(
take(1),
map((formatTypes: any) => {
const entityList: ItemType[] = Object.keys(formatTypes)
.filter((entityType: string) => isNotNull(entityType) && entityType !== 'null')
.map((entityType: string) => ({
id: entityType,
label: entityType
} as any));
return createSuccessfulRemoteDataObject(buildPaginatedList(null, entityList));
}),
tap(() => this.hasNextPage = false)
);
}
this.searchListEntity$ = searchListEntity$.pipe(
switchMap((entityType: RemoteData<PaginatedList<ItemType>>) => entityType.payload.page),
reduce((acc: any, value: any) => [...acc, value], []),
startWith([])
);
this.subs.push(
this.searchListEntity$.subscribe({
next: (result: ItemType[]) => { this.searchListEntity.push(...result); },
complete: () => { this.hideShowLoader(false); this.changeDetectorRef.detectChanges(); }
})
);
}
/**
* Reset pagination values
*/
public resetPagination() {
this.currentPage = 1;
this.hasNextPage = true;
this.searchListEntity = [];
}
/**
* Hide/Show the entity list loader
* @param hideShow true for show, false otherwise
*/
public hideShowLoader(hideShow: boolean) {
this.isLoadingList.next(hideShow);
}
/**
* Unsubscribe from all subscriptions
*/
ngOnDestroy(): void {
this.subs.filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe());
}
}