]> git.uio.no Git - u/erikhf/frm.git/blobdiff - src/components/map/map.ts
[ExpandLevels] bug fixes, no expanding and going up works as it should
[u/erikhf/frm.git] / src / components / map / map.ts
index ebf20ba7cc7b2390d711cc8bff8905eb7ff7b17a..7639ee70fba6da55575182e23f55728e2d6f2ead 100644 (file)
@@ -1,10 +1,11 @@
-import {Component, CORE_DIRECTIVES,} from 'angular2/angular2';
+import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
 import {Headers, Http} from 'angular2/http';
 
 
 @Component({
     selector: 'mou-map',
     directives: [CORE_DIRECTIVES],
+    events: ['newactive'],
     templateUrl: './components/map/map.html'
 })
 
@@ -12,69 +13,75 @@ import {Headers, Http} from 'angular2/http';
 export class Map {
 
     map:Object;
-    marker:Object;
     http: Http;
-
+    LEVEL: number;
+    runned: boolean;
+    parent: Object;
+    currentPos : Object;
+    uprunned: boolean;
     constructor(http:Http) {
-
+        this.newactive = new EventEmitter();
+        this.newOrg = new EventEmitter();
         this.map = new google.maps.Map(document.getElementById("map"),{center: {lat:0,lng:0}, zoom:12});
         this.init();
         this.http = http;
-
+        this.LEVEL = 2;
+        this.runned = false;
         this.getData('?paging=false&level=2',this);
-    }
-
+        this.parent =null ;
+        this.currentPos = null;
+        this.uprunned = false;
 
-    init() {
+    }
 
-        let initMap = this.initMap;
-        let addMarker = this.addMarker;
-        let map = this.map;
-        if (navigator.geolocation) {
-            navigator.geolocation.getCurrentPosition(function (position) {
-                    let pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
-                    initMap(pos,map,addMarker);
-                }, function () {
-                //handleNoGeoLocation()
-                }
-            );
-        } else {
-            alert("You do not support geolocation");
-        }
+    setcurrentPos(latlng){
+        this.currentPos = latlng;
+    }
+     getcurrentPos(){
+         return this.currentPos;
+     }
 
+    setParent(id){
+        this.parent=id;
+    }
+    getParent(){
+        return this.parent;
 
     }
 
+    setRunned(value){
+        this.runned = value;
+    }
 
-    initMap(location,map,addMarker){
+    setupRunned(value){
+        this.uprunned = value;
+    }
 
-        map.setCenter(location,12);
-        addMarker(location,map,'This is YOU');
+    addLevel(){
+        this.LEVEL++;
+    }
+    upLevel(){
+        this.LEVEL--;
+    }
 
+    init() {
 
+        let initMap = this.initMap;
+        let instance = this;
+        let map = this.map;
 
-        map.addListener('click', function (event) {
-                addMarker(event.latLng,map, 'Want to add a new marker here ? <br> <button onclick=\"createOrgUnit()\">Yes</button> <button onclick=\"deleteMarker()">No</button> ');
-            }
-        );
+        let pos = {lat: 9.1, lng: -10.6};
+        initMap(pos,map,instance);
 
     }
 
-    addMarker(location, map, title) {
 
-        let marker = new google.maps.Marker({
-            position: location,
-            map: map
-        });
+    initMap(location,map,instance){
+        let add = instance.myFunction;
+
+        map.setCenter(location,12);
 
-        let infowindow = new google.maps.InfoWindow({
-            content: title
-        });
 
-        marker.addListener('click', function () {
-            console.log(marker);
-            infowindow.open(map,marker);
-        });
 
     }
 
@@ -83,35 +90,56 @@ export class Map {
 
     }
 
-    getData(query,instance){
-        console.log(instance.http);
+    getData(query,instance, isParent){
         instance.http.get(dhisAPI+'/api/organisationUnits'+query)
             .map(res => res.json())
             .subscribe(
-                res => instance.parseResult(res),
+                res => instance.parseResult(res,instance,isParent),
                 error => instance.logError(error)
+
             );
 
     }
 
-    parseResult(res){
+    parseResult(res,instance,isParent){
+        console.log("parent: "+isParent);
 
-        if(res.organisationUnits) {
-            for (let item in res.organisationUnits) {
-                this.getData('/' + res.organisationUnits[item].id,this);
+        if(isParent) {
+            console.log(instance.LEVEL);
+            console.log('/'+res.parent.id+'/children');
+            console.log(res);
+            instance.setParent(res.parent.id);
+            instance.getData('/'+res.parent.id+'/children',instance,false);
+        }
+        else{
+
+            console.log(res);
+            if (res.organisationUnits) {
+                for (let item in res.organisationUnits) {
+                    this.getData('/' + res.organisationUnits[item].id, this);
+
+                }
+                instance.setupRunned(false);
+                instance.setRunned(false);
+                //liten hack
+            } else if (!res.displayName && res.children) {
+                for (let item in res.children) {
+                    if (res.children[item].level == instance.LEVEL) {
+                        this.getData('/' + res.children[item].id, this);
+                    }
+                }
+                instance.setRunned(false);
+                instance.setupRunned(false);
+            }
+            else {
+                this.drawPolygon(res, instance);
             }
-            //liten hack
-        }//else if(res.name != false){
-           // for (let item in res.children) {
-             //   this.getData('/' + res.children[item].id,this);
-            //}
-        //}
-        else {
 
-            this.drawPolygon(res);};
+        }
+
     }
-    drawPolygon(item){
-        let instance = this;
+
+    drawPolygon(item, instance){
         let feature;
         let incoming: string;
         incoming = item.featureType.toLowerCase();
@@ -137,25 +165,105 @@ export class Map {
                 },
                 "properties": {
                     "name": item.name,
-                    "id": item.id
-                }
+                    "id": item.id,
+
+                },
+                "style": null
             };
+            if(unit.geometry.type == 'Point'){
+
+               //ToDO: add en style på markeren !
+
+            }
             this.map.data.addGeoJson(unit);
 
             this.map.data.addListener('click', function(event) {
                //TODO: spør om man vil ned/opp eller se info
+                //TODO: finne liste over alle levels slike at man ikke har hardkodet inn < 4 !!
+
+                console.log(instance.LEVEL);
+
+                if(instance.runned == false && instance.LEVEL < 4 ){
+                    instance.setRunned(true);
+
+                    let infowindow = new google.maps.InfoWindow({
+                        //TODO: Style this
+                        content:'<div> <button >DrillUP</button>' +
+                        ' <button ">DrillDOWN</button>' +
+                        '<button ">SEEINFO</button></div>'
+                    });
+
+                    infowindow.setPosition(event.latlng);
+                   // infowindow.open(instance.map);
+
+                    let id = event.feature.O.id;
+                    instance.setParent(id);
+                    console.log(id);
+
+                    instance.map.data.forEach(function(feature) {
+                        instance.map.data.remove(feature);
+                    });
+
+                    instance.addLevel();
+                    instance.getData('/' + id+'/children',instance);
+                }else if(instance.runned == false && instance.LEVEL >= 4){
+                        instance.setRunned(true);
+                    let infowindowNew = new google.maps.InfoWindow({
+                        //TODO: Style this
+                        content:'<div>Du you want to add a new OrgUnit here ?    <button onclick="myFunction()">Yes</button></div>'
+                    });
+                    instance.setcurrentPos(event.latLng);
+
+                    var marker = new google.maps.Marker({
+                        position: event.latLng,
+                        map: instance.map,
+                        title: 'newOrg',
+                        icon: {
+                            path: google.maps.SymbolPath.CIRCLE,
+                            scale: 5
+                        }
+
+                    });
 
-                let id = event.feature.O.id;
-                console.log(id);
+                    marker.setMap(instance.map);
 
-                instance.map.data.forEach(function(feature) {
-                    instance.map.data.remove(feature);
-                });
-               // instance.getData('/' + id+'/children',instance);
-                instance.getData('/' + id,instance);
+                    infowindowNew.open(instance.map, marker);
+
+                    infowindowNew.addListener('closeclick', function (e) {
+                        marker.setMap(null);
+                    });
+
+                    instance.addUnit();
+
+
+                }
 
             });
 
+            this.map.data.addListener('rightclick', function(event) {
+                if(instance.uprunned == false) {
+                    instance.setupRunned(true);
+
+                    instance.upLevel();
+
+                    if (instance.LEVEL > 1) {
+                        instance.map.data.forEach(function (feature) {
+                            instance.map.data.remove(feature);
+                        });
+
+                        let parent = instance.getParent();
+                        instance.getData('/'+parent, instance,true);
+                    }
+                    else {
+                        instance.addLevel();
+                        instance.setupRunned(true);
+                        //TODO skriv en warning om at man ikke kan gå opp
+
+                }
+
+                }
+            });
+
 
         }else {
             // ToDO:
@@ -165,16 +273,25 @@ export class Map {
 
     }
 
+    addUnit(){
 
-    createOrgUnit(){
-        console.log('you just added a new organisation unit');
-    }
+        let parent = this.getParent();
+        let pos = this.getcurrentPos();
+        let lat = pos.lat();
+        let lng = pos.lng()
+        let location= {lat: lat, lng: lng};
+        let event =  {location,parent};
+        this.newOrg.next(event);
 
-    deleteMarker(){
-        console.log('you just deleted the marker');
     }
 
+    myFunction(){
+        console.log("Inne i myfunksjonen");
+    }
 
+    update(event){
+        this.newactive.next(event);
+    }
 }