diff options
| author | Dennis Fink | 2026-11-03 23:18:23 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-03 23:18:23 +0100 |
| commit | ebe123a6d40bf17ab2a35f331fb08c8c0fc76a3a (patch) | |
| tree | 441d3a817736e9db25d5c14aa3938c0392166160 /assets/static/vendor/leaflet-elevation/src/control.js | |
| parent | 7f4de25eb8abca76a619bc70bb0fce7e8be36561 (diff) | |
| download | dennisfink.me-ebe123a6d40bf17ab2a35f331fb08c8c0fc76a3a.tar.gz dennisfink.me-ebe123a6d40bf17ab2a35f331fb08c8c0fc76a3a.zip | |
Update vendored Leaflet Elevation and map dependencies
Diffstat (limited to 'assets/static/vendor/leaflet-elevation/src/control.js')
| -rw-r--r-- | assets/static/vendor/leaflet-elevation/src/control.js | 300 |
1 files changed, 217 insertions, 83 deletions
diff --git a/assets/static/vendor/leaflet-elevation/src/control.js b/assets/static/vendor/leaflet-elevation/src/control.js index 27ecf77..cc0edd0 100644 --- a/assets/static/vendor/leaflet-elevation/src/control.js +++ b/assets/static/vendor/leaflet-elevation/src/control.js @@ -8,15 +8,10 @@ if (!L._ || !L.i18n) { export const Elevation = L.Control.Elevation = L.Control.extend({ - includes: L.Evented ? L.Evented.prototype : L.Mixin.Events, - options: Options, __mileFactor: 0.621371, // 1 km = (0.621371 mi) - __footFactor: 3.28084, // 1 m = (3.28084 ft) - __D3: 'https://unpkg.com/d3@7.8.4/dist/d3.min.js', + __footFactor: 3.28084, // 1 m = (3.28084 ft) __TOGEOJSON: 'https://unpkg.com/@tmcw/togeojson@5.6.2/dist/togeojson.umd.js', - __LGEOMUTIL: 'https://unpkg.com/leaflet-geometryutil@0.10.1/src/leaflet.geometryutil.js', - __LALMOSTOVER: 'https://unpkg.com/leaflet-almostover@1.0.1/src/leaflet.almostover.js', __LHOTLINE: '../libs/leaflet-hotline.min.js', __LDISTANCEM: '../libs/leaflet-distance-marker.min.js', __LEDGESCALE: '../libs/leaflet-edgescale.min.js', @@ -30,17 +25,16 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ * Add data to the diagram either from GPX or GeoJSON and update the axis domain and data */ addData(d, layer) { - this.import(this.__D3) - .then(() => { - if (this._modulesLoaded) { - layer = layer ?? (d.on && d); - this._addData(d); - this._addLayer(layer); - this._fireEvt("eledata_added", { data: d, layer: layer, track_info: this.track_info }); - } else { - this.once('modules_loaded', () => this.addData(d,layer)); - } - }); + return Promise.resolve().then(() => { + if (!this._modulesLoaded) { + return new Promise(resolve => this.once('modules_loaded', () => resolve(this.addData(d, layer)))); + } + layer = layer ?? (d.on && d); + this._addData(d); + this._addLayer(layer); + this._fireEvt("eledata_added", { data: d, layer: layer, track_info: this.track_info }); + }); + }, /** @@ -71,6 +65,8 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this._data = []; this.track_info = {}; + this._yCoordMin = 0; + this._yCoordMax = -Infinity; this._fireEvt("eledata_clear"); @@ -82,7 +78,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this._events.elechart_updated.forEach(({fn, ctx}) => this.off('elechart_updated', fn, ctx)); } if (this._chart && this._chart._container) { - this._chart._container.selectAll('g.point .point').remove(); + this._chart._container.querySelectorAll('g.point .point').forEach(node => node.remove()); this._chart.clear(); } }, @@ -100,7 +96,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ * * @link https://leafletjs.com/examples/extending/extending-3-controls.html#handlers */ - // addHandler: function (name, HandlerClass) { + // addHandler(name, HandlerClass) { // if (HandlerClass) { // let handler = this[name] = new HandlerClass(this); // this.handlers.push(handler); @@ -150,7 +146,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ }, getBounds(data) { - return L.latLngBounds((data || this._data).map((d) => d.latlng)); + return new L.LatLngBounds((data || this._data).map((d) => d.latlng)); }, /** @@ -172,20 +168,19 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ */ initialize(opts) { - // opts = L.setOptions(this, opts); - // Fixes: https://github.com/Raruto/leaflet-elevation/pull/240 - opts = L.setOptions(this, L.extend({}, _.cloneDeep(Options), opts)); // "deep copy" nested objects (multiple charts) + opts = L.Util.setOptions(this, Object.assign({}, _.cloneDeep(Options), opts)); // "deep copy" nested objects (multiple charts) this._data = []; - this._layers = L.featureGroup(); - this._markers = L.featureGroup(); - this._hotline = L.featureGroup(); - this._circleMarkers = L.featureGroup(); - this._markedSegments = L.polyline([]); - this._start = L.circleMarker([0,0], (opts.trkStart || Options.trkStart)); - this._end = L.circleMarker([0,0], (opts.trkEnd || Options.trkEnd)); + this._layers = new L.FeatureGroup(); + this._markers = new L.FeatureGroup(); + this._hotline = new L.FeatureGroup(); + this._circleMarkers = new L.FeatureGroup(); + this._markedSegments = new L.Polyline([]); + this._start = new L.CircleMarker([0,0], (opts.trkStart || Options.trkStart)); + this._end = new L.CircleMarker([0,0], (opts.trkEnd || Options.trkEnd)); this._chartEnabled = true; + this._yCoordMin = 0; this._yCoordMax = -Infinity; this.track_info = {}; // this.handlers = []; @@ -202,7 +197,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this._markedSegments.setStyle(opts.polylineSegments); // Leaflet canvas renderer colors - L.extend(_.Colors, opts.colors || {}); + Object.assign(_.Colors, opts.colors || {}); // Various stuff this._fixCanvasPaths(); @@ -218,10 +213,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ return Promise.all(src.map(m => this.import(m))); } switch(src) { - case this.__D3: condition = typeof d3 !== 'object'; break; case this.__TOGEOJSON: condition = typeof toGeoJSON !== 'object'; break; - case this.__LGEOMUTIL: condition = typeof L.GeometryUtil !== 'object'; break; - case this.__LALMOSTOVER: condition = typeof L.Handler.AlmostOver !== 'function'; break; case this.__LDISTANCEM: condition = typeof L.DistanceMarkers !== 'function'; break; case this.__LEDGESCALE: condition = typeof L.Control.EdgeScale !== 'function'; break; case this.__LHOTLINE: condition = typeof L.Hotline !== 'function'; break; @@ -245,7 +237,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ let container = this._container = _.create("div", "elevation-control " + this.options.theme + " " + (this.options.detached ? 'elevation-detached' : 'leaflet-control'), this.options.detached ? { id: 'elevation-' + _.randomId() } : {}); - if (!this.eleDiv) this.eleDiv = container; + this.eleDiv = container; this._loadModules(this.options.handlers).then(() => { // Inject here required modules (data handlers) this._initChart(container); @@ -257,6 +249,8 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this.fire('modules_loaded'); }); + + this.fire('add'); return container; @@ -362,6 +356,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this.fire("elepoint_added", { point: point, index: this._data.length - 1 }); + if (this._yCoordMin > this._data[this._data.length - 1][this.options.yAttr]) this._yCoordMin = this._data[this._data.length - 1][this.options.yAttr]; if (this._yCoordMax < this._data[this._data.length - 1][this.options.yAttr]) this._yCoordMax = this._data[this._data.length - 1][this.options.yAttr]; }); @@ -380,7 +375,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ x: x, y: y, z: z, - latlng: L.latLng(x, y, z) + latlng: new L.LatLng(x, y, z) }); this.fire("eledata_updated", { index: this._data.length - 1 }); @@ -402,31 +397,155 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ /** * Initialize "L.AlmostOver" integration + * + * Inspired by: https://github.com/makinacorpus/Leaflet.AlmostOver */ _initAlmostOverHandler(map, layer) { - return (map && this.options.almostOver && !L.Browser.mobile) ? this.import([this.__LGEOMUTIL, this.__LALMOSTOVER]) - .then(() => { - map.addHandler('almostOver', L.Handler.AlmostOver) - if (L.GeometryUtil && map.almostOver && map.almostOver.enabled()) { - map.almostOver.addLayer(layer); - map - .on('almost:move', this._onMouseMoveLayer, this) - .on('almost:out', this._onMouseOut, this); - this.once('eledata_clear', () => { - map.almostOver.removeLayer(layer); - map - .off('almost:move', this._onMouseMoveLayer, this) - .off('almost:out', this._onMouseOut, this); - }) + if (!map || !this.options.almostOver || L.Browser.mobile) return Promise.resolve(); + map.almostOver = map.almostOver || { + _map: map, + _projectedLayers: new WeakMap(), + _layers: [], + _previous: null, + _lastSample: 0, + enabled() { + return !!this._enabled; + }, + enable() { + if (this.enabled()) return; + this._enabled = true; + if (this._map.options.almostOnMouseMove !== false) this._map.on('mousemove', this._sampleMouseMove, this); + this._map.on('click dblclick', this._onMouseClick, this); + }, + disable() { + if (!this.enabled()) return; + this._map.off('mousemove', this._sampleMouseMove, this); + this._map.off('click dblclick', this._onMouseClick, this); + if (this._previous) this._map.fire('almost:out', { layer: this._previous.layer }); + this._previous = null; + this._enabled = false; + }, + addLayer(layer) { + if (layer.eachLayer) layer.eachLayer(child => this.addLayer(child)); + else if (!this._layers.includes(layer)) { + this._layers.push(layer); + this._projectedLayers.delete(layer); } - }) : Promise.resolve(); + }, + removeLayer(layer) { + if (layer.eachLayer) layer.eachLayer(child => this.removeLayer(child)); + else { + this._layers = this._layers.filter(item => item !== layer); + this._projectedLayers.delete(layer); + } + this._previous = null; + }, + getClosest(latlng) { + let closest; + const maxDistance = this._map.options.almostDistance ?? 25; + + const closestPointOnLayer = (map, layer, latlng, maxDistance, cache) => { + let closest; + const zoom = map.getZoom(); + const target = map.project(latlng, zoom); + + function checkPath(points) { + let closestSquared = closest ? closest.distance * closest.distance : maxDistance * maxDistance; + + for (let i = 1; i < points.length; i++) { + const start = points[i - 1]; + const end = points[i]; + const point = L.LineUtil.closestPointOnSegment(target, start, end); + const distanceSquared = (target.x - point.x) ** 2 + (target.y - point.y) ** 2; + + if (distanceSquared <= maxDistance * maxDistance && distanceSquared < closestSquared) { + closestSquared = distanceSquared; + closest = { + layer, + latlng: map.unproject(point, zoom), + distance: Math.sqrt(distanceSquared), + }; + } + } + } + if (layer.eachLayer) { + layer.eachLayer(child => { + const match = closestPointOnLayer(map, child, latlng, closest?.distance ?? maxDistance, cache); + if (match && (!closest || match.distance < closest.distance)) closest = match; + }); + } else if (layer.getLatLngs) { + const cached = cache.get(layer); + if (cached?.zoom === zoom) { + cached.paths.forEach(checkPath); + } else { + const paths = []; + const collectPaths = (coords) => { + if (!Array.isArray(coords)) return; + if (coords.length && Number.isFinite(coords[0]?.lat)) paths.push(coords.map(point => map.project(point, zoom))); + else coords.forEach(collectPaths); + } + collectPaths(layer.getLatLngs()); + cache.set(layer, { zoom, paths }); + paths.forEach(checkPath); + } + } + return closest; + } + + // ponytail: scans all route segments per sampled move; add a spatial index if large collections become slow. + for (const layer of this._layers) { + const match = closestPointOnLayer(this._map, layer, latlng, closest?.distance ?? maxDistance, this._projectedLayers); + if (match && (!closest || match.distance < closest.distance)) closest = match; + } + + return closest; + }, + _sampleMouseMove(event) { + const now = Date.now(); + if (this._map._animatingZoom || this._map._panAnim?._inProgress || + now - this._lastSample < (this._map.options.almostSamplingPeriod ?? 50) || !this._layers.length) return; + this._lastSample = now; + this._onMouseMove(event); + }, + _onMouseMove(event) { + const closest = this.getClosest(event.latlng); + if (closest) { + if (!this._previous || this._previous.layer !== closest.layer) { + if (this._previous) this._map.fire('almost:out', { layer: this._previous.layer }); + this._map.fire('almost:over', closest); + } + this._map.fire('almost:move', closest); + } else if (this._previous) { + this._map.fire('almost:out', { layer: this._previous.layer }); + } + this._previous = closest; + }, + _onMouseClick(event) { + const closest = this.getClosest(event.latlng); + if (closest) this._map.fire(`almost:${event.type}`, closest); + }, + }; + if (!map.almostOver.enabled() && map.options.almostOver !== false) map.almostOver.enable(); + if (map.almostOver.enabled()) { + map.almostOver.addLayer(layer); + map + .on('almost:move', this._onMouseMoveLayer, this) + .on('almost:out', this._onMouseOut, this); + this.once('eledata_clear', () => { + map.almostOver.removeLayer(layer); + map + .off('almost:move', this._onMouseMoveLayer, this) + .off('almost:out', this._onMouseOut, this); + }); + } + return Promise.resolve(); }, /** * Initialize "L.DistanceMarkers" integration */ _initDistanceMarkers() { - return this.options.distanceMarkers ? this.import([this.__LGEOMUTIL, this.__LDISTANCEM]) : Promise.resolve(); + return this.options.distanceMarkers ? this.import(this.__LDISTANCEM) : Promise.resolve(); }, /** @@ -445,7 +564,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ .then(() => { layer.eachLayer((trkseg) => { if (trkseg.feature.geometry.type != "Point") { - let geo = L.geoJson(trkseg.toGeoJSON(), { coordsToLatLng: (coords) => L.latLng(coords[0], coords[1], coords[2] * (this.options.altitudeFactor || 1))}); + let geo = new L.GeoJSON(trkseg.toGeoJSON(), { coordsToLatLng: (coords) => new L.LatLng(coords[0], coords[1], coords[2] * (this.options.altitudeFactor || 1))}); let line = L.hotline(geo.toGeoJSON().features[0].geometry.coordinates, { renderer: L.Hotline.renderer(), min: isFinite(this.track_info[prop + '_min']) ? this.track_info[prop + '_min'] : 0, @@ -532,7 +651,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ let options = layer.options; - options.color = color.line || color.area || theme; + options.color = control._chartEnabled ? (color.line || color.area || theme) : '#000'; options.stroke = !!options.color; oldProto.call(this, ctx, layer); @@ -596,7 +715,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ * Hides the position/height indicator marker drawn onto the map */ _hideMarker() { - if (this.options.autohideMarker) { + if (this.options.autohideMarker && this._marker) { this._marker.remove(); } }, @@ -620,17 +739,15 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ } this - .import([this.__D3, this.__LCHART]) + .import(this.__LCHART) .then((m) => { - let chart = this._chart = new (m[1] || Elevation).Chart(opts, this); + let chart = this._chart = new (m || Elevation).Chart(opts, this); this._x = this._chart._x; this._y = this._chart._y; - d3 - .select(container) - .call(chart.render()) + chart.render()(container); chart .on('reset_drag', this._hideMarker, this) @@ -663,6 +780,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this._updateChart(); this._updateSummary(); + map.invalidateSize(); }); @@ -687,11 +805,11 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ } if (this._renderer) this._renderer.remove() - this._renderer = L.svg({ pane: "elevationPane" }).addTo(this._map); // default leaflet svg renderer + this._renderer = new L.SVG({ pane: "elevationPane" }).addTo(this._map); // default leaflet svg renderer - this.import([this.__D3, this.__LMARKER]) + this.import(this.__LMARKER) .then((m) => { - this._marker = new (m[1] || Elevation).Marker(this.options, this); + this._marker = new (m || Elevation).Marker(this.options, this); this.fire("elechart_marker"); }); }, @@ -724,10 +842,12 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ _initSummary(container) { this.import(this.__LSUMMARY).then((m)=>{ this._summary = new (m || Elevation).Summary({ summary: this.options.summary }, this); - - this.on('elechart_init', () => { - d3.select(container).call(this._summary.render()); - }); + const render = () => { + this._summary.render()(container); + this._updateSummary(); + }; + if (this._chart && container.contains(this._chart._container)) render(); + else this.once('elechart_init', render); }); }, @@ -768,7 +888,8 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ */ _loadLayer(geojson) { let { polyline, theme, waypoints, wptIcons, wptLabels, distanceMarkers } = this.options; - let style = L.extend({}, polyline); + let style = Object.assign({}, polyline); + let dataAdded = []; if (theme) { style.className += ' ' + theme; @@ -778,7 +899,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this.track_info.name = geojson.name; } - let layer = L.geoJson(geojson, { + let layer = new L.GeoJSON(geojson, { distanceMarkers: distanceMarkers, style: style, pointToLayer: (feature, latlng) => { @@ -797,16 +918,16 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ if ([true, 'markers'].includes(waypoints) && wptIcons != false) { return this._registerMarker({ latlng : latlng, - sym : (sym ?? name).replace(' ', '-').replace('"', '').replace("'", '').toLowerCase(), + sym : (sym ?? name).replace(/["']/g, '').replace(/ /g, '-').toLowerCase(), content: [true, 'markers'].includes(wptLabels) && (name || desc) && decodeURI("<b>" + name + "</b>" + (desc.length > 0 ? '<br>' + desc : '')) }); } } }, - onEachFeature: (feature, layer) => feature.geometry && feature.geometry.type != 'Point' && this.addData(feature, layer), + onEachFeature: (feature, layer) => feature.geometry && feature.geometry.type != 'Point' && dataAdded.push(this.addData(feature, layer)), }); - this.import(this.__D3).then(() => { + Promise.all(dataAdded).then(() => { this._initMapIntegrations(layer); const event_data = { data: geojson, layer: layer, name: this.track_info.name, track_info: this.track_info }; if (this._modulesLoaded) { @@ -821,7 +942,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ _onDragEnd({ dragstart, dragend}) { this._hideMarker(); - this.fitBounds(L.latLngBounds([dragstart.latlng, dragend.latlng])); + this.fitBounds(new L.LatLngBounds([dragstart.latlng, dragend.latlng])); this.fire("elechart_dragged"); }, @@ -910,7 +1031,10 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this._chartEnabled = this._chart._hasActiveLayers(); // toggle layer visibility on empty chart - this._layers.eachLayer(layer => _.toggleClass(layer.getElement && layer.getElement(), this.options.polyline.className + ' ' + this.options.theme, this._chartEnabled)); + this._layers.eachLayer(layer => { + _.toggleClass(layer.getElement && layer.getElement(), this.options.polyline.className + ' ' + this.options.theme, this._chartEnabled); + if (layer._renderer instanceof L.Canvas) layer.redraw(); + }); // toggle option value (eg. altitude = { 'disabled' || 'enabled' }) this.options[name] = !enabled && this.options[name] == 'disabled' ? 'enabled' : 'disabled'; @@ -1130,20 +1254,20 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ }); if (grid) { - this._registerAxisGrid(L.extend({ name }, grid)); + this._registerAxisGrid(Object.assign({ name }, grid)); } if (this.options[name] !== "summary") { - if (scale) this._registerAxisScale(L.extend({ name, label: unit }, scale)); - if (path) this._registerAreaPath(L.extend({ name }, path)); + if (scale) this._registerAxisScale(Object.assign({ name, label: unit }, scale)); + if (path) this._registerAreaPath(Object.assign({ name }, path)); } if (tooltip || props.tooltips) { - _.each([tooltip, ...(props.tooltips || [])], t => t && this._registerTooltip(L.extend({ name }, t))); + _.each([tooltip, ...(props.tooltips || [])], t => t && this._registerTooltip(Object.assign({ name }, t))); } if (summary) { - _.each(summary, (s, k) => summary[k] = L.extend({ unit }, s)); + _.each(summary, (s, k) => summary[k] = Object.assign({ unit }, s)); this._registerSummary(summary); } } @@ -1153,9 +1277,9 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ let { wptIcons } = this.options; // generate and cache appropriate icon symbol if (!wptIcons.hasOwnProperty(sym)) { - wptIcons[sym] = L.divIcon(L.extend({}, wptIcons[""].options, { html: '<i class="elevation-waypoint-icon ' + sym + '"></i>' } )); + wptIcons[sym] = new L.DivIcon(Object.assign({}, wptIcons[""].options, { html: '<i class="elevation-waypoint-icon ' + sym + '"></i>' } )); } - let marker = L.marker(latlng, { icon: wptIcons[sym] }); + let marker = new L.Marker(latlng, { icon: wptIcons[sym] }); if (content) { marker.bindPopup(content, { className: 'elevation-popup', keepInView: true }).openPopup(); marker.bindTooltip(content, { className: 'elevation-tooltip', direction: 'auto', sticky: true, opacity: 1 }).openTooltip(); @@ -1167,8 +1291,8 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ * Add chart or marker tooltip info */ _registerTooltip(props) { - props.chart && this.on("elechart_init", () => this._chart._registerTooltip(L.extend({}, props, { value: props.chart }))); - props.marker && this.on("elechart_marker", () => this._marker._registerTooltip(L.extend({}, props, { value: props.marker }))); + props.chart && this.on("elechart_init", () => this._chart._registerTooltip(Object.assign({}, props, { value: props.chart }))); + props.marker && this.on("elechart_marker", () => this._marker._registerTooltip(Object.assign({}, props, { value: props.marker }))); }, /** @@ -1262,6 +1386,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ this._marker.update({ map : this._map, item : item, + yCoordMin : this._yCoordMin, yCoordMax : this._yCoordMax || 0, options : this.options }); @@ -1333,3 +1458,12 @@ export const Elevation = L.Control.Elevation = L.Control.extend({ }, }); + +// backcomp for leaflet v1 +if (!('on' in Elevation.prototype)) { + for (const name of Object.getOwnPropertyNames(L.Evented.prototype)) { + if (name !== 'constructor') { + Object.defineProperty(Elevation.prototype, name, Object.getOwnPropertyDescriptor(L.Evented.prototype, name)); + } + } +} |
