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'
22 constructor(http:Http) {
23 this.newactive = new EventEmitter();
24 this.newOrg = new EventEmitter();
25 this.map = new google.maps.Map(document.getElementById("map"),{center: {lat:0,lng:0}, zoom:12});
30 this.getData('?paging=false&level=2',this);
32 this.currentPos = null;
33 this.uprunned = false;
44 setcurrentPos(latlng){
45 this.currentPos = latlng;
48 return this.currentPos;
52 console.log("satte parents");
65 this.uprunned = value;
81 let pos = {lat: 9.1, lng: -10.6};
83 map.setCenter(pos,12);
92 getData(query,instance, isParent){
93 instance.http.get(dhisAPI+'/api/organisationUnits'+query)
94 .map(res => res.json())
96 res => instance.parseResult(res,instance,isParent),
97 error => instance.logError(error)
103 parseResult(res,instance,isParent){
104 console.log("parent: "+isParent);
107 console.log(instance.LEVEL);
108 console.log('/'+res.parent.id+'/children');
110 instance.setParent(res.parent.id);
111 instance.getData('/'+res.parent.id+'/children',instance,false);
116 if (res.organisationUnits) {
117 for (let item in res.organisationUnits) {
118 this.getData('/' + res.organisationUnits[item].id, this);
121 instance.setupRunned(false);
122 instance.setRunned(false);
124 } else if (!res.displayName && res.children) {
125 for (let item in res.children) {
126 if (res.children[item].level == instance.LEVEL) {
127 this.getData('/' + res.children[item].id, this);
130 instance.setRunned(false);
131 instance.setupRunned(false);
134 this.drawPolygon(res, instance);
141 drawPolygon(item, instance){
142 console.log("tegne polygon");
143 let bounds = new google.maps.LatLngBounds();
145 let incoming: string;
146 incoming = item.featureType.toLowerCase();
151 case "multi_polygon":
152 feature = 'MultiPolygon';
155 feature = 'MultiPolygon';
159 // TODO: test på feature og behandle type: NONE
160 if(feature !== undefined) {
165 "coordinates": JSON.parse(item.coordinates)
174 if(unit.geometry.type == 'Point'){
176 //ToDO: add en style på markeren !
179 this.map.data.addGeoJson(unit);
181 this.map.data.addListener('click', function (event) {
182 //TODO: spør om man vil ned/opp eller se info
183 //TODO: finne liste over alle levels slike at man ikke har hardkodet inn < 4 !!
185 console.log(instance.LEVEL);
187 if (instance.runned == false && instance.LEVEL < 4) {
188 instance.setRunned(true);
190 let infowindow = new google.maps.InfoWindow({
192 content: '<div> <button >DrillUP</button>' +
193 ' <button ">DrillDOWN</button>' +
194 '<button ">SEEINFO</button></div>'
197 infowindow.setPosition(event.latlng);
198 // infowindow.open(instance.map);
200 let id = event.feature.O.id;
201 instance.setParent(id);
204 instance.map.data.forEach(function (feature) {
205 instance.map.data.remove(feature);
209 instance.getData('/' + id + '/children', instance);
210 } else if (instance.runned == false && instance.LEVEL >= 4) {
211 instance.setRunned(true);
212 let infowindowNew = new google.maps.InfoWindow({
214 content: '<div>Du you want to add a new OrgUnit here ? <button onclick="myFunction()">Yes</button></div>'
216 instance.setcurrentPos(event.latLng);
218 var marker = new google.maps.Marker({
219 position: event.latLng,
223 path: google.maps.SymbolPath.CIRCLE,
229 marker.setMap(instance.map);
231 infowindowNew.open(instance.map, marker);
233 infowindowNew.addListener('closeclick', function (e) {
246 this.map.data.addListener('rightclick', function(event) {
247 if(instance.uprunned == false) {
248 instance.setupRunned(true);
252 if (instance.LEVEL > 1) {
253 instance.map.data.forEach(function (feature) {
254 instance.map.data.remove(feature);
257 let parent = instance.getParent();
258 instance.getData('/'+parent, instance,true);
262 instance.setupRunned(true);
263 //TODO skriv en warning om at man ikke kan gå opp
273 console.log("fiks meg! gi warning på topp av kart");
281 let parent = this.getParent();
282 let pos = this.getcurrentPos();
285 let location= {lat: lat, lng: lng};
286 let event = {location,parent};
287 this.newOrg.next(event);
292 console.log("Inne i myfunksjonen");
297 this.newactive.next(event);
298 let getResult = Object;
299 let test = this.getMap();
300 let http = this.getHttp();
302 test.data.forEach(function (feature) {
303 test.data.remove(feature);
305 http.get(dhisAPI+'/api/organisationUnits/'+event)
306 .map(res => res.json())
308 res=> this.mapUpdate(res, this)
317 mapUpdate(res, instance){
318 console.log(res.level);
319 this.setLevel(res.level);
320 this.setParent(res.parent.id);
321 this.drawPolygon(res,instance);