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