[ACA-1473] fixes for navigation to searched folder (#429)

* search effects

* "view folder" action

* folder navigation actions and effects

* navigate to folder on double-click (search results)

* update tests

* fix tests
This commit is contained in:
Denys Vuika 2018-06-17 19:12:08 +01:00 committed by GitHub
parent e2eed9b71c
commit 1c4f658017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 196 additions and 57 deletions

View File

@ -25,12 +25,12 @@
import { Component, Input, ChangeDetectionStrategy, OnInit, ViewEncapsulation } from '@angular/core'; import { Component, Input, ChangeDetectionStrategy, OnInit, ViewEncapsulation } from '@angular/core';
import { AlfrescoApiService, DataColumn, DataRow, DataTableAdapter } from '@alfresco/adf-core'; import { AlfrescoApiService, DataColumn, DataRow, DataTableAdapter } from '@alfresco/adf-core';
import { PathInfoEntity } from 'alfresco-js-api'; import { PathInfoEntity, MinimalNodeEntity } from 'alfresco-js-api';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppStore } from '../../store/states/app.state'; import { AppStore } from '../../store/states/app.state';
import { NavigateToLocationAction } from '../../store/actions'; import { NavigateToParentFolder } from '../../store/actions';
@Component({ @Component({
selector: 'app-location-link', selector: 'app-location-link',
@ -64,9 +64,8 @@ export class LocationLinkComponent implements OnInit {
goToLocation() { goToLocation() {
if (this.context) { if (this.context) {
const { node } = this.context.row; const node: MinimalNodeEntity = this.context.row.node;
this.store.dispatch(new NavigateToParentFolder(node));
this.store.dispatch(new NavigateToLocationAction(node.entry));
} }
} }

View File

@ -24,23 +24,23 @@
*/ */
import { NO_ERRORS_SCHEMA } from '@angular/core'; import { NO_ERRORS_SCHEMA } from '@angular/core';
import { TestBed, async } from '@angular/core/testing'; import { TestBed, async, ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { SearchInputComponent } from './search-input.component'; import { SearchInputComponent } from './search-input.component';
import { TranslateModule } from '@ngx-translate/core'; import { AppTestingModule } from '../../testing/app-testing.module';
import { Actions, ofType } from '@ngrx/effects';
import { ViewNodeAction, VIEW_NODE, NAVIGATE_FOLDER, NavigateToFolder } from '../../store/actions';
import { map } from 'rxjs/operators';
describe('SearchInputComponent', () => { describe('SearchInputComponent', () => {
let fixture; let fixture: ComponentFixture<SearchInputComponent>;
let component; let component: SearchInputComponent;
let router: Router; let actions$: Actions;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
RouterTestingModule, AppTestingModule
TranslateModule.forRoot()
], ],
declarations: [ declarations: [
SearchInputComponent SearchInputComponent
@ -49,32 +49,40 @@ describe('SearchInputComponent', () => {
}) })
.compileComponents() .compileComponents()
.then(() => { .then(() => {
actions$ = TestBed.get(Actions);
fixture = TestBed.createComponent(SearchInputComponent); fixture = TestBed.createComponent(SearchInputComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
router = TestBed.get(Router);
fixture.detectChanges(); fixture.detectChanges();
}); });
})); }));
describe('onItemClicked()', () => { describe('onItemClicked()', () => {
it('opens preview if node is file', () => { it('opens preview if node is file', fakeAsync(done => {
spyOn(router, 'navigate').and.stub(); actions$.pipe(
ofType<ViewNodeAction>(VIEW_NODE),
map(action => {
expect(action.payload.id).toBe('node-id');
done();
})
);
const node = { entry: { isFile: true, id: 'node-id', parentId: 'parent-id' } }; const node = { entry: { isFile: true, id: 'node-id', parentId: 'parent-id' } };
component.onItemClicked(node); component.onItemClicked(node);
tick();
}));
expect(router.navigate['calls'].argsFor(0)[0]) it('navigates if node is folder', fakeAsync(done => {
.toEqual([`/personal-files/${node.entry.parentId}/preview/`, node.entry.id]); actions$.pipe(
}); ofType<NavigateToFolder>(NAVIGATE_FOLDER),
map(action => {
it('navigates if node is folder', () => { expect(action.payload.entry.id).toBe('folder-id');
const node = { entry: { isFolder: true } }; done();
spyOn(router, 'navigate'); })
);
const node = { entry: { id: 'folder-id', isFolder: true } };
component.onItemClicked(node); component.onItemClicked(node);
tick();
expect(router.navigate).toHaveBeenCalled(); }));
});
}); });
}); });

