1 import {Component, EventEmitter,CORE_DIRECTIVES} from 'angular2/angular2';
2 import {Headers, Http} from 'angular2/http';
6 directives: [CORE_DIRECTIVES],
7 events: ['newactive', 'neworg'],
8 templateUrl: './components/map/map.html'
32 * initializes all the global variabels
33 * @param http - for http requests
35 constructor(http:Http) {
38 this.newactive = new EventEmitter();
39 this.neworg = new EventEmitter();
40 this.map = new google.maps.Map(document.getElementById("map"), {
41 center: {lat: 0, lng: 0},
43 mapTypeControlOptions: {
44 position: google.maps.ControlPosition.BOTTOM_CENTER
47 position: google.maps.ControlPosition.LEFT_BOTTOM
49 streetViewControl: false
57 this.currentPos = null;
58 this.uprunned = false;
59 this.currentMarker = null;
60 this.isSearched = false;
62 this.COLORS = ['#ede1bb', '#1d407e', '#ff512e', '#662d47', '#3b3a35', '#419175', '#983e41', '#f3002d', '#b0a875', '#00bfb5', '#926851', '#47a0a4', '#333f50', '#6f007b'];
63 //this.popupON = false;
68 * Sets the global variabel
69 * @param id - id of the active marker
76 * returns the global map
92 * Sets the avctive markers position
93 * @param latlng - position of the active marker
95 setcurrentPos(latlng) {
96 this.currentPos = latlng;
100 * returns the active markers position
104 return this.currentPos;
108 * sets the parent of the avtive marker
109 * @param id - of the parent
117 * returns the actice markers parent
125 * sets a bool value for if the addListner for drilling down has runned (little hack)
126 * @param value - for the runned variabel
133 * sets a bool value for if the addListner for drilling up has runned (little hack)
134 * @param value - for the upRunned variabel
137 this.uprunned = value;
141 * sets the current level in the org.unit hierarchy
142 * @param value - for the level variabel
149 * add level when drilling down (little hack for synconisity)
156 * goes up level when drilling up (little hack for synconisity)
163 * initiates the map with position and zoom
168 let pos = {lat: 9.1, lng: -11.6};
169 map.setCenter(pos, 0);
175 * prints out error messages in the console
176 * @param error - the error massage
179 console.error(error);
184 * gets data from DHIS API
185 * @param query - for what kind of data to retrieve
186 * @param instance - this instance to use
187 * @param isParent - little hack to see if you want to levels up (the parent of a parent)
189 getData(query, instance, isParent) {
190 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
191 .map(res => res.json())
193 res => instance.parseResult(res, instance, isParent),
194 error => instance.logError(error)
199 * Gets the number of levels in the org.unit hierarchy from DHIS
202 this.http.get(dhisAPI + '/api/organisationUnitLevels')
203 .map(res => res.json())
205 res => this.saveLevelTotalandGetdata(res, this),
206 err => this.logError(err)
211 * Saves the data from getLevels() in a global variabel and gets all the data from the second level.
212 * @param res - result from getLevels()
213 * @param instance - witch scope we are in
215 saveLevelTotalandGetdata(res, instance) {
216 instance.allLevels = res.pager.total;
217 instance.getData('?paging=false&level=2', instance, false);
221 * parses all the data from getData() and calles methods based on the incomming data.
222 * @param res - result from getData()
223 * @param instance - witch scope we are in
224 * @param isParent - if it is a parent we have asked for
226 parseResult(res, instance, isParent) {
228 instance.setParent(res.parent.id);
229 instance.getData('/' + res.parent.id + '/children', instance, false);
232 if (res.organisationUnits) {
233 for (let item in res.organisationUnits) {
234 this.getData('/' + res.organisationUnits[item].id, this);
237 instance.setupRunned(false);
238 instance.setRunned(false);
239 } else if (!res.displayName && res.children) {
240 for (let item in res.children) {
241 if (res.children[item].level == instance.LEVEL) {
242 this.getData('/' + res.children[item].id, this);
245 instance.setRunned(false);
246 instance.setupRunned(false);
249 this.drawPolygon(res, instance);
255 * creates and draws up the geojson polygons and adds listeners to them.
256 * @param item - an org.unit object
257 * @param instance - witch scope we are in
259 drawPolygon(item, instance) {
262 incoming = item.featureType.toLowerCase();
267 case "multi_polygon":
268 feature = 'MultiPolygon';
271 feature = 'MultiPolygon';
276 if (feature !== undefined) {
281 "coordinates": JSON.parse(item.coordinates)
287 "color": instance.COLORS[instance.colornum],
291 if (instance.COLORS.length == instance.colornum) {
292 instance.colornum = 0;
297 if (unit.geometry.type == 'Point') {
298 unit.properties.icon = {
299 path: google.maps.SymbolPath.CIRCLE,
300 strokeColor: 'black',
303 instance.map.setCenter({lat: unit.geometry.coordinates[1], lng: unit.geometry.coordinates[0]});
306 this.map.data.addGeoJson(unit);
307 this.map.data.setStyle(function (feature) {
310 if (feature.getProperty('icon') !== null) {
311 icon = feature.getProperty('icon');
313 color = feature.getProperty('color');
314 return /** @type {google.maps.Data.StyleOptions} */({
317 strokeColor: 'white',
323 if (instance.isSearched) {
324 instance.seeDetails();
326 this.map.data.addListener('click', function (event) {
327 $('#myModal').modal('show');
328 instance.setActiveId(event.feature.O.id);
329 instance.setcurrentPos(event.latLng);
331 if (instance.uprunned == false && instance.LEVEL == 2) {
332 document.getElementById("topLevel").style.display = "block";
333 document.getElementById("middleLevel").style.display = "none";
334 document.getElementById("bottomLevel").style.display = "none";
336 else if (instance.runned == false && instance.LEVEL < instance.allLevels) {
337 document.getElementById("topLevel").style.display = "none";
338 document.getElementById("middleLevel").style.display = "block";
339 document.getElementById("bottomLevel").style.display = "none";
340 } else if (instance.runned == false && instance.LEVEL <= instance.allLevels) {
341 document.getElementById("topLevel").style.display = "none";
342 document.getElementById("middleLevel").style.display = "none";
343 document.getElementById("bottomLevel").style.display = "block";
345 instance.setcurrentPos(event.latLng);
349 //slette ?? §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
350 /* this.map.data.addListener('mouseover', function (e) {
351 if(!instance.popupON) {
352 instance.popupON = true;
354 instance.popup = new google.maps.InfoWindow({
355 content: e.feature.getProperty('name'),
358 instance.popup.open(instance.map);
362 this.map.data.addListener('mouseout', function (event) {
363 instance.popupON = false;
364 instance.popup.open(null);
371 ///////////////////////////////////////////////////////////////////////////////////
373 updateAfterAddorEdit(event) {
374 console.log(" nå har jeg updates eller adda" + this.currentMarker);
375 if (this.currentMarker) {
376 this.currentMarker.setMap(null);
379 let map = this.getMap();
380 let http = this.getHttp();
383 map.data.forEach(function (feature) {
384 map.data.remove(feature);
386 http.get(dhisAPI + '/api/organisationUnits/' + event)
387 .map(res => res.json())
389 res => this.updateMap(res, this)
393 updateMap(res, instance) {
394 this.isSearched = false;
395 this.setLevel(res.level);
396 this.setActiveId(res.id);
397 this.setParent(res.parent.id);
398 this.setcurrentPos({lat: JSON.parse(res.coordinates)[1], lng: JSON.parse(res.coordinates)[0]});
400 instance.getData('/' + res.parent.id + '/children', instance);
401 if (res.coordinates == null || instance.LEVEL == instance.allLevels) {
402 instance.http.get(dhisAPI + '/api/organisationUnits/' + res.parent.id)
403 .map(res => res.json())
405 res => instance.drawPolygon(res, instance)
411 //////////////////////////////////////////////////////////////////////////////////////////
413 * removes the polygon on current level and calles getData on one level down in the org.unit hierarchy
417 let map = this.getMap();
418 let id = this.activeId;
419 let level = this.LEVEL;
420 let lev = (this.allLevels) - 1;
421 this.setRunned(true);
424 map.data.forEach(function (feature) {
425 if (!(feature.O.id == id && level == lev)) {
426 map.data.remove(feature);
432 this.getData('/' + id + '/children', this);
437 *removes the plogons on the current level and calles the get data with tha parents id and set parent true. this to say that we want this parent's parent
440 this.setupRunned(true);
444 this.map.data.forEach(function (feature) {
445 instance.map.data.remove(feature);
448 if (this.currentMarker !== null) {
449 this.currentMarker.setMap(null);
451 let parent = instance.getParent();
452 instance.getData('/' + parent, instance, true);
458 * focuses map and colors to the clicked marker/polygon and fires an event to sidebar with the id of the marker
461 console.log("KOM INN HER");
462 let map = this.getMap();
463 let id = this.activeId;
465 map.data.forEach(function (feature) {
466 if (feature.getProperty('id') == id) {
467 feature.setProperty('color', 'red');
468 if (feature.getProperty('icon') !== null) {
469 feature.O.icon.strokeColor = 'red';
471 this.isSearched = false;
474 feature.setProperty('color', 'gray');
475 if (feature.getProperty('icon') !== null) {
476 feature.O.icon.strokeColor = 'black';
480 this.newactive.next(this.activeId);
484 * gets the position of the clicked position on the map, saves the parent and sends it in an event.
488 let pos = this.getcurrentPos();
491 let parent = this.getParent();
493 let location = {lat: lat, lng: lng};
494 let event = {location, parent};
495 this.neworg.next(event);
497 this.setRunned(false);
501 * triggered from an event in search and gets the search object from the DHIS API
502 * then calles mapupdate()
503 * @param event - event from an emitter
506 this.newactive.next(event);
507 let map = this.getMap();
508 let http = this.getHttp();
510 map.data.forEach(function (feature) {
511 map.data.remove(feature);
513 http.get(dhisAPI + '/api/organisationUnits/' + event)
514 .map(res => res.json())
516 res => this.mapUpdate(res, this)
523 * updates varabels activeId, level and parent to matche the incomming object and gets all the children on the same level.
524 * Then it calles drawPolygon()
525 * @param res - org.unit object
528 mapUpdate(res, instance) {
529 this.setLevel(res.level);
530 this.setActiveId(res.id);
531 this.isSearched = true;
532 this.setParent(res.parent.id);
534 instance.getData('/' + res.parent.id + '/children', instance);
535 if (res.coordinates == null || instance.LEVEL == instance.allLevels) {
536 instance.http.get(dhisAPI + '/api/organisationUnits/' + res.parent.id)
537 .map(res => res.json())
539 res => instance.drawPolygon(res, instance)
546 * adds a temperary marker so the user can see an update of the latitude and longitude of a marker
547 * @param pos - position for the temp marker
550 if (this.currentMarker) {
551 this.currentMarker.setMap(null);
554 let current = {lat: null, lng: null};
555 current.lat = Math.round(this.getcurrentPos().lat() * 10000) / 10000;
556 current.lng = Math.round(this.getcurrentPos().lng() * 10000) / 10000;
558 let position = {lat: null, lng: null};
559 position.lat = Math.round(pos.lat * 10000) / 10000;
560 position.lng = Math.round(pos.lng * 10000) / 10000;
562 if ((current.lat != position.lat) || (current.lng != position.lng)) {
566 if (this.currentMarker)
567 this.currentMarker.setMap(null);
569 this.currentMarker = new google.maps.Marker({
574 path: google.maps.SymbolPath.CIRCLE,
579 this.currentMarker.setMap(map);
580 map.panTo(this.currentMarker.getPosition());
587 * closes the modal box over the map.
590 $("#myModal").modal("hide");
591 this.setRunned(false);