merged AddorgUnit into ExpandLevels
[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
JHR
19 parent: Object;
20 currentPos = Object;
dd095993
JHR
21
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;
9d471cab 33
a8451d12 34 }
9992f01a
JHR
35
36 setcurrentPos(latlng){
37 this.currentPos = latlng;
38 }
39 getcurrentPos(){
40 return this.currentPos;
41 }
42
a8451d12
JHR
43 setParent(id){
44 this.parent=id;
45 }
46 getParent(){
47 return this.parent;
9d471cab 48
1e6ce2f5
EHF
49 }
50
a8ba81d3
JHR
51 setRunned(value){
52 this.runned = value;
53 }
a2d7d6b4 54
b470b939
JHR
55 addLevel(){
56 this.LEVEL++;
57 }
58
6ced1bc7 59 init() {
a2d7d6b4 60
6ced1bc7 61 let initMap = this.initMap;
71cd4bde 62 let instance = this;
6ced1bc7 63 let map = this.map;
a2d7d6b4 64
9992f01a 65 let pos = {lat: 9.1, lng: -10.6};
71cd4bde 66 initMap(pos,map,instance);
cdcaf46c 67
a2d7d6b4
JHR
68 }
69
dd095993 70
9992f01a 71 initMap(location,map,instance){
6ced1bc7 72
6ced1bc7 73
9992f01a 74 map.setCenter(location,2);
9d471cab 75
a8451d12 76 let infowindow = new google.maps.InfoWindow({
9992f01a 77 //TODO: Style this
71cd4bde 78 content:'<div>Du you want to add a new OrgUnit here ? <button onclick="instance.myFunction()">Yes</button></div>'
a8451d12
JHR
79 });
80 map.addListener('click', function (e) {
71cd4bde
JHR
81 instance.setcurrentPos(e.latLng);
82
83 instance.myFunction();
a8451d12
JHR
84 var marker = new google.maps.Marker({
85 position: e.latLng,
86 map: map,
87 title: 'newOrg',
88 icon: {
89 path: google.maps.SymbolPath.CIRCLE,
90 scale: 5
91 }
92
93 });
94 marker.setMap(map);
6ced1bc7 95
a8451d12 96 infowindow.open(map, marker);
6ced1bc7 97
9992f01a
JHR
98 infowindow.addListener('closeclick', function (e) {
99 marker.setMap(null);
100 });
6ced1bc7 101
dd095993 102
6ced1bc7
JHR
103 }
104 );
105
106 }
107
a2d7d6b4
JHR
108 logError(error) {
109 console.error(error);
110
cb2c4ba8 111 }
28765058 112
c7e8b786
EHF
113 getData(query,instance){
114 console.log(instance.http);
115 instance.http.get(dhisAPI+'/api/organisationUnits'+query)
6ced1bc7
JHR
116 .map(res => res.json())
117 .subscribe(
b470b939 118 res => instance.parseResult(res,instance),
c7e8b786 119 error => instance.logError(error)
6ced1bc7
JHR
120 );
121
122 }
123
b470b939 124 parseResult(res,instance){
b470b939 125
6ced1bc7
JHR
126 if(res.organisationUnits) {
127 for (let item in res.organisationUnits) {
c7e8b786 128 this.getData('/' + res.organisationUnits[item].id,this);
6ced1bc7 129 }
0a4273ac 130 //liten hack
f3e550a1 131 }else if(!res.displayName && res.children){
b470b939 132 for (let item in res.children) {
b470b939 133 if(res.children[item].level == instance.LEVEL){
b470b939
JHR
134 this.getData('/' + res.children[item].id,this);
135 }
136 }
f3e550a1 137 instance.setRunned(false);
b470b939 138 }
0a4273ac 139 else {
f3e550a1
JHR
140 this.drawPolygon(res);
141 };
6ced1bc7
JHR
142 }
143 drawPolygon(item){
c7e8b786 144 let instance = this;
1f8c27ee
JHR
145 let feature;
146 let incoming: string;
147 incoming = item.featureType.toLowerCase();
148 switch(incoming){
149 case "point":
150 feature = 'Point';
151 break;
152 case "multi_polygon":
153 feature = 'MultiPolygon';
154 break;
155 case "polygon":
156 feature = 'MultiPolygon';
157 break;
158 default:
159 }
160 // TODO: test på feature og behandle type: NONE
161 if(feature !== undefined) {
162 let unit = {
163 "type": "Feature",
164 "geometry": {
165 "type": feature,
166 "coordinates": JSON.parse(item.coordinates)
167 },
168 "properties": {
169 "name": item.name,
98cc809b
JHR
170 "id": item.id,
171
172 },
173 "style": null
1f8c27ee 174 };
f3e550a1 175 if(unit.geometry.type == 'Point'){
98cc809b
JHR
176 //ToDO: add en style på markeren !
177
178
f3e550a1 179 }
1f8c27ee 180 this.map.data.addGeoJson(unit);
c7e8b786 181
1f8c27ee 182 this.map.data.addListener('click', function(event) {
bbee9db0 183 //TODO: spør om man vil ned/opp eller se info
0d91e9f9 184 if(instance.runned == false){
a8ba81d3
JHR
185 instance.setRunned(true);
186
1f8c27ee 187
bbee9db0 188 let id = event.feature.O.id;
a8451d12 189 instance.setParent(id);
bbee9db0
JHR
190 console.log(id);
191
bbee9db0 192
0d91e9f9
JHR
193 instance.map.data.forEach(function(feature) {
194 instance.map.data.remove(feature);
195 });
b470b939 196 instance.addLevel();
a8ba81d3 197 instance.getData('/' + id+'/children',instance);
0d91e9f9 198 }
0a4273ac 199
0d91e9f9 200 });
bbee9db0 201
1f8c27ee
JHR
202 }else {
203 // ToDO:
204 console.log("fiks meg! gi warning på topp av kart");
205 }
6ced1bc7 206
6ced1bc7
JHR
207
208 }
209
a8451d12
JHR
210 addUnit(){
211 console.log("Inne i Add funksjonen");
212 let parent = this.getParent();
9992f01a 213 let pos = this.getcurrentPos();
a8451d12
JHR
214 let event = {pos,parent};
215 this.newOrg.next(event);
216
217 }
dd095993 218
9992f01a
JHR
219 myFunction(){
220 console.log("Inne i myfunksjonen");
71cd4bde 221
dd095993 222
6ced1bc7 223 }
dd095993 224
7ee898bc
EHF
225 update(event){
226 this.newactive.next(event);
227 }
6ced1bc7 228}
dd095993 229
dd095993
JHR
230
231
232
233