]> git.uio.no Git - u/erikhf/frm.git/blob - src/components/search/search.ts
Cleanup
[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     emptySearch:any;
29
30     constructor(public http:Http) {
31         this.newsearch = new EventEmitter();
32         this.visible = true;
33         this.emptySearch = document.getElementById("divresult");
34         this.getUnitGroupSets();
35         this.ownershipSelector = document.getElementById("ownershipSelector");
36         this.typeSelector = document.getElementById("typeSelector");
37         this.locationSelector = document.getElementById("locationSelector");
38         this.searchBar = document.getElementById("livesearch");
39     }
40
41     getMoreInfo(orgunit) {
42         this.orgunits = [];
43         this.newsearch.next(orgunit.id);
44     }
45
46     //pil opp og ned
47
48     toggle() {
49         this.visible = !this.visible;
50         if (this.visible) {
51             this.resetSelector();
52         }
53     }
54
55     resetSelector(){
56         this.ownershipSelector.selectedIndex = 0;
57         this.typeSelector.selectedIndex = 0;
58         this.locationSelector.selectedIndex = 0;
59         this.checkOrgunits();
60     }
61
62     hideDiv() {
63         if (this.searchBar.value == "")
64             return true;
65     }
66
67     emptyByClick() {
68         return this.emptySearch = document.getElementById("divresult").style.visibility = "hidden";
69     }
70
71     getUnitGroupSets() {
72         this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
73             .map(res => res.json())
74             .map(res => res.organisationUnitGroupSets)
75             .subscribe(
76                 zone.bind(res => {
77                     this.setOptionHeader(this.ownershipSelector, res[0].name);
78                     this.setOptionHeader(this.typeSelector, res[1].name);
79                     this.setOptionHeader(this.locationSelector, res[2].name);
80
81                     for (var i = 0; i < res.length; i++) {
82                         this.http.get(res[i].href)
83                             .map(result => result.json())
84                             .subscribe(
85                                 zone.bind(result => {
86                                     if (result.displayName == "Facility Ownership") {
87                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
88                                             this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
89                                         }
90                                     }
91                                     else if (result.displayName == "Facility Type") {
92                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
93                                             this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
94                                         }
95                                     }
96                                     else if (result.displayName == "Location Rural/Urban") {
97                                         for (var j = 0; j < result.organisationUnitGroups.length; j++) {
98                                             this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
99                                         }
100                                     }
101                                 }));
102                     }
103                 })
104             )
105     }
106
107     setOptionHeader(selector, value) {
108         this.option = document.createElement("option");
109         this.option.text = "-- " + value + " --";
110         this.option.value = "";
111         selector.appendChild(this.option);
112     }
113
114     setOption(selector, value) {
115         this.option = document.createElement("option");
116         this.option.text = value;
117         this.option.value = value;
118         selector.appendChild(this.option);
119     }
120
121     checkOrgunits() {
122         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
123             this.filteredOrgunits = [];
124             for (var i = 0; i < this.orgunits.length; i++) {
125                 this.filteredOrgunits.push(this.orgunits[i]);
126             }
127         }
128         else if (!this.orgunits.length == false && !this.filterset) {
129             this.setFilter();
130         }
131         else if (!this.orgunits.length) {
132             this.filteredOrgunits = [];
133             if (this.filterset) {
134                 this.filterset = false;
135             }
136         }
137         if(this.filteredOrgunits.length == 0){
138             return false;
139         }
140
141         else{
142             return !this.orgunits.length;
143         }
144     }
145
146
147     setFilter() {
148         this.filteredOrgunits = [];
149         this.filterset = true;
150         for (var i = 0; i < this.orgunits.length; i++) {
151             this.http.get(this.orgunits[i].href)
152                 .map(res => res.json())
153                 .subscribe(
154                     zone.bind(orgunits => {
155                         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
156                             this.filteredOrgunits.push(orgunits);
157                         }
158                         else {
159                             var os = false;
160                             var ls = false;
161                             var ts = false;
162                             for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
163                                 if (this.ownershipSelector.value != "") {
164                                     if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
165                                         os = true;
166                                     }
167                                 }
168                                 if (this.ownershipSelector.value == "") {
169                                     os = true;
170                                 }
171                                 if (this.typeSelector.value != "") {
172                                     if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
173                                         ts = true;
174                                     }
175                                 }
176                                 if (this.typeSelector.value == "") {
177                                     ts = true;
178                                 }
179                                 if (this.locationSelector.value != "") {
180                                     if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
181                                         ls = true;
182                                     }
183                                 }
184                                 if (this.locationSelector.value == "") {
185                                     ls = true;
186                                 }
187                                 if (os == true && ts == true && ls == true) {
188                                     this.filteredOrgunits.push(orgunits);
189                                     os = false;
190                                     ts = false;
191                                     ls = false;
192                                 }
193                             }
194                         }
195                     })
196                 )
197         }
198     }
199 }
200
201