@raruto/leaflet-elevation 2.0.6 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/control.js CHANGED
@@ -1,7 +1,11 @@
1
- import 'leaflet-i18n';
2
1
  import * as _ from './utils';
3
2
  import { Options } from './options';
4
3
 
4
+ // "leaflet-i18n" fallback
5
+ if (!L._ || !L.i18n) {
6
+ L._ = L.i18n = (string, data) => string;
7
+ }
8
+
5
9
  export const Elevation = L.Control.Elevation = L.Control.extend({
6
10
 
7
11
  includes: L.Evented ? L.Evented.prototype : L.Mixin.Events,
@@ -55,7 +59,8 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
55
59
  if (this._chart) this._clearChart();
56
60
  if (this._layers) this._clearLayers(this._layers);
57
61
  if (this._markers) this._clearLayers(this._markers);
58
- if (this._circleMarkers) this._clearLayers(this._circleMarkers);
62
+ if (this._circleMarkers) this._circleMarkers.remove();
63
+ if (this._hotline) this._clearLayers(this._hotline);
59
64
 
60
65
  this._data = [];
61
66
  this.track_info = {};
@@ -165,6 +170,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
165
170
  this._data = [];
166
171
  this._layers = L.featureGroup();
167
172
  this._markers = L.featureGroup();
173
+ this._hotline = L.featureGroup();
168
174
  this._circleMarkers = L.featureGroup();
169
175
  this._markedSegments = L.polyline([]);
170
176
  this._start = (opts.trkStart || Options.trkStart);
@@ -236,12 +242,15 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
236
242
  ...this.options.handlers
237
243
  ].filter(m => false !== this.options[typeof m == "string" && m.toLowerCase()]);
238
244
 
239
- this._loadModules(modules); // Inject here required modules (data handlers)
240
- this._initButton(container);
241
- this._initChart(container);
242
- this._initSummary(container);
243
- this._initMarker(map);
244
- this._initLayer(map);
245
+ this._loadModules(modules).then(() => { // Inject here required modules (data handlers)
246
+ this._initChart(container);
247
+ this._initButton(container);
248
+ this._initSummary(container);
249
+ this._initMarker(map);
250
+ this._initLayer(map);
251
+ });
252
+
253
+ this.fire('add');
245
254
 
246
255
  return container;
247
256
  },
@@ -266,6 +275,8 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
266
275
  this
267
276
  .off('eledata_added eledata_loaded', this._updateChart, this)
268
277
  .off('eledata_added eledata_loaded', this._updateSummary, this);
278
+
279
+ this.fire('remove');
269
280
  },
270
281
 
271
282
  /**
@@ -386,8 +397,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
386
397
  * Initialize "L.AlmostOver" integration
387
398
  */
388
399
  _initAlmostOverHandler: function(map, layer) {
389
- if(!map || !this.options.almostOver) return;
390
- Promise.all([
400
+ return (map && this.options.almostOver && !L.Browser.mobile) ? Promise.all([
391
401
  this.import(this.__LGEOMUTIL),
392
402
  this.import(this.__LALMOSTOVER)
393
403
  ]).then(() => {
@@ -398,77 +408,72 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
398
408
  .on('almost:move', (e) => this._onMouseMoveLayer(e))
399
409
  .on('almost:out', (e) => this._onMouseOut(e));
400
410
  }
401
- });
411
+ }) : Promise.resolve();
402
412
  },
403
413
 
404
414
  /**
405
415
  * Initialize "L.DistanceMarkers" integration
406
416
  */
