1 import {Directive, View, EventEmitter, ElementRef} from 'angular2/angular2';
4 import * as Rx from '@reactivex/rxjs/dist/cjs/Rx';
6 import {SearchService} from "./SearchService";
8 declare var zone: Zone;
11 selector: 'input[type=text][mou-live-search]',
12 outputs: ['results', 'loading'],
13 providers: [SearchService]
15 export class LiveSearch {
16 results: EventEmitter = new EventEmitter();
17 loading: EventEmitter = new EventEmitter();
19 constructor( private el: ElementRef, public search: SearchService){
24 console.log("starting");
25 (<any>Rx).Observable.fromEvent(this.el.nativeElement, 'keyup')
26 .map(e => e.target.value)
27 .filter(text => text.length > 2)
29 .distinctUntilChanged()
30 .do(zone.bind(() => this.loading.next(true)))
31 .flatMap(query => this.search.search(query))
32 .do(zone.bind(() => this.loading.next(false)))
34 zone.bind( orgunits => {
35 //this.results.next(orgunits);
36 console.log(orgunits);
37 this.results.next(orgunits);
41 this.results.next(['ERROR, see console']);
44 console.log("complete");