ryddet opp i css filene, benytter kun map.css
[u/erikhf/frm.git] / src / components / search / search.ts
CommitLineData
1a0d2e35 1import {Component,EventEmitter, View, CORE_DIRECTIVES} from 'angular2/angular2';
7f3e9674 2import {Http} from 'angular2/http';
690e4045 3import {LiveSearch} from "./livesearch";
3562820e 4import {Sidebar} from "../sidebar/sidebar";
c489ddac
YF
5import * as Rx from '@reactivex/rxjs/dist/cjs/Rx';
6
7declare var zone: Zone;
b74efa44
EHF
8
9@Component({
10 selector: 'mou-search',
690e4045 11 directives: [CORE_DIRECTIVES, LiveSearch],
1a0d2e35 12 events: ['newsearch'],
59dd6388 13 templateUrl: './components/search/search.html'
b74efa44 14})
59dd6388 15
6e8d36df 16export class Search {
690e4045 17 orgunits: Array<any> = [];
431ff5c5 18 filteredOrgunits: Array<any> = [];
690e4045 19 loading: boolean = false;
c489ddac
YF
20 groups: Array<any> = [];
21 groupSet: Array<any> = [];
37fb701b 22 ownershipSelector: any;
5cc1f6d0
YF
23 typeSelector: any;
24 locationSelector: any;
25 option: any;
26 searchBar: any;
431ff5c5 27 filterset: boolean = false;
ef90c499 28
7f3e9674 29 constructor(public http:Http) {
1a0d2e35 30 this.newsearch = new EventEmitter();
27af2a91 31 this.visible = true;
c489ddac 32 this.getUnitGroupSets();
37fb701b 33 this.ownershipSelector = document.getElementById("ownershipSelector");
5cc1f6d0
YF
34 this.typeSelector = document.getElementById("typeSelector");
35 this.locationSelector = document.getElementById("locationSelector");
36 this.searchBar = document.getElementById("livesearch");
6e8d36df 37 }
75f95f28 38
1a0d2e35 39 getMoreInfo(orgunit) {
0614791e 40 this.orgunits = [];
1a0d2e35 41 this.newsearch.next(orgunit.id);
39af9c65 42 return document.getElementById("searchform").reset();
270ddb67 43
1a0d2e35 44 }
75f95f28 45
8bd146d9 46 //When filtermenu is open show x else show arraowdown
5bc68022
RM
47 toggle() {
48 this.visible = !this.visible;
27cd7c38
YF
49 if (this.visible) {
50 this.resetSelector();
14fb81ec 51 }
5bc68022 52 }
75f95f28 53
27cd7c38
YF
54 resetSelector(){
55 this.ownershipSelector.selectedIndex = 0;
56 this.typeSelector.selectedIndex = 0;
57 this.locationSelector.selectedIndex = 0;
58 this.checkOrgunits();
59 }
60
8bd146d9 61 //Hide results when search bar input is erased
27cd7c38 62 hideDiv() {
d93bc644 63 if (this.searchBar.value == "")
18709ba8 64 return true;
4b7e7545 65
18709ba8
RM
66 }
67
8bd146d9 68 //Click out results and empty the search bar
3562820e 69 emptyByClick(){
270ddb67 70 this.orgunits = [];
8bd146d9 71 return document.getElementById("searchform").reset();
3562820e
RM
72 }
73
d62612f3 74 //Gets all unit group sets (category groups) and the unit groups
27cd7c38 75 getUnitGroupSets() {
d62612f3 76 //gets unit group sets and display in selector
c489ddac 77 this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
27cd7c38
YF
78 .map(res => res.json())
79 .map(res => res.organisationUnitGroupSets)
80 .subscribe(
81 zone.bind(res => {
82 this.setOptionHeader(this.ownershipSelector, res[0].name);
83 this.setOptionHeader(this.typeSelector, res[1].name);
84 this.setOptionHeader(this.locationSelector, res[2].name);
85
86 for (var i = 0; i < res.length; i++) {
d62612f3 87 //gets unit groups for each group set and display in selector
27cd7c38
YF
88 this.http.get(res[i].href)
89 .map(result => result.json())
90 .subscribe(
91 zone.bind(result => {
92 if (result.displayName == "Facility Ownership") {
93 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
94 this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
95 }
96 }
97 else if (result.displayName == "Facility Type") {
98 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
99 this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
100 }
101 }
102 else if (result.displayName == "Location Rural/Urban") {
103 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
104 this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
105 }
106 }
1b806970
YF
107 })
108 );
27cd7c38
YF
109 }
110 })
111 )
7f3e9674
YF
112 }
113
d62612f3 114 //Add group set "header" to selector
27cd7c38 115 setOptionHeader(selector, value) {
37fb701b 116 this.option = document.createElement("option");
87bd5039 117 this.option.text = "All";
37fb701b
YF
118 this.option.value = "";
119 selector.appendChild(this.option);
120 }
121
d62612f3 122 //Add group to selector
27cd7c38 123 setOption(selector, value) {
5cc1f6d0
YF
124 this.option = document.createElement("option");
125 this.option.text = value;
126 this.option.value = value;
37fb701b 127 selector.appendChild(this.option);
5cc1f6d0
YF
128 }
129
d62612f3 130 //Checks the status of orgunits-array and if filter is set
27cd7c38 131 checkOrgunits() {
14fb81ec
YF
132 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
133 this.filteredOrgunits = [];
27cd7c38 134 for (var i = 0; i < this.orgunits.length; i++) {
14fb81ec
YF
135 this.filteredOrgunits.push(this.orgunits[i]);
136 }
137 }
27cd7c38 138 else if (!this.orgunits.length == false && !this.filterset) {
431ff5c5 139 this.setFilter();
431ff5c5 140 }
27cd7c38 141 else if (!this.orgunits.length) {
431ff5c5 142 this.filteredOrgunits = [];
27cd7c38 143 if (this.filterset) {
431ff5c5
YF
144 this.filterset = false;
145 }
431ff5c5 146 }
d93bc644 147 if(this.filteredOrgunits.length == 0){
27cd7c38 148 return false;
d93bc644 149 }
d93bc644
YF
150 else{
151 return !this.orgunits.length;
152 }
431ff5c5 153 }
5cc1f6d0 154
d62612f3 155 //Filtering the orgunits-array by checking what filter is active
27cd7c38 156 setFilter() {
431ff5c5 157 this.filteredOrgunits = [];
14fb81ec 158 this.filterset = true;
431ff5c5
YF
159 for (var i = 0; i < this.orgunits.length; i++) {
160 this.http.get(this.orgunits[i].href)
161 .map(res => res.json())
162 .subscribe(
163 zone.bind(orgunits => {
164 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
165 this.filteredOrgunits.push(orgunits);
166 }
167 else {
27cd7c38
YF
168 var os = false;
169 var ls = false;
170 var ts = false;
14fb81ec 171 for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
431ff5c5 172 if (this.ownershipSelector.value != "") {
14fb81ec 173 if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
431ff5c5
YF
174 os = true;
175 }
176 }
177 if (this.ownershipSelector.value == "") {
178 os = true;
179 }
180 if (this.typeSelector.value != "") {
14fb81ec 181 if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
431ff5c5
YF
182 ts = true;
183 }
184 }
185 if (this.typeSelector.value == "") {
186 ts = true;
187 }
188 if (this.locationSelector.value != "") {
14fb81ec 189 if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
431ff5c5
YF
190 ls = true;
191 }
192 }
193 if (this.locationSelector.value == "") {
194 ls = true;
195 }
196 if (os == true && ts == true && ls == true) {
197 this.filteredOrgunits.push(orgunits);
198 os = false;
199 ts = false;
200 ls = false;
431ff5c5
YF
201 }
202 }
203 }
204 })
205 )
206 }
5cc1f6d0 207 }
b74efa44 208}
6e8d36df
EHF
209
210