2016-05-04 03:04:57 +08:00
|
|
|
///<reference path="../../../browser.d.ts"/>
|
|
|
|
|
2016-05-04 23:20:21 +08:00
|
|
|
import {Component} from "@angular/core";
|
2016-05-04 03:04:57 +08:00
|
|
|
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-04 03:04:57 +08:00
|
|
|
|
|
|
|
@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 {
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-05-05 00:34:54 +08:00
|
|
|
autoCompleteItems:Array<AutoCompleteRenderItem> = [];
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-05-04 03:04:57 +08:00
|
|
|
constructor(private _autoCompleteService:AutoCompleteService) {
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
getSuggestions(event:KeyboardEvent) {
|
|
|
|
let searchText = (<HTMLInputElement>event.target).value;
|
|
|
|
if (searchText.trim().length > 0) {
|
|
|
|
this._autoCompleteService.autoComplete(searchText).then((message:Message<Array<AutoCompleteItem>>) => {
|
|
|
|
if (message.error) {
|
2016-05-05 00:34:54 +08:00
|
|
|
//TODO: implement
|
|
|
|
console.error(message.error);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
this.showSuggestions(message.result, searchText);
|
2016-05-05 00:34:54 +08:00
|
|
|
});
|
2016-05-09 23:04:56 +08:00
|
|
|
} else {
|
|
|
|
this.emptyAutoComplete();
|
2016-05-04 03:04:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
private showSuggestions(suggestions:Array<AutoCompleteItem>, searchText:string) {
|
2016-05-05 00:57:31 +08:00
|
|
|
this.emptyAutoComplete();
|
2016-05-09 23:04:56 +08:00
|
|
|
suggestions.forEach((item)=> {
|
2016-05-05 00:34:54 +08:00
|
|
|
let preIndex = item.text.toLowerCase().indexOf(searchText.toLowerCase());
|
|
|
|
let renderItem = new AutoCompleteRenderItem();
|
2016-05-09 23:04:56 +08:00
|
|
|
if (preIndex > -1) {
|
|
|
|
renderItem.preText = item.text.substring(0, preIndex);
|
2016-05-05 00:34:54 +08:00
|
|
|
renderItem.highLightText = item.text.substring(preIndex, preIndex + searchText.length);
|
2016-05-09 23:04:56 +08:00
|
|
|
renderItem.postText = item.text.substring(preIndex + searchText.length);
|
|
|
|
} else {
|
2016-05-05 00:34:54 +08:00
|
|
|
renderItem.postText = item.text;
|
2016-05-04 03:04:57 +08:00
|
|
|
}
|
2016-05-05 00:34:54 +08:00
|
|
|
this.autoCompleteItems.push(renderItem);
|
2016-05-04 03:04:57 +08:00
|
|
|
});
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
public onFocusLost(event) {
|
2016-05-05 00:57:31 +08:00
|
|
|
this.autoCompleteItems = [];
|
|
|
|
}
|
2016-05-04 03:04:57 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
private emptyAutoComplete() {
|
|
|
|
this.autoCompleteItems = [];
|
|
|
|
}
|
2016-05-04 03:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
class AutoCompleteRenderItem {
|
|
|
|
constructor(public preText:string = "", public highLightText:string = "", public postText:string = "") {
|
|
|
|
|
2016-05-04 03:04:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|