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