litte code cleanup
[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;
b27f57f1
JHR
85 let pos = {lat: 9.1, lng: -11.6};
86
87 map.setCenter(pos, 0);
88 map.setZoom(7);
89
cdcaf46c 90
a2d7d6b4
JHR
91 }
92
93 logError(error) {
94 console.error(error);
95
cb2c4ba8 96 }
28765058 97
65c7cdf5
JHR
98 getData(query, instance, isParent) {
99 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
6ced1bc7
JHR
100 .map(res => res.json())
101 .subscribe(
65c7cdf5 102 res => instance.parseResult(res, instance, isParent),
c7e8b786 103 error => instance.logError(error)
6ced1bc7 104 );
6ced1bc7
JHR
105 }
106
65c7cdf5 107 parseResult(res, instance, isParent) {
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
JHR
121 } else if (!res.displayName && res.children) {
122 for (let item in res.children) {
123 if (res.children[item].level == instance.LEVEL) {
124 this.getData('/' + res.children[item].id, this);
125 }
126 }
127 instance.setRunned(false);
128 instance.setupRunned(false);
b470b939 129 }
951b1c9f
JHR
130 else {
131 this.drawPolygon(res, instance);
132 }
03c7df04 133 }
6ced1bc7 134 }
951b1c9f 135
65c7cdf5 136 drawPolygon(item, instance) {
36f8898a 137 let bounds = new google.maps.LatLngBounds();
1f8c27ee 138 let feature;
65c7cdf5 139 let incoming:string;
1f8c27ee 140 incoming = item.featureType.toLowerCase();
65c7cdf5 141 switch (incoming) {
1f8c27ee
JHR
142 case "point":
143 feature = 'Point';
144 break;
145 case "multi_polygon":
146 feature = 'MultiPolygon';
147 break;
65c7cdf5
JHR
148 case "polygon":
149 feature = 'MultiPolygon';
1f8c27ee
JHR
150 break;
151 default:
152 }
65c7cdf5
JHR
153 // TODO: test på feature og behandle type: NONE
154 if (feature !== undefined) {
1f8c27ee
JHR
155 let unit = {
156 "type": "Feature",
157 "geometry": {
158 "type": feature,
159 "coordinates": JSON.parse(item.coordinates)
160 },
161 "properties": {
162 "name": item.name,
98cc809b 163 "id": item.id,
65c7cdf5
JHR
164 "color": "yellow",
165 }
1f8c27ee 166 };
65c7cdf5
JHR
167 if (unit.geometry.type == 'Point') {
168 //ToDO: add en style på markeren !
98cc809b 169
f3e550a1 170 }
65c7cdf5 171
1f8c27ee 172 this.map.data.addGeoJson(unit);
c7e8b786 173
65c7cdf5 174 this.map.data.addListener('click', function (event) {
951b1c9f 175
65c7cdf5
JHR
176 //TODO: spør om man vil ned/opp eller se info
177 //TODO: finne liste over alle levels slike at man ikke har hardkodet inn < 4 !!
a8ba81d3 178
65c7cdf5
JHR
179 if (instance.runned == false && instance.LEVEL < 4) {
180 instance.setRunned(true);
1f8c27ee 181
65c7cdf5
JHR
182 let infowindow = new google.maps.InfoWindow({
183 //TODO: Style this
184 content: '<div> <button >DrillUP</button>' +
185 ' <button ">DrillDOWN</button>' +
186 '<button ">SEEINFO</button></div>'
187 });
951b1c9f 188
65c7cdf5
JHR
189 infowindow.setPosition(event.latlng);
190 // infowindow.open(instance.map);
951b1c9f 191
65c7cdf5
JHR
192 let id = event.feature.O.id;
193 instance.setParent(id);
bbee9db0 194
65c7cdf5 195 instance.map.data.forEach(function (feature) {
365fd2e0 196 if (!(feature.O.id == id && instance.LEVEL == 3)) {
36f8898a 197 instance.map.data.remove(feature);
365fd2e0 198
65c7cdf5
JHR
199 }
200 });
03c7df04 201
65c7cdf5
JHR
202 instance.addLevel();
203 instance.getData('/' + id + '/children', instance);
204 } else if (instance.runned == false && instance.LEVEL >= 4) {
205 instance.setRunned(true);
206 let infowindowNew = new google.maps.InfoWindow({
207 //TODO: Style this
b27f57f1 208 content: '<div>Du you want to add a new OrgUnit here ? <button onclick="addUnit()">Yes</button></div>'
65c7cdf5
JHR
209 });
210 instance.setcurrentPos(event.latLng);
03c7df04 211
65c7cdf5
JHR
212 var marker = new google.maps.Marker({
213 position: event.latLng,
214 map: instance.map,
215 title: 'newOrg',
216 icon: {
217 path: google.maps.SymbolPath.CIRCLE,
218 scale: 5
219 }
03c7df04 220
65c7cdf5 221 });
03c7df04 222
65c7cdf5 223 marker.setMap(instance.map);
03c7df04 224
65c7cdf5 225 infowindowNew.open(instance.map, marker);
03c7df04 226
65c7cdf5
JHR
227 infowindowNew.addListener('closeclick', function (e) {
228 instance.setRunned(false);
229 marker.setMap(null);
230 });
03c7df04 231
65c7cdf5 232 instance.addUnit();
03c7df04 233
65c7cdf5 234 }
65c7cdf5 235 });
0a4273ac 236
bbee9db0 237
65c7cdf5
JHR
238 this.map.data.addListener('rightclick', function (event) {
239 if (instance.uprunned == false) {
951b1c9f 240 instance.setupRunned(true);
951b1c9f
JHR
241 instance.upLevel();
242
03c7df04 243 if (instance.LEVEL > 1) {
951b1c9f
JHR
244 instance.map.data.forEach(function (feature) {
245 instance.map.data.remove(feature);
246 });
03c7df04 247
951b1c9f 248 let parent = instance.getParent();
65c7cdf5 249 instance.getData('/' + parent, instance, true);
03c7df04 250 }
951b1c9f 251 else {
03c7df04
JHR
252 instance.addLevel();
253 instance.setupRunned(true);
951b1c9f
JHR
254 //TODO skriv en warning om at man ikke kan gå opp
255
65c7cdf5 256 }
951b1c9f
JHR
257 }
258 });
65c7cdf5 259 } else {
1f8c27ee
JHR
260 // ToDO:
261 console.log("fiks meg! gi warning på topp av kart");
262 }
6ced1bc7
JHR
263 }
264
65c7cdf5 265 addUnit() {
a8451d12 266 let parent = this.getParent();
9992f01a 267 let pos = this.getcurrentPos();
951b1c9f
JHR
268 let lat = pos.lat();
269 let lng = pos.lng()
65c7cdf5
JHR
270 let location = {lat: lat, lng: lng};
271 let event = {location, parent};
a8451d12 272 this.newOrg.next(event);
a8451d12 273 }
dd095993 274
65c7cdf5 275 update(event) {
7ee898bc 276 this.newactive.next(event);
36f8898a
JHR
277 let test = this.getMap();
278 let http = this.getHttp();
279
280 test.data.forEach(function (feature) {
281 test.data.remove(feature);
282 });
65c7cdf5 283 http.get(dhisAPI + '/api/organisationUnits/' + event)
36f8898a
JHR
284 .map(res => res.json())
285 .subscribe(
d647078e 286 res=> this.mapUpdate(res, this)
36f8898a 287 );
7ee898bc 288 }
d647078e 289
65c7cdf5 290 mapUpdate(res, instance) {
d647078e
JHR
291 this.setLevel(res.level);
292 this.setParent(res.parent.id);
65c7cdf5 293 this.drawPolygon(res, instance);
d647078e
JHR
294
295 }
296
6ced1bc7 297}
dd095993 298
dd095993
JHR
299
300
301
302