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'
23 constructor(http:Http) {
24 this.newactive = new EventEmitter();
25 this.newOrg = new EventEmitter();
26 this.map = new google.maps.Map(document.getElementById("map"), {center: {lat: 0, lng: 0}, zoom: 12});
31 this.getData('?paging=false&level=2', this);
33 this.currentPos = null;
34 this.uprunned = false;
46 setcurrentPos(latlng) {
47 this.currentPos = latlng;
51 return this.currentPos;
67 this.uprunned = value;
85 let pos = {lat: 9.1, lng: -10.6};
87 map.setCenter(pos, 12);
95 getData(query, instance, isParent) {
97 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
98 .map(res => res.json())
100 res => instance.parseResult(res, instance, isParent),
101 error => instance.logError(error)
105 parseResult(res, instance, isParent) {
110 instance.setParent(res.parent.id);
111 instance.getData('/' + res.parent.id + '/children', instance, false);
114 if (res.organisationUnits) {
115 for (let item in res.organisationUnits) {
116 this.getData('/' + res.organisationUnits[item].id, this);
119 instance.setupRunned(false);
120 instance.setRunned(false);
121 } else if (!res.displayName && res.children) {
122 console.log("children");
123 for (let item in res.children) {
124 console.log(res.children[item].level + " og " + instance.LEVEL);
125 if (res.children[item].level == instance.LEVEL) {
126 this.getData('/' + res.children[item].id, this);
129 instance.setRunned(false);
130 instance.setupRunned(false);
134 this.drawPolygon(res, instance);
139 drawPolygon(item, instance) {
140 let bounds = new google.maps.LatLngBounds();
143 incoming = item.featureType.toLowerCase();
148 case "multi_polygon":
149 feature = 'MultiPolygon';
152 feature = 'MultiPolygon';
156 // TODO: test på feature og behandle type: NONE
157 if (feature !== undefined) {
162 "coordinates": JSON.parse(item.coordinates)
171 if (unit.geometry.type == 'Point') {
172 //ToDO: add en style på markeren !
174 instance.map.data.setStyle(function(feature) {
176 if (feature.getProperty('isColorful')) {
177 color = feature.getProperty('color');
180 return /** @type {google.maps.Data.StyleOptions} */({
189 this.map.data.addGeoJson(unit);
191 this.map.data.addListener('click', function (event) {
193 console.log("klikket " + instance.runned + " og " + instance.LEVEL + " og " + event.feature.O.id);
195 //TODO: spør om man vil ned/opp eller se info
196 //TODO: finne liste over alle levels slike at man ikke har hardkodet inn < 4 !!
198 if (instance.runned == false && instance.LEVEL < 4) {
199 instance.setRunned(true);
201 let infowindow = new google.maps.InfoWindow({
203 content: '<div> <button >DrillUP</button>' +
204 ' <button ">DrillDOWN</button>' +
205 '<button ">SEEINFO</button></div>'
208 infowindow.setPosition(event.latlng);
209 // infowindow.open(instance.map);
211 let id = event.feature.O.id;
212 instance.setParent(id);
215 instance.map.data.forEach(function (feature) {
216 if (!(feature.O.id == id && instance.LEVEL == 3)) {
217 instance.map.data.remove(feature);
224 instance.getData('/' + id + '/children', instance);
225 } else if (instance.runned == false && instance.LEVEL >= 4) {
226 instance.setRunned(true);
227 let infowindowNew = new google.maps.InfoWindow({
229 content: '<div>Du you want to add a new OrgUnit here ? <button onclick="myFunction()">Yes</button></div>'
231 instance.setcurrentPos(event.latLng);
233 var marker = new google.maps.Marker({
234 position: event.latLng,
238 path: google.maps.SymbolPath.CIRCLE,
244 marker.setMap(instance.map);
246 infowindowNew.open(instance.map, marker);
248 infowindowNew.addListener('closeclick', function (e) {
249 instance.setRunned(false);
262 this.map.data.addListener('rightclick', function (event) {
263 if (instance.uprunned == false) {
264 instance.setupRunned(true);
268 if (instance.LEVEL > 1) {
269 instance.map.data.forEach(function (feature) {
270 instance.map.data.remove(feature);
273 let parent = instance.getParent();
274 instance.getData('/' + parent, instance, true);
278 instance.setupRunned(true);
279 //TODO skriv en warning om at man ikke kan gå opp
289 console.log("fiks meg! gi warning på topp av kart");
296 let parent = this.getParent();
297 let pos = this.getcurrentPos();
300 let location = {lat: lat, lng: lng};
301 let event = {location, parent};
302 this.newOrg.next(event);
305 //TODO slett denne når popup er klar !
307 console.log("Inne i myfunksjonen");
311 this.newactive.next(event);
312 let getResult = Object;
313 let test = this.getMap();
314 let http = this.getHttp();
316 test.data.forEach(function (feature) {
317 test.data.remove(feature);
319 http.get(dhisAPI + '/api/organisationUnits/' + event)
320 .map(res => res.json())
322 res=> this.mapUpdate(res, this)
326 mapUpdate(res, instance) {
327 this.setLevel(res.level);
328 this.setParent(res.parent.id);
329 this.drawPolygon(res, instance);