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'
24 constructor(http:Http) {
25 this.newactive = new EventEmitter();
26 this.newOrg = new EventEmitter();
27 this.map = new google.maps.Map(document.getElementById("map"), {center: {lat: 0, lng: 0}, zoom: 12});
32 this.getData('?paging=false&level=2', this);
34 this.currentPos = null;
35 this.uprunned = false;
36 this.hideModal = document.getElementById("divModal").style.visibility = "hidden";
40 return this.hideModal = document.getElementById("divModal").style.visibility = "visible";
45 return this.hideModal = document.getElementById("divModal").style.visibility = "hidden";
57 setcurrentPos(latlng) {
58 this.currentPos = latlng;
62 return this.currentPos;
78 this.uprunned = value;
96 let pos = {lat: 9.1, lng: -11.6};
98 map.setCenter(pos, 0);
105 console.error(error);
109 getData(query, instance, isParent) {
110 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
111 .map(res => res.json())
113 res => instance.parseResult(res, instance, isParent),
114 error => instance.logError(error)
118 parseResult(res, instance, isParent) {
121 instance.setParent(res.parent.id);
122 instance.getData('/' + res.parent.id + '/children', instance, false);
125 if (res.organisationUnits) {
126 for (let item in res.organisationUnits) {
127 this.getData('/' + res.organisationUnits[item].id, this);
130 instance.setupRunned(false);
131 instance.setRunned(false);
132 } else if (!res.displayName && res.children) {
133 for (let item in res.children) {
134 if (res.children[item].level == instance.LEVEL) {
135 this.getData('/' + res.children[item].id, this);
138 instance.setRunned(false);
139 instance.setupRunned(false);
142 this.drawPolygon(res, instance);
147 drawPolygon(item, instance) {
148 let bounds = new google.maps.LatLngBounds();
151 incoming = item.featureType.toLowerCase();
156 case "multi_polygon":
157 feature = 'MultiPolygon';
160 feature = 'MultiPolygon';
164 // TODO: test på feature og behandle type: NONE
165 if (feature !== undefined) {
170 "coordinates": JSON.parse(item.coordinates)
178 if (unit.geometry.type == 'Point') {
179 //ToDO: add en style på markeren !
183 this.map.data.addGeoJson(unit);
185 this.map.data.addListener('click', function (event) {
187 //TODO: spør om man vil ned/opp eller se info
188 //TODO: finne liste over alle levels slike at man ikke har hardkodet inn < 4 !!
190 if (instance.runned == false && instance.LEVEL < 4) {
191 instance.setRunned(true);
193 let infowindow = new google.maps.InfoWindow({
195 content: '<div> <button >DrillUP</button>' +
196 ' <button ">DrillDOWN</button>' +
197 '<button ">SEEINFO</button></div>'
200 infowindow.setPosition(event.latlng);
201 // infowindow.open(instance.map);
203 let id = event.feature.O.id;
204 instance.setParent(id);
206 instance.map.data.forEach(function (feature) {
207 if (!(feature.O.id == id && instance.LEVEL == 3)) {
208 instance.map.data.remove(feature);
214 instance.getData('/' + id + '/children', instance);
215 } else if (instance.runned == false && instance.LEVEL >= 4) {
216 instance.setRunned(true);
218 instance.setcurrentPos(event.latLng);
220 var marker = new google.maps.Marker({
221 position: event.latLng,
225 path: google.maps.SymbolPath.CIRCLE,
231 marker.setMap(instance.map);
232 instance.showModal();
241 this.map.data.addListener('rightclick', function (event) {
242 if (instance.uprunned == false) {
243 instance.setupRunned(true);
246 if (instance.LEVEL > 1) {
247 instance.map.data.forEach(function (feature) {
248 instance.map.data.remove(feature);
251 let parent = instance.getParent();
252 instance.getData('/' + parent, instance, true);
256 instance.setupRunned(true);
257 //TODO skriv en warning om at man ikke kan gå opp
264 console.log("fiks meg! gi warning på topp av kart");
270 let parent = this.getParent();
271 let pos = this.getcurrentPos();
274 let location = {lat: lat, lng: lng};
275 let event = {location, parent};
276 this.newOrg.next(event);
280 this.newactive.next(event);
281 let test = this.getMap();
282 let http = this.getHttp();
284 test.data.forEach(function (feature) {
285 test.data.remove(feature);
287 http.get(dhisAPI + '/api/organisationUnits/' + event)
288 .map(res => res.json())
290 res=> this.mapUpdate(res, this)
294 mapUpdate(res, instance) {
295 this.setLevel(res.level);
296 this.setParent(res.parent.id);
297 this.drawPolygon(res, instance);