[ADF-2150] improved queryBody mechanism (#2852)

* [ADF-2150] changed query body parameter to a function

* [ADF-2150] added an example page where to try change query body

* [ADF-2150] improved queryBody mechanism

* [ADF-2150] fixed content node test

* [ADF-2150] extended docs added another way to use the query node

* [ADF-2150] fixed test for search on content node

* [ADF-2150] added some improvements to service config

* [ADF-2150] changed the documentation accordingly

* [ADF-2150] added PR changes

* [ADF-2150] fixed jdoc

* [ADF-2150] added checkbox to switch from service approach to input object approach

* [ADF-2150] fixed build error on demo shell
This commit is contained in:
Vito
2018-01-23 11:12:52 +01:00
committed by Eugenio Romano
parent 8788eaeb80
commit a401ebd35d
22 changed files with 430 additions and 78 deletions

View File

@@ -301,7 +301,7 @@ describe('ContentNodeSelectorComponent', () => {
typeToSearchBox('kakarot');
setTimeout(() => {
expect(searchSpy).toHaveBeenCalledWith(defaultSearchOptions('kakarot'));
expect(searchSpy).toHaveBeenCalledWith(defaultSearchOptions('kakarot'), '25', '0');
done();
}, 300);
});
@@ -325,7 +325,7 @@ describe('ContentNodeSelectorComponent', () => {
component.siteChanged(<SiteEntry> { entry: { guid: 'namek' } });
expect(searchSpy.calls.count()).toBe(2, 'Search count should be two after the site change');
expect(searchSpy.calls.argsFor(1)).toEqual([defaultSearchOptions('vegeta', 'namek')]);
expect(searchSpy.calls.argsFor(1)).toEqual([defaultSearchOptions('vegeta', 'namek'), '25', '0'] );
done();
}, 300);
});
@@ -523,7 +523,7 @@ describe('ContentNodeSelectorComponent', () => {
component.getNextPageOfSearch({ skipCount });
expect(searchSpy).toHaveBeenCalledWith(defaultSearchOptions('kakarot', undefined, skipCount));
expect(searchSpy).toHaveBeenCalledWith(defaultSearchOptions('kakarot', undefined, skipCount), '25', skipCount.toString());
});
it('should be shown when pagination\'s hasMoreItems is true', () => {

View File

@@ -59,6 +59,6 @@ export class ContentNodeSelectorService {
}
};
return this.searchService.search(defaultSearchNode);
return this.searchService.search(defaultSearchNode, maxItems.toString(), skipCount.toString());
}
}

View File

@@ -27,8 +27,8 @@
<adf-search #auto="searchAutocomplete"
class="adf-search-result-autocomplete"
[queryBody]="customQueryBody"
[maxResults]="liveSearchMaxResults">
[maxResults]="liveSearchMaxResults"
[queryBody]="customQueryBody">
<ng-template let-data>
<mat-list *ngIf="isSearchBarActive()" id="autocomplete-search-result-list">
<mat-list-item

View File

