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