11import { ChangeDetectorRef , Component , Inject } from '@angular/core' ;
22import {
33 BrowseByMetadataPageComponent ,
4- browseParamsToOptions , getBrowseSearchOptions
4+ browseParamsToOptions ,
5+ getBrowseSearchOptions
56} from '../browse-by-metadata-page/browse-by-metadata-page.component' ;
67import { combineLatest as observableCombineLatest } from 'rxjs' ;
7- import { RemoteData } from '../../core/data/remote-data' ;
8- import { Item } from '../../core/shared/item.model' ;
98import { hasValue , isNotEmpty } from '../../shared/empty.util' ;
109import { ActivatedRoute , Params , Router } from '@angular/router' ;
1110import { BrowseService } from '../../core/browse/browse.service' ;
@@ -16,7 +15,9 @@ import { map } from 'rxjs/operators';
1615import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model' ;
1716import { SortDirection , SortOptions } from '../../core/cache/models/sort-options.model' ;
1817import { isValidDate } from '../../shared/date.util' ;
19- import { AppConfig , APP_CONFIG } from '../../../config/app-config.interface' ;
18+ import { APP_CONFIG , AppConfig } from '../../../config/app-config.interface' ;
19+ import { RemoteData } from '../../core/data/remote-data' ;
20+ import { Item } from '../../core/shared/item.model' ;
2021
2122@Component ( {
2223 selector : 'ds-browse-by-date-page' ,
@@ -72,38 +73,32 @@ export class BrowseByDatePageComponent extends BrowseByMetadataPageComponent {
7273
7374 /**
7475 * Update the StartsWith options
75- * In this implementation, it creates a list of years starting from now, going all the way back to the earliest
76- * date found on an item within this scope. The further back in time, the bigger the change in years become to avoid
77- * extremely long lists with a one-year difference.
76+ * In this implementation, it creates a list of years starting from the most recent item or the current year, going
77+ * all the way back to the earliest date found on an item within this scope. The further back in time, the bigger
78+ * the change in years become to avoid extremely long lists with a one-year difference.
7879 * To determine the change in years, the config found under GlobalConfig.BrowseBy is used for this.
7980 * @param definition The metadata definition to fetch the first item for
8081 * @param metadataKeys The metadata fields to fetch the earliest date from (expects a date field)
8182 * @param scope The scope under which to fetch the earliest item for
8283 */
8384 updateStartsWithOptions ( definition : string , metadataKeys : string [ ] , scope ?: string ) {
85+ const firstItemRD = this . browseService . getFirstItemFor ( definition , scope , SortDirection . ASC ) ;
86+ const lastItemRD = this . browseService . getFirstItemFor ( definition , scope , SortDirection . DESC ) ;
8487 this . subs . push (
85- this . browseService . getFirstItemFor ( definition , scope ) . subscribe ( ( firstItemRD : RemoteData < Item > ) => {
86- let lowerLimit = this . appConfig . browseBy . defaultLowerLimit ;
87- if ( hasValue ( firstItemRD . payload ) ) {
88- const date = firstItemRD . payload . firstMetadataValue ( metadataKeys ) ;
89- if ( isNotEmpty ( date ) && isValidDate ( date ) ) {
90- const dateObj = new Date ( date ) ;
91- // TODO: it appears that getFullYear (based on local time) is sometimes unreliable. Switching to UTC.
92- lowerLimit = isNaN ( dateObj . getUTCFullYear ( ) ) ? lowerLimit : dateObj . getUTCFullYear ( ) ;
93- }
94- }
88+ observableCombineLatest ( [ firstItemRD , lastItemRD ] ) . subscribe ( ( [ firstItem , lastItem ] ) => {
89+ let lowerLimit = this . getLimit ( firstItem , metadataKeys , this . appConfig . browseBy . defaultLowerLimit ) ;
90+ let upperLimit = this . getLimit ( lastItem , metadataKeys , new Date ( ) . getUTCFullYear ( ) ) ;
9591 const options = [ ] ;
96- const currentYear = new Date ( ) . getUTCFullYear ( ) ;
97- const oneYearBreak = Math . floor ( ( currentYear - this . appConfig . browseBy . oneYearLimit ) / 5 ) * 5 ;
98- const fiveYearBreak = Math . floor ( ( currentYear - this . appConfig . browseBy . fiveYearLimit ) / 10 ) * 10 ;
92+ const oneYearBreak = Math . floor ( ( upperLimit - this . appConfig . browseBy . oneYearLimit ) / 5 ) * 5 ;
93+ const fiveYearBreak = Math . floor ( ( upperLimit - this . appConfig . browseBy . fiveYearLimit ) / 10 ) * 10 ;
9994 if ( lowerLimit <= fiveYearBreak ) {
10095 lowerLimit -= 10 ;
10196 } else if ( lowerLimit <= oneYearBreak ) {
10297 lowerLimit -= 5 ;
10398 } else {
10499 lowerLimit -= 1 ;
105100 }
106- let i = currentYear ;
101+ let i = upperLimit ;
107102 while ( i > lowerLimit ) {
108103 options . push ( i ) ;
109104 if ( i <= fiveYearBreak ) {
@@ -121,4 +116,24 @@ export class BrowseByDatePageComponent extends BrowseByMetadataPageComponent {
121116 } )
122117 ) ;
123118 }
119+
120+ /**
121+ * Returns the year from the item metadata field or the limit.
122+ * @param itemRD the item remote data
123+ * @param metadataKeys The metadata fields to fetch the earliest date from (expects a date field)
124+ * @param limit the limit to use if the year can't be found in metadata
125+ * @private
126+ */
127+ private getLimit ( itemRD : RemoteData < Item > , metadataKeys : string [ ] , limit : number ) : number {
128+ if ( hasValue ( itemRD . payload ) ) {
129+ const date = itemRD . payload . firstMetadataValue ( metadataKeys ) ;
130+ if ( isNotEmpty ( date ) && isValidDate ( date ) ) {
131+ const dateObj = new Date ( date ) ;
132+ // TODO: it appears that getFullYear (based on local time) is sometimes unreliable. Switching to UTC.
133+ return isNaN ( dateObj . getUTCFullYear ( ) ) ? limit : dateObj . getUTCFullYear ( ) ;
134+ } else {
135+ return new Date ( ) . getUTCFullYear ( ) ;
136+ }
137+ }
138+ }
124139}
0 commit comments