-
Notifications
You must be signed in to change notification settings - Fork 1
/
beecontrol.js
574 lines (521 loc) · 19.1 KB
/
beecontrol.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
L.Control.BeeControl = L.Control.extend({
options: {
position: "topright" // default position of control
, r1: 3 // default radius of main area
, r2: 5 // default radius of wide area
, color: '#03f' // default color of areas
, useGeolocation: true // flag to disable geolocation
, instructiontext: "" // additional text for marker popup
, markerimage: 'beemarker.png' // default marker image
, markersize: [37, 65] // size of markerimage
, markeranchor: [18, 63] // anchor of markerimgae
, markerpopupanchor: [0, -63] // anchor of markerimage's popup
, maxBees: 5 // set maximum number of beehive positions, 0 for no limit
},
initialize: function(options) {
L.Util.setOptions(this, options);
this._beeIcon = L.icon({
iconUrl: this.options.markerimage
, iconSize: this.options.markersize
, iconAnchor: this.options.markeranchor
, popupAnchor: this.options.markerpopupanchor
});
this._r1_list = [1, 2, 2.5, 3, 3.5];
this._r2_list = [4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11];
this._bees = {}; // holds all bee location data objects
this._permalinkseparator = "'";
},
onAdd: function(map) {
this._countBees = 1;
this._initMarkerCalled = false;
this._initLayout();
this._map.on('updatebeecontrol', this._update_beecontrol, this);
this._map.on('locationfound', this._geolocationFound, this);
this._map.on('locationerror', this._geolocationError, this);
this._map.on('beecontroladdelement', this._addBeeElement, this);
return this._container;
},
/**
* Initializes a structure holding all data of one bee location.
* @returns object
*/
_initBeeData: function() {
var bee = {};
bee.centerChecked = false;
bee.center = null;
bee.innerChecked = false;
bee.innerRadius = this.options.r1;
bee.innerColor = this.options.color;
bee.innerCircle = null;
bee.outerChecked = false;
bee.outerRadius = this.options.r2;
bee.outerColor = this.options.color;
bee.outerCircle = null;
bee.marker = null;
return bee;
},
/**
* Create DOM for one beehive position in a compact way.
* @param object baseDiv container for new DOM elements
* @param integer counter consecutive number of beehive position elements
*/
_initElementCompact: function(baseDiv, counter) {
// add checkbox for bee location
var input = L.DomUtil.create('input', 'beecontrol-checkbox');
input.type = 'checkbox';
input.id = 'idBeeControlCenter_' + counter;
baseDiv.appendChild(input);
// add label for bee location
var name = L.DomUtil.create('label', 'beecontrol-label');
name.setAttribute('for', input.id);
name.innerHTML = (counter > 1 ? '' + counter + '. ' : '') + 'Bienenstandort';
baseDiv.appendChild(name);
L.DomEvent.on(input, 'click', this._onInputClickPosition, this);
L.DomUtil.create('br', '', baseDiv);
// add checkbox for main area
var input1 = L.DomUtil.create('input', 'beecontrol-checkbox');
input1.type = 'checkbox';
input1.id = 'idBeeControlInner_' + counter;
baseDiv.appendChild(input1);
L.DomEvent.on(input1, 'click', this._onInputClickRadius, this);
// add radius for main area
var select1 = L.DomUtil.create('select', 'beecontrol-select');
select1.id = 'idBeeControlRI_' + counter;
for (var i=0; i<this._r1_list.length; i++) {
var val = this._r1_list[i];
var opt = document.createElement('option');
opt.value = val;
opt.innerHTML = (val + ' km').replace('.', ',');
if (this.options.r1 == val) {
opt.selected = true;
}
select1.appendChild(opt);
}
baseDiv.appendChild(select1);
L.DomEvent.on(select1, 'change', this._onSelectRadius, this);
L.DomUtil.create('br', '', baseDiv);
// add checkbox for wide area
var input2 = L.DomUtil.create('input', 'beecontrol-checkbox');
input2.type = 'checkbox';
input2.id = 'idBeeControlOuter_' + counter;
baseDiv.appendChild(input2);
L.DomEvent.on(input2, 'click', this._onInputClickRadius, this);
// add radius for wide area
var select2 = L.DomUtil.create('select', 'beecontrol-select');
select2.id = 'idBeeControlRO_' + counter;
for (var i=0; i<this._r2_list.length; i++) {
var val = this._r2_list[i];
var opt = document.createElement('option');
opt.value = val;
opt.innerHTML = (val + ' km').replace('.', ',');
if (this.options.r2 == val) {
opt.selected = true;
}
select2.appendChild(opt);
}
baseDiv.appendChild(select2);
L.DomEvent.on(select2, 'change', this._onSelectRadius, this);
},
/**
* Create DOM for one beehive position in detail.
* @param object baseDiv container for new DOM elements
* @param integer counter consecutive number of beehive position elements
*/
_initElementDetailed: function(baseDiv, counter) {
// add checkbox for bee location
var input = L.DomUtil.create('input', 'beecontrol-checkbox');
input.type = 'checkbox';
input.id = 'idBeeControlCenter_' + counter;
baseDiv.appendChild(input);
// add label for bee location
var name = L.DomUtil.create('label', 'beecontrol-label');
name.setAttribute('for', input.id);
name.innerHTML = (counter > 1 ? '' + counter + '. ' : '') + 'Bienenstandort';
baseDiv.appendChild(name);
L.DomEvent.on(input, 'click', this._onInputClickPosition, this);
L.DomUtil.create('br', '', baseDiv);
// add checkbox for main area
var input1 = L.DomUtil.create('input', 'beecontrol-checkbox');
input1.type = 'checkbox';
input1.id = 'idBeeControlInner_' + counter;
baseDiv.appendChild(input1);
L.DomEvent.on(input1, 'click', this._onInputClickRadius, this);
// add label for main area
var name1 = L.DomUtil.create('label', 'beecontrol-label');
name1.setAttribute('for', input1.id);
name1.innerHTML = 'Hauptfluggebiet';
baseDiv.appendChild(name1);
L.DomUtil.create('br', '', baseDiv);
// add radius for main area
var dummy1 = L.DomUtil.create('input', 'beecontrol-checkbox', baseDiv);
dummy1.type = 'checkbox';
dummy1.style.visibility = 'hidden';
var nameS1 = L.DomUtil.create('span', 'beecontrol-label');
nameS1.innerHTML = 'Radius ';
baseDiv.appendChild(nameS1);
var select1 = L.DomUtil.create('select', 'beecontrol-select');
select1.id = 'idBeeControlRI_' + counter;
for (var i=0; i<this._r1_list.length; i++) {
var val = this._r1_list[i];
var opt = document.createElement('option');
opt.value = val;
opt.innerHTML = (val + ' km').replace('.', ',');
if (this.options.r1 == val) {
opt.selected = true;
}
select1.appendChild(opt);
}
baseDiv.appendChild(select1);
L.DomEvent.on(select1, 'change', this._onSelectRadius, this);
L.DomUtil.create('br', '', baseDiv);
// add checkbox for wide area
var input2 = L.DomUtil.create('input', 'beecontrol-checkbox');
input2.type = 'checkbox';
input2.id = 'idBeeControlOuter_' + counter;
baseDiv.appendChild(input2);
L.DomEvent.on(input2, 'click', this._onInputClickRadius, this);
// add label for wide area
var name2 = L.DomUtil.create('label', 'beecontrol-label');
name2.setAttribute('for', input2.id);
name2.innerHTML = 'Erreichbares Gebiet';
baseDiv.appendChild(name2);
L.DomUtil.create('br', '', baseDiv);
// add radius for wide area
var dummy2 = L.DomUtil.create('input', 'beecontrol-checkbox', baseDiv);
dummy2.type = 'checkbox';
dummy2.style.visibility = 'hidden';
var nameS2 = L.DomUtil.create('span', 'beecontrol-label');
nameS2.innerHTML = 'Radius ';
baseDiv.appendChild(nameS2);
var select2 = L.DomUtil.create('select', 'beecontrol-select');
select2.id = 'idBeeControlRO_' + counter;
for (var i=0; i<this._r2_list.length; i++) {
var val = this._r2_list[i];
var opt = document.createElement('option');
opt.value = val;
opt.innerHTML = (val + ' km').replace('.', ',');
if (this.options.r2 == val) {
opt.selected = true;
}
select2.appendChild(opt);
}
baseDiv.appendChild(select2);
L.DomEvent.on(select2, 'change', this._onSelectRadius, this);
},
/**
* Create DOM for one beehive position.
* @param object baseDiv container for new DOM elements
* @param integer counter consecutive number of beehive position elements
* @param boolean beCompact flag to use a compact view if true
*/
_initElement: function(baseDiv, counter, beCompact) {
var beeElement = L.DomUtil.create('div', 'beecontrol-element', baseDiv);
if (beCompact) {
this._initElementCompact(beeElement, counter);
} else {
this._initElementDetailed(beeElement, counter);
}
},
_addBeeElement: function() {
if (this.options.maxBees && this._countBees >= this.options.maxBees) {
// hide link for adding beehive positions if limit is reached
document.getElementById('beecontroladdline').style.display = 'none';
}
// initialize new bee data structure
this._bees['bee' + this._countBees] = this._initBeeData();
// get the container
var container = document.getElementById('idBeeElementContainer');
// add a bee element to the GUI
this._initElement(container, this._countBees++, true);
L.DomUtil.create('hr', 'beecontrol-hr', container);
},
_initLayout: function() {
this._container = L.DomUtil.create('div', 'beecontrol');
L.DomEvent.disableClickPropagation(this._container);
var heading = L.DomUtil.create('div', 'beecontrol-header', this._container);
heading.innerHTML = 'Flugbereich eines<br />Bienenvolkes';
var beeElements = L.DomUtil.create('div', 'beecontrol-elements', this._container);
beeElements.id = 'idBeeElementContainer';
this._initElement(beeElements, this._countBees++, false);
L.DomUtil.create('hr', 'beecontrol-hr', beeElements);
var linkDiv = L.DomUtil.create('div', 'beecontrol-linkdiv', this._container);
var addLine = L.DomUtil.create('label', 'beecontrol-line', linkDiv);
addLine.id = 'beecontroladdline'
var addLink = L.DomUtil.create('a', 'beecontrol-link');
addLink.innerHTML = 'Zusätzlicher Standort';
var _this = this; // need a copy of this for onclick event
addLink.onclick = function() { _this._map.fire('beecontroladdelement'); return false; }
addLink.setAttribute('href', 'index.html');
addLine.appendChild(addLink);
var resetLine = L.DomUtil.create('label', 'beecontrol-line', linkDiv);
var resetLink = L.DomUtil.create('a', 'beecontrol-link');
resetLink.innerHTML = 'Alles zurücksetzen';
resetLink.setAttribute('href', 'index.html');
resetLine.appendChild(resetLink);
},
/**
* A beehive position will be marked or unmarked as requested by user or programm.
* @param integer beenr the consecutive number of the beehive position
*/
_markPosition: function(beenr) {
var bee = typeof this._bees['bee' + beenr] == 'undefined' ? this._initBeeData() : this._bees['bee' + beenr];
var input = document.getElementById('idBeeControlCenter_' + beenr);
if (input.checked) {
bee.centerChecked = true;
if (!bee.center) {
bee.center = this._map.getCenter();
}
if (bee.marker && this._map.hasLayer(bee.marker)) {
this._map.removeLayer(bee.marker);
}
if (typeof bee.marker == 'undefined' || !bee.marker) {
bee.marker = L.marker(bee.center, {draggable: true, icon: this._beeIcon});
bee.marker._beenr = beenr;
bee.marker.bindPopup('Standort ' + beenr);
bee.marker.on('dragend', this._onMarkerDragend, this);
}
bee.marker.addTo(this._map);
} else {
this._map.removeLayer(bee.marker);
bee.marker = null;
bee.centerChecked = false;
if (!bee.innerCircle && !bee.outerCircle) {
bee.center = null;
}
}
this._bees['bee' + beenr] = bee;
// change popup of initial marker if additional markers are added
if (beenr != '1' && typeof this._bees.bee1 != 'undefined'
&& typeof this._bees.bee1.marker != 'undefined'
&& this._bees.bee1.marker) {
this._bees.bee1.marker.bindPopup('Standort 1');
}
},
/**
* Set or unset marker depending on input field change.
* @param object e event
*/
_onInputClickPosition: function(e) {
var beenr = e.target.id.split('_')[1];
this._markPosition(beenr);
this._map.fire('beecontrolchanged');
},
_drawRadius: function(beenr) {
var bee = this._bees['bee' + beenr];
// clean up inner circle
if (bee.innerCircle && this._map.hasLayer(bee.innerCircle)) {
this._map.removeLayer(bee.innerCircle);
bee.innerCircle = null;
}
// clean up outer circle
if (bee.outerCircle && this._map.hasLayer(bee.outerCircle)) {
this._map.removeLayer(bee.outerCircle);
bee.outerCircle = null;
}
// add inner circle if requested
if (!bee.center) {
bee.center = this._map.getCenter();
}
var input = document.getElementById('idBeeControlInner_' + beenr);
if (input && input.checked) {
bee.innerCircle = L.circle(bee.center, bee.innerRadius*1000, {clickable: true}).addTo(this._map);
}
// add outer circle if requested
input = document.getElementById('idBeeControlOuter_' + beenr);
if (input && input.checked) {
bee.outerCircle = L.circle(bee.center, bee.outerRadius*1000, {clickable: true}).addTo(this._map);
}
// clear center if it isn't needed any more
if (!bee.marker && !bee.innerCircle && !bee.outerCircle) {
bee.center = null;
}
},
_onSelectRadius: function(e) {
var beenr = e.target.id.split('_')[1];
var bee = this._bees['bee' + beenr];
var radiusType = e.target.id.split('_')[0];
radiusType = radiusType.substr(radiusType.length -1, 1);
if (radiusType == 'I') {
bee.innerRadius = e.target.value;
} else {
bee.outerRadius = e.target.value;
}
this._drawRadius(beenr);
this._map.fire('beecontrolchanged');
},
_onInputClickRadius: function(e) {
var beenr = e.target.id.split('_')[1];
if (e.target.id.substr(0, 17) == 'idBeeControlOuter') {
this._bees['bee' + beenr].outerChecked = e.target.checked;
} else if (e.target.id.substr(0, 17) == 'idBeeControlInner') {
this._bees['bee' + beenr].innerChecked = e.target.checked;
}
this._drawRadius(beenr);
this._map.fire('beecontrolchanged');
},
_onMarkerDragend: function(e) {
var marker = e.target;
var bee = this._bees['bee' + marker._beenr];
bee.center = marker.getLatLng();
this._drawRadius(marker._beenr);
this._map.fire('beecontrolchanged');
},
_getBeeFromOldUrlParams: function(params) {
// Example of old params: r1=1, r2=4, mlat=51.58, mlon=10.1, m=1, c1=1, c2=1
var bee = this._initBeeData();
var hasParams = false;
// marker position
if (params.mlat && params.mlon) {
bee.center = L.latLng(params.mlat, params.mlon);
hasParams = true;
}
// marker visibility
if (params.m && params.m == '1' && params.mlat && params.mlon) {
bee.centerChecked = true;
hasParams = true;
}
// main area
if (params.r1) {
// only radius values of our list are valid
for (var i=0; i<this._r1_list.length; i++) {
if (this._r1_list[i] == params.r1) {
bee.innerRadius = params.r1;
hasParams = true;
break;
}
}
}
if (!bee.innerRadius) {
// no valid radius? use default
bee.innerRadius = this.options.r1;
}
if (params.c1 && params.c1 == '1' && params.mlat && params.mlon) {
bee.innerChecked = true;
hasParams = true;
document.getElementById('idBeeControlOuter_1').checked = true;
}
document.getElementById('idBeeControlRI_1').value = bee.innerRadius;
// wide area
if (params.r2) {
// only radius values of our list are valid
for (var i=0; i<this._r1_list.length; i++) {
if (this._r2_list[i] == params.r2) {
bee.outerRadius = params.r2;
hasParams = true;
break;
}
}
}
if (!bee.outerRadius) {
// no valid radius? use default
bee.outerRadius = this.options.r2;
}
if (params.c2 && params.c2 == '1' && params.mlat && params.mlon) {
bee.outerChecked = true;
hasParams = true;
document.getElementById('idBeeControlInner_1').checked = true;
}
document.getElementById('idBeeControlRO_1').value = bee.outerRadius;
return hasParams ? bee : null;
},
_getBeesFromUrlParams: function(params) {
// TODO
},
/**
* Update beecontrol to use the provided url parameters.
* First check if old style parameters are used.
* If no oldstyle parameters are found check for new style parameters.
* @param object evnt event
*/
_update_beecontrol: function(evnt) {
if (typeof L.UrlUtil == 'undefined') {
// permalink is only available when permalink plugin is used
return
}
// first check if we got some old URL parameters
var bee = this._getBeeFromOldUrlParams(evnt.params);
if (bee) {
// some old bee data still there?
if (typeof this._bees.bee1 != 'undefined' && this._bees.bee1.marker && this._map.hasLayer(this_bees.bee1.marker)) {
this._map.removeLayer(this._bees.bee1.marker);
this._bee.bee1.marker = null;
}
this._bees.bee1 = bee;
this.initMarker(false, false);
return; // finish now, do not mix oldstyle and newstyle parameters
}
// TODO: check if we got some new style URL parameters
// TODO: use _getBeesFromUrlParams()
},
/**
* Set an initial marker. It is always bee1.
* @param boolean askGeolocation ask for geolocation if true
* @param boolean openPopup do not open a popup if false, defaults to true
*/
initMarker: function(askGeolocation, openPopup) {
if (this._initMarkerCalled) {
// do not accidentally init the marker again when it is already called by permalink
return;
}
this._initMarkerCalled = true;
var ask = typeof askGeolocation == 'undefined' ? true : !!askGeolocation;
ask = this.options.useGeolocation && ask && typeof navigator.geolocation != "undefined";
var open = typeof openPopup == 'undefined' ? true : !!openPopup;
var bee = (typeof this._bees.bee1 == 'undefined') ? this._initBeeData() : this._bees.bee1;
this._bees.bee1 = bee;
if (bee.marker && this._map.hasLayer(bee.marker)) {
this._map.removeLayer(bee.marker);
bee.marker = null;
}
bee.centerChecked = true;
document.getElementById('idBeeControlCenter_1').checked = true;
this._markPosition('1');
this._drawRadius('1');
var markerText = "Zieh' mich dorthin,<br />wo deine Bienen stehen.<br />"
// we still need the global variable "map" for starting geolocation
+ ((ask && typeof map != 'undefined')
? '(<a href="#" onClick="map.locate({timeout: 10000})">Oder lass mich heraus-<br />'
+ 'finden, wo du gerade bist</a>)<br /><br />'
: '')
+ this.options.instructiontext;
this._bees.bee1.marker.bindPopup(markerText);
if (open) {
this._bees.bee1.marker.openPopup();
}
},
/**
* Set an initial marker after calling geolocation. Don't ask for geolocation again.
*/
setMarkerAfterGeolocation: function() {
this._initMarkerCalled = false; // force re-initializing of the existing marker
this.initMarker(false);
},
/**
* Geolocation returned with an error.
* Set marker in the center of the map without asking again for geolocation.
*/
_geolocationError: function(msg) {
alert("Keine Ahnung, wo du steckst.\nDu musst den Bienenkorb leider selbst platzieren.");
this.setMarkerAfterGeolocation();
},
/**
* Geolocation returned with a position. Center the map on this position
* and set a marker when position is changed.
* @param object location data
*/
_geolocationFound: function(data) {
if (typeof this._map != "undefined") {
if (typeof this._bees.bee1 != 'undefined') {
this._bees.bee1.center = data.latlng;
}
this._map.addOneTimeEventListener('moveend', this.setMarkerAfterGeolocation, this)
this._map.setView(data.latlng, 13);
}
}
});
/**
* Shortcut function for creating an instance of BeeControl.
*/
L.control.beeControl = function(options) {
return new L.Control.BeeControl(options);
}