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'
28 constructor(http:Http) {
31 this.newactive = new EventEmitter();
32 this.neworg = new EventEmitter();
33 this.map = new google.maps.Map(document.getElementById("map"), {
34 center: {lat: 0, lng: 0},
36 mapTypeControlOptions: {
37 position: google.maps.ControlPosition.BOTTOM_CENTER
40 position: google.maps.ControlPosition.LEFT_BOTTOM
42 streetViewControl: false
49 // this.getData('?paging=false&level=2', this);/////////////////////////////////////////////////////////
51 this.currentPos = null;
52 this.uprunned = false;
53 this.currentMarker = null;
54 // this.COLORS = {'red','brown',',yellow','green',',pink','purple','gray','black'};
55 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
56 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
57 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
58 this.hideModal = document.getElementById("divModal").style.display = "none";
73 setcurrentPos(latlng) {
74 this.currentPos = latlng;
78 return this.currentPos;
94 this.uprunned = value;
112 let pos = {lat: 9.1, lng: -11.6};
114 map.setCenter(pos, 0);
120 console.error(error);
124 getData(query, instance, isParent) {
125 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
126 .map(res => res.json())
128 res => instance.parseResult(res, instance, isParent),
129 error => instance.logError(error)
134 this.http.get(dhisAPI + '/api/organisationUnitLevels')
135 .map(res => res.json())
137 res => this.saveLevelTotalandGetdata(res,this),
138 err => this.logError(err)
142 saveLevelTotalandGetdata(res,instance){
143 instance.allLevels = res.pager.total;
144 instance.getData('?paging=false&level=2',instance,false);
147 parseResult(res, instance, isParent) {
149 instance.setParent(res.parent.id);
150 instance.getData('/' + res.parent.id + '/children', instance, false);
153 if (res.organisationUnits) {
154 for (let item in res.organisationUnits) {
155 this.getData('/' + res.organisationUnits[item].id, this);
158 instance.setupRunned(false);
159 instance.setRunned(false);
160 } else if (!res.displayName && res.children) {
161 for (let item in res.children) {
162 if (res.children[item].level == instance.LEVEL) {
163 this.getData('/' + res.children[item].id, this);
166 instance.setRunned(false);
167 instance.setupRunned(false);
170 this.drawPolygon(res, instance);
175 drawPolygon(item, instance) {
178 incoming = item.featureType.toLowerCase();
183 case "multi_polygon":
184 feature = 'MultiPolygon';
187 feature = 'MultiPolygon';
191 // TODO: test på feature og behandle type: NONE
192 if (feature !== undefined) {
197 "coordinates": JSON.parse(item.coordinates)
206 if (unit.geometry.type == 'Point') {
207 unit.properties.icon = {
208 path: google.maps.SymbolPath.CIRCLE,
209 strokeColor: 'black',
214 this.map.data.addGeoJson(unit);
215 this.map.data.setStyle(function (feature) {
218 if (feature.getProperty('icon') !== null) {
219 icon = feature.getProperty('icon');
221 color = feature.getProperty('color');
222 return /** @type {google.maps.Data.StyleOptions} */({
230 this.map.data.addListener('click', function (event) {
231 instance.setActiveId(event.feature.O.id);
232 instance.setcurrentPos(event.latLng);
234 if (instance.uprunned == false && instance.LEVEL == 2) {
235 this.hideModal = document.getElementById("topLevel").style.visibility = "visible";
236 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
237 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
238 instance.showModal();
241 else if (instance.runned == false && instance.LEVEL < instance.allLevels) {
242 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
243 this.hideModal = document.getElementById("middleLevel").style.visibility = "visible";
244 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
245 instance.showModal();
246 } else if (instance.runned == false && instance.LEVEL <= instance.allLevels) {
248 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
249 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
250 this.hideModal = document.getElementById("bottomLevel").style.visibility = "visible";
252 instance.setcurrentPos(event.latLng);
253 instance.showModal();
259 console.log("fiks meg! gi warning på topp av kart");
265 let map = this.getMap();
266 let id = this.activeId;
267 let level = this.LEVEL;
269 this.setRunned(true);
272 map.data.forEach(function (feature) {
273 if (!(feature.O.id == id && level == 3)) {
274 map.data.remove(feature);
280 this.getData('/' + id + '/children', this);
287 this.setupRunned(true);
291 this.map.data.forEach(function (feature) {
292 instance.map.data.remove(feature);
295 if(this.currentMarker !== null){
296 this.currentMarker.setMap(null);
298 let parent = instance.getParent();
299 instance.getData('/' + parent, instance, true);
305 let map = this.getMap();
306 let id = this.activeId;
308 map.data.forEach(function (feature) {
309 if (feature.getProperty('id') == id) {
310 feature.setProperty('color', 'red');
311 if (feature.getProperty('icon') !== null) {
312 feature.O.icon.strokeColor = 'red';
316 feature.setProperty('color', 'gray');
317 if (feature.getProperty('icon') !== null) {
318 feature.O.icon.strokeColor = 'black';
322 this.newactive.next(this.activeId);
327 let pos = this.getcurrentPos();
330 let parent = this.getParent();
332 let location = {lat: lat, lng: lng};
333 let event = {location, parent};
334 this.neworg.next(event);
336 this.setRunned(false);
340 this.newactive.next(event);
341 let map = this.getMap();
342 let http = this.getHttp();
344 map.data.forEach(function (feature) {
345 map.data.remove(feature);
347 http.get(dhisAPI + '/api/organisationUnits/' + event)
348 .map(res => res.json())
350 res=> this.mapUpdate(res, this)
354 mapUpdate(res, instance) {
355 this.setLevel(res.level);
356 this.setParent(res.parent.id);
357 this.drawPolygon(res, instance);
363 if (this.currentMarker)
364 this.currentMarker.setMap(null);
366 this.currentMarker = new google.maps.Marker({
371 path: google.maps.SymbolPath.CIRCLE,
375 this.currentMarker.setMap(map);
379 return this.hideModal = document.getElementById("divModal").style.display = "block";
383 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
384 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
385 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
386 this.hideModal = document.getElementById("divModal").style.display = "none";
388 this.setRunned(false);