Merge branch 'w2p-122839_vocabulary-preloadlevel-fix' into vocabulary-preloadlevel-fix-7_x

# Conflicts:
#	src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts
This commit is contained in:
Andreas Awouters
2025-01-02 15:12:50 +01:00
2 changed files with 35 additions and 7 deletions

View File

@@ -22,6 +22,7 @@ import { authReducer } from '../../../core/auth/auth.reducer';
import { storeModuleConfig } from '../../../app.reducer'; import { storeModuleConfig } from '../../../app.reducer';
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service'; import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils';
describe('VocabularyTreeviewComponent test suite', () => { describe('VocabularyTreeviewComponent test suite', () => {
@@ -56,7 +57,8 @@ describe('VocabularyTreeviewComponent test suite', () => {
findEntryDetailById: jasmine.createSpy('findEntryDetailById'), findEntryDetailById: jasmine.createSpy('findEntryDetailById'),
searchTopEntries: jasmine.createSpy('searchTopEntries'), searchTopEntries: jasmine.createSpy('searchTopEntries'),
getEntryDetailChildren: jasmine.createSpy('getEntryDetailChildren'), getEntryDetailChildren: jasmine.createSpy('getEntryDetailChildren'),
clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests') clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests'),
findVocabularyById: createSuccessfulRemoteDataObject$({ preloadLevel: 2 }),
}); });
initialState = { initialState = {

View File

@@ -1,6 +1,7 @@
import { FlatTreeControl } from '@angular/cdk/tree'; import { FlatTreeControl } from '@angular/cdk/tree';
import { Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, OnChanges, SimpleChanges, ViewChild } from '@angular/core'; import { Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, OnChanges, SimpleChanges, ViewChild } from '@angular/core';
import { map, tap, switchMap } from 'rxjs/operators';
import { Observable, Subscription } from 'rxjs'; import { Observable, Subscription } from 'rxjs';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
@@ -16,8 +17,10 @@ import { VocabularyTreeFlattener } from './vocabulary-tree-flattener';
import { VocabularyTreeFlatDataSource } from './vocabulary-tree-flat-data-source'; import { VocabularyTreeFlatDataSource } from './vocabulary-tree-flat-data-source';
import { CoreState } from '../../../core/core-state.model'; import { CoreState } from '../../../core/core-state.model';
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service'; import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators'; import { getFirstSucceededRemoteDataPayload, getFirstCompletedRemoteData } from '../../../core/shared/operators';
import { AlertType } from '../../alert/alert-type'; import { AlertType } from '../../alert/alert-type';
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
import { RemoteData } from '../../../core/data/remote-data';
/** /**
* Component that shows a hierarchical vocabulary in a tree view * Component that shows a hierarchical vocabulary in a tree view
@@ -166,12 +169,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
); );
this.nodeMap.set(node.item.id, newNode); this.nodeMap.set(node.item.id, newNode);
if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded) if ((((level + 1) < this.preloadLevel))
|| (newNode.isSearchNode && newNode.childrenLoaded) || (newNode.isSearchNode && newNode.childrenLoaded)
|| newNode.isInInitValueHierarchy) { || newNode.isInInitValueHierarchy) {
if (!newNode.isSearchNode) {
if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) {
// When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
// so this is a good point to stop expanding.
return newNode;
}
if (!newNode.childrenLoaded) {
this.loadChildren(newNode); this.loadChildren(newNode);
} }
this.treeControl.expand(newNode); this.treeControl.expand(newNode);
} }
return newNode; return newNode;
@@ -212,14 +223,29 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
*/ */
ngOnInit(): void { ngOnInit(): void {
this.subs.push( this.subs.push(
this.vocabularyTreeviewService.getData().subscribe((data) => { this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
// Retrieve the configured preloadLevel from REST
getFirstCompletedRemoteData(),
map((vocabularyRD: RemoteData<Vocabulary>) => {
if (vocabularyRD.hasSucceeded &&
hasValue(vocabularyRD.payload.preloadLevel) &&
vocabularyRD.payload.preloadLevel > 1) {
return vocabularyRD.payload.preloadLevel;
} else {
// Set preload level to 1 in case request fails
return 1;
}
}),
tap(preloadLevel => this.preloadLevel = preloadLevel),
tap(() => this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null)),
switchMap(() => this.vocabularyTreeviewService.getData()),
).subscribe((data) => {
this.dataSource.data = data; this.dataSource.data = data;
}) })
); );
this.loading = this.vocabularyTreeviewService.isLoading();
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null); this.loading = this.vocabularyTreeviewService.isLoading();
} }
/** /**