[ExpandLevels] getting childs array from dhis api
[u/erikhf/frm.git] / src / components / map / map.ts
CommitLineData
7ee898bc 1import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
a2d7d6b4 2import {Headers, Http} from 'angular2/http';
cb2c4ba8 3
1e6ce2f5 4
cb2c4ba8 5@Component({
28765058 6 selector: 'mou-map',
cb2c4ba8 7 directives: [CORE_DIRECTIVES],
7ee898bc 8 events: ['newactive'],
28765058 9 templateUrl: './components/map/map.html'
cb2c4ba8
JHR
10})
11
12
28765058 13export class Map {
6ced1bc7 14
dd095993 15 map:Object;
6ced1bc7 16 http: Http;
0d91e9f9
JHR
17 LEVEL: number;
18 runned: boolean;
dd095993
JHR
19
20 constructor(http:Http) {
7ee898bc 21 this.newactive = new EventEmitter();
6ced1bc7
JHR
22 this.map = new google.maps.Map(document.getElementById("map"),{center: {lat:0,lng:0}, zoom:12});
23 this.init();
24 this.http = http;
0d91e9f9
JHR
25 this.LEVEL = 2;
26 this.runned = false;
c7e8b786 27 this.getData('?paging=false&level=2',this);
1e6ce2f5
EHF
28 }
29
a8ba81d3
JHR
30 setRunned(value){
31 this.runned = value;
32 }
a2d7d6b4 33
b470b939
JHR
34 addLevel(){
35 this.LEVEL++;
36 }
37
6ced1bc7 38 init() {
a2d7d6b4 39
6ced1bc7 40 let initMap = this.initMap;
6ced1bc7 41 let map = this.map;
dd095993
JHR
42 if (navigator.geolocation) {
43 navigator.geolocation.getCurrentPosition(function (position) {
0d91e9f9 44 //let pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
a8ba81d3 45 let pos = {lat:10,lng:39}
30593bb6 46 initMap(pos,map);
dd095993 47 }, function () {
bbee9db0 48 //handleNoGeoLocation()
dd095993
JHR
49 }
50 );
51 } else {
52 alert("You do not support geolocation");
53 }
a2d7d6b4 54
cdcaf46c 55
a2d7d6b4
JHR
56 }
57
dd095993 58
30593bb6 59 initMap(location,map){
6ced1bc7 60
b470b939 61 map.setCenter(location,3);
6ced1bc7
JHR
62
63
dd095993 64
6ced1bc7 65 map.addListener('click', function (event) {
0d91e9f9 66 console.log(event.latlng);
6ced1bc7
JHR
67 }
68 );
69
70 }
71
a2d7d6b4
JHR
72 logError(error) {
73 console.error(error);
74
cb2c4ba8 75 }
28765058 76
c7e8b786
EHF
77 getData(query,instance){
78 console.log(instance.http);
79 instance.http.get(dhisAPI+'/api/organisationUnits'+query)
6ced1bc7
JHR
80 .map(res => res.json())
81 .subscribe(
b470b939 82 res => instance.parseResult(res,instance),
c7e8b786 83 error => instance.logError(error)
6ced1bc7
JHR
84 );
85
86 }
87
b470b939 88 parseResult(res,instance){
0d91e9f9 89 console.log(res);
b470b939 90
6ced1bc7 91 if(res.organisationUnits) {
b470b939
JHR
92 console.log("inne i res.organisationUnits");
93 console.log(res instanceof JSONArray);
6ced1bc7 94 for (let item in res.organisationUnits) {
c7e8b786 95 this.getData('/' + res.organisationUnits[item].id,this);
6ced1bc7 96 }
0a4273ac 97 //liten hack
b470b939
JHR
98 }else if(res instanceof Array && res.children){
99 console.log("inne i res.children"+ res.organisationUnits+ " og " + res.children);
100 for (let item in res.children) {
101 console.log(res.children[item].level + " = " + instance.LEVEL + "skal jeg inn?");
102 if(res.children[item].level == instance.LEVEL){
103 console.log(res.children[item].level + " = " + instance.LEVEL + " så nå skal jeg inn");
104 this.getData('/' + res.children[item].id,this);
105 }
106 }
107 }
0a4273ac 108 else {
b470b939 109 console.log("tegner" + res[2]);
6ced1bc7
JHR
110 this.drawPolygon(res);};
111 }
112 drawPolygon(item){
c7e8b786 113 let instance = this;
1f8c27ee
JHR
114 let feature;
115 let incoming: string;
116 incoming = item.featureType.toLowerCase();
117 switch(incoming){
118 case "point":
119 feature = 'Point';
120 break;
121 case "multi_polygon":
122 feature = 'MultiPolygon';
123 break;
124 case "polygon":
125 feature = 'MultiPolygon';
126 break;
127 default:
128 }
129 // TODO: test på feature og behandle type: NONE
130 if(feature !== undefined) {
131 let unit = {
132 "type": "Feature",
133 "geometry": {
134 "type": feature,
135 "coordinates": JSON.parse(item.coordinates)
136 },
137 "properties": {
138 "name": item.name,
139 "id": item.id
140 }
141 };
142 this.map.data.addGeoJson(unit);
c7e8b786 143
1f8c27ee 144 this.map.data.addListener('click', function(event) {
0d91e9f9 145
bbee9db0 146 //TODO: spør om man vil ned/opp eller se info
0d91e9f9 147 if(instance.runned == false){
a8ba81d3
JHR
148 instance.setRunned(true);
149
1f8c27ee 150
0d91e9f9 151 let id = event.feature.O.id;
a8ba81d3 152 console.log(id);
bbee9db0 153
0d91e9f9
JHR
154 instance.map.data.forEach(function(feature) {
155 instance.map.data.remove(feature);
156 });
b470b939 157 instance.addLevel();
a8ba81d3
JHR
158 instance.getData('/' + id+'/children',instance);
159 // instance.getData('/' + id,instance);
6ced1bc7 160
0d91e9f9 161 }
0a4273ac 162
0d91e9f9 163 });
bbee9db0 164
1f8c27ee
JHR
165 }else {
166 // ToDO:
167 console.log("fiks meg! gi warning på topp av kart");
168 }
6ced1bc7 169
6ced1bc7
JHR
170
171 }
172
dd095993 173
6ced1bc7
JHR
174 createOrgUnit(){
175 console.log('you just added a new organisation unit');
176 }
dd095993 177
7ee898bc
EHF
178 update(event){
179 this.newactive.next(event);
180 }
6ced1bc7 181}
dd095993 182
dd095993
JHR
183
184
185
186