[drwparent] bug fix
[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;
65c7cdf5
JHR
16 http:Http;
17 LEVEL:number;
18 runned:boolean;
19 parent:Object;
20 currentPos:Object;
21 uprunned:boolean;
22
dd095993 23 constructor(http:Http) {
7ee898bc 24 this.newactive = new EventEmitter();
a8451d12 25 this.newOrg = new EventEmitter();
65c7cdf5 26 this.map = new google.maps.Map(document.getElementById("map"), {center: {lat: 0, lng: 0}, zoom: 12});
6ced1bc7
JHR
27 this.init();
28 this.http = http;
0d91e9f9
JHR
29 this.LEVEL = 2;
30 this.runned = false;
65c7cdf5
JHR
31 this.getData('?paging=false&level=2', this);
32 this.parent = null;
a8451d12 33 this.currentPos = null;
951b1c9f 34 this.uprunned = false;
9d471cab 35
a8451d12 36 }
9992f01a 37
65c7cdf5 38 getMap() {
36f8898a
JHR
39 return this.map;
40 }
41
65c7cdf5 42 getHttp() {
36f8898a
JHR
43 return this.http;
44 }
65c7cdf5
JHR
45
46 setcurrentPos(latlng) {
9992f01a
JHR
47 this.currentPos = latlng;
48 }
9992f01a 49
65c7cdf5
JHR
50 getcurrentPos() {
51 return this.currentPos;
52 }
53
54 setParent(id) {
55 this.parent = id;
a8451d12 56 }
9d471cab 57
65c7cdf5
JHR
58 getParent() {
59 return this.parent;
1e6ce2f5
EHF
60 }
61
65c7cdf5 62 setRunned(value) {
a8ba81d3
JHR
63 this.runned = value;
64 }
a2d7d6b4 65
65c7cdf5 66 setupRunned(value) {
951b1c9f
JHR
67 this.uprunned = value;
68 }
69
65c7cdf5 70 setLevel(value) {
d647078e
JHR
71 this.LEVEL = value;
72 }
65c7cdf5
JHR
73
74 addLevel() {
b470b939
JHR
75 this.LEVEL++;
76 }
65c7cdf5
JHR
77
78 upLevel() {
951b1c9f
JHR
79 this.LEVEL--;
80 }
b470b939 81
6ced1bc7 82 init() {
a2d7d6b4 83
6ced1bc7 84 let map = this.map;
9992f01a 85 let pos = {lat: 9.1, lng: -10.6};
cdcaf46c 86
65c7cdf5 87 map.setCenter(pos, 12);
a2d7d6b4
JHR
88 }
89
90 logError(error) {
91 console.error(error);
92
cb2c4ba8 93 }
28765058 94
65c7cdf5 95 getData(query, instance, isParent) {
365fd2e0 96 console.log("hoi");
65c7cdf5 97 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
6ced1bc7
JHR
98 .map(res => res.json())
99 .subscribe(
65c7cdf5 100 res => instance.parseResult(res, instance, isParent),
c7e8b786 101 error => instance.logError(error)
6ced1bc7 102 );
6ced1bc7
JHR
103 }
104
65c7cdf5 105 parseResult(res, instance, isParent) {
365fd2e0
JHR
106 console.log(res);
107
951b1c9f 108
65c7cdf5 109 if (isParent) {
03c7df04 110 instance.setParent(res.parent.id);
65c7cdf5 111 instance.getData('/' + res.parent.id + '/children', instance, false);
03c7df04 112 }
65c7cdf5 113 else {
951b1c9f
JHR
114 if (res.organisationUnits) {
115 for (let item in res.organisationUnits) {
116 this.getData('/' + res.organisationUnits[item].id, this);
117
b470b939 118 }
951b1c9f 119 instance.setupRunned(false);
03c7df04 120 instance.setRunned(false);
951b1c9f 121 } else if (!res.displayName && res.children) {
365fd2e0 122 console.log("children");
951b1c9f 123 for (let item in res.children) {
365fd2e0 124 console.log(res.children[item].level + " og " + instance.LEVEL);
951b1c9f
JHR
125 if (res.children[item].level == instance.LEVEL) {
126 this.getData('/' + res.children[item].id, this);
127 }
128 }
129 instance.setRunned(false);
130 instance.setupRunned(false);
b470b939 131 }
951b1c9f 132 else {
365fd2e0 133 console.log("jeei");
951b1c9f
JHR
134 this.drawPolygon(res, instance);
135 }
03c7df04 136 }
6ced1bc7 137 }
951b1c9f 138
65c7cdf5 139 drawPolygon(item, instance) {
36f8898a 140 let bounds = new google.maps.LatLngBounds();
1f8c27ee 141 let feature;
65c7cdf5 142 let incoming:string;
1f8c27ee 143 incoming = item.featureType.toLowerCase();
65c7cdf5 144 switch (incoming) {
1f8c27ee
JHR
145 case "point":
146 feature = 'Point';
147 break;
148 case "multi_polygon":
149 feature = 'MultiPolygon';
150 break;
65c7cdf5
JHR
151 case "polygon":
152 feature = 'MultiPolygon';
1f8c27ee
JHR
153 break;
154 default:
155 }
65c7cdf5
JHR
156 // TODO: test på feature og behandle type: NONE
157 if (feature !== undefined) {
1f8c27ee
JHR
158 let unit = {
159 "type": "Feature",
160 "geometry": {
161 "type": feature,
162 "coordinates": JSON.parse(item.coordinates)
163 },
164 "properties": {
165 "name": item.name,
98cc809b 166 "id": item.id,
65c7cdf5
JHR
167 "color": "yellow",
168 }
1f8c27ee 169 };
65c7cdf5
JHR
170 if (unit.geometry.type == 'Point') {
171 //ToDO: add en style på markeren !
98cc809b 172
f3e550a1 173 }
65c7cdf5 174
1f8c27ee 175 this.map.data.addGeoJson(unit);
c7e8b786 176
65c7cdf5 177 this.map.data.addListener('click', function (event) {
951b1c9f 178
65c7cdf5 179 console.log("klikket " + instance.runned + " og " + instance.LEVEL + " og " + event.feature.O.id);
365fd2e0 180
65c7cdf5
JHR
181 //TODO: spør om man vil ned/opp eller se info
182 //TODO: finne liste over alle levels slike at man ikke har hardkodet inn < 4 !!
a8ba81d3 183
65c7cdf5
JHR
184 if (instance.runned == false && instance.LEVEL < 4) {
185 instance.setRunned(true);
1f8c27ee 186
65c7cdf5
JHR
187 let infowindow = new google.maps.InfoWindow({
188 //TODO: Style this
189 content: '<div> <button >DrillUP</button>' +
190 ' <button ">DrillDOWN</button>' +
191 '<button ">SEEINFO</button></div>'
192 });
951b1c9f 193
65c7cdf5
JHR
194 infowindow.setPosition(event.latlng);
195 // infowindow.open(instance.map);
951b1c9f 196
65c7cdf5
JHR
197 let id = event.feature.O.id;
198 instance.setParent(id);
199 console.log(id);
bbee9db0 200
65c7cdf5 201 instance.map.data.forEach(function (feature) {
365fd2e0 202 if (!(feature.O.id == id && instance.LEVEL == 3)) {
36f8898a 203 instance.map.data.remove(feature);
365fd2e0 204
65c7cdf5
JHR
205 }
206 });
03c7df04 207
65c7cdf5 208 instance.addLevel();
365fd2e0 209
65c7cdf5
JHR
210 instance.getData('/' + id + '/children', instance);
211 } else if (instance.runned == false && instance.LEVEL >= 4) {
212 instance.setRunned(true);
213 let infowindowNew = new google.maps.InfoWindow({
214 //TODO: Style this
215 content: '<div>Du you want to add a new OrgUnit here ? <button onclick="myFunction()">Yes</button></div>'
216 });
217 instance.setcurrentPos(event.latLng);
03c7df04 218
65c7cdf5
JHR
219 var marker = new google.maps.Marker({
220 position: event.latLng,
221 map: instance.map,
222 title: 'newOrg',
223 icon: {
224 path: google.maps.SymbolPath.CIRCLE,
225 scale: 5
226 }
03c7df04 227
65c7cdf5 228 });
03c7df04 229
65c7cdf5 230 marker.setMap(instance.map);
03c7df04 231
65c7cdf5 232 infowindowNew.open(instance.map, marker);
03c7df04 233
65c7cdf5
JHR
234 infowindowNew.addListener('closeclick', function (e) {
235 instance.setRunned(false);
236 marker.setMap(null);
237 });
03c7df04 238
65c7cdf5 239 instance.addUnit();
03c7df04
JHR
240
241
65c7cdf5 242 }
36f8898a
JHR
243
244
65c7cdf5 245 });
0a4273ac 246
bbee9db0 247
65c7cdf5
JHR
248 this.map.data.addListener('rightclick', function (event) {
249 if (instance.uprunned == false) {
951b1c9f
JHR
250 instance.setupRunned(true);
251
252 instance.upLevel();
253
03c7df04 254 if (instance.LEVEL > 1) {
951b1c9f
JHR
255 instance.map.data.forEach(function (feature) {
256 instance.map.data.remove(feature);
257 });
03c7df04 258
951b1c9f 259 let parent = instance.getParent();
65c7cdf5 260 instance.getData('/' + parent, instance, true);
03c7df04 261 }
951b1c9f 262 else {
03c7df04
JHR
263 instance.addLevel();
264 instance.setupRunned(true);
951b1c9f
JHR
265 //TODO skriv en warning om at man ikke kan gå opp
266
65c7cdf5 267 }
951b1c9f
JHR
268
269 }
270 });
271
272
65c7cdf5 273 } else {
1f8c27ee
JHR
274 // ToDO:
275 console.log("fiks meg! gi warning på topp av kart");
276 }
6ced1bc7 277
6ced1bc7
JHR
278
279 }
280
65c7cdf5 281 addUnit() {
a8451d12 282 let parent = this.getParent();
9992f01a 283 let pos = this.getcurrentPos();
951b1c9f
JHR
284 let lat = pos.lat();
285 let lng = pos.lng()
65c7cdf5
JHR
286 let location = {lat: lat, lng: lng};
287 let event = {location, parent};
a8451d12 288 this.newOrg.next(event);
a8451d12 289 }
dd095993 290
65c7cdf5
JHR
291 //TODO slett denne når popup er klar !
292 myFunction() {
9992f01a 293 console.log("Inne i myfunksjonen");
6ced1bc7 294 }
dd095993 295
65c7cdf5 296 update(event) {
7ee898bc 297 this.newactive.next(event);
d647078e 298 let getResult = Object;
36f8898a
JHR
299 let test = this.getMap();
300 let http = this.getHttp();
301
302 test.data.forEach(function (feature) {
303 test.data.remove(feature);
304 });
65c7cdf5 305 http.get(dhisAPI + '/api/organisationUnits/' + event)
36f8898a
JHR
306 .map(res => res.json())
307 .subscribe(
d647078e 308 res=> this.mapUpdate(res, this)
36f8898a 309 );
7ee898bc 310 }
d647078e 311
65c7cdf5 312 mapUpdate(res, instance) {
d647078e
JHR
313 this.setLevel(res.level);
314 this.setParent(res.parent.id);
65c7cdf5 315 this.drawPolygon(res, instance);
d647078e
JHR
316
317 }
318
6ced1bc7 319}
dd095993 320
dd095993
JHR
321
322
323
324