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