8621df966fed31a03f9824e58c0af59399473d5a
[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     constructor(public http:Http) {
30         this.newsearch = new EventEmitter();
31         this.visible = true;
32         this.getUnitGroupSets();
33         this.ownershipSelector = document.getElementById("ownershipSelector");
34         this.typeSelector = document.getElementById("typeSelector");
35         this.locationSelector = document.getElementById("locationSelector");
36         this.searchBar = document.getElementById("livesearch");
37     }
38
39     getMoreInfo(orgunit) {
40         this.orgunits = [];
41         this.newsearch.next(orgunit.id);
42         return document.getElementById("searchform").reset();
43
44     }
45
46     //When filtermenu is open show x else show arraowdown
47     toggle() {
48         this.visible = !this.visible;
49         if (this.visible) {
50             this.resetSelector();
51         }
52     }
53
54     resetSelector(){
55         this.ownershipSelector.selectedIndex = 0;
56         this.typeSelector.selectedIndex = 0;
57         this.locationSelector.selectedIndex = 0;
58         this.checkOrgunits();
59     }
60
61     //Hide results when search bar input is erased
62     hideDiv() {
63         if (this.searchBar.value == "")
64             return true;
65
66     }
67
68     //Click out results and empty the search bar
69     emptyByClick(){
70         this.orgunits = [];
71         return document.getElementById("searchform").reset();
72     }
73
74     //Gets all unit group sets (category groups) and the unit groups
75     getUnitGroupSets() {
76         //gets unit group sets and display in selector
77         this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
78             .map(res => res.json())
79             .map(res => res.organisationUnitGroupSets)
80             .subscribe(
81                 zone.bind(res => {
82                     this.setOptionHeader(this.ownershipSelector, res[0].name);
83                     this.setOptionHeader(this.typeSelector, res[1].name);
84                     this.setOptionHeader(this.locationSelector, res[2].name);
85
86                     for (var i = 0; i < res.length; i++) {
87                         //gets unit groups for each group set and display in selector
88                         this.http.get(res[i].href)
89                             .map(result => result.json())
90                             .subscribe(
91                                 zone.bind(result => {
92                                     if (result.displayName == "Facility Ownership") {
93                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
94                                             this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
95                                         }
96                                     }
97                                     else if (result.displayName == "Facility Type") {
98                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
99                                             this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
100                                         }
101                                     }
102                                     else if (result.displayName == "Location Rural/Urban") {
103                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
104                                             this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
105                                         }
106                                     }
107                                 })
108                             );
109                     }
110                 })
111             )
112     }
113
114     //Add group set "header" to selector
115     setOptionHeader(selector, value) {
116         this.option = document.createElement("option");
117         this.option.text = "All";
118         this.option.value = "";
119         selector.appendChild(this.option);
120     }
121
122     //Add group to selector
123     setOption(selector, value) {
124         this.option = document.createElement("option");
125         this.option.text = value;
126         this.option.value = value;
127         selector.appendChild(this.option);
128     }
129
130     //Checks the status of orgunits-array and if filter is set
131     checkOrgunits() {
132         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
133             this.filteredOrgunits = [];
134             for (var i = 0; i < this.orgunits.length; i++) {
135                 this.filteredOrgunits.push(this.orgunits[i]);
136             }
137         }
138         else if (!this.orgunits.length == false && !this.filterset) {
139             this.setFilter();
140         }
141         else if (!this.orgunits.length) {
142             this.filteredOrgunits = [];
143             if (this.filterset) {
144                 this.filterset = false;
145             }
146         }
147         if(this.filteredOrgunits.length == 0){
148             return false;
149         }
150         else{
151             return !this.orgunits.length;
152         }
153     }
154
155     //Filtering the orgunits-array by checking what filter is active
156     setFilter() {
157         this.filteredOrgunits = [];
158         this.filterset = true;
159         for (var i = 0; i < this.orgunits.length; i++) {
160             this.http.get(this.orgunits[i].href)
161                 .map(res => res.json())
162                 .subscribe(
163                     zone.bind(orgunits => {
164                         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
165                             this.filteredOrgunits.push(orgunits);
166                         }
167                         else {
168                             var os = false;
169                             var ls = false;
170                             var ts = false;
171                             for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
172                                 if (this.ownershipSelector.value != "") {
173                                     if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
174                                         os = true;
175                                     }
176                                 }
177                                 if (this.ownershipSelector.value == "") {
178                                     os = true;
179                                 }
180                                 if (this.typeSelector.value != "") {
181                                     if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
182                                         ts = true;
183                                     }
184                                 }
185                                 if (this.typeSelector.value == "") {
186                                     ts = true;
187                                 }
188                                 if (this.locationSelector.value != "") {
189                                     if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
190                                         ls = true;
191                                     }
192                                 }
193                                 if (this.locationSelector.value == "") {
194                                     ls = true;
195                                 }
196                                 if (os == true && ts == true && ls == true) {
197                                     this.filteredOrgunits.push(orgunits);
198                                     os = false;
199                                     ts = false;
200                                     ls = false;
201                                 }
202                             }
203                         }
204                     })
205                 )
206         }
207     }
208 }
209
210