|
| 1 | +import { Component, Input, OnDestroy, OnInit } from '@angular/core'; |
| 2 | +import { Item } from '../../../../core/shared/item.model'; |
| 3 | +import { Version } from '../../../../core/shared/version.model'; |
| 4 | +import { RemoteData } from '../../../../core/data/remote-data'; |
| 5 | +import { combineLatest, Observable, Subscription } from 'rxjs'; |
| 6 | +import { VersionHistory } from '../../../../core/shared/version-history.model'; |
| 7 | +import { |
| 8 | + getAllSucceededRemoteData, |
| 9 | + getFirstCompletedRemoteData, |
| 10 | + getFirstSucceededRemoteDataPayload, |
| 11 | + getRemoteDataPayload |
| 12 | +} from '../../../../core/shared/operators'; |
| 13 | +import { map, switchMap } from 'rxjs/operators'; |
| 14 | +import { PaginatedList } from '../../../../core/data/paginated-list.model'; |
| 15 | +import { PaginationComponentOptions } from '../../../../shared/pagination/pagination-component-options.model'; |
| 16 | +import { VersionHistoryDataService } from '../../../../core/data/version-history-data.service'; |
| 17 | +import { PaginatedSearchOptions } from '../../../../shared/search/models/paginated-search-options.model'; |
| 18 | +import { followLink } from '../../../../shared/utils/follow-link-config.model'; |
| 19 | +import { hasValue, hasValueOperator } from '../../../../shared/empty.util'; |
| 20 | +import { PaginationService } from '../../../../core/pagination/pagination.service'; |
| 21 | +import { getItemVersionRoute } from '../../../item-page-routing-paths'; |
| 22 | +import { WorkspaceItem } from '../../../../core/submission/models/workspaceitem.model'; |
| 23 | +import { WorkspaceitemDataService } from '../../../../core/submission/workspaceitem-data.service'; |
| 24 | +import { WorkflowItemDataService } from '../../../../core/submission/workflowitem-data.service'; |
| 25 | + |
| 26 | +interface VersionsDTO { |
| 27 | + totalElements: number; |
| 28 | + versionDTOs: VersionDTO[]; |
| 29 | +} |
| 30 | + |
| 31 | +interface VersionDTO { |
| 32 | + version: Version; |
| 33 | +} |
| 34 | + |
| 35 | +@Component({ |
| 36 | + selector: 'ds-clarin-item-versions-field', |
| 37 | + templateUrl: './clarin-item-versions-field.component.html', |
| 38 | + styleUrls: ['./clarin-item-versions-field.component.scss'] |
| 39 | +}) |
| 40 | + |
| 41 | +/** |
| 42 | + * Component for displaying item versions as a field in the clarin item page |
| 43 | + */ |
| 44 | +export class ClarinItemVersionsFieldComponent implements OnDestroy, OnInit { |
| 45 | + |
| 46 | + /** |
| 47 | + * The item to display version history for |
| 48 | + */ |
| 49 | + @Input() item: Item; |
| 50 | + |
| 51 | + /** |
| 52 | + * Fontawesome v5. icon name with default settings. |
| 53 | + */ |
| 54 | + @Input() iconName: string; |
| 55 | + |
| 56 | + /** |
| 57 | + * Array of active subscriptions |
| 58 | + */ |
| 59 | + subs: Subscription[] = []; |
| 60 | + |
| 61 | + /** |
| 62 | + * The item's version |
| 63 | + */ |
| 64 | + versionRD$: Observable<RemoteData<Version>>; |
| 65 | + |
| 66 | + /** |
| 67 | + * The item's full version history (remote data) |
| 68 | + */ |
| 69 | + versionHistoryRD$: Observable<RemoteData<VersionHistory>>; |
| 70 | + |
| 71 | + /** |
| 72 | + * The item's full version history |
| 73 | + */ |
| 74 | + versionHistory$: Observable<VersionHistory>; |
| 75 | + |
| 76 | + /** |
| 77 | + * The version history information that is used to render the HTML |
| 78 | + */ |
| 79 | + versionsDTO$: Observable<VersionsDTO>; |
| 80 | + |
| 81 | + /** |
| 82 | + * Verify if there is an inprogress submission in the version history |
| 83 | + */ |
| 84 | + hasDraftVersion$: Observable<boolean>; |
| 85 | + |
| 86 | + /** |
| 87 | + * The amount of versions to display per page |
| 88 | + */ |
| 89 | + pageSize = 10; |
| 90 | + |
| 91 | + /** |
| 92 | + * The page options to use for fetching the versions |
| 93 | + * Start at page 1 and always use the set page size |
| 94 | + */ |
| 95 | + options = Object.assign(new PaginationComponentOptions(), { |
| 96 | + id: 'clarin-ivo', |
| 97 | + currentPage: 1, |
| 98 | + pageSize: this.pageSize |
| 99 | + }); |
| 100 | + |
| 101 | + /** |
| 102 | + * Toggle state for version history |
| 103 | + */ |
| 104 | + showVersionHistory = false; |
| 105 | + |
| 106 | + /** |
| 107 | + * Observable to check if component should be displayed |
| 108 | + */ |
| 109 | + showMetadataValue: Observable<boolean>; |
| 110 | + |
| 111 | + constructor(private versionHistoryService: VersionHistoryDataService, |
| 112 | + private paginationService: PaginationService, |
| 113 | + private workspaceItemDataService: WorkspaceitemDataService, |
| 114 | + private workflowItemDataService: WorkflowItemDataService |
| 115 | + ) { |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Toggle the visibility of version history |
| 120 | + */ |
| 121 | + toggleVersionHistory(): void { |
| 122 | + this.showVersionHistory = !this.showVersionHistory; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get the route to the specified version |
| 127 | + * @param versionId the ID of the version for which the route will be retrieved |
| 128 | + */ |
| 129 | + getVersionRoute(versionId: string) { |
| 130 | + return getItemVersionRoute(versionId); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Get all versions for the given version history and store them in versionsDTO$ |
| 135 | + * @param versionHistory$ |
| 136 | + */ |
| 137 | + getAllVersions(versionHistory$: Observable<VersionHistory>): void { |
| 138 | + const currentPagination = this.paginationService.getCurrentPagination(this.options.id, this.options); |
| 139 | + this.versionsDTO$ = combineLatest([versionHistory$, currentPagination]).pipe( |
| 140 | + switchMap(([versionHistory, options]: [VersionHistory, PaginationComponentOptions]) => { |
| 141 | + return this.versionHistoryService.getVersions(versionHistory.id, |
| 142 | + new PaginatedSearchOptions({pagination: Object.assign({}, options, {currentPage: options.currentPage})}), |
| 143 | + false, true, followLink('item')); |
| 144 | + }), |
| 145 | + getFirstCompletedRemoteData(), |
| 146 | + getRemoteDataPayload(), |
| 147 | + map((versions: PaginatedList<Version>) => ({ |
| 148 | + totalElements: versions.totalElements, |
| 149 | + versionDTOs: (versions?.page ?? []).reverse().map((version: Version) => ({ |
| 150 | + version: version, |
| 151 | + })), |
| 152 | + })), |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Get the ID of the workspace item, if present, otherwise return undefined |
| 158 | + * @param versionItem the item for which retrieve the workspace item id |
| 159 | + */ |
| 160 | + getWorkspaceId(versionItem): Observable<string> { |
| 161 | + return versionItem.pipe( |
| 162 | + getFirstSucceededRemoteDataPayload(), |
| 163 | + map((item: Item) => item.uuid), |
| 164 | + switchMap((itemUuid: string) => this.workspaceItemDataService.findByItem(itemUuid, true)), |
| 165 | + getFirstCompletedRemoteData<WorkspaceItem>(), |
| 166 | + map((res: RemoteData<WorkspaceItem>) => res?.payload?.id ), |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Get the ID of the workflow item, if present, otherwise return undefined |
| 172 | + * @param versionItem the item for which retrieve the workspace item id |
| 173 | + */ |
| 174 | + getWorkflowId(versionItem): Observable<string> { |
| 175 | + return versionItem.pipe( |
| 176 | + getFirstSucceededRemoteDataPayload(), |
| 177 | + map((item: Item) => item.uuid), |
| 178 | + switchMap((itemUuid: string) => this.workflowItemDataService.findByItem(itemUuid, true)), |
| 179 | + getFirstCompletedRemoteData<WorkspaceItem>(), |
| 180 | + map((res: RemoteData<WorkspaceItem>) => res?.payload?.id ), |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Get the display name for a version item |
| 186 | + * @param versionItem the item to get the name for |
| 187 | + */ |
| 188 | + getVersionItemDisplayName(versionItem: Item): string { |
| 189 | + return versionItem.firstMetadataValue('dc.title') || versionItem.name || 'Untitled'; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Initialize all observables |
| 194 | + */ |
| 195 | + ngOnInit(): void { |
| 196 | + if (hasValue(this.item?.version)) { |
| 197 | + this.versionRD$ = this.item.version; |
| 198 | + this.versionHistoryRD$ = this.versionRD$.pipe( |
| 199 | + getAllSucceededRemoteData(), |
| 200 | + getRemoteDataPayload(), |
| 201 | + hasValueOperator(), |
| 202 | + switchMap((version: Version) => version.versionhistory), |
| 203 | + ); |
| 204 | + this.versionHistory$ = this.versionHistoryRD$.pipe( |
| 205 | + getFirstSucceededRemoteDataPayload(), |
| 206 | + hasValueOperator(), |
| 207 | + ); |
| 208 | + |
| 209 | + // If there is a draft item in the version history |
| 210 | + this.hasDraftVersion$ = this.versionHistoryRD$.pipe( |
| 211 | + getFirstSucceededRemoteDataPayload(), |
| 212 | + map((res) => Boolean(res?.draftVersion)), |
| 213 | + ); |
| 214 | + |
| 215 | + this.getAllVersions(this.versionHistory$); |
| 216 | + |
| 217 | + // Determine if component should be displayed (when there are multiple versions) |
| 218 | + this.showMetadataValue = this.versionsDTO$.pipe( |
| 219 | + map((versionsDTO: VersionsDTO) => versionsDTO && versionsDTO.totalElements > 1) |
| 220 | + ); |
| 221 | + } else { |
| 222 | + // No version history available |
| 223 | + this.showMetadataValue = new Observable(observer => observer.next(false)); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Unsub all subscriptions |
| 229 | + */ |
| 230 | + cleanupSubscribes() { |
| 231 | + this.subs.filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe()); |
| 232 | + } |
| 233 | + |
| 234 | + ngOnDestroy(): void { |
| 235 | + this.cleanupSubscribes(); |
| 236 | + this.paginationService.clearPagination(this.options.id); |
| 237 | + } |
| 238 | +} |
0 commit comments