Restore navigation to search page in demo-shell

- Remove deprecated router component
- searchChanges and new searchSubmit component outputs work as you would expect
- Update tests

Refs #737
This commit is contained in:
Will Abson
2016-09-23 18:07:40 +01:00
parent 79fc3085c0
commit 147af98bda
6 changed files with 85 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
<alfresco-search-control *ngIf="isLoggedIn()" [searchTerm]="searchTerm" [autocomplete]="false" <alfresco-search-control *ngIf="isLoggedIn()" [searchTerm]="searchTerm" [autocomplete]="false"
(searchChange)="searchTermChange($event);" (expand)="onExpandToggle($event);" (preview)="onFileClicked($event)"></alfresco-search-control> (searchSubmit)="onSearchSubmit($event);" (searchChange)="onSearchTermChange($event);" (expand)="onExpandToggle($event);" (preview)="onFileClicked($event)"></alfresco-search-control>
<alfresco-viewer [(showViewer)]="fileShowed" <alfresco-viewer [(showViewer)]="fileShowed"
[fileNodeId]="fileNodeId" [fileNodeId]="fileNodeId"

View File

@@ -16,6 +16,7 @@
*/ */
import { Component, EventEmitter, Output } from '@angular/core'; import { Component, EventEmitter, Output } from '@angular/core';
import { Router } from '@angular/router';
import { AlfrescoAuthenticationService } from 'ng2-alfresco-core'; import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
declare let __moduleName: string; declare let __moduleName: string;
@@ -34,13 +35,25 @@ export class SearchBarComponent {
@Output() @Output()
expand = new EventEmitter(); expand = new EventEmitter();
constructor(public auth: AlfrescoAuthenticationService) { constructor(public router: Router,
public auth: AlfrescoAuthenticationService) {
} }
isLoggedIn(): boolean { isLoggedIn(): boolean {
return this.auth.isLoggedIn(); return this.auth.isLoggedIn();
} }
/**
* Called when the user submits the search, e.g. hits enter or clicks submit
*
* @param event Parameters relating to the search
*/
onSearchSubmit(event) {
this.router.navigate(['/search', {
q: event.value
}]);
}
onFileClicked(event) { onFileClicked(event) {
if (event.value.entry.isFile) { if (event.value.entry.isFile) {
this.fileNodeId = event.value.entry.id; this.fileNodeId = event.value.entry.id;
@@ -48,8 +61,7 @@ export class SearchBarComponent {
} }
} }
searchTermChange(event) { onSearchTermChange(event) {
console.log('Search term changed', event);
this.searchTerm = event.value; this.searchTerm = event.value;
} }

View File

@@ -118,5 +118,39 @@ describe('AlfrescoSearchControlComponent', () => {
expect(element.querySelectorAll('label.mdl-button--icon').length).toBe(0); expect(element.querySelectorAll('label.mdl-button--icon').length).toBe(0);
}); });
}); });
describe('search submit', () => {
it('should fire a search when a term has been entered', () => {
spyOn(component.searchSubmit, 'emit');
alfrescoSearchControlComponentFixture.detectChanges();
let formEl = element.querySelector('form');
component.searchTerm = 'searchTerm1';
component.searchControl.updateValue('searchTerm1');
alfrescoSearchControlComponentFixture.detectChanges();
formEl.dispatchEvent(new Event('submit'));
alfrescoSearchControlComponentFixture.detectChanges();
expect(component.searchSubmit.emit).toHaveBeenCalledWith({
'value': 'searchTerm1'
});
});
it('should not fire a search when no term has been entered', () => {
spyOn(component.searchSubmit, 'emit');
alfrescoSearchControlComponentFixture.detectChanges();
let inputEl = element.querySelector('input[type="text"]');
let formEl = element.querySelector('form');
inputEl.value = '';
formEl.dispatchEvent(new Event('submit'));
alfrescoSearchControlComponentFixture.detectChanges();
expect(component.searchSubmit.emit).not.toHaveBeenCalled();
});
});
}); });
*/ */

View File

