1 import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
2 import {Headers, Http} from 'angular2/http';
7 directives: [CORE_DIRECTIVES],
9 templateUrl: './components/map/map.html'
26 constructor(http:Http) {
27 this.newactive = new EventEmitter();
28 this.newOrg = new EventEmitter();
29 this.map = new google.maps.Map(document.getElementById("map"), {center: {lat: 0, lng: 0}, zoom: 12});
34 this.getData('?paging=false&level=2', this);
36 this.currentPos = null;
37 this.uprunned = false;
38 // this.COLORS = {'red','brown',',yellow','green',',pink','purple','gray','black'};
39 this.hideModal = document.getElementById("divModal").style.visibility = "hidden";
40 this.expandModal = document.getElementById("expandModal").style.visibility = "hidden";
45 return this.hideModal = document.getElementById("divModal").style.visibility = "visible";
50 return this.hideModal = document.getElementById("divModal").style.visibility = "hidden";
55 return this.hideModal = document.getElementById("expandModal").style.visibility = "visible";
59 return this.hideModal = document.getElementById("expandModal").style.visibility = "hidden";
71 setcurrentPos(latlng) {
72 this.currentPos = latlng;
76 return this.currentPos;
92 this.uprunned = value;
110 let pos = {lat: 9.1, lng: -11.6};
112 map.setCenter(pos, 0);
119 console.error(error);
123 getData(query, instance, isParent) {
124 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
125 .map(res => res.json())
127 res => instance.parseResult(res, instance, isParent),
128 error => instance.logError(error)
132 parseResult(res, instance, isParent) {
135 instance.setParent(res.parent.id);
136 instance.getData('/' + res.parent.id + '/children', instance, false);
139 if (res.organisationUnits) {
140 for (let item in res.organisationUnits) {
141 this.getData('/' + res.organisationUnits[item].id, this);
144 instance.setupRunned(false);
145 instance.setRunned(false);
146 } else if (!res.displayName && res.children) {
147 for (let item in res.children) {
148 if (res.children[item].level == instance.LEVEL) {
149 this.getData('/' + res.children[item].id, this);
152 instance.setRunned(false);
153 instance.setupRunned(false);
156 this.drawPolygon(res, instance);
161 drawPolygon(item, instance) {
162 let bounds = new google.maps.LatLngBounds();
165 incoming = item.featureType.toLowerCase();
170 case "multi_polygon":
171 feature = 'MultiPolygon';
174 feature = 'MultiPolygon';
178 // TODO: test på feature og behandle type: NONE
179 if (feature !== undefined) {
184 "coordinates": JSON.parse(item.coordinates)
194 if (unit.geometry.type == 'Point') {
195 unit.properties.icon = {
196 path: google.maps.SymbolPath.CIRCLE,
202 this.map.data.addGeoJson(unit);
203 this.map.data.setStyle(function(feature) {
206 if (feature.getProperty('icon')!== null) {
207 icon = feature.getProperty('icon');
209 color = feature.getProperty('color');
210 return /** @type {google.maps.Data.StyleOptions} */({
219 this.map.data.addListener('click', function (event) {
221 //TODO: spør om man vil ned/opp eller se info
222 //TODO: finne liste over alle levels slike at man ikke har hardkodet inn < 4 !!
224 if (instance.runned == false && instance.LEVEL < 4) {
225 instance.setRunned(true);
227 instance.showExpandModal();
229 let id = event.feature.O.id;
230 instance.setParent(id);
232 instance.map.data.forEach(function (feature) {
233 if (!(feature.O.id == id && instance.LEVEL == 3)) {
234 instance.map.data.remove(feature);
240 instance.getData('/' + id + '/children', instance);
241 } else if (instance.runned == false && instance.LEVEL >= 4) {
242 instance.setRunned(true);
244 instance.setcurrentPos(event.latLng);
246 var marker = new google.maps.Marker({
247 position: event.latLng,
251 path: google.maps.SymbolPath.CIRCLE,
257 marker.setMap(instance.map);
258 instance.showModal();
266 this.map.data.addListener('rightclick', function (event) {
267 if (instance.uprunned == false) {
268 instance.setupRunned(true);
271 if (instance.LEVEL > 1) {
272 instance.map.data.forEach(function (feature) {
273 instance.map.data.remove(feature);
276 let parent = instance.getParent();
277 instance.getData('/' + parent, instance, true);
281 instance.setupRunned(true);
282 //TODO skriv en warning om at man ikke kan gå opp
290 console.log("fiks meg! gi warning på topp av kart");
295 let parent = this.getParent();
296 let pos = this.getcurrentPos();
299 let location = {lat: lat, lng: lng};
300 let event = {location, parent};
301 this.newOrg.next(event);
305 this.newactive.next(event);
306 let test = this.getMap();
307 let http = this.getHttp();
309 test.data.forEach(function (feature) {
310 test.data.remove(feature);
312 http.get(dhisAPI + '/api/organisationUnits/' + event)
313 .map(res => res.json())
315 res=> this.mapUpdate(res, this)
319 mapUpdate(res, instance) {
320 this.setLevel(res.level);
321 this.setParent(res.parent.id);
322 this.drawPolygon(res, instance);