]> git.uio.no Git - u/erikhf/frm.git/blame - src/components/map/map.ts
quick bug fix
[u/erikhf/frm.git] / src / components / map / map.ts
CommitLineData
cad70644 1import {Component, EventEmitter,CORE_DIRECTIVES, NgIf} from 'angular2/angular2';
a2d7d6b4 2import {Headers, Http} from 'angular2/http';
cb2c4ba8
JHR
3
4@Component({
28765058 5 selector: 'mou-map',
cb2c4ba8 6 directives: [CORE_DIRECTIVES],
0fc3af96 7 events: ['newactive', 'neworg'],
28765058 8 templateUrl: './components/map/map.html'
cb2c4ba8
JHR
9})
10
11
28765058 12export class Map {
6ced1bc7 13
0fc3af96 14 hideModal:any;
dd095993 15 map:Object;
65c7cdf5
JHR
16 http:Http;
17 LEVEL:number;
57a79ad4 18 allLevels:Object;
65c7cdf5 19 runned:boolean;
65c7cdf5 20 uprunned:boolean;
cad70644 21 toplevel: boolean = false;
57a79ad4 22 parent:Object;
59ffa1e6 23 activeId:string;
57a79ad4 24 currentPos:Object;
59ffa1e6 25 currentMarker:Object;
e58627b7 26 isSearched:boolean;
59ffa1e6 27
15b422d5 28 // COLORS:Object;
65c7cdf5 29
dd095993 30 constructor(http:Http) {
59ffa1e6
JHR
31
32 this.activeId = null;
7ee898bc 33 this.newactive = new EventEmitter();
ebaf2f58 34 this.neworg = new EventEmitter();
f800869b
EHF
35 this.map = new google.maps.Map(document.getElementById("map"), {
36 center: {lat: 0, lng: 0},
37 zoom: 12,
38 mapTypeControlOptions: {
39 position: google.maps.ControlPosition.BOTTOM_CENTER
40 },
57a79ad4 41 zoomControlOptions: {
f800869b
EHF
42 position: google.maps.ControlPosition.LEFT_BOTTOM
43 },
44 streetViewControl: false
45 });
6ced1bc7
JHR
46 this.init();
47 this.http = http;
57a79ad4 48 this.LEVEL = 2;//
0d91e9f9 49 this.runned = false;
57a79ad4 50 this.getLevels(this);
65c7cdf5 51 this.parent = null;
a8451d12 52 this.currentPos = null;
951b1c9f 53 this.uprunned = false;
59ffa1e6 54 this.currentMarker = null;
e58627b7 55 this.isSearched = false;
15b422d5 56 // this.COLORS = {'red','brown',',yellow','green',',pink','purple','gray','black'};
784566de 57 }
9d471cab 58
57a79ad4
JHR
59 setActiveId(id) {
60 this.activeId = id;
61 }
9992f01a 62
57a79ad4
JHR
63 getMap() {
64 return this.map;
65 }
330dd6d3 66
57a79ad4
JHR
67 getHttp() {
68 return this.http;
69 }
b41d2490 70
57a79ad4
JHR
71 setcurrentPos(latlng) {
72 this.currentPos = latlng;
73 }
b41d2490 74
57a79ad4
JHR
75 getcurrentPos() {
76 return this.currentPos;
77 }
9d471cab 78
57a79ad4
JHR
79 setParent(id) {
80 this.parent = id;
81 }
59ffa1e6 82
57a79ad4
JHR
83 getParent() {
84 return this.parent;
85 }
9992f01a 86
57a79ad4
JHR
87 setRunned(value) {
88 this.runned = value;
89 }
36f8898a 90
57a79ad4
JHR
91 setupRunned(value) {
92 this.uprunned = value;
93 }
65c7cdf5 94
57a79ad4
JHR
95 setLevel(value) {
96 this.LEVEL = value;
97 }
9992f01a 98
57a79ad4
JHR
99 addLevel() {
100 this.LEVEL++;
101 }
65c7cdf5 102
57a79ad4
JHR
103 upLevel() {
104 this.LEVEL--;
105 }
b470b939 106
6ced1bc7 107 init() {
a2d7d6b4 108
6ced1bc7 109 let map = this.map;
b27f57f1 110 let pos = {lat: 9.1, lng: -11.6};
b27f57f1
JHR
111 map.setCenter(pos, 0);
112 map.setZoom(7);
113
a2d7d6b4
JHR
114 }
115
116 logError(error) {
117 console.error(error);
118
cb2c4ba8 119 }
28765058 120
65c7cdf5
JHR
121 getData(query, instance, isParent) {
122 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
6ced1bc7
JHR
123 .map(res => res.json())
124 .subscribe(
65c7cdf5 125 res => instance.parseResult(res, instance, isParent),
c7e8b786 126 error => instance.logError(error)
6ced1bc7 127 );
6ced1bc7
JHR
128 }
129
57a79ad4
JHR
130 getLevels() {
131 this.http.get(dhisAPI + '/api/organisationUnitLevels')
132 .map(res => res.json())
133 .subscribe(
e58627b7 134 res => this.saveLevelTotalandGetdata(res, this),
57a79ad4
JHR
135 err => this.logError(err)
136 );
137 }
138
e58627b7 139 saveLevelTotalandGetdata(res, instance) {
57a79ad4 140 instance.allLevels = res.pager.total;
e58627b7 141 instance.getData('?paging=false&level=2', instance, false);
57a79ad4
JHR
142 }
143
65c7cdf5 144 parseResult(res, instance, isParent) {
65c7cdf5 145 if (isParent) {
03c7df04 146 instance.setParent(res.parent.id);
65c7cdf5 147 instance.getData('/' + res.parent.id + '/children', instance, false);
03c7df04 148 }
65c7cdf5 149 else {
951b1c9f
JHR
150 if (res.organisationUnits) {
151 for (let item in res.organisationUnits) {
152 this.getData('/' + res.organisationUnits[item].id, this);
153
b470b939 154 }
951b1c9f 155 instance.setupRunned(false);
03c7df04 156 instance.setRunned(false);
951b1c9f
JHR
157 } else if (!res.displayName && res.children) {
158 for (let item in res.children) {
159 if (res.children[item].level == instance.LEVEL) {
160 this.getData('/' + res.children[item].id, this);
161 }
162 }
163 instance.setRunned(false);
164 instance.setupRunned(false);
b470b939 165 }
951b1c9f
JHR
166 else {
167 this.drawPolygon(res, instance);
168 }
03c7df04 169 }
6ced1bc7 170 }
57a79ad4 171
65c7cdf5 172 drawPolygon(item, instance) {
1f8c27ee 173 let feature;
65c7cdf5 174 let incoming:string;
1f8c27ee 175 incoming = item.featureType.toLowerCase();
65c7cdf5 176 switch (incoming) {
1f8c27ee
JHR
177 case "point":
178 feature = 'Point';
179 break;
180 case "multi_polygon":
181 feature = 'MultiPolygon';
182 break;
65c7cdf5
JHR
183 case "polygon":
184 feature = 'MultiPolygon';
1f8c27ee
JHR
185 break;
186 default:
187 }
2cab9a95 188
65c7cdf5 189 if (feature !== undefined) {
1f8c27ee
JHR
190 let unit = {
191 "type": "Feature",
192 "geometry": {
193 "type": feature,
194 "coordinates": JSON.parse(item.coordinates)
195 },
196 "properties": {
e58627b7 197 "title": item.name,
1f8c27ee 198 "name": item.name,
98cc809b 199 "id": item.id,
15b422d5
JHR
200 "color": "gray",
201 "icon": null
65c7cdf5 202 }
1f8c27ee 203 };
65c7cdf5 204 if (unit.geometry.type == 'Point') {
15b422d5
JHR
205 unit.properties.icon = {
206 path: google.maps.SymbolPath.CIRCLE,
b3b5cc9c 207 strokeColor: 'black',
15b422d5
JHR
208 scale: 3
209 };
3981be2b 210 //instance.map.panTo({lat:unit.geometry.coordinates[0],lng:unit.geometry.coordinates[1]});
f3e550a1 211 }
65c7cdf5 212
1f8c27ee 213 this.map.data.addGeoJson(unit);
0fc3af96 214 this.map.data.setStyle(function (feature) {
15b422d5
JHR
215 let color = 'gray';
216 let icon;
0fc3af96
EHF
217 if (feature.getProperty('icon') !== null) {
218 icon = feature.getProperty('icon');
15b422d5
JHR
219 }
220 color = feature.getProperty('color');
221 return /** @type {google.maps.Data.StyleOptions} */({
222 fillColor: color,
223 strokeColor: color,
59ffa1e6 224 strokeWeight: 3,
15b422d5
JHR
225 icon: icon
226 });
227 });
e58627b7
JHR
228 if(instance.isSearched){
229 instance.seeDetails();
230 }
65c7cdf5 231 this.map.data.addListener('click', function (event) {
cad70644 232 $('#myModal').modal('show');
59ffa1e6
JHR
233 instance.setActiveId(event.feature.O.id);
234 instance.setcurrentPos(event.latLng);
951b1c9f 235
59ffa1e6 236 if (instance.uprunned == false && instance.LEVEL == 2) {
cad70644
EHF
237 this.hideModal = document.getElementById("topLevel").style.display = "block";
238 this.hideModal = document.getElementById("middleLevel").style.display = "none";
239 this.hideModal = document.getElementById("bottomLevel").style.display = "none";
59ffa1e6 240 }
57a79ad4 241 else if (instance.runned == false && instance.LEVEL < instance.allLevels) {
cad70644
EHF
242 this.hideModal = document.getElementById("topLevel").style.display = "none";
243 this.hideModal = document.getElementById("middleLevel").style.display = "block";
244 this.hideModal = document.getElementById("bottomLevel").style.display = "none";
57a79ad4 245 } else if (instance.runned == false && instance.LEVEL <= instance.allLevels) {
cad70644
EHF
246 this.hideModal = document.getElementById("topLevel").style.display = "none";
247 this.hideModal = document.getElementById("middleLevel").style.display = "none";
248 this.hideModal = document.getElementById("bottomLevel").style.display = "block";
951b1c9f 249
65c7cdf5 250 instance.setcurrentPos(event.latLng);
59ffa1e6 251 }
59ffa1e6
JHR
252 });
253 }
59ffa1e6 254 }
03c7df04 255
59ffa1e6
JHR
256 drillDown() {
257 this.closeModal();
258 let map = this.getMap();
259 let id = this.activeId;
260 let level = this.LEVEL;
59ffa1e6
JHR
261 this.setRunned(true);
262 this.setParent(id);
784566de 263
59ffa1e6
JHR
264 map.data.forEach(function (feature) {
265 if (!(feature.O.id == id && level == 3)) {
266 map.data.remove(feature);
03c7df04 267
59ffa1e6
JHR
268 }
269 });
03c7df04 270
59ffa1e6
JHR
271 this.addLevel();
272 this.getData('/' + id + '/children', this);
03c7df04 273
59ffa1e6 274 }
bbee9db0 275
59ffa1e6 276 drillUp() {
951b1c9f 277
03c7df04 278
57a79ad4
JHR
279 this.setupRunned(true);
280 this.upLevel();
281 let instance = this;
282 this.closeModal();
283 this.map.data.forEach(function (feature) {
284 instance.map.data.remove(feature);
285
286 });
e58627b7 287 if (this.currentMarker !== null) {
57a79ad4 288 this.currentMarker.setMap(null);
15b422d5 289 }
57a79ad4
JHR
290 let parent = instance.getParent();
291 instance.getData('/' + parent, instance, true);
292
59ffa1e6
JHR
293 this.closeModal();
294 }
295
296 seeDetails() {
297 let map = this.getMap();
298 let id = this.activeId;
299 this.closeModal();
300 map.data.forEach(function (feature) {
301 if (feature.getProperty('id') == id) {
302 feature.setProperty('color', 'red');
b3b5cc9c
JHR
303 if (feature.getProperty('icon') !== null) {
304 feature.O.icon.strokeColor = 'red';
305 }
e58627b7 306 this.isSearched=false;
b3b5cc9c
JHR
307 }
308 else {
309 feature.setProperty('color', 'gray');
310 if (feature.getProperty('icon') !== null) {
311 feature.O.icon.strokeColor = 'black';
312 }
59ffa1e6
JHR
313 }
314 });
315 this.newactive.next(this.activeId);
6ced1bc7
JHR
316 }
317
65c7cdf5 318 addUnit() {
59ffa1e6 319 this.closeModal();
9992f01a 320 let pos = this.getcurrentPos();
951b1c9f 321 let lat = pos.lat();
59ffa1e6 322 let lng = pos.lng();
59ffa1e6
JHR
323 let parent = this.getParent();
324
65c7cdf5
JHR
325 let location = {lat: lat, lng: lng};
326 let event = {location, parent};
ebaf2f58 327 this.neworg.next(event);
0fc3af96 328 this.closeModal();
bc2f4c9e 329 this.setRunned(false);
a8451d12 330 }
dd095993 331
65c7cdf5 332 update(event) {
fa052229 333 this.newactive.next(event);
59ffa1e6 334 let map = this.getMap();
36f8898a
JHR
335 let http = this.getHttp();
336
59ffa1e6
JHR
337 map.data.forEach(function (feature) {
338 map.data.remove(feature);
36f8898a 339 });
65c7cdf5 340 http.get(dhisAPI + '/api/organisationUnits/' + event)
36f8898a
JHR
341 .map(res => res.json())
342 .subscribe(
d647078e 343 res=> this.mapUpdate(res, this)
36f8898a 344 );
e58627b7 345
7ee898bc 346 }
d647078e 347
65c7cdf5 348 mapUpdate(res, instance) {
d647078e 349 this.setLevel(res.level);
e58627b7
JHR
350 this.setActiveId(res.id);
351 this.isSearched = true;
d647078e 352 this.setParent(res.parent.id);
e58627b7
JHR
353
354 instance.getData('/' + res.parent.id + '/children', instance);
355 if (res.coordinates == null || instance.LEVEL == instance.allLevels) {
356 instance.http.get(dhisAPI + '/api/organisationUnits/' + res.parent.id)
357 .map(res => res.json())
358 .subscribe(
359 res => instance.drawPolygon(res, instance)
360 );
361 }
d647078e
JHR
362
363 }
364
fa052229
EHF
365 tempMarker(pos) {
366 let map = this.map;
b3b5cc9c 367 if (this.currentMarker)
fa052229
EHF
368 this.currentMarker.setMap(null);
369
370 this.currentMarker = new google.maps.Marker({
371 position: pos,
372 map: map,
373 title: 'neworg',
374 icon: {
375 path: google.maps.SymbolPath.CIRCLE,
376 scale: 3
377 }
378 });
379 this.currentMarker.setMap(map);
3981be2b 380 //map.panTo(this.currentMarker.getPosition());
b3b5cc9c 381
258519d6
JHR
382 }
383
3981be2b 384
258519d6 385 closeModal() {
cad70644 386 $("#myModal").modal("hide");
258519d6
JHR
387 this.setRunned(false);
388 }
389
6ced1bc7 390}
dd095993 391
dd095993
JHR
392
393
394
395