1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/gallery/search/search.gallery.component.ts

110 lines
3.3 KiB
TypeScript
Raw Normal View History

///<reference path="../../../browser.d.ts"/>
2016-05-04 23:20:21 +08:00
import {Component} from "@angular/core";
import {AutoCompleteService} from "./autocomplete.service";
2016-05-05 00:34:54 +08:00
import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem";
import {Message} from "../../../../common/entities/Message";
2016-05-10 03:43:52 +08:00
import {GalleryService} from "../gallery.service";
import {FORM_DIRECTIVES} from "@angular/common";
import {Config} from "../../config/Config";
@Component({
selector: 'gallery-search',
templateUrl: 'app/gallery/search/search.gallery.component.html',
styleUrls: ['app/gallery/search/search.gallery.component.css'],
2016-05-10 03:43:52 +08:00
providers: [AutoCompleteService],
directives: [FORM_DIRECTIVES]
})
export class GallerySearchComponent {
2016-05-09 23:04:56 +08:00
2016-05-05 00:34:54 +08:00
autoCompleteItems:Array<AutoCompleteRenderItem> = [];
2016-05-10 03:43:52 +08:00
private searchText:string = "";
2016-05-09 23:04:56 +08:00
2016-05-10 03:43:52 +08:00
constructor(private _autoCompleteService:AutoCompleteService, private _galleryService:GalleryService) {
}
2016-05-10 03:43:52 +08:00
onSearchChange(event:KeyboardEvent) {
2016-05-09 23:04:56 +08:00
let searchText = (<HTMLInputElement>event.target).value;
2016-05-10 03:43:52 +08:00
if (Config.Client.Search.autocompleteEnabled) {
this.autocomplete(searchText);
}
if (Config.Client.Search.instantSearchEnabled) {
this._galleryService.instantSearch(searchText);
}
}
2016-05-10 03:43:52 +08:00
public onSearch() {
if (Config.Client.Search.searchEnabled) {
this._galleryService.search(this.searchText);
}
2016-05-10 03:43:52 +08:00
}
public search(item:AutoCompleteItem) {
console.log("clicked");
this.searchText = item.text;
this.onSearch();
}
2016-05-09 23:04:56 +08:00
public onFocusLost(event) {
2016-05-05 00:57:31 +08:00
this.autoCompleteItems = [];
}
public onFocus(event) {
this.autocomplete(this.searchText);
}
2016-05-09 23:04:56 +08:00
private emptyAutoComplete() {
this.autoCompleteItems = [];
}
private autocomplete(searchText:string) {
if (!Config.Client.Search.autocompleteEnabled) {
return
}
if (searchText.trim().length > 0) {
this._autoCompleteService.autoComplete(searchText).then((message:Message<Array<AutoCompleteItem>>) => {
if (message.error) {
//TODO: implement
console.error(message.error);
return;
}
this.showSuggestions(message.result, searchText);
});
} else {
this.emptyAutoComplete();
}
}
private showSuggestions(suggestions:Array<AutoCompleteItem>, searchText:string) {
this.emptyAutoComplete();
suggestions.forEach((item)=> {
let renderItem = new AutoCompleteRenderItem(item.text, searchText);
this.autoCompleteItems.push(renderItem);
});
}
}
2016-05-09 23:04:56 +08:00
class AutoCompleteRenderItem {
public preText:string = "";
public highLightText:string = "";
public postText:string = "";
2016-05-09 23:04:56 +08:00
constructor(public text:string, searchText:string) {
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;
}
}
}