|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import L from "leaflet"; |
| 3 | + |
| 4 | +const PhotosLayer = L.FeatureGroup.extend({ |
| 5 | + options: { |
| 6 | + icon: { |
| 7 | + iconSize: [40, 40], |
| 8 | + }, |
| 9 | + }, |
| 10 | + |
| 11 | + initialize: function (photos: any, options: any) { |
| 12 | + L.setOptions(this, options); |
| 13 | + // @ts-expect-error initialize does exists |
| 14 | + L.FeatureGroup.prototype.initialize.call(this, photos); |
| 15 | + }, |
| 16 | + |
| 17 | + addLayers: function (photos: any) { |
| 18 | + if (photos) { |
| 19 | + for (let i = 0, len = photos.length; i < len; i++) { |
| 20 | + this.addLayer(photos[i]); |
| 21 | + } |
| 22 | + } |
| 23 | + return this; |
| 24 | + }, |
| 25 | + |
| 26 | + addLayer: function (photo: any) { |
| 27 | + L.FeatureGroup.prototype.addLayer.call(this, this.createMarker(photo)); |
| 28 | + }, |
| 29 | + |
| 30 | + createMarker: function (photo: any) { |
| 31 | + const marker: L.Marker & { photo?: any } = L.marker(photo, { |
| 32 | + icon: L.divIcon( |
| 33 | + L.extend( |
| 34 | + { |
| 35 | + html: '<div style="background-image: url(' + photo.thumbnail + ');"></div>', |
| 36 | + className: "leaflet-marker-photo", |
| 37 | + }, |
| 38 | + photo, |
| 39 | + this.options.icon, |
| 40 | + ), |
| 41 | + ), |
| 42 | + title: photo.caption || "", |
| 43 | + }); |
| 44 | + marker.photo = photo; |
| 45 | + return marker; |
| 46 | + }, |
| 47 | +}); |
| 48 | + |
| 49 | +const photosLayerFunc = function (photos: any, options: any) { |
| 50 | + // @ts-expect-error we are expecting 2 arguments |
| 51 | + return new PhotosLayer(photos, options); |
| 52 | +}; |
| 53 | + |
| 54 | +const Cluster = L.MarkerClusterGroup.extend({ |
| 55 | + options: { |
| 56 | + featureGroup: photosLayerFunc, |
| 57 | + maxClusterRadius: 100, |
| 58 | + showCoverageOnHover: false, |
| 59 | + iconCreateFunction: function (cluster: any) { |
| 60 | + return new L.DivIcon( |
| 61 | + L.extend( |
| 62 | + { |
| 63 | + className: "leaflet-marker-photo", |
| 64 | + html: |
| 65 | + '<div style="background-image: url(' + |
| 66 | + cluster.getAllChildMarkers()[0].photo.thumbnail + |
| 67 | + ');"></div><b>' + |
| 68 | + cluster.getChildCount() + |
| 69 | + "</b>", |
| 70 | + }, |
| 71 | + this.icon, |
| 72 | + ), |
| 73 | + ); |
| 74 | + }, |
| 75 | + icon: { |
| 76 | + iconSize: new L.Point(40, 40), |
| 77 | + }, |
| 78 | + }, |
| 79 | + |
| 80 | + initialize: function (options: any) { |
| 81 | + options = L.Util.setOptions(this, options); |
| 82 | + // @ts-expect-error initialize does exists |
| 83 | + L.MarkerClusterGroup.prototype.initialize.call(this); |
| 84 | + this._photos = options.featureGroup(null, options); |
| 85 | + }, |
| 86 | + |
| 87 | + add: function (photos: any) { |
| 88 | + this.addLayer(this._photos.addLayers(photos)); |
| 89 | + return this; |
| 90 | + }, |
| 91 | + |
| 92 | + clear: function () { |
| 93 | + this._photos.clearLayers(); |
| 94 | + this.clearLayers(); |
| 95 | + }, |
| 96 | +}); |
| 97 | + |
| 98 | +const clusterFunc = function (options: any) { |
| 99 | + // @ts-expect-error we do expect an argument |
| 100 | + return new Cluster(options); |
| 101 | +}; |
| 102 | + |
| 103 | +export { photosLayerFunc, clusterFunc }; |
0 commit comments