407
- _initDistanceMarkers: function(map, layer) {
408
- if (!map) return;
409
- if (this.options.distanceMarkers) {
410
- Promise.all([
411
- this.import(this.__LGEOMUTIL),
412
- this.import(this.__LDISTANCEM)
413
- ]).then(() => this.options.polyline && layer.addTo(map) && this._circleMarkers.bringToFront());
414
- } else if (this.options.polyline) {
415
- layer.addTo(map);
416
- }
417
+ _initDistanceMarkers: function() {
418
+ return this.options.distanceMarkers ? Promise.all([this.import(this.__LGEOMUTIL), this.import(this.__LDISTANCEM)]) : Promise.resolve();
417
419
  },
418
420
 
419
- _initHotLine: function(map, layer) {
420
- if (this.options.hotline) {
421
- let prop = typeof this.options.hotline == 'string' ? this.options.hotline : 'elevation';
422
- this.import(this.__LHOTLINE)
423
- .then(() => {
424
- this._hotline = L.featureGroup();
425
- layer.setStyle({opacity: 0});
426
- layer.eachLayer((trkseg) => {
427
- if(trkseg.feature.geometry.type != "Point") {
428
- let geo = L.geoJson(trkseg.toGeoJSON(), { coordsToLatLng: (coords) => L.latLng(coords[0], coords[1], coords[2])});
429
- L.hotline(geo.toGeoJSON().features[0].geometry.coordinates, {
430
- min: isFinite(this.track_info[prop + '_min']) ? this.track_info[prop + '_min'] : 0,
431
- max: isFinite(this.track_info[prop + '_max']) ? this.track_info[prop + '_max'] : 1,
432
- palette: {
433
- 0.0: '#008800',
434
- 0.5: '#ffff00',
435
- 1.0: '#ff0000'
436
- },
437
- weight: 5,
438
- outlineColor: '#000000',
439
- outlineWidth: 1
440
- }).addTo(this._hotline);
441
- }
442
- });
443
- if (map) {
444
- this._hotline.addTo(map);
445
- }
446
- });
447
- }
421
+ _initHotLine: function(layer) {
422
+ let prop = typeof this.options.hotline == 'string' ? this.options.hotline : 'elevation';
423
+ return this.options.hotline ? this.import(this.__LHOTLINE)
424
+ .then(() => {
425
+ layer.eachLayer((trkseg) => {
426
+ if(trkseg.feature.geometry.type != "Point") {
427
+ let geo = L.geoJson(trkseg.toGeoJSON(), { coordsToLatLng: (coords) => L.latLng(coords[0], coords[1], coords[2])});
428
+ let line = L.hotline(geo.toGeoJSON().features[0].geometry.coordinates, {
429
+ min: isFinite(this.track_info[prop + '_min']) ? this.track_info[prop + '_min'] : 0,
430
+ max: isFinite(this.track_info[prop + '_max']) ? this.track_info[prop + '_max'] : 1,
431
+ palette: {
432
+ 0.0: '#008800',
433
+ 0.5: '#ffff00',
434
+ 1.0: '#ff0000'
435
+ },
436
+ weight: 5,
437
+ outlineColor: '#000000',
438
+ outlineWidth: 1
439
+ }).addTo(this._hotline);
440
+ let alpha = trkseg.options.style && trkseg.options.style.opacity || 1;
441
+ trkseg.on('add remove', ({type}) => {
442
+ trkseg.setStyle({opacity: (type == 'add' ? 0 : alpha)});
443
+ line[(type == 'add' ? 'addTo' : 'removeFrom')](trkseg._map);
444
+ if (line._renderer) line._renderer._container.parentElement.insertBefore(line._renderer._container, line._renderer._container.parentElement.firstChild);
445
+ });
446
+ }
447
+ });
448
+ }) : Promise.resolve();
448
449
  },
449
450
 
450
451
  /**
451
452
  * Initialize "L.AlmostOver" and "L.DistanceMarkers"
452
453
  */
453
- _initMapIntegrations: function(control, layer) {
454
- let map = control._map;
455
- if (control._data.length) {
456
- control._start.setLatLng(control._data[0].latlng);
457
- control._end.setLatLng(control._data[control._data.length -1].latlng);
458
- }
454
+ _initMapIntegrations: function(layer) {
455
+ let map = this._map;
459
456
  if (map) {
460
- control._circleMarkers.addTo(map);
461
- map.once('layeradd', (e) => control.options.autofitBounds && control.fitBounds(layer.getBounds()));
462
- if (!L.Browser.mobile) {
463
- control._initHotLine(map, layer);
464
- control._initAlmostOverHandler(map, layer);
465
- control._initDistanceMarkers(map, layer);
466
- } else if (control.options.polyline) {
467
- layer.addTo(map);
457
+ if (this._data.length) {
458
+ this._start.setLatLng(this._data[0].latlng);
459
+ this._end.setLatLng(this._data[this._data.length -1].latlng);
468
460
  }
469
- map.invalidateSize();
461
+ Promise.all([
462
+ this._initHotLine(layer),
463
+ this._initAlmostOverHandler(map, layer),
464
+ this._initDistanceMarkers(),
465
+ ]).then(() => {
466
+ if (this.options.polyline) {
467
+ layer.addTo(map);
468
+ this._circleMarkers.addTo(map);
469
+ }
470
+ if (this.options.autofitBounds) {
471
+ this.fitBounds(layer.getBounds());
472
+ }
473
+ map.invalidateSize();
474
+ });
470
475
  } else {
471
- console.warn("Undefined elevation map object");;
476
+ this.once('add', () => this._initMapIntegrations(layer));
472
477
  }
473
478
  },
474
479
 
@@ -637,6 +642,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
637
642
  this._updateSummary();
638
643
  });
639
644
 
645
+
640
646
  },
641
647
 
642
648
  _initLayer: function() {
@@ -673,12 +679,9 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
673
679
  * Inspired by L.Control.Layers
674
680
  */
675
681
  _initButton: function(container) {
676
-
677
- if (L.Browser.mobile || !this.options.detached) {
678
- L.DomEvent
679
- .disableClickPropagation(container)
680
- .disableScrollPropagation(container);
681
- }
682
+ L.DomEvent
683
+ .disableClickPropagation(container)
684
+ .disableScrollPropagation(container);
682
685
 
683
686
  this.options.collapsed ? this._collapse() : this._expand();
684
687
 
@@ -726,7 +729,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
726
729
  // First map known classnames (eg. "Altitude" --> L.Control.Elevation.Altitude)
727
730
  handlers = handlers.map((h) => typeof h === 'string' && typeof Elevation[h] !== "undefined" ? Elevation[h] : h);
728
731
  // Then load optional classes and custom imports (eg. "Cadence" --> import('../src/handlers/cadence.js'))
729
- Promise.
732
+ return Promise.
730
733
  all(
731
734
  handlers
732
735
  .filter(h => typeof h === 'string' || h instanceof Promise)
@@ -788,7 +791,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
788
791
  });
789
792
 
790
793
  this.import(this.__D3).then(() => {
791
- this._initMapIntegrations(this, layer);
794
+ this._initMapIntegrations(layer);
792
795
  this._fireEvt("eledata_loaded", { data: geojson, layer: layer, name: this.track_info.name, track_info: this.track_info });
793
796
  });
794
797
 
package/src/index.js CHANGED
@@ -34,21 +34,9 @@
34
34
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35
35
  */
36
36
 
37
- import 'leaflet-i18n';
38
37
  import * as _ from './utils';
39
38
  import { Elevation } from './control';
40
39
 
41
40
  Elevation.Utils = _;
42
41
 
43
- /* Temporary fix for empty values evaluated as false (leaflet-i18n v0.3.1) */
44
- (function(){
45
- let proto = L.i18n.bind({});
46
- L.i18n = L._ = (string, data) => {
47
- if (L.locale && L.locales[L.locale] && L.locales[L.locale][string] == "") {
48
- L.locales[L.locale][string] = "\u200B";
49
- }
50
- return proto.call(null, string, data);
51
- };
52
- })();
53
-
54
42
  L.control.elevation = (options) => new Elevation(options);