@@ -45,6 +45,9 @@ export class AlfrescoSearchControlComponent {
@Output() @Output()
searchChange = new EventEmitter(); searchChange = new EventEmitter();
@Output()
searchSubmit = new EventEmitter();
@Output() @Output()
preview = new EventEmitter(); preview = new EventEmitter();
@@ -74,6 +77,10 @@ export class AlfrescoSearchControlComponent {
(value: string) => { (value: string) => {
this.autocompleteSearchTerm = value; this.autocompleteSearchTerm = value;
this.searchValid = this.searchControl.valid; this.searchValid = this.searchControl.valid;
this.searchChange.emit({
value: value,
valid: this.searchValid
});
} }
); );
@@ -98,11 +105,8 @@ export class AlfrescoSearchControlComponent {
* @param event Submit event that was fired * @param event Submit event that was fired
*/ */
onSearch(event): void { onSearch(event): void {
if (event) {
event.preventDefault();
}
if (this.searchControl.valid) { if (this.searchControl.valid) {
this.searchChange.emit({ this.searchSubmit.emit({
value: this.searchTerm value: this.searchTerm
}); });
this.searchInput.nativeElement.blur(); this.searchInput.nativeElement.blur();

View File

@@ -15,11 +15,11 @@
* limitations under the License. * limitations under the License.
*/ */
/* import { PLATFORM_PIPES, ReflectiveInjector } from '@angular/core';
import { PLATFORM_PIPES } from '@angular/core';
import { it, describe, expect, inject, beforeEachProviders, beforeEach } from '@angular/core/testing'; import { it, describe, expect, inject, beforeEachProviders, beforeEach } from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing'; import { TestComponentBuilder } from '@angular/compiler/testing';
import { RouteParams } from '@angular/router-deprecated'; import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { AlfrescoSearchComponent } from './alfresco-search.component'; import { AlfrescoSearchComponent } from './alfresco-search.component';
import { AlfrescoThumbnailService } from './../services/alfresco-thumbnail.service'; import { AlfrescoThumbnailService } from './../services/alfresco-thumbnail.service';
import { TranslationMock } from './../assets/translation.service.mock'; import { TranslationMock } from './../assets/translation.service.mock';
@@ -103,12 +103,20 @@ describe('AlfrescoSearchComponent', () => {
}); });
it('should take the provided search term from query param provided via RouteParams', () => { it('should take the provided search term from query param provided via RouteParams', () => {
let search = new AlfrescoSearchComponent(null, null, null, new RouteParams({q: 'exampleTerm692'})); let injector = ReflectiveInjector.resolveAndCreate([
{ provide: ActivatedRoute, useValue: { params: Observable.from([{ q: 'exampleTerm692' }]) } }
]);
let search = new AlfrescoSearchComponent(null, null, null, injector.get(ActivatedRoute));
search.ngOnInit();
expect(search.searchTerm).toBe('exampleTerm692'); expect(search.searchTerm).toBe('exampleTerm692');
}); });
it('should have a null search term if no query param provided via RouteParams', () => { it('should have a null search term if no query param provided via RouteParams', () => {
let search = new AlfrescoSearchComponent(null, null, null, new RouteParams({})); let injector = ReflectiveInjector.resolveAndCreate([
{ provide: ActivatedRoute, useValue: { params: Observable.from([{}]) } }
]);
let search = new AlfrescoSearchComponent(null, null, null, injector.get(ActivatedRoute));
search.ngOnInit();
expect(search.searchTerm).toBeNull(); expect(search.searchTerm).toBeNull();
}); });
@@ -191,4 +199,3 @@ describe('AlfrescoSearchComponent', () => {
}); });
}); });
*/

View File

@@ -15,8 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
import { Component, EventEmitter, Input, Output, OnChanges, OnInit } from '@angular/core'; import { Component, EventEmitter, Input, Output, Optional, OnChanges, SimpleChanges, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute, Params } from '@angular/router';
import { AlfrescoSearchService } from './../services/alfresco-search.service'; import { AlfrescoSearchService } from './../services/alfresco-search.service';
import { AlfrescoThumbnailService } from './../services/alfresco-thumbnail.service'; import { AlfrescoThumbnailService } from './../services/alfresco-thumbnail.service';
import { AlfrescoTranslationService } from 'ng2-alfresco-core'; import { AlfrescoTranslationService } from 'ng2-alfresco-core';
@@ -46,12 +46,12 @@ export class AlfrescoSearchComponent implements OnChanges, OnInit {
errorMessage; errorMessage;
route: any[] = []; queryParamName = 'q';
constructor(private alfrescoSearchService: AlfrescoSearchService, constructor(private alfrescoSearchService: AlfrescoSearchService,
private translate: AlfrescoTranslationService, private translate: AlfrescoTranslationService,
private _alfrescoThumbnailService: AlfrescoThumbnailService, private _alfrescoThumbnailService: AlfrescoThumbnailService,
private activatedRoute: ActivatedRoute) { @Optional() private route: ActivatedRoute) {
if (translate !== null) { if (translate !== null) {
translate.addTranslationFolder('node_modules/ng2-alfresco-search/dist/src'); translate.addTranslationFolder('node_modules/ng2-alfresco-search/dist/src');
@@ -61,15 +61,19 @@ export class AlfrescoSearchComponent implements OnChanges, OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
this.activatedRoute.params.subscribe(params => { if (this.route) {
this.searchTerm = params['q']; this.route.params.forEach((params: Params) => {
this.searchTerm = params.hasOwnProperty(this.queryParamName) ? params[this.queryParamName] : null;
this.displaySearchResults(this.searchTerm);
});
} else {
this.displaySearchResults(this.searchTerm); this.displaySearchResults(this.searchTerm);
}); }
} }
ngOnChanges(changes): void { ngOnChanges(changes: SimpleChanges): void {
if (changes.searchTerm) { if (changes['searchTerm']) {
this.displaySearchResults(changes.searchTerm.currentValue); this.displaySearchResults(changes['searchTerm'].currentValue);
} }
} }