2018-03-31 03:30:30 +08:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
import {AutoCompleteService} from './autocomplete.service';
|
|
|
|
import {AutoCompleteItem, SearchTypes} from '../../../../common/entities/AutoCompleteItem';
|
|
|
|
import {ActivatedRoute, Params, RouterLink} from '@angular/router';
|
|
|
|
import {GalleryService} from '../gallery.service';
|
|
|
|
import {Config} from '../../../../common/config/public/Config';
|
2016-05-04 03:04:57 +08:00
|
|
|
|
|
|
|
@Component({
|
2017-06-11 04:32:56 +08:00
|
|
|
selector: 'gallery-search',
|
|
|
|
templateUrl: './search.gallery.component.html',
|
|
|
|
styleUrls: ['./search.gallery.component.css'],
|
|
|
|
providers: [AutoCompleteService, RouterLink]
|
2016-05-04 03:04:57 +08:00
|
|
|
})
|
|
|
|
export class GallerySearchComponent {
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
autoCompleteItems: Array<AutoCompleteRenderItem> = [];
|
2018-03-31 03:30:30 +08:00
|
|
|
public searchText: string = '';
|
2017-06-11 04:32:56 +08:00
|
|
|
private cache = {
|
2018-03-31 03:30:30 +08:00
|
|
|
lastAutocomplete: '',
|
|
|
|
lastInstantSearch: ''
|
2017-06-11 04:32:56 +08:00
|
|
|
};
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
SearchTypes: any = [];
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
private subscription = null;
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
constructor(private _autoCompleteService: AutoCompleteService,
|
|
|
|
private _galleryService: GalleryService,
|
|
|
|
private _route: ActivatedRoute) {
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
this.SearchTypes = SearchTypes;
|
2016-12-27 06:36:38 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
this.subscription = this._route.params.subscribe((params: Params) => {
|
|
|
|
let searchText = params['searchText'];
|
2018-03-31 03:30:30 +08:00
|
|
|
if (searchText && searchText != '') {
|
2017-07-04 01:17:49 +08:00
|
|
|
this.searchText = searchText;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-04 03:04:57 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
ngOnDestroy() {
|
|
|
|
if (this.subscription !== null) {
|
2018-03-31 03:30:30 +08:00
|
|
|
this.subscription.unsubscribe();
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-10 16:59:12 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
onSearchChange(event: KeyboardEvent) {
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
let searchText = (<HTMLInputElement>event.target).value.trim();
|
2016-05-11 03:33:58 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (Config.Client.Search.autocompleteEnabled && this.cache.lastAutocomplete != searchText) {
|
|
|
|
this.cache.lastAutocomplete = searchText;
|
|
|
|
this.autocomplete(searchText);
|
2016-05-04 03:04:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (Config.Client.Search.instantSearchEnabled && this.cache.lastInstantSearch != searchText) {
|
|
|
|
this.cache.lastInstantSearch = searchText;
|
|
|
|
this._galleryService.instantSearch(searchText);
|
2016-05-10 03:43:52 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-10 16:59:12 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public onSearch() {
|
2017-07-14 05:39:09 +08:00
|
|
|
if (Config.Client.Search.enabled) {
|
2017-06-11 04:32:56 +08:00
|
|
|
this._galleryService.search(this.searchText);
|
2016-05-10 16:59:12 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-10 16:59:12 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public search(item: AutoCompleteItem) {
|
|
|
|
this.searchText = item.text;
|
|
|
|
this.onSearch();
|
|
|
|
}
|
2016-05-11 03:33:58 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
mouseOverAutoComplete: boolean = false;
|
2016-12-27 06:36:38 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public setMouseOverAutoComplete(value: boolean) {
|
|
|
|
this.mouseOverAutoComplete = value;
|
|
|
|
}
|
2016-05-04 03:04:57 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public onFocusLost() {
|
|
|
|
if (this.mouseOverAutoComplete == false) {
|
|
|
|
this.autoCompleteItems = [];
|
2016-05-10 16:59:12 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-10 16:59:12 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public onFocus() {
|
|
|
|
this.autocomplete(this.searchText);
|
|
|
|
}
|
2016-05-04 03:04:57 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private emptyAutoComplete() {
|
|
|
|
this.autoCompleteItems = [];
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
private async autocomplete(searchText: string) {
|
2017-06-11 04:32:56 +08:00
|
|
|
if (!Config.Client.Search.autocompleteEnabled) {
|
2018-03-31 03:30:30 +08:00
|
|
|
return;
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2018-03-31 03:30:30 +08:00
|
|
|
if (searchText.trim() == '.') {
|
2017-07-08 06:18:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (searchText.trim().length > 0) {
|
2017-07-04 01:17:49 +08:00
|
|
|
try {
|
|
|
|
const items = await this._autoCompleteService.autoComplete(searchText);
|
|
|
|
this.showSuggestions(items, searchText);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
} else {
|
|
|
|
this.emptyAutoComplete();
|
2016-05-10 16:59:12 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-04 03:04:57 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private showSuggestions(suggestions: Array<AutoCompleteItem>, searchText: string) {
|
|
|
|
this.emptyAutoComplete();
|
|
|
|
suggestions.forEach((item: AutoCompleteItem) => {
|
|
|
|
let renderItem = new AutoCompleteRenderItem(item.text, searchText, item.type);
|
|
|
|
this.autoCompleteItems.push(renderItem);
|
|
|
|
});
|
|
|
|
}
|
2016-05-11 03:33:58 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public setSearchText(searchText: string) {
|
|
|
|
this.searchText = searchText;
|
|
|
|
}
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2016-05-04 03:04:57 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
class AutoCompleteRenderItem {
|
2018-03-31 03:30:30 +08:00
|
|
|
public preText: string = '';
|
|
|
|
public highLightText: string = '';
|
|
|
|
public postText: string = '';
|
2017-06-11 04:32:56 +08:00
|
|
|
public type: SearchTypes;
|
|
|
|
|
|
|
|
constructor(public text: string, searchText: string, type: SearchTypes) {
|
|
|
|
let preIndex = text.toLowerCase().indexOf(searchText.toLowerCase());
|
|
|
|
if (preIndex > -1) {
|
|
|
|
this.preText = text.substring(0, preIndex);
|
|
|
|
this.highLightText = text.substring(preIndex, preIndex + searchText.length);
|
|
|
|
this.postText = text.substring(preIndex + searchText.length);
|
|
|
|
} else {
|
|
|
|
this.postText = text;
|
2016-05-04 03:04:57 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.type = type;
|
|
|
|
}
|
2016-05-04 03:04:57 +08:00
|
|
|
}
|
|
|
|
|