index.js (2282B)
1 import L from 'leaflet'; 2 import {fetch} from '~/lib/xhr-promise'; 3 import '~/lib/leaflet.layer.canvasMarkers'; 4 import * as logging from '~/lib/logging'; 5 import {notify} from '~/lib/notifications'; 6 import './style.css'; 7 import iconFromBackgroundImage from '~/lib/iconFromBackgroundImage'; 8 import {openPopupWindow} from '~/lib/popup-window'; 9 10 const GeocachingSu = L.Layer.CanvasMarkers.extend({ 11 options: { 12 scaleDependent: true 13 }, 14 15 initialize: function(url, options) { 16 this.dataUrl = url; 17 L.Layer.CanvasMarkers.prototype.initialize.call(this, null, options); 18 this.on('markerclick', this.openCachePage, this); 19 }, 20 21 onAdd: function(map) { 22 L.Layer.CanvasMarkers.prototype.onAdd.call(this, map); 23 this._loadData(); 24 }, 25 26 _loadData: function() { 27 if (this._downloadStarted) { 28 return; 29 } 30 this._downloadStarted = true; 31 fetch(this.dataUrl, {responseType: 'json'}) 32 .then( 33 (xhr) => this._loadMarkers(xhr.response), 34 (e) => { 35 this._downloadStarted = false; 36 logging.captureException(e, 'failed to get geocaching kml'); 37 notify('Failed to get geocaching data'); 38 } 39 ); 40 }, 41 42 cloneMarker: function(marker) { 43 return { 44 latlng: {lat: marker.latlng.lat, lng: marker.latlng.lng}, 45 label: marker.label, 46 icon: marker.icon, 47 _label: marker._label 48 }; 49 }, 50 51 _loadMarkers: function(data) { 52 const icon = iconFromBackgroundImage('geocaching-icon'); 53 54 function getLabel(marker, zoom) { 55 return zoom >= 10 ? marker._label : null; 56 } 57 58 const markers = data.map(([label, cacheId, lat, lng]) => ({ 59 latlng: {lat, lng}, 60 _label: label, 61 label: getLabel, 62 icon, 63 cacheId 64 })); 65 this.addMarkers(markers); 66 this._dataLoaded = true; 67 this.fire('data-loaded'); 68 }, 69 70 openCachePage: function(e) { 71 const url = `https://geocaching.su/?pn=101&cid=${e.marker.cacheId}`; 72 openPopupWindow(url, 900, 'geocaching_su'); 73 } 74 }); 75 76 export {GeocachingSu};