493321018bb66bd71d473380a3536669e61dacc1
[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 {Sidebar} from "../sidebar/sidebar";
5 import * as Rx from '@reactivex/rxjs/dist/cjs/Rx';
6
7 declare var zone: Zone;
8
9 @Component({
10     selector: 'mou-search',
11     directives: [CORE_DIRECTIVES, LiveSearch],
12     events: ['newsearch'],
13     templateUrl: './components/search/search.html',
14     styleUrls: ['./components/search/search.css']
15 })
16 export class Search {
17     orgunits: Array<any> = [];
18     filteredOrgunits: Array<any> = [];
19     loading: boolean = false;
20     groups: Array<any> = [];
21     groupSet: Array<any> = [];
22     ownershipSelector: any;
23     typeSelector: any;
24     locationSelector: any;
25     option: any;
26     searchBar: any;
27     filterset: boolean = false;
28
29
30     constructor(public http:Http) {
31         this.newsearch = new EventEmitter();
32         this.visible = true;
33         this.getUnitGroupSets();
34         this.ownershipSelector = document.getElementById("ownershipSelector");
35         this.typeSelector = document.getElementById("typeSelector");
36         this.locationSelector = document.getElementById("locationSelector");
37         this.searchBar = document.getElementById("livesearch");
38         this.orglist = document.getElementById("orglist");
39         this.a = document.getElementById("testunit");
40     }
41
42     getMoreInfo(orgunit) {
43         this.orgunits = [];
44         this.newsearch.next(orgunit.id);
45         return document.getElementById("myForm").reset();
46
47     }
48
49     //pil opp og ned
50
51     toggle() {
52         this.visible = !this.visible;
53         //this.getUnitGroupSets();
54     }
55
56     hideDiv(){
57         if(livesearch.value == "")
58             return true;
59     }
60
61
62     emptyByClick(){
63         this.orgunits = [];
64         return document.getElementById("myForm").reset();
65     }
66
67     getUnitGroupSets(){
68         this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
69         .map(res => res.json())
70         .map(res => res.organisationUnitGroupSets)
71         .subscribe(
72             zone.bind( res =>{
73                 this.setOptionHeader(this.ownershipSelector, res[0].name);
74                 this.setOptionHeader(this.typeSelector, res[1].name);
75                 this.setOptionHeader(this.locationSelector, res[2].name);
76
77                 for(var i = 0; i < res.length; i++) {
78                     this.http.get(res[i].href)
79                     .map(result => result.json())
80                     .subscribe(
81                         zone.bind(result => {
82                             if(result.displayName == "Facility Ownership"){
83                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
84                                     this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
85                                 }
86                             }
87                             else if(result.displayName == "Facility Type"){
88                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
89                                     this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
90                                 }
91                             }
92                             else if(result.displayName == "Location Rural/Urban"){
93                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
94                                     this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
95                                 }
96                             }
97                     }));
98                 }
99             })
100         )
101     }
102
103     setOptionHeader(selector, value){
104         this.option = document.createElement("option");
105         this.option.text = "All";
106         this.option.value = "";
107         selector.appendChild(this.option);
108     }
109
110     setOption(selector, value){
111         this.option = document.createElement("option");
112         this.option.text = value;
113         this.option.value = value;
114         selector.appendChild(this.option);
115     }
116
117     checkOrgunits(){
118         if(!this.orgunits.length == false && !this.filterset){
119             this.setFilter();
120             this.filterset = true;
121         }
122         else if(!this.orgunits.length){
123             this.filteredOrgunits = [];
124             if(this.filterset) {
125                 this.filterset = false;
126             }
127
128         }
129         return !this.orgunits.length;
130     }
131
132
133     setFilter(){
134         this.filteredOrgunits = [];
135
136         for (var i = 0; i < this.orgunits.length; i++) {
137             this.http.get(this.orgunits[i].href)
138                 .map(res => res.json())
139                 .subscribe(
140                     zone.bind(orgunits => {
141                         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
142                             this.filteredOrgunits.push(orgunits);
143                         }
144                         else {
145                             var os = false; var ls = false;var ts = false;
146                             for (var group in orgunits.organisationUnitGroups) {
147                                 if (this.ownershipSelector.value != "") {
148                                     if (orgunits.organisationUnitGroups[group].name == this.ownershipSelector.value) {
149                                         os = true;
150                                     }
151                                 }
152                                 if (this.ownershipSelector.value == "") {
153                                     os = true;
154                                 }
155                                 if (this.typeSelector.value != "") {
156                                     if (orgunits.organisationUnitGroups[group].name == this.typeSelector.value) {
157                                         ts = true;
158                                     }
159                                 }
160                                 if (this.typeSelector.value == "") {
161                                     ts = true;
162                                 }
163                                 if (this.locationSelector.value != "") {
164                                     if (orgunits.organisationUnitGroups[group].name == this.locationSelector.value) {
165                                         ls = true;
166                                     }
167                                 }
168                                 if (this.locationSelector.value == "") {
169                                     ls = true;
170                                 }
171                                 if (os == true && ts == true && ls == true) {
172                                     this.filteredOrgunits.push(orgunits);
173                                     os = false;
174                                     ts = false;
175                                     ls = false;
176
177                                 }
178                             }
179                         }
180                     })
181                 )
182         }
183     }
184 }
185
186