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
2016-05-04 18:34:54 +02:00

61 lines
2.1 KiB
TypeScript

///<reference path="../../../browser.d.ts"/>
import {Component} from "@angular/core";
import {AutoCompleteService} from "./autocomplete.service";
import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem";
import {Message} from "../../../../common/entities/Message";
@Component({
selector: 'gallery-search',
templateUrl: 'app/gallery/search/search.gallery.component.html',
styleUrls: ['app/gallery/search/search.gallery.component.css'],
providers: [AutoCompleteService]
})
export class GallerySearchComponent {
autoCompleteItems:Array<AutoCompleteRenderItem> = [];
constructor(private _autoCompleteService:AutoCompleteService) {
}
getSuggestions(event:KeyboardEvent){
let searchText = (<HTMLInputElement>event.target).value;
if(searchText.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);
});
}
}
private showSuggestions(suggestions:Array<AutoCompleteItem>,searchText:string){
this.autoCompleteItems = [];
suggestions.forEach((item)=>{
let preIndex = item.text.toLowerCase().indexOf(searchText.toLowerCase());
let renderItem = new AutoCompleteRenderItem();
if(preIndex > -1){
renderItem.preText = item.text.substring(0,preIndex);
renderItem.highLightText = item.text.substring(preIndex, preIndex + searchText.length);
renderItem.postText = item.text.substring(preIndex + searchText.length);
}else{
renderItem.postText = item.text;
}
this.autoCompleteItems.push(renderItem);
});
}
}
class AutoCompleteRenderItem{
constructor(public preText:string = "",public highLightText:string = "", public postText:string = ""){
}
}