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