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';
7 declare var zone: Zone;
10 selector: 'mou-search',
11 directives: [CORE_DIRECTIVES, LiveSearch],
12 events: ['newsearch'],
13 templateUrl: './components/search/search.html',
14 styleUrls: ['./components/search/search.css']
17 orgunits: Array<any> = [];
18 filteredOrgunits: Array<any> = [];
19 loading: boolean = false;
20 groups: Array<any> = [];
21 groupSet: Array<any> = [];
22 ownershipSelector: any;
24 locationSelector: any;
27 filterset: boolean = false;
29 constructor(public http:Http) {
30 this.newsearch = new EventEmitter();
32 this.getUnitGroupSets();
33 this.ownershipSelector = document.getElementById("ownershipSelector");
34 this.typeSelector = document.getElementById("typeSelector");
35 this.locationSelector = document.getElementById("locationSelector");
36 this.searchBar = document.getElementById("livesearch");
39 getMoreInfo(orgunit) {
41 this.newsearch.next(orgunit.id);
42 return document.getElementById("searchform").reset();
46 //When filtermenu is open show x else show arraowdown
48 this.visible = !this.visible;
55 this.ownershipSelector.selectedIndex = 0;
56 this.typeSelector.selectedIndex = 0;
57 this.locationSelector.selectedIndex = 0;
61 //Hide results when search bar input is erased
63 if (this.searchBar.value == ""){
69 //Click out results and empty the search bar
72 return document.getElementById("searchform").reset();
75 //Gets all unit group sets (category groups) and the unit groups
77 //gets unit group sets and display in selector
78 this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
79 .map(res => res.json())
80 .map(res => res.organisationUnitGroupSets)
83 this.setOptionHeader(this.ownershipSelector, res[0].name);
84 this.setOptionHeader(this.typeSelector, res[1].name);
85 this.setOptionHeader(this.locationSelector, res[2].name);
87 for (var i = 0; i < res.length; i++) {
88 //gets unit groups for each group set and display in selector
89 this.http.get(res[i].href)
90 .map(result => result.json())
93 if (result.displayName == "Facility Ownership") {
94 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
95 this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
98 else if (result.displayName == "Facility Type") {
99 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
100 this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
103 else if (result.displayName == "Location Rural/Urban") {
104 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
105 this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
115 //Add group set "header" to selector
116 setOptionHeader(selector, value) {
117 this.option = document.createElement("option");
118 this.option.text = "All";
119 this.option.value = "";
120 selector.appendChild(this.option);
123 //Add group to selector
124 setOption(selector, value) {
125 this.option = document.createElement("option");
126 this.option.text = value;
127 this.option.value = value;
128 selector.appendChild(this.option);
131 //Checks the status of orgunits-array and if filter is set
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]);
139 else if (!this.orgunits.length == false && !this.filterset) {
142 else if (!this.orgunits.length) {
143 this.filteredOrgunits = [];
144 if (this.filterset) {
145 this.filterset = false;
148 if(this.filteredOrgunits.length == 0){
152 return !this.orgunits.length;
156 //Filtering the orgunits-array by checking what filter is active
158 this.filteredOrgunits = [];
159 this.filterset = true;
160 for (var i = 0; i < this.orgunits.length; i++) {
161 this.http.get(this.orgunits[i].href)
162 .map(res => res.json())
164 zone.bind(orgunits => {
165 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
166 this.filteredOrgunits.push(orgunits);
172 for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
173 if (this.ownershipSelector.value != "") {
174 if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
178 if (this.ownershipSelector.value == "") {
181 if (this.typeSelector.value != "") {
182 if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
186 if (this.typeSelector.value == "") {
189 if (this.locationSelector.value != "") {
190 if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
194 if (this.locationSelector.value == "") {
197 if (os == true && ts == true && ls == true) {
198 this.filteredOrgunits.push(orgunits);