View File

@ -30,6 +30,9 @@ import {
} from '@angular/router'; } from '@angular/router';
import { MinimalNodeEntity } from 'alfresco-js-api'; import { MinimalNodeEntity } from 'alfresco-js-api';
import { SearchControlComponent } from '@alfresco/adf-content-services'; import { SearchControlComponent } from '@alfresco/adf-content-services';
import { Store } from '@ngrx/store';
import { AppStore } from '../../store/states/app.state';
import { SearchByTermAction, ViewNodeAction, NavigateToFolder } from '../../store/actions';
@Component({ @Component({
selector: 'aca-search-input', selector: 'aca-search-input',
@ -46,7 +49,7 @@ export class SearchInputComponent implements OnInit {
@ViewChild('searchControl') @ViewChild('searchControl')
searchControl: SearchControlComponent; searchControl: SearchControlComponent;
constructor(private router: Router) { constructor(private router: Router, private store: Store<AppStore>) {
this.router.events.filter(e => e instanceof RouterEvent).subscribe(event => { this.router.events.filter(e => e instanceof RouterEvent).subscribe(event => {
if (event instanceof NavigationEnd) { if (event instanceof NavigationEnd) {
this.showInputValue(); this.showInputValue();
@ -84,10 +87,17 @@ export class SearchInputComponent implements OnInit {
onItemClicked(node: MinimalNodeEntity) { onItemClicked(node: MinimalNodeEntity) {
if (node && node.entry) { if (node && node.entry) {
if (node.entry.isFile) { const { id, nodeId, name, isFile, isFolder, parentId } = node.entry;
this.router.navigate([`/personal-files/${node.entry.parentId}/preview/`, node.entry.id]); if (isFile) {
} else if (node.entry.isFolder) { this.store.dispatch(new ViewNodeAction({
this.router.navigate([ '/personal-files', node.entry.id ]); parentId,
id: nodeId || id,
name,
isFile,
isFolder
}));
} else if (isFolder) {
this.store.dispatch(new NavigateToFolder(node));
} }
} }
} }
@ -98,13 +108,11 @@ export class SearchInputComponent implements OnInit {
* @param event Parameters relating to the search * @param event Parameters relating to the search
*/ */
onSearchSubmit(event: KeyboardEvent) { onSearchSubmit(event: KeyboardEvent) {
const value = (event.target as HTMLInputElement).value; const searchTerm = (event.target as HTMLInputElement).value;
this.router.navigate(['/search', { this.store.dispatch(new SearchByTermAction(searchTerm));
q: value
}]);
} }
onSearchChange(event: string) { onSearchChange(searchTerm: string) {
if (this.onSearchResults) { if (this.onSearchResults) {
if (this.hasOneChange) { if (this.hasOneChange) {
@ -119,8 +127,8 @@ export class SearchInputComponent implements OnInit {
} }
this.navigationTimer = setTimeout(() => { this.navigationTimer = setTimeout(() => {
if (event) { if (searchTerm) {
this.router.navigate(['/search', {q: event}]); this.store.dispatch(new SearchByTermAction(searchTerm));
} }
this.hasOneChange = false; this.hasOneChange = false;
}, 1000); }, 1000);

View File

@ -31,7 +31,7 @@ import { UserPreferencesService } from '@alfresco/adf-core';
import { PageComponent } from '../page.component'; import { PageComponent } from '../page.component';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppStore } from '../../store/states/app.state'; import { AppStore } from '../../store/states/app.state';
import { NavigateToLocationAction } from '../../store/actions'; import { NavigateToFolder } from '../../store/actions';
@Component({ @Component({
selector: 'app-search', selector: 'app-search',
@ -136,7 +136,7 @@ export class SearchComponent extends PageComponent implements OnInit {
onNodeDoubleClick(node: MinimalNodeEntity) { onNodeDoubleClick(node: MinimalNodeEntity) {
if (node && node.entry) { if (node && node.entry) {
if (node.entry.isFolder) { if (node.entry.isFolder) {
this.store.dispatch(new NavigateToLocationAction(node.entry)); this.store.dispatch(new NavigateToFolder(node));
return; return;
} }

View File

@ -28,3 +28,4 @@ export * from './actions/node.actions';
export * from './actions/snackbar.actions'; export * from './actions/snackbar.actions';
export * from './actions/router.actions'; export * from './actions/router.actions';
export * from './actions/viewer.actions'; export * from './actions/viewer.actions';
export * from './actions/search.actions';

View File

@ -24,16 +24,24 @@
*/ */
import { Action } from '@ngrx/store'; import { Action } from '@ngrx/store';
import { MinimalNodeEntity } from 'alfresco-js-api';
export const NAVIGATE_ROUTE = 'NAVIGATE_ROUTE'; export const NAVIGATE_ROUTE = 'NAVIGATE_ROUTE';
export const NAVIGATE_LOCATION = 'NAVIGATE_LOCATION'; export const NAVIGATE_FOLDER = 'NAVIGATE_FOLDER';
export const NAVIGATE_PARENT_FOLDER = 'NAVIGATE_PARENT_FOLDER';
export class NavigateRouteAction implements Action { export class NavigateRouteAction implements Action {
readonly type = NAVIGATE_ROUTE; readonly type = NAVIGATE_ROUTE;
constructor(public payload: any[]) {} constructor(public payload: any[]) {}
} }
export class NavigateToLocationAction implements Action { export class NavigateToFolder implements Action {
readonly type = NAVIGATE_LOCATION; readonly type = NAVIGATE_FOLDER;
constructor(public payload: any) {} constructor(public payload: MinimalNodeEntity) {}
}
export class NavigateToParentFolder implements Action {
readonly type = NAVIGATE_PARENT_FOLDER;
constructor(public payload: MinimalNodeEntity) {}
} }

View File

@ -0,0 +1,33 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2018 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Action } from '@ngrx/store';
export const SEARCH_BY_TERM = 'SEARCH_BY_TERM';
export class SearchByTermAction implements Action {
readonly type = SEARCH_BY_TERM;
constructor(public payload: string) {}
}

View File

@ -36,7 +36,8 @@ import {
NodeEffects, NodeEffects,
RouterEffects, RouterEffects,
DownloadEffects, DownloadEffects,
ViewerEffects ViewerEffects,
SearchEffects
} from './effects'; } from './effects';
@NgModule({ @NgModule({
@ -51,7 +52,8 @@ import {
NodeEffects, NodeEffects,
RouterEffects, RouterEffects,
DownloadEffects, DownloadEffects,
ViewerEffects ViewerEffects,
SearchEffects
]), ]),
!environment.production !environment.production
? StoreDevtoolsModule.instrument({ maxAge: 25 }) ? StoreDevtoolsModule.instrument({ maxAge: 25 })

View File

@ -28,3 +28,4 @@ export * from './effects/node.effects';
export * from './effects/router.effects'; export * from './effects/router.effects';
export * from './effects/snackbar.effects'; export * from './effects/snackbar.effects';
export * from './effects/viewer.effects'; export * from './effects/viewer.effects';
export * from './effects/search.effects';

View File

@ -30,10 +30,11 @@ import { MinimalNodeEntryEntity, PathInfoEntity } from 'alfresco-js-api';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { import {
NavigateRouteAction, NavigateRouteAction,
NavigateToLocationAction, NavigateToParentFolder,
NAVIGATE_LOCATION, NAVIGATE_PARENT_FOLDER,
NAVIGATE_ROUTE NAVIGATE_ROUTE
} from '../actions'; } from '../actions';
import { NavigateToFolder, NAVIGATE_FOLDER } from '../actions/router.actions';
@Injectable() @Injectable()
export class RouterEffects { export class RouterEffects {
@ -48,16 +49,49 @@ export class RouterEffects {
); );
@Effect({ dispatch: false }) @Effect({ dispatch: false })
navigateLocation$ = this.actions$.pipe( navigateToFolder$ = this.actions$.pipe(
ofType<NavigateToLocationAction>(NAVIGATE_LOCATION), ofType<NavigateToFolder>(NAVIGATE_FOLDER),
map(action => { map(action => {
if (action.payload) { if (action.payload && action.payload.entry) {
this.navigateToLocation(action.payload); this.navigateToFolder(action.payload.entry);
} }
}) })
); );
private navigateToLocation(node: MinimalNodeEntryEntity) { @Effect({ dispatch: false })
navigateToParentFolder$ = this.actions$.pipe(
ofType<NavigateToParentFolder>(NAVIGATE_PARENT_FOLDER),
map(action => {
if (action.payload && action.payload.entry) {
this.navigateToParentFolder(action.payload.entry);
}
})
);
private navigateToFolder(node: MinimalNodeEntryEntity) {
let link = null;
const { path, id } = node;
if (path && path.name && path.elements) {
const isLibraryPath = this.isLibraryContent(<PathInfoEntity>path);
const parent = path.elements[path.elements.length - 1];
const area = isLibraryPath ? '/libraries' : '/personal-files';
if (!isLibraryPath) {
link = [area, id];
} else {
// parent.id could be 'Site' folder or child as 'documentLibrary'
link = [area, parent.name === 'Sites' ? {} : id];
}
}
setTimeout(() => {
this.router.navigate(link);
}, 10);
}
private navigateToParentFolder(node: MinimalNodeEntryEntity) {
let link = null; let link = null;
const { path } = node; const { path } = node;

View File

@ -0,0 +1,43 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2018 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Effect, Actions, ofType } from '@ngrx/effects';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { SEARCH_BY_TERM, SearchByTermAction } from '../actions/search.actions';
import { Router } from '@angular/router';
@Injectable()
export class SearchEffects {
constructor(private actions$: Actions, private router: Router) {}
@Effect({ dispatch: false })
searchByTerm$ = this.actions$.pipe(
ofType<SearchByTermAction>(SEARCH_BY_TERM),
map(action => {
this.router.navigateByUrl('/search;q=' + action.payload);
})
);
}

View File

@ -34,13 +34,15 @@ import { StoreModule } from '@ngrx/store';
import { appReducer } from '../store/reducers/app.reducer'; import { appReducer } from '../store/reducers/app.reducer';
import { INITIAL_STATE } from '../store/states/app.state'; import { INITIAL_STATE } from '../store/states/app.state';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { EffectsModule } from '@ngrx/effects';
@NgModule({ @NgModule({
imports: [ imports: [
NoopAnimationsModule, NoopAnimationsModule,
HttpClientModule, HttpClientModule,
RouterTestingModule, RouterTestingModule,
StoreModule.forRoot({ app: appReducer }, { initialState: INITIAL_STATE }) StoreModule.forRoot({ app: appReducer }, { initialState: INITIAL_STATE }),
EffectsModule.forRoot([])
], ],
declarations: [ declarations: [
TranslatePipeMock TranslatePipeMock