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