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