Improved readability in search.ts
[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     groups: Array<any> = [];
19     groupSet: Array<any> = [];
20     counter: number = 0;
21     ownershipSelector: any;
22     typeSelector: any;
23     locationSelector: any;
24     option: any;
25     searchBar: any;
26
27     constructor(public http:Http) {
28         this.newsearch = new EventEmitter();
29         this.visible = true;
30         this.getUnitGroupSets();
31         this.ownershipSelector = document.getElementById("ownershipSelector");
32         this.typeSelector = document.getElementById("typeSelector");
33         this.locationSelector = document.getElementById("locationSelector");
34         this.searchBar = document.getElementById("livesearch");
35     }
36
37     getMoreInfo(orgunit) {
38         console.log("yolo");
39         this.newsearch.next(orgunit.id);
40     }
41
42     //pil opp og ned
43
44     toggle() {
45         this.visible = !this.visible;
46         //this.getUnitGroupSets();
47     }
48
49
50     getUnitGroupSets(){
51         this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
52         .map(res => res.json())
53         .map(res => res.organisationUnitGroupSets)
54         .subscribe(
55             zone.bind( res =>{
56                 this.setOptionHeader(this.ownershipSelector, res[0].name);
57                 this.setOptionHeader(this.typeSelector, res[1].name);
58                 this.setOptionHeader(this.locationSelector, res[2].name);
59
60                 for(var i = 0; i < res.length; i++) {
61                     this.http.get(res[i].href)
62                     .map(result => result.json())
63                     .subscribe(
64                         zone.bind(result => {
65                             if(result.displayName == "Facility Ownership"){
66                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
67                                     this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
68                                 }
69                             }
70                             else if(result.displayName == "Facility Type"){
71                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
72                                     this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
73                                 }
74                             }
75                             else if(result.displayName == "Location Rural/Urban"){
76                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
77                                     this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
78                                 }
79                             }
80                     }));
81                 }
82             })
83         )
84     }
85
86     setOptionHeader(selector, value){
87         this.option = document.createElement("option");
88         this.option.text = "-- " + value + " --";
89         this.option.value = "";
90         selector.appendChild(this.option);
91     }
92
93     setOption(selector, value){
94         this.option = document.createElement("option");
95         this.option.text = value;
96         this.option.value = value;
97         selector.appendChild(this.option);
98     }
99
100     setFilter(){
101         console.log("Dette er setFilter");
102         console.log(this.ownershipSelector.value);
103         var text = livesearch.value;
104
105         //livesearch.value = "";
106         //console.log(this.searchBar.key);
107
108         /*for(var i = 0; i < text.length; i++){
109             livesearch.value += text.charAt(i);
110         }*/
111         //this.searchBar.createEvent('keyup');
112
113         this.searchBar.focus(true);
114     }
115 }
116
117