Working on filling in selectors for filter
[u/erikhf/frm.git] / src / components / search / search.ts
1 import {Component,EventEmitter, View, CORE_DIRECTIVES} from 'angular2/angular2';
2 import {Http} from 'angular2/http';
3 import {LiveSearch} from "./livesearch";
4 import * as Rx from '@reactivex/rxjs/dist/cjs/Rx';
5
6 declare var zone: Zone;
7
8 @Component({
9     selector: 'mou-search',
10     directives: [CORE_DIRECTIVES, LiveSearch],
11     events: ['newsearch'],
12     templateUrl: './components/search/search.html',
13     styleUrls: ['./components/search/search.css']
14 })
15 export class Search {
16     orgunits: Array<any> = [];
17     loading: boolean = false;
18     facilityType: Array<any> = [];
19     facilityOwnership: Array<any> = [];
20     facilityLocation: Array<any> = [];
21     groups: Array<any> = [];
22     groupSet: Array<any> = [];
23     counter: number = 0;
24
25     constructor(public http:Http) {
26         this.newsearch = new EventEmitter();
27         this.visible = true;
28         //this.getFilterGroups();
29         this.getUnitGroupSets();
30
31     }
32
33     getMoreInfo(orgunit) {
34         console.log("yolo");
35         this.newsearch.next(orgunit.id);
36     }
37
38     //pil opp og ned
39
40     toggle() {
41         this.visible = !this.visible;
42         //this.getUnitGroupSets();
43     }
44
45     getUnitGroupSets(){
46         this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
47         .map(res => res.json())
48         .map(res => res.organisationUnitGroupSets)
49         .subscribe(
50             zone.bind( res =>{
51                 this.facilityOwnership .push("-- " + res[0].name + " --");
52                 this.facilityType.push("-- " + res[1].name + " --");
53                 this.facilityLocation.push("-- " + res[2].name + " --");
54
55                 for(var i = 0; i < res.length; i++) {
56                     this.http.get(res[i].href)
57                     .map(result => result.json())
58                     .map(result => result.organisationUnitGroups)
59                     .subscribe(
60                         zone.bind(result => {
61                             if(this.facilityOwnership.length == 1){
62                                 for(var j = 0; j < result.length; j++) {
63                                     this.facilityOwnership.push(result[j].name);
64                                 }
65                             }
66                             else if(this.facilityType.length == 1){
67                                 for(var j = 0; j < result.length; j++) {
68                                     this.facilityType.push(result[j].name);
69                                 }
70                             }
71                             else if(this.facilityLocation.length == 1){
72                                 for(var j = 0; j < result.length; j++) {
73                                     this.facilityLocation.push(result[j].name);
74                                 }
75                             }
76                     }));
77                 }
78             })
79         )
80     }
81
82
83     setFilter(){
84         var text = livesearch.value;
85         livesearch.value = "";
86         console.log(text);
87         for(var i = 0; i < text.length; i++){
88             livesearch.value += text.charAt(i);
89         }
90     }
91
92 }
93
94