Skip to content

Commit

Permalink
[DURACOM-208] Restore functionality to open the tree on the given item
Browse files Browse the repository at this point in the history
  • Loading branch information
atarix83 committed Nov 22, 2023
1 parent 1197474 commit 9268ef9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import { hasValue, isEmpty, isNotEmpty, isNotNull } from '../../../../../empty.u
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
import { ConfidenceType } from '../../../../../../core/shared/confidence-type';
import { getFirstSucceededRemoteDataPayload } from '../../../../../../core/shared/operators';
import {
PaginatedList,
buildPaginatedList
} from '../../../../../../core/data/paginated-list.model';
import { buildPaginatedList, PaginatedList } from '../../../../../../core/data/paginated-list.model';
import { VocabularyEntry } from '../../../../../../core/submission/vocabularies/models/vocabulary-entry.model';
import { PageInfo } from '../../../../../../core/shared/page-info.model';
import { DsDynamicVocabularyComponent } from '../dynamic-vocabulary.component';
import { Vocabulary } from '../../../../../../core/submission/vocabularies/models/vocabulary.model';
import { VocabularyEntryDetail } from '../../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
import { VocabularyTreeviewModalComponent } from '../../../../vocabulary-treeview-modal/vocabulary-treeview-modal.component';
import {
VocabularyEntryDetail
} from '../../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
import {
VocabularyTreeviewModalComponent
} from '../../../../vocabulary-treeview-modal/vocabulary-treeview-modal.component';

/**
* Component representing a onebox input field.
Expand Down Expand Up @@ -228,7 +229,7 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
const modalRef: NgbModalRef = this.modalService.open(VocabularyTreeviewModalComponent, { size: 'lg', windowClass: 'treeview' });
modalRef.componentInstance.vocabularyOptions = this.model.vocabularyOptions;
modalRef.componentInstance.preloadLevel = preloadLevel;
modalRef.componentInstance.selectedItems = this.currentValue ? [this.currentValue.value] : [];
modalRef.componentInstance.selectedItems = this.currentValue ? [this.currentValue] : [];
modalRef.result.then((result: VocabularyEntryDetail) => {
if (result) {
this.currentValue = result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { TreeviewFlatNode, TreeviewNode } from './vocabulary-treeview-node.model
import { FormFieldMetadataValueObject } from '../builder/models/form-field-metadata-value.model';
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
import { PageInfo } from '../../../core/shared/page-info.model';
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';
import { AuthTokenInfo } from '../../../core/auth/models/auth-token-info.model';
import { authReducer } from '../../../core/auth/auth.reducer';
import { storeModuleConfig } from '../../../app.reducer';
import { By } from '@angular/platform-browser';
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';

describe('VocabularyTreeviewComponent test suite', () => {

Expand Down Expand Up @@ -144,10 +144,10 @@ describe('VocabularyTreeviewComponent test suite', () => {
currentValue.otherInformation = {
id: 'entryID'
};
comp.selectedItems = [currentValue.value];
comp.selectedItems = [currentValue];
fixture.detectChanges();
expect(comp.dataSource.data).toEqual([]);
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['testValue'], null);
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID');
});

it('should should init component properly with init value as VocabularyEntry', () => {
Expand All @@ -156,10 +156,20 @@ describe('VocabularyTreeviewComponent test suite', () => {
currentValue.otherInformation = {
id: 'entryID'
};
comp.selectedItems = [currentValue.value];
comp.selectedItems = [currentValue];
fixture.detectChanges();
expect(comp.dataSource.data).toEqual([]);
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID');
});

it('should should init component properly with init value as VocabularyEntryDetail', () => {
const currentValue = new VocabularyEntryDetail();
currentValue.value = 'testValue';
currentValue.id = 'entryID';
comp.selectedItems = [currentValue];
fixture.detectChanges();
expect(comp.dataSource.data).toEqual([]);
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['testValue'], null);
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID');
});

it('should call loadMore function', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges

/**
* An event fired when a vocabulary entry is selected.
* Event's payload equals to {@link VocabularyEntryDetail} selected.
* Event's payload equals to {@link VocabularyTreeItemType} selected.
*/
@Output() select: EventEmitter<VocabularyEntryDetail> = new EventEmitter<VocabularyEntryDetail>(null);
@Output() select: EventEmitter<VocabularyTreeItemType> = new EventEmitter<VocabularyTreeItemType>(null);

/**
* An event fired when a vocabulary entry is deselected.
* Event's payload equals to {@link VocabularyEntryDetail} deselected.
* Event's payload equals to {@link VocabularyTreeItemType} deselected.
*/
@Output() deselect: EventEmitter<VocabularyEntryDetail> = new EventEmitter<VocabularyEntryDetail>(null);
@Output() deselect: EventEmitter<VocabularyTreeItemType> = new EventEmitter<VocabularyTreeItemType>(null);

/**
* A boolean representing if user is authenticated
Expand Down Expand Up @@ -151,7 +151,8 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
* @param level The node level information
*/
transformer = (node: TreeviewNode, level: number) => {
const existingNode = this.nodeMap.get(node.item.id);
const entryId = this.getEntryId(node.item);
const existingNode = this.nodeMap.get(entryId);

if (existingNode && existingNode.item.id !== LOAD_MORE && existingNode.item.id !== LOAD_MORE_ROOT) {
return existingNode;
Expand All @@ -168,7 +169,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
node.isInInitValueHierarchy,
node.isSelected
);
this.nodeMap.set(node.item.id, newNode);
this.nodeMap.set(entryId, newNode);

if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded)
|| (newNode.isSearchNode && newNode.childrenLoaded)
Expand Down Expand Up @@ -223,42 +224,43 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges

