mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
imlementing search autocomplete list click
This commit is contained in:
parent
deefd64d11
commit
4caa499faf
@ -34,6 +34,7 @@ export class GalleryService {
|
|||||||
|
|
||||||
//TODO: cache
|
//TODO: cache
|
||||||
public search(text:string):Promise<Message<ContentWrapper>> {
|
public search(text:string):Promise<Message<ContentWrapper>> {
|
||||||
|
clearTimeout(this.searchId);
|
||||||
if (text === null || text === '') {
|
if (text === null || text === '') {
|
||||||
return Promise.resolve(new Message(null, null));
|
return Promise.resolve(new Message(null, null));
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,21 @@
|
|||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
border-bottom-left-radius: 4px;
|
border-bottom-left-radius: 4px;
|
||||||
border-bottom-right-radius: 4px;
|
border-bottom-right-radius: 4px;
|
||||||
padding:12px;
|
padding-left: 3px;
|
||||||
|
padding-right: 3px;
|
||||||
|
padding-top: 5px;
|
||||||
|
padding-bottom: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.autocomplete-item{
|
.autocomplete-item{
|
||||||
|
cursor: pointer;
|
||||||
|
padding-left: 3px;
|
||||||
|
padding-right: 3px;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.autocomplete-item:hover {
|
||||||
|
background-color: #eaeaea;
|
||||||
}
|
}
|
||||||
|
|
||||||
#srch-term {
|
#srch-term {
|
||||||
|
@ -2,11 +2,12 @@
|
|||||||
<form class="navbar-form" role="search" #SearchForm="ngForm">
|
<form class="navbar-form" role="search" #SearchForm="ngForm">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="text" class="form-control" placeholder="Search" (keyup)="onSearchChange($event)"
|
<input type="text" class="form-control" placeholder="Search" (keyup)="onSearchChange($event)"
|
||||||
(blur)="onFocusLost($event)" [(ngModel)]="searchText" #name="ngForm" ngControl="search"
|
(blur)="onFocusLost($event)" (focus)="onFocus($evnet)" [(ngModel)]="searchText" #name="ngForm"
|
||||||
|
ngControl="search"
|
||||||
name="srch-term" id="srch-term" autocomplete="off" >
|
name="srch-term" id="srch-term" autocomplete="off" >
|
||||||
|
|
||||||
<div class="autocomplete-list" *ngIf="autoCompleteItems.length > 0" >
|
<div class="autocomplete-list" *ngIf="autoCompleteItems.length > 0" >
|
||||||
<div class="autocomplete-item" *ngFor="let item of autoCompleteItems">
|
<div class="autocomplete-item" *ngFor="let item of autoCompleteItems" (mousedown)="search(item)">
|
||||||
{{item.preText}}<strong>{{item.highLightText}}</strong>{{item.postText}}
|
{{item.preText}}<strong>{{item.highLightText}}</strong>{{item.postText}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -23,7 +23,44 @@ export class GallerySearchComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onSearchChange(event:KeyboardEvent) {
|
onSearchChange(event:KeyboardEvent) {
|
||||||
|
|
||||||
let searchText = (<HTMLInputElement>event.target).value;
|
let searchText = (<HTMLInputElement>event.target).value;
|
||||||
|
this.autocomplete(searchText);
|
||||||
|
|
||||||
|
this._galleryService.instantSearch(searchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
public onSearch() {
|
||||||
|
this._galleryService.search(this.searchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
public search(item:AutoCompleteItem) {
|
||||||
|
console.log("clicked");
|
||||||
|
this.searchText = item.text;
|
||||||
|
this.onSearch();
|
||||||
|
}
|
||||||
|
|
||||||
|
private showSuggestions(suggestions:Array<AutoCompleteItem>, searchText:string) {
|
||||||
|
this.emptyAutoComplete();
|
||||||
|
suggestions.forEach((item)=> {
|
||||||
|
let renderItem = new AutoCompleteRenderItem(item.text, searchText);
|
||||||
|
this.autoCompleteItems.push(renderItem);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public onFocusLost(event) {
|
||||||
|
this.autoCompleteItems = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public onFocus(event) {
|
||||||
|
this.autocomplete(this.searchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
private emptyAutoComplete() {
|
||||||
|
this.autoCompleteItems = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
private autocomplete(searchText:string) {
|
||||||
if (searchText.trim().length > 0) {
|
if (searchText.trim().length > 0) {
|
||||||
this._autoCompleteService.autoComplete(searchText).then((message:Message<Array<AutoCompleteItem>>) => {
|
this._autoCompleteService.autoComplete(searchText).then((message:Message<Array<AutoCompleteItem>>) => {
|
||||||
if (message.error) {
|
if (message.error) {
|
||||||
@ -36,44 +73,24 @@ export class GallerySearchComponent {
|
|||||||
} else {
|
} else {
|
||||||
this.emptyAutoComplete();
|
this.emptyAutoComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
this._galleryService.instantSearch(searchText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public onSearch() {
|
|
||||||
this._galleryService.search(this.searchText);
|
|
||||||
}
|
|
||||||
|
|
||||||
private showSuggestions(suggestions:Array<AutoCompleteItem>, searchText:string) {
|
|
||||||
this.emptyAutoComplete();
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public onFocusLost(event) {
|
|
||||||
this.autoCompleteItems = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
private emptyAutoComplete() {
|
|
||||||
this.autoCompleteItems = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class AutoCompleteRenderItem {
|
class AutoCompleteRenderItem {
|
||||||
constructor(public preText:string = "", public highLightText:string = "", public postText:string = "") {
|
public preText:string = "";
|
||||||
|
public highLightText:string = "";
|
||||||
|
public postText:string = "";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user