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;
54 //Resets the filter values in selectors
56 this.ownershipSelector.selectedIndex = 0;
57 this.typeSelector.selectedIndex = 0;
58 this.locationSelector.selectedIndex = 0;
62 //Hide results when search bar input is erased
64 if (this.searchBar.value == ""){
70 //Click out results and empty the search bar
73 return document.getElementById("searchform").reset();
76 //Gets all unit group sets (category groups) and the unit groups
78 //gets unit group sets and display in selector
79 this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
80 .map(res => res.json())
81 .map(res => res.organisationUnitGroupSets)
84 this.setOptionHeader(this.ownershipSelector, res[0].name);
85 this.setOptionHeader(this.typeSelector, res[1].name);
86 this.setOptionHeader(this.locationSelector, res[2].name);
88 for (var i = 0; i < res.length; i++) {
89 //gets unit groups for each group set and display in selector
90 this.http.get(res[i].href)
91 .map(result => result.json())
94 if (result.displayName == "Facility Ownership") {
95 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
96 this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
99 else if (result.displayName == "Facility Type") {
100 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
101 this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
104 else if (result.displayName == "Location Rural/Urban") {
105 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
106 this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
116 //Add group set "header" to selector
117 setOptionHeader(selector, value) {
118 this.option = document.createElement("option");
119 this.option.text = "All";
120 this.option.value = "";
121 selector.appendChild(this.option);
124 //Add group to selector
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);
132 //Checks the status of orgunits-array and if filter is set
134 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
135 this.filteredOrgunits = [];
136 for (var i = 0; i < this.orgunits.length; i++) {
137 this.filteredOrgunits.push(this.orgunits[i]);
140 else if (!this.orgunits.length == false && !this.filterset) {
143 else if (!this.orgunits.length) {
144 this.filteredOrgunits = [];
145 if (this.filterset) {
146 this.filterset = false;
149 if(this.filteredOrgunits.length == 0){
153 return !this.orgunits.length;
157 //Filtering the orgunits-array by checking what filter is active
159 this.filteredOrgunits = [];
160 this.filterset = true;
161 for (var i = 0; i < this.orgunits.length; i++) {
162 this.http.get(this.orgunits[i].href)
163 .map(res => res.json())
165 zone.bind(orgunits => {
166 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
167 this.filteredOrgunits.push(orgunits);
173 for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
174 if (this.ownershipSelector.value != "") {
175 if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
179 if (this.ownershipSelector.value == "") {
182 if (this.typeSelector.value != "") {
183 if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
187 if (this.typeSelector.value == "") {
190 if (this.locationSelector.value != "") {
191 if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
195 if (this.locationSelector.value == "") {
198 if (os == true && ts == true && ls == true) {
199 this.filteredOrgunits.push(orgunits);