@@ -63,6 +63,7 @@ export class SearchControlComponent implements OnInit, OnDestroy {
@Input()
liveSearchMaxResults: number = 5;
/** @deprecated in 2.1.0 */
@Input()
customQueryBody: QueryBody;

View File

@@ -23,10 +23,10 @@ import { SearchModule } from '../../index';
import { differentResult, folderResult, result, SimpleSearchTestComponent } from '../../mock';
function fakeNodeResultSearch(searchNode: QueryBody): Observable<any> {
if (searchNode.query.query === 'FAKE_SEARCH_EXMPL') {
if (searchNode && searchNode.query.query === 'FAKE_SEARCH_EXMPL') {
return Observable.of(differentResult);
}
if (searchNode.filterQueries.length === 1 &&
if (searchNode && searchNode.filterQueries.length === 1 &&
searchNode.filterQueries[0].query === "TYPE:'cm:folder'") {
return Observable.of(folderResult);
}
@@ -132,18 +132,18 @@ describe('SearchComponent', () => {
});
it('should perform a search based on the query node given', async(() => {
spyOn(searchService, 'search')
.and.callFake((searchObj) => fakeNodeResultSearch(searchObj));
spyOn(searchService, 'searchByQueryBody')
.and.callFake((searchObj) => fakeNodeResultSearch(searchObj));
let fakeSearchNode: QueryBody = {
query: {
query: ''
query: 'TEST-FAKE-NODE'
},
filterQueries: [
{ 'query': "TYPE:'cm:folder'" }
]
};
component.setSearchNodeTo(fakeSearchNode);
component.setSearchWordTo('searchTerm');
component.setSearchNodeTo(fakeSearchNode);
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
@@ -156,7 +156,7 @@ describe('SearchComponent', () => {
it('should perform a search with a defaultNode if no searchnode is given', async(() => {
spyOn(searchService, 'search')
.and.callFake((searchObj) => fakeNodeResultSearch(searchObj));
.and.returnValue(Observable.of(result));
component.setSearchWordTo('searchTerm');
fixture.detectChanges();
fixture.whenStable().then(() => {
@@ -169,7 +169,7 @@ describe('SearchComponent', () => {
}));
it('should perform a search with the searchNode given', async(() => {
spyOn(searchService, 'search')
spyOn(searchService, 'searchByQueryBody')
.and.callFake((searchObj) => fakeNodeResultSearch(searchObj));
let fakeSearchNode: QueryBody = {
query: {
@@ -179,6 +179,7 @@ describe('SearchComponent', () => {
{ 'query': "TYPE:'cm:folder'" }
]
};
component.setSearchWordTo('searchTerm');
component.setSearchNodeTo(fakeSearchNode);
fixture.detectChanges();
fixture.whenStable().then(() => {

View File

@@ -18,8 +18,6 @@
import { SearchService } from '@alfresco/adf-core';
import {
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
ElementRef,
@@ -40,7 +38,6 @@ import { Subject } from 'rxjs/Subject';
styleUrls: ['./search.component.scss'],
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
exportAs: 'searchAutocomplete',
host: {
'class': 'adf-search'
@@ -63,12 +60,13 @@ export class SearchComponent implements AfterContentInit, OnChanges {
@Input()
skipResults: number = 0;
@Input()
searchTerm: string = '';
/** @deprecated in 2.1.0 */
@Input()
queryBody: QueryBody;
@Input()
searchTerm: string = '';
@Input('class')
set classList(classList: string) {
if (classList && classList.length) {
@@ -101,13 +99,13 @@ export class SearchComponent implements AfterContentInit, OnChanges {
_classList: { [key: string]: boolean } = {};
constructor(private searchService: SearchService,
private changeDetectorRef: ChangeDetectorRef,
private _elementRef: ElementRef) {
this.keyPressedStream.asObservable()
.debounceTime(200)
.subscribe((searchedWord: string) => {
this.loadSearchResults(searchedWord);
});
}
ngAfterContentInit() {
@@ -115,14 +113,12 @@ export class SearchComponent implements AfterContentInit, OnChanges {
}
ngOnChanges(changes) {
this.resetResults();
if (changes.queryBody &&
this.hasDifferentQueryBody(changes.queryBody.previousValue, changes.queryBody.currentValue)) {
this.loadSearchResults();
}
if (changes.searchTerm && changes.searchTerm.currentValue) {
this.loadSearchResults(changes.searchTerm.currentValue);
} else if (changes.queryBody && changes.queryBody.currentValue) {
this.loadSearchResults();
} else {
this.loadSearchResults(this.searchTerm);
}
}
@@ -135,23 +131,27 @@ export class SearchComponent implements AfterContentInit, OnChanges {
this.loadSearchResults(this.searchTerm);
}
private hasDifferentQueryBody(previousQueryBody: QueryBody, currentQueryBody: QueryBody) {
return JSON.stringify(previousQueryBody) !== JSON.stringify(currentQueryBody);
}
private cleanResults() {
if (this.results) {
this.results = {};
}
}
private hasValidSearchQuery(searchOpts: QueryBody) {
return searchOpts && searchOpts.query && searchOpts.query.query;
}
private loadSearchResults(searchTerm?: string) {
let searchOpts: QueryBody = this.getQueryBody(searchTerm);
if (this.hasValidSearchQuery(searchOpts)) {
this.searchService
.search(searchOpts)
.subscribe(
this.resetResults();
if (searchTerm) {
let search$;
if (this.queryBody) {
search$ = this.searchService.searchByQueryBody(this.queryBody);
} else {
search$ = this.searchService
.search(searchTerm, this.maxResults.toString(), this.skipResults.toString());
}
search$.subscribe(
results => {
this.results = <NodePaging> results;
this.resultLoaded.emit(this.results);
@@ -169,41 +169,11 @@ export class SearchComponent implements AfterContentInit, OnChanges {
}
}
private getQueryBody(searchTerm: string): QueryBody {
if (this.queryBody) {
if (!this.queryBody.query.query && searchTerm) {
this.queryBody.query.query = searchTerm;
}
return this.queryBody;
} else {
return this.generateDefaultSearchNode(searchTerm);
}
}
private generateDefaultSearchNode(searchTerm: string): QueryBody {
let defaultQueryBody: QueryBody = {
query: {
query: searchTerm ? `${searchTerm}* OR name:${searchTerm}*` : searchTerm
},
include: ['path', 'allowableOperations'],
paging: {
maxItems: this.maxResults.toString(),
skipCount: this.skipResults.toString()
},
filterQueries: [
{ query: "TYPE:'cm:folder' OR TYPE:'cm:content'" },
{ query: 'NOT cm:creator:System' }]
};
return defaultQueryBody;
}
hidePanel() {
if (this.isOpen) {
this._classList['adf-search-show'] = false;
this._classList['adf-search-hide'] = true;
this.isOpen = false;
this.changeDetectorRef.markForCheck();
}
}
@@ -211,6 +181,5 @@ export class SearchComponent implements AfterContentInit, OnChanges {
this.showPanel = !!this.results && !!this.results.list;
this._classList['adf-search-show'] = this.showPanel;
this._classList['adf-search-hide'] = !this.showPanel;
this.changeDetectorRef.markForCheck();
}
}

View File

@@ -17,3 +17,4 @@
export * from './authentication.interface';
export * from './injection.tokens';
export * from './search-configuration.interface';

View File

@@ -0,0 +1,24 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { QueryBody } from 'alfresco-js-api';
export interface SearchConfigurationInterface {
generateQueryBody(searchTerm: string, maxResults: string, skipCount: string): QueryBody;
}

View File

@@ -46,3 +46,4 @@ export * from './shared-links-api.service';
export * from './sites.service';
export * from './discovery-api.service';
export * from './comment-process.service';
export * from './search-configuration.service';

View File

@@ -0,0 +1,45 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { QueryBody } from 'alfresco-js-api';
import { SearchConfigurationInterface } from '../interface/search-configuration.interface';
@Injectable()
export class SearchConfigurationService implements SearchConfigurationInterface {
constructor() {
}
public generateQueryBody(searchTerm: string, maxResults: string, skipCount: string): QueryBody {
let defaultQueryBody: QueryBody = {
query: {
query: searchTerm ? `${searchTerm}* OR name:${searchTerm}*` : searchTerm
},
include: ['path', 'allowableOperations'],
paging: {
maxItems: maxResults,
skipCount: skipCount
},
filterQueries: [
{ query: "TYPE:'cm:folder' OR TYPE:'cm:content'" },
{ query: 'NOT cm:creator:System' }]
};
return defaultQueryBody;
}
}

View File

@@ -21,15 +21,14 @@ import { Observable } from 'rxjs/Observable';
import { AlfrescoApiService } from './alfresco-api.service';
import { AuthenticationService } from './authentication.service';
import 'rxjs/add/observable/throw';
import { SearchConfigurationService } from './search-configuration.service';
/**
* Internal service used by Document List component.
*/
@Injectable()
export class SearchService {
constructor(public authService: AuthenticationService,
private apiService: AlfrescoApiService) {
private apiService: AlfrescoApiService,
private searchConfigurationService: SearchConfigurationService) {
}
getNodeQueryResults(term: string, options?: SearchOptions): Observable<NodePaging> {
@@ -38,8 +37,8 @@ export class SearchService {
.catch(err => this.handleError(err));
}
search(query: QueryBody): Observable<NodePaging> {
const searchQuery = Object.assign(query);
search(searchTerm: string, maxResults: string, skipCount: string): Observable<NodePaging> {
const searchQuery = Object.assign(this.searchConfigurationService.generateQueryBody(searchTerm, maxResults, skipCount));
const promise = this.apiService.getInstance().search.searchApi.search(searchQuery);
return Observable
@@ -47,6 +46,14 @@ export class SearchService {
.catch(err => this.handleError(err));
}
searchByQueryBody(queryBody: QueryBody): Observable<NodePaging> {
const promise = this.apiService.getInstance().search.searchApi.search(queryBody);
return Observable
.fromPromise(promise)
.catch(err => this.handleError(err));
}
private handleError(error: any): Observable<any> {
return Observable.throw(error || 'Server error');
}

View File

@@ -47,6 +47,7 @@ import { TranslateLoaderService } from './translate-loader.service';
import { TranslationService } from './translation.service';
import { UploadService } from './upload.service';
import { UserPreferencesService } from './user-preferences.service';
import { SearchConfigurationService } from './search-configuration.service';
@NgModule({
imports: [],
@@ -81,7 +82,8 @@ import { UserPreferencesService } from './user-preferences.service';
SharedLinksApiService,
SitesService,
DiscoveryApiService,
CommentProcessService
CommentProcessService,
SearchConfigurationService
],
exports: [
]