Added message for no result when searching
[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
27cd7c38 7declare var zone:Zone;
b74efa44
EHF
8
9@Component({
10 selector: 'mou-search',
690e4045 11 directives: [CORE_DIRECTIVES, LiveSearch],
1a0d2e35 12 events: ['newsearch'],
21341a61
EHF
13 templateUrl: './components/search/search.html',
14 styleUrls: ['./components/search/search.css']
b74efa44 15})
6e8d36df 16export class Search {
27cd7c38
YF
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;
431ff5c5 32
7f3e9674
YF
33
34 constructor(public http:Http) {
1a0d2e35 35 this.newsearch = new EventEmitter();
27af2a91 36 this.visible = true;
3562820e 37 this.emptySearch = document.getElementById("divresult");
c489ddac 38 this.getUnitGroupSets();
37fb701b 39 this.ownershipSelector = document.getElementById("ownershipSelector");
5cc1f6d0
YF
40 this.typeSelector = document.getElementById("typeSelector");
41 this.locationSelector = document.getElementById("locationSelector");
42 this.searchBar = document.getElementById("livesearch");
431ff5c5
YF
43 this.orglist = document.getElementById("orglist");
44 this.a = document.getElementById("testunit");
a9e375c3 45 this.dropdown = document.getElementById("dropdown-menu");
27cd7c38 46 this.noresult = {name: "No matching result", lastUpdated: ""};
6e8d36df 47 }
75f95f28 48
1a0d2e35 49 getMoreInfo(orgunit) {
0614791e 50 this.orgunits = [];
1a0d2e35
EHF
51 this.newsearch.next(orgunit.id);
52 }
75f95f28 53
1aec1256 54 //pil opp og ned
27af2a91 55
5bc68022
RM
56 toggle() {
57 this.visible = !this.visible;
27cd7c38
YF
58 if (this.visible) {
59 this.resetSelector();
14fb81ec 60 }
5bc68022 61 }
75f95f28 62
27cd7c38
YF
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 == "")
18709ba8 72 return true;
cb2667b5 73
18709ba8
RM
74 }
75
27cd7c38 76 emptyByClick() {
3562820e
RM
77 return this.emptySearch = document.getElementById("divresult").style.visibility = "hidden";
78 }
79
27cd7c38
YF
80 searchKeydown() {
81 if (this.filterset) {
a9e375c3
YF
82 this.filterset = false;
83 this.setFilter();
84 }
85 }
86
27cd7c38 87 getUnitGroupSets() {
c489ddac 88 this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
27cd7c38
YF
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 )
7f3e9674
YF
121 }
122
27cd7c38 123 setOptionHeader(selector, value) {
37fb701b
YF
124 this.option = document.createElement("option");
125 this.option.text = "-- " + value + " --";
126 this.option.value = "";
127 selector.appendChild(this.option);
128 }
129
27cd7c38 130 setOption(selector, value) {
5cc1f6d0
YF
131 this.option = document.createElement("option");
132 this.option.text = value;
133 this.option.value = value;
37fb701b 134 selector.appendChild(this.option);
5cc1f6d0
YF
135 }
136
27cd7c38 137 checkOrgunits() {
14fb81ec
YF
138 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
139 this.filteredOrgunits = [];
27cd7c38 140 for (var i = 0; i < this.orgunits.length; i++) {
14fb81ec
YF
141 this.filteredOrgunits.push(this.orgunits[i]);
142 }
143 }
27cd7c38 144 else if (!this.orgunits.length == false && !this.filterset) {
431ff5c5 145 this.setFilter();
431ff5c5 146 }
27cd7c38 147 else if (!this.orgunits.length) {
431ff5c5 148 this.filteredOrgunits = [];
27cd7c38 149 if (this.filterset) {
431ff5c5
YF
150 this.filterset = false;
151 }
431ff5c5 152 }
27cd7c38
YF
153 if(livesearch.value > 2)
154 return false;
155
156 else
157 !this.orgunits.length;
431ff5c5 158 }
5cc1f6d0 159
5cc1f6d0 160
27cd7c38 161 setFilter() {
431ff5c5 162 this.filteredOrgunits = [];
14fb81ec 163 this.filterset = true;
431ff5c5
YF
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 {
27cd7c38
YF
173 var os = false;
174 var ls = false;
175 var ts = false;
14fb81ec 176 for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
431ff5c5 177 if (this.ownershipSelector.value != "") {
14fb81ec 178 if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
431ff5c5
YF
179 os = true;
180 }
181 }
182 if (this.ownershipSelector.value == "") {
183 os = true;
184 }
185 if (this.typeSelector.value != "") {
14fb81ec 186 if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
431ff5c5
YF
187 ts = true;
188 }
189 }
190 if (this.typeSelector.value == "") {
191 ts = true;
192 }
193 if (this.locationSelector.value != "") {
14fb81ec 194 if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
431ff5c5
YF
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;
431ff5c5
YF
206 }
207 }
208 }
209 })
210 )
211 }
5cc1f6d0 212 }
b74efa44 213}
6e8d36df
EHF
214
215