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