diff --git a/src/app/components/files/files.component.spec.ts b/src/app/components/files/files.component.spec.ts index 2b19d3323..597500b50 100644 --- a/src/app/components/files/files.component.spec.ts +++ b/src/app/components/files/files.component.spec.ts @@ -25,7 +25,7 @@ import { TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing'; import { NO_ERRORS_SCHEMA, SimpleChange, SimpleChanges } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; +import { Router, ActivatedRoute, convertToParamMap } from '@angular/router'; import { NodeFavoriteDirective, DataTableComponent, UploadService, AppConfigModule, DataTableModule, PaginationModule } from '@alfresco/adf-core'; import { DocumentListComponent, DocumentListService, FilterSearch } from '@alfresco/adf-content-services'; import { NodeActionsService } from '../../services/node-actions.service'; @@ -44,6 +44,7 @@ describe('FilesComponent', () => { let uploadService: UploadService; let nodeActionsService: NodeActionsService; let contentApi: ContentApiService; + let route: ActivatedRoute; let router: any = { url: '', navigate: jasmine.createSpy('navigate') @@ -74,7 +75,7 @@ describe('FilesComponent', () => { { provide: ActivatedRoute, useValue: { - snapshot: { data: { preferencePrefix: 'prefix' } }, + snapshot: { data: { preferencePrefix: 'prefix' }, paramMap: convertToParamMap({ folderId: undefined }) }, params: of({ folderId: 'someId' }), queryParamMap: of({}) } @@ -94,6 +95,7 @@ describe('FilesComponent', () => { uploadService = TestBed.inject(UploadService); router = TestBed.inject(Router); + route = TestBed.get(ActivatedRoute); nodeActionsService = TestBed.inject(NodeActionsService); contentApi = TestBed.inject(ContentApiService); spyContent = spyOn(contentApi, 'getNode'); @@ -283,6 +285,7 @@ describe('FilesComponent', () => { }); it('should navigate home if node is root also if it contain a uuid', () => { + spyOn(route.snapshot.paramMap, 'get').and.returnValue('some-node-id'); component.node = { path: { elements: [{ id: 'node-id' }] @@ -294,6 +297,21 @@ describe('FilesComponent', () => { expect(router.navigate).toHaveBeenCalledWith(['personal-files']); }); + + it('should navigate to sub folder from a parent folder', () => { + router.url = '/personal-files/parent-folder-node-id'; + const childFolderNodeId = node.id; + spyOn(route.snapshot.paramMap, 'get').and.returnValue('parent-folder-node-id'); + component.navigate(childFolderNodeId); + expect(router.navigate).toHaveBeenCalledWith(['personal-files', childFolderNodeId]); + }); + + it('should navigate to smart folder content', () => { + router.url = '/libraries/vH1-6-1-1-115wji7092f0-41-MTg%3D-1-115hpo76l3h2e1f'; + spyOn(route.snapshot.paramMap, 'get').and.returnValue('vH1-6-1-1-115wji7092f0-41-MTg=-1-115hpo76l3h2e1f'); + component.navigate(node.id); + expect(router.navigate).toHaveBeenCalledWith(['libraries', node.id]); + }); }); describe('isSiteContainer', () => { diff --git a/src/app/components/files/files.component.ts b/src/app/components/files/files.component.ts index 5ddde5491..d43ca00d8 100644 --- a/src/app/components/files/files.component.ts +++ b/src/app/components/files/files.component.ts @@ -123,29 +123,49 @@ export class FilesComponent extends PageComponent implements OnInit, OnDestroy { } navigate(nodeId: string = null) { - const uuidRegEx = /[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-4[0-9A-Za-z]{3}-[89ABab][0-9A-Za-z]{3}-[0-9A-Za-z]{12}/gi; - let urlToNavigate: string[]; - const urlWithoutParams = this.router.url.split('?')[0]; - - if (urlWithoutParams.match(uuidRegEx)) { - if (nodeId && !this.isRootNode(nodeId)) { - urlToNavigate = urlWithoutParams.replace(uuidRegEx, nodeId).split('/'); - } else { - urlToNavigate = urlWithoutParams.replace(uuidRegEx, '').split('/'); - urlToNavigate.pop(); - } - urlToNavigate.shift(); - } else { - urlToNavigate = urlWithoutParams.split('/'); - if (nodeId && !this.isRootNode(nodeId)) { - urlToNavigate.push(nodeId); - } - urlToNavigate.shift(); - } - + const currentNodeId = this.route.snapshot.paramMap.get('folderId'); + const urlWithoutParams = decodeURIComponent(this.router.url).split('?')[0]; + const urlToNavigate: string[] = this.getUrlToNavigate(urlWithoutParams, currentNodeId, nodeId); this.router.navigate(urlToNavigate); } + private getUrlToNavigate(currentURL: string, currentNodeId: string, nextNodeId: string): string[] { + return currentNodeId ? this.getNextNodeUrlToNavigate(currentURL, currentNodeId, nextNodeId) : this.appendNextNodeIdToUrl(currentURL, nextNodeId); + } + + private getNextNodeUrlToNavigate(currentURL: string, currentNodeId: string, nextNodeId: string): string[] { + const urlToNavigate: string[] = + nextNodeId && !this.isRootNode(nextNodeId) + ? this.replaceCurrentNodeIdWithNextNodeId(currentURL, currentNodeId, nextNodeId) + : this.removeNodeIdFromUrl(currentURL, currentNodeId); + urlToNavigate.shift(); + return urlToNavigate; + } + + private replaceCurrentNodeIdWithNextNodeId(currentURL: string, currentNodeId: string, nextNodeId: string): string[] { + const nextNodeUrlToNavigate = currentURL.split('/'); + const index = nextNodeUrlToNavigate.indexOf(currentNodeId); + if (index > 0) { + nextNodeUrlToNavigate[index] = nextNodeId; + } + return nextNodeUrlToNavigate; + } + + private removeNodeIdFromUrl(currentURL: string, currentNodeId: string): string[] { + const rootUrl: string[] = currentURL.replace(currentNodeId, '').split('/'); + rootUrl.pop(); + return rootUrl; + } + + private appendNextNodeIdToUrl(currentURL: string, nodeId: string): string[] { + const navigateToNodeUrl = currentURL.split('/'); + if (nodeId && !this.isRootNode(nodeId)) { + navigateToNodeUrl.push(nodeId); + } + navigateToNodeUrl.shift(); + return navigateToNodeUrl; + } + onUploadNewVersion(ev: CustomEvent) { this.store.dispatch(new UploadFileVersionAction(ev)); }