litte code cleanup
[u/erikhf/frm.git] / src / components / map / map.ts
1 import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
2 import {Headers, Http} from 'angular2/http';
3
4
5 @Component({
6     selector: 'mou-map',
7     directives: [CORE_DIRECTIVES],
8     events: ['newactive'],
9     templateUrl: './components/map/map.html'
10 })
11
12
13 export class Map {
14
15     map:Object;
16     http:Http;
17     LEVEL:number;
18     runned:boolean;
19     parent:Object;
20     currentPos:Object;
21     uprunned:boolean;
22
23     constructor(http:Http) {
24         this.newactive = new EventEmitter();
25         this.newOrg = new EventEmitter();
26         this.map = new google.maps.Map(document.getElementById("map"), {center: {lat: 0, lng: 0}, zoom: 12});
27         this.init();
28         this.http = http;
29         this.LEVEL = 2;
30         this.runned = false;
31         this.getData('?paging=false&level=2', this);
32         this.parent = null;
33         this.currentPos = null;
34         this.uprunned = false;
35
36     }
37
38     getMap() {
39         return this.map;
40     }
41
42     getHttp() {
43         return this.http;
44     }
45
46     setcurrentPos(latlng) {
47         this.currentPos = latlng;
48     }
49
50     getcurrentPos() {
51         return this.currentPos;
52     }
53
54     setParent(id) {
55         this.parent = id;
56     }
57
58     getParent() {
59         return this.parent;
60     }
61
62     setRunned(value) {
63         this.runned = value;
64     }
65
66     setupRunned(value) {
67         this.uprunned = value;
68     }
69
70     setLevel(value) {
71         this.LEVEL = value;
72     }
73
74     addLevel() {
75         this.LEVEL++;
76     }
77
78     upLevel() {
79         this.LEVEL--;
80     }
81
82     init() {
83
84         let map = this.map;
85         let pos = {lat: 9.1, lng: -11.6};
86
87         map.setCenter(pos, 0);
88         map.setZoom(7);
89
90
91     }
92
93     logError(error) {
94         console.error(error);
95
96     }
97
98     getData(query, instance, isParent) {
99         instance.http.get(dhisAPI + '/api/organisationUnits' + query)
100             .map(res => res.json())
101             .subscribe(
102                 res => instance.parseResult(res, instance, isParent),
103                 error => instance.logError(error)
104             );
105     }
106
107     parseResult(res, instance, isParent) {
108
109         if (isParent) {
110             instance.setParent(res.parent.id);
111             instance.getData('/' + res.parent.id + '/children', instance, false);
112         }
113         else {
114             if (res.organisationUnits) {
115                 for (let item in res.organisationUnits) {
116                     this.getData('/' + res.organisationUnits[item].id, this);
117
118                 }
119                 instance.setupRunned(false);
120                 instance.setRunned(false);
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);
129             }
130             else {
131                 this.drawPolygon(res, instance);
132             }
133         }
134     }
135
136     drawPolygon(item, instance) {
137         let bounds = new google.maps.LatLngBounds();
138         let feature;
139         let incoming:string;
140         incoming = item.featureType.toLowerCase();
141         switch (incoming) {
142             case "point":
143                 feature = 'Point';
144                 break;
145             case "multi_polygon":
146                 feature = 'MultiPolygon';
147                 break;
148             case "polygon":
149                 feature = 'MultiPolygon';
150                 break;
151             default:
152         }
153         // TODO: test på feature og behandle type: NONE
154         if (feature !== undefined) {
155             let unit = {
156                 "type": "Feature",
157                 "geometry": {
158                     "type": feature,
159                     "coordinates": JSON.parse(item.coordinates)
160                 },
161                 "properties": {
162                     "name": item.name,
163                     "id": item.id,
164                     "color": "yellow",
165                 }
166             };
167             if (unit.geometry.type == 'Point') {
168                 //ToDO: add en style på markeren !
169
170             }
171
172             this.map.data.addGeoJson(unit);
173
174             this.map.data.addListener('click', function (event) {
175
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 !!
178
179                 if (instance.runned == false && instance.LEVEL < 4) {
180                     instance.setRunned(true);
181
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                     });
188
189                     infowindow.setPosition(event.latlng);
190                     // infowindow.open(instance.map);
191
192                     let id = event.feature.O.id;
193                     instance.setParent(id);
194
195                     instance.map.data.forEach(function (feature) {
196                         if (!(feature.O.id == id && instance.LEVEL == 3)) {
197                             instance.map.data.remove(feature);
198
199                         }
200                     });
201
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
208                         content: '<div>Du you want to add a new OrgUnit here ?    <button onclick="addUnit()">Yes</button></div>'
209                     });
210                     instance.setcurrentPos(event.latLng);
211
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                         }
220
221                     });
222
223                     marker.setMap(instance.map);
224
225                     infowindowNew.open(instance.map, marker);
226
227                     infowindowNew.addListener('closeclick', function (e) {
228                         instance.setRunned(false);
229                         marker.setMap(null);
230                     });
231
232                     instance.addUnit();
233
234                 }
235             });
236
237
238             this.map.data.addListener('rightclick', function (event) {
239                 if (instance.uprunned == false) {
240                     instance.setupRunned(true);
241                     instance.upLevel();
242
243                     if (instance.LEVEL > 1) {
244                         instance.map.data.forEach(function (feature) {
245                             instance.map.data.remove(feature);
246                         });
247
248                         let parent = instance.getParent();
249                         instance.getData('/' + parent, instance, true);
250                     }
251                     else {
252                         instance.addLevel();
253                         instance.setupRunned(true);
254                         //TODO skriv en warning om at man ikke kan gå opp
255
256                     }
257                 }
258             });
259         } else {
260             // ToDO:
261             console.log("fiks meg! gi warning på topp av kart");
262         }
263     }
264
265     addUnit() {
266         let parent = this.getParent();
267         let pos = this.getcurrentPos();
268         let lat = pos.lat();
269         let lng = pos.lng()
270         let location = {lat: lat, lng: lng};
271         let event = {location, parent};
272         this.newOrg.next(event);
273     }
274
275     update(event) {
276         this.newactive.next(event);
277         let test = this.getMap();
278         let http = this.getHttp();
279
280         test.data.forEach(function (feature) {
281             test.data.remove(feature);
282         });
283         http.get(dhisAPI + '/api/organisationUnits/' + event)
284             .map(res => res.json())
285             .subscribe(
286                 res=> this.mapUpdate(res, this)
287             );
288     }
289
290     mapUpdate(res, instance) {
291         this.setLevel(res.level);
292         this.setParent(res.parent.id);
293         this.drawPolygon(res, instance);
294
295     }
296
297 }
298
299
300
301
302