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