28aad7a8e41a26fbba6f03753644a9c308752191
[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         //this.getUnitGroupSets();
55     }
56
57     hideDiv(){
58         if(livesearch.value == "")
59             return true;
60
61     }
62
63
64     emptyByClick(){
65         return this.emptySearch = document.getElementById("divresult").style.visibility = "hidden";
66     }
67
68
69     getUnitGroupSets(){
70         this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
71         .map(res => res.json())
72         .map(res => res.organisationUnitGroupSets)
73         .subscribe(
74             zone.bind( res =>{
75                 this.setOptionHeader(this.ownershipSelector, res[0].name);
76                 this.setOptionHeader(this.typeSelector, res[1].name);
77                 this.setOptionHeader(this.locationSelector, res[2].name);
78
79                 for(var i = 0; i < res.length; i++) {
80                     this.http.get(res[i].href)
81                     .map(result => result.json())
82                     .subscribe(
83                         zone.bind(result => {
84                             if(result.displayName == "Facility Ownership"){
85                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
86                                     this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
87                                 }
88                             }
89                             else if(result.displayName == "Facility Type"){
90                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
91                                     this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
92                                 }
93                             }
94                             else if(result.displayName == "Location Rural/Urban"){
95                                 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
96                                     this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
97                                 }
98                             }
99                     }));
100                 }
101             })
102         )
103     }
104
105     setOptionHeader(selector, value){
106         this.option = document.createElement("option");
107         this.option.text = "-- " + value + " --";
108         this.option.value = "";
109         selector.appendChild(this.option);
110     }
111
112     setOption(selector, value){
113         this.option = document.createElement("option");
114         this.option.text = value;
115         this.option.value = value;
116         selector.appendChild(this.option);
117     }
118
119     checkOrgunits(){
120         if(!this.orgunits.length == false && !this.filterset){
121             this.setFilter();
122             this.filterset = true;
123         }
124         else if(!this.orgunits.length){
125             this.filteredOrgunits = [];
126             if(this.filterset) {
127                 this.filterset = false;
128             }
129
130         }
131         return !this.orgunits.length;
132     }
133
134
135     setFilter(){
136         this.filteredOrgunits = [];
137
138         for (var i = 0; i < this.orgunits.length; i++) {
139             this.http.get(this.orgunits[i].href)
140                 .map(res => res.json())
141                 .subscribe(
142                     zone.bind(orgunits => {
143                         if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
144                             this.filteredOrgunits.push(orgunits);
145                         }
146                         else {
147                             var os = false; var ls = false;var ts = false;
148                             for (var group in orgunits.organisationUnitGroups) {
149                                 if (this.ownershipSelector.value != "") {
150                                     if (orgunits.organisationUnitGroups[group].name == this.ownershipSelector.value) {
151                                         os = true;
152                                     }
153                                 }
154                                 if (this.ownershipSelector.value == "") {
155                                     os = true;
156                                 }
157                                 if (this.typeSelector.value != "") {
158                                     if (orgunits.organisationUnitGroups[group].name == this.typeSelector.value) {
159                                         ts = true;
160                                     }
161                                 }
162                                 if (this.typeSelector.value == "") {
163                                     ts = true;
164                                 }
165                                 if (this.locationSelector.value != "") {
166                                     if (orgunits.organisationUnitGroups[group].name == this.locationSelector.value) {
167                                         ls = true;
168                                     }
169                                 }
170                                 if (this.locationSelector.value == "") {
171                                     ls = true;
172                                 }
173                                 if (os == true && ts == true && ls == true) {
174                                     this.filteredOrgunits.push(orgunits);
175                                     os = false;
176                                     ts = false;
177                                     ls = false;
178
179                                 }
180                             }
181                         }
182                     })
183                 )
184         }
185     }
186 }
187
188