]> git.uio.no Git - u/erikhf/frm.git/blob - src/components/search/search.ts
c3aed550ae8e50e4a09ce99b639c85ea33a8d7ac
[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             //this.toggle();
65             return true;
66         }
67     }
68
69     //Click out results and empty the search bar
70     emptyByClick(){
71         this.orgunits = [];
72         return document.getElementById("searchform").reset();
73     }
74
75     //Gets all unit group sets (category groups) and the unit groups
76     getUnitGroupSets() {
77         //gets unit group sets and display in selector
78         this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
79             .map(res => res.json())
80             .map(res => res.organisationUnitGroupSets)
81             .subscribe(
82                 zone.bind(res => {
83                     this.setOptionHeader(this.ownershipSelector, res[0].name);
84                     this.setOptionHeader(this.typeSelector, res[1].name);
85                     this.setOptionHeader(this.locationSelector, res[2].name);
86
87                     for (var i = 0; i < res.length; i++) {
88                         //gets unit groups for each group set and display in selector
89                         this.http.get(res[i].href)
90                             .map(result => result.json())
91                             .subscribe(
92                                 zone.bind(result => {
93                                     if (result.displayName == "Facility Ownership") {
94                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
95                                             this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
96                                         }
97                                     }
98                                     else if (result.displayName == "Facility Type") {
99                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
100                                             this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
101                                         }
102                                     }
103                                     else if (result.displayName == "Location Rural/Urban") {
104                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
105                                             this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
106                                         }
107                                     }
108                                 })
109                             );
110                     }
111                 })
112             )
113     }
114
115     //Add group set "header" to selector
116     setOptionHeader(selector, value) {
117         this.option = document.createElement("option");
118         this.option.text = "All";
119         this.option.value = "";
120         selector.appendChild(this.option);
121     }
122
123     //Add group to selector
124     setOption(selector, value) {
125         this.option = document.createElement("option");
126         this.option.text = value;
127         this.option.value = value;
128         selector.appendChild(this.option);
129     }
130
131     //Checks the status of orgunits-array and if filter is set
132     checkOrgunits() {
133         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
134             this.filteredOrgunits = [];
135             for (var i = 0; i < this.orgunits.length; i++) {
136                 this.filteredOrgunits.push(this.orgunits[i]);
137             }
138         }
139         else if (!this.orgunits.length == false && !this.filterset) {
140             this.setFilter();
141         }
142         else if (!this.orgunits.length) {
143             this.filteredOrgunits = [];
144             if (this.filterset) {
145                 this.filterset = false;
146             }
147         }
148         if(this.filteredOrgunits.length == 0){
149             return false;
150         }
151         else{
152             return !this.orgunits.length;
153         }
154     }
155
156     //Filtering the orgunits-array by checking what filter is active
157     setFilter() {
158         this.filteredOrgunits = [];
159         this.filterset = true;
160         for (var i = 0; i < this.orgunits.length; i++) {
161             this.http.get(this.orgunits[i].href)
162                 .map(res => res.json())
163                 .subscribe(
164                     zone.bind(orgunits => {
165                         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
166                             this.filteredOrgunits.push(orgunits);
167                         }
168                         else {
169                             var os = false;
170                             var ls = false;
171                             var ts = false;
172                             for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
173                                 if (this.ownershipSelector.value != "") {
174                                     if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
175                                         os = true;
176                                     }
177                                 }
178                                 if (this.ownershipSelector.value == "") {
179                                     os = true;
180                                 }
181                                 if (this.typeSelector.value != "") {
182                                     if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
183                                         ts = true;
184                                     }
185                                 }
186                                 if (this.typeSelector.value == "") {
187                                     ts = true;
188                                 }
189                                 if (this.locationSelector.value != "") {
190                                     if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
191                                         ls = true;
192                                     }
193                                 }
194                                 if (this.locationSelector.value == "") {
195                                     ls = true;
196                                 }
197                                 if (os == true && ts == true && ls == true) {
198                                     this.filteredOrgunits.push(orgunits);
199                                     os = false;
200                                     ts = false;
201                                     ls = false;
202                                 }
203                             }
204                         }
205                     })
206                 )
207         }
208     }
209 }
210
211