Fixed issue with filter where multiple instances of an object appered when no filter...
[u/erikhf/frm.git] / src / components / search / livesearch.ts
CommitLineData
7bf7fd27
YF
1import {CORE_DIRECTIVES, Directive, View, EventEmitter, ElementRef} from 'angular2/angular2';
2import {Http} from 'angular2/http';
6e8d36df 3
690e4045
EHF
4// RxJs
5import * as Rx from '@reactivex/rxjs/dist/cjs/Rx';
6e8d36df 6
690e4045 7import {SearchService} from "./SearchService";
6e8d36df 8
690e4045
EHF
9declare var zone: Zone;
10
11@Directive({
12 selector: 'input[type=text][mou-live-search]',
13 outputs: ['results', 'loading'],
7bf7fd27 14 providers: [CORE_DIRECTIVES, SearchService]
6e8d36df 15})
690e4045 16export class LiveSearch {
7bf7fd27
YF
17 results:EventEmitter = new EventEmitter();
18 loading:EventEmitter = new EventEmitter();
7f3e9674 19
690e4045 20
7bf7fd27 21 constructor(private el:ElementRef, public http:Http, public search:SearchService) {
690e4045
EHF
22
23 }
24
7bf7fd27 25 onInit() {
690e4045
EHF
26 console.log("starting");
27 (<any>Rx).Observable.fromEvent(this.el.nativeElement, 'keyup')
28 .map(e => e.target.value)
29 .filter(text => text.length > 2)
30 .debounceTime(250)
31 .distinctUntilChanged()
32 .do(zone.bind(() => this.loading.next(true)))
33 .flatMap(query => this.search.search(query))
34 .do(zone.bind(() => this.loading.next(false)))
35 .subscribe(
7bf7fd27
YF
36 zone.bind(orgunits => {
37 //this.filterUnits(orgunits)
38 this.results.next(this.filterUnits(orgunits));
690e4045
EHF
39 }),
40 zone.bind(err => {
41 console.log(err);
42 this.results.next(['ERROR, see console']);
43 }),
44 () => {
45 console.log("complete");
46 }
47 )
48 }
7bf7fd27
YF
49
50 filterUnits(orgunits) {
51 var filteredOrgunits: Array<any> = [];
52 for (var i = 0; i < orgunits.length; i++) {
53 this.http.get(orgunits[i].href)
54 .map(res => res.json())
55 .subscribe(
56 zone.bind(orgunits => {
b94018eb
YF
57 if (ownershipSelector.value == "" && typeSelector.value == "" && locationSelector.value == "") {
58 filteredOrgunits.push(orgunits);
59 }
60 else {
61 var os = false;
62 var ls = false;
63 var ts = false;
64 for (var group in orgunits.organisationUnitGroups) {
65 if (ownershipSelector.value != "") {
66 if (orgunits.organisationUnitGroups[group].name == ownershipSelector.value) {
67 os = true;
68 }
69 }
70 if (ownershipSelector.value == "") {
7f3e9674
YF
71 os = true;
72 }
b94018eb
YF
73 if (typeSelector.value != "") {
74 if (orgunits.organisationUnitGroups[group].name == typeSelector.value) {
75 ts = true;
76 }
77 }
78 if (typeSelector.value == "") {
7f3e9674
YF
79 ts = true;
80 }
b94018eb
YF
81 if (locationSelector.value != "") {
82 if (orgunits.organisationUnitGroups[group].name == locationSelector.value) {
83 ls = true;
84 }
85 }
86 if (locationSelector.value == "") {
7f3e9674
YF
87 ls = true;
88 }
b94018eb
YF
89 if (os == true && ts == true && ls == true) {
90 filteredOrgunits.push(orgunits);
91 os = false;
92 ts = false;
93 ls = false;
94 }
7bf7fd27
YF
95 }
96 }
97 })
98 )
99 }
7f3e9674 100 console.log(filteredOrgunits);
7bf7fd27
YF
101 return filteredOrgunits;
102 }
6e8d36df
EHF
103}
104