this.loading = this.vocabularyTreeviewService.isLoading();

this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null);
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
}

/**
* Expand a node whose children are not loaded
* @param item The VocabularyEntryDetail for which to load more nodes
*/
loadMore(item: VocabularyEntryDetail) {
this.vocabularyTreeviewService.loadMore(item, this.selectedItems);
this.vocabularyTreeviewService.loadMore(item, this.getSelectedEntryIds());
}

/**
* Expand the root node whose children are not loaded
* @param node The TreeviewFlatNode for which to load more nodes
*/
loadMoreRoot(node: TreeviewFlatNode) {
this.vocabularyTreeviewService.loadMoreRoot(node, this.selectedItems);
this.vocabularyTreeviewService.loadMoreRoot(node, this.getSelectedEntryIds());
}

/**
* Load children nodes for a node
* @param node The TreeviewFlatNode for which to load children nodes
*/
loadChildren(node: TreeviewFlatNode) {
this.vocabularyTreeviewService.loadMore(node.item, this.selectedItems, true);
this.vocabularyTreeviewService.loadMore(node.item, this.getSelectedEntryIds(), true);
}

/**
* Method called on entry select/deselect
*/
onSelect(item: VocabularyEntryDetail) {
if (!this.selectedItems.includes(item.id)) {
this.selectedItems.push(item.id);
if (!this.getSelectedEntryIds().includes(this.getEntryId(item))) {
this.selectedItems.push(item);
this.select.emit(item);
} else {
this.selectedItems = this.selectedItems.filter((detail: string) => { return detail !== item.id; });
this.selectedItems = this.selectedItems.filter((detail: VocabularyTreeItemType) => this.getEntryId(detail) !== this.getEntryId(item));
this.deselect.emit(item);
}
}
Expand All @@ -272,7 +274,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
this.storedNodeMap = this.nodeMap;
}
this.nodeMap = new Map<string, TreeviewFlatNode>();
this.vocabularyTreeviewService.searchByQuery(this.searchText, this.selectedItems);
this.vocabularyTreeviewService.searchByQuery(this.searchText, this.getSelectedEntryIds());
}
}

Expand All @@ -289,12 +291,8 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
reset() {
this.searchText = '';
for (const item of this.selectedItems) {
this.subs.push(this.vocabularyService.findEntryDetailById(item, this.vocabularyOptions.name, true, true, false).pipe(
getFirstSucceededRemoteDataPayload(),
).subscribe((detail: VocabularyEntryDetail) => {
this.deselect.emit(detail);
}));
this.nodeMap.get(item).isSelected = false;
this.deselect.emit(item);
this.nodeMap.get(this.getEntryId(item)).isSelected = false;
}
this.selectedItems = [];

Expand Down Expand Up @@ -325,14 +323,27 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
}

/**
* Return an id for a given {@link VocabularyEntry}
* Return an id for a given {@link VocabularyTreeItemType}
*/
private getEntryId(entry: VocabularyTreeItemType): string {
return entry?.authority || entry?.otherInformation?.id || (entry as any)?.id || undefined;
}

/**
* Return an ids for all selected entries
*/
private getEntryId(entry: VocabularyEntry): string {
return entry.authority || entry.otherInformation.id || undefined;
private getSelectedEntryIds(): string[] {
return this.selectedItems
.map((entry: VocabularyTreeItemType) => this.getEntryId(entry))
.filter((value) => isNotEmpty(value));
}

ngOnChanges(changes: SimpleChanges): void {
this.reset();
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null);
if (!changes.vocabularyOptions.isFirstChange() && changes.vocabularyOptions.currentValue !== changes.vocabularyOptions.previousValue) {
this.selectedItems = [];
this.searchText = '';
this.vocabularyTreeviewService.cleanTree();
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@ import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
import { map, merge, mergeMap, scan } from 'rxjs/operators';
import findIndex from 'lodash/findIndex';

import {
LOAD_MORE_NODE,
LOAD_MORE_ROOT_NODE,
TreeviewFlatNode,
TreeviewNode
} from './vocabulary-treeview-node.model';
import { LOAD_MORE_NODE, LOAD_MORE_ROOT_NODE, TreeviewFlatNode, TreeviewNode } from './vocabulary-treeview-node.model';
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
import { PageInfo } from '../../../core/shared/page-info.model';
import { isEmpty, isNotEmpty } from '../../empty.util';
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
import {
getFirstSucceededRemoteDataPayload,
getFirstSucceededRemoteListPayload
} from '../../../core/shared/operators';
import { getFirstSucceededRemoteDataPayload, getFirstSucceededRemoteListPayload } from '../../../core/shared/operators';
import { PaginatedList } from '../../../core/data/paginated-list.model';
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';

Expand Down Expand Up @@ -249,7 +241,7 @@ export class VocabularyTreeviewService {
const hasChildren = entry.hasOtherInformation() && (entry.otherInformation as any)!.hasChildren === 'true';
const pageInfo: PageInfo = this.pageInfo;
const isInInitValueHierarchy = this.initValueHierarchy.includes(entryId);
const isSelected: boolean = selectedItems.some(() => selectedItems.includes(entry.id));
const isSelected: boolean = selectedItems.some(() => selectedItems.includes(entryId));
const result = new TreeviewNode(
entry,
hasChildren,
Expand Down

0 comments on commit 9268ef9

Please sign in to comment.