Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass to remove global L #987

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/DistanceGrid.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Util } from 'leaflet/src/core';

L.DistanceGrid = function (cellSize) {
export var DistanceGrid = function (cellSize) {
this._cellSize = cellSize;
this._sqCellSize = cellSize * cellSize;
this._grid = {};
this._objectPoint = { };
};

L.DistanceGrid.prototype = {
DistanceGrid.prototype = {

addObject: function (obj, point) {
var x = this._getCoord(point.x),
y = this._getCoord(point.y),
grid = this._grid,
row = grid[y] = grid[y] || {},
cell = row[x] = row[x] || [],
stamp = L.Util.stamp(obj);
stamp = Util.stamp(obj);

this._objectPoint[stamp] = point;

Expand All @@ -35,7 +36,7 @@ L.DistanceGrid.prototype = {
cell = row[x] = row[x] || [],
i, len;

delete this._objectPoint[L.Util.stamp(obj)];
delete this._objectPoint[Util.stamp(obj)];

for (i = 0, len = cell.length; i < len; i++) {
if (cell[i] === obj) {
Expand Down Expand Up @@ -91,7 +92,7 @@ L.DistanceGrid.prototype = {

for (k = 0, len = cell.length; k < len; k++) {
obj = cell[k];
dist = this._sqDist(objectPoint[L.Util.stamp(obj)], point);
dist = this._sqDist(objectPoint[Util.stamp(obj)], point);
if (dist < closestDistSq ||
dist <= closestDistSq && closest === null) {
closestDistSq = dist;
Expand Down
14 changes: 8 additions & 6 deletions src/MarkerCluster.QuickHull.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Retrieved from: http://en.literateprograms.org/Quickhull_(Javascript)?oldid=18434
*/

(function () {
L.QuickHull = {
import { MarkerCluster } from './MarkerCluster.js';

//(function () {
export var QuickHull = {

/*
* @param {Object} cpt a point to be measured from the baseline
Expand Down Expand Up @@ -133,7 +135,7 @@ Retrieved from: http://en.literateprograms.org/Quickhull_(Javascript)?oldid=1843
minLng = pt.lng;
}
}

if (minLat !== maxLat) {
minPt = minLatPt;
maxPt = maxLatPt;
Expand All @@ -147,9 +149,9 @@ Retrieved from: http://en.literateprograms.org/Quickhull_(Javascript)?oldid=1843
return ch;
}
};
}());
//}());

L.MarkerCluster.include({
MarkerCluster.include({
getConvexHull: function () {
var childMarkers = this.getAllChildMarkers(),
points = [],
Expand All @@ -160,6 +162,6 @@ L.MarkerCluster.include({
points.push(p);
}

return L.QuickHull.getConvexHull(points);
return QuickHull.getConvexHull(points);
}
});
39 changes: 23 additions & 16 deletions src/MarkerCluster.Spiderfier.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
//This code is 100% based on https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet
//Huge thanks to jawj for implementing it first to make my job easy :-)

L.MarkerCluster.include({
import { MarkerCluster } from './MarkerCluster.js';
import { MarkerClusterGroup } from './MarkerClusterGroup.js';
import { Browser, Util } from 'leaflet/src/core';
import { DomUtil } from 'leaflet/src/dom';
import { Point } from 'leaflet/src/geometry';
import { Path, Polyline } from 'leaflet/src/layer/vector';

MarkerCluster.include({

_2PI: Math.PI * 2,
_circleFootSeparation: 25, //related to circumference of circle
Expand Down Expand Up @@ -65,7 +72,7 @@ L.MarkerCluster.include({

for (i = 0; i < count; i++) { // Clockwise, like spiral.
angle = this._circleStartAngle + i * angleStep;
res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
res[i] = new Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
}

return res;
Expand All @@ -87,7 +94,7 @@ L.MarkerCluster.include({
// Skip the first position, so that we are already farther from center and we avoid
// being under the default cluster icon (especially important for Circle Markers).
if (i < count) {
res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
res[i] = new Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
}
angle += separation / legLength + i * 0.0005;
legLength += lengthFactor / angle;
Expand Down Expand Up @@ -134,7 +141,7 @@ L.MarkerCluster.include({
});

//Non Animated versions of everything
L.MarkerClusterNonAnimated = L.MarkerCluster.extend({
let MarkerClusterNonAnimated = MarkerCluster.extend({
attiks marked this conversation as resolved.
Show resolved Hide resolved
_animationSpiderfy: function (childMarkers, positions) {
var group = this._group,
map = group._map,
Expand All @@ -151,7 +158,7 @@ L.MarkerClusterNonAnimated = L.MarkerCluster.extend({
m = childMarkers[i];

// Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it.
leg = new L.Polyline([this._latlng, newPos], legOptions);
leg = new Polyline([this._latlng, newPos], legOptions);
map.addLayer(leg);
m._spiderLeg = leg;

Expand Down Expand Up @@ -179,7 +186,7 @@ L.MarkerClusterNonAnimated = L.MarkerCluster.extend({
});

//Animated versions here
L.MarkerCluster.include({
MarkerCluster.include({

_animationSpiderfy: function (childMarkers, positions) {
var me = this,
Expand All @@ -188,13 +195,13 @@ L.MarkerCluster.include({
fg = group._featureGroup,
thisLayerLatLng = this._latlng,
thisLayerPos = map.latLngToLayerPoint(thisLayerLatLng),
svg = L.Path.SVG,
legOptions = L.extend({}, this._group.options.spiderLegPolylineOptions), // Copy the options so that we can modify them for animation.
svg = Path.SVG,
legOptions = Util.extend({}, this._group.options.spiderLegPolylineOptions), // Copy the options so that we can modify them for animation.
finalLegOpacity = legOptions.opacity,
i, m, leg, legPath, legLength, newPos;

if (finalLegOpacity === undefined) {
finalLegOpacity = L.MarkerClusterGroup.prototype.options.spiderLegPolylineOptions.opacity;
finalLegOpacity = MarkerClusterGroup.prototype.options.spiderLegPolylineOptions.opacity;
}

if (svg) {
Expand All @@ -219,7 +226,7 @@ L.MarkerCluster.include({
newPos = map.layerPointToLatLng(positions[i]);

// Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it.
leg = new L.Polyline([thisLayerLatLng, newPos], legOptions);
leg = new Polyline([thisLayerLatLng, newPos], legOptions);
map.addLayer(leg);
m._spiderLeg = leg;

Expand All @@ -239,7 +246,7 @@ L.MarkerCluster.include({
if (m.clusterHide) {
m.clusterHide();
}

// Vectors just get immediately added
fg.addLayer(m);

Expand All @@ -259,7 +266,7 @@ L.MarkerCluster.include({
//Move marker to new position
m._preSpiderfyLatlng = m._latlng;
m.setLatLng(newPos);

if (m.clusterShow) {
m.clusterShow();
}
Expand Down Expand Up @@ -293,7 +300,7 @@ L.MarkerCluster.include({
fg = group._featureGroup,
thisLayerPos = zoomDetails ? map._latLngToNewLayerPoint(this._latlng, zoomDetails.zoom, zoomDetails.center) : map.latLngToLayerPoint(this._latlng),
childMarkers = this.getAllChildMarkers(null, true),
svg = L.Path.SVG,
svg = Path.SVG,
m, i, leg, legPath, legLength, nonAnimatable;

group._ignoreMove = true;
Expand Down Expand Up @@ -384,7 +391,7 @@ L.MarkerCluster.include({
});


L.MarkerClusterGroup.include({
MarkerClusterGroup.include({
//The MarkerCluster currently spiderfied (if any)
_spiderfied: null,

Expand All @@ -401,7 +408,7 @@ L.MarkerClusterGroup.include({
//Browsers without zoomAnimation or a big zoom don't fire zoomstart
this._map.on('zoomend', this._noanimationUnspiderfy, this);

if (!L.Browser.touch) {
if (!Browser.touch) {
this._map.getRenderer(this);
//Needs to happen in the pageload, not after, or animations don't work in webkit
// http://stackoverflow.com/questions/8455200/svg-animate-with-dynamically-added-elements
Expand Down Expand Up @@ -432,7 +439,7 @@ L.MarkerClusterGroup.include({

_unspiderfyZoomAnim: function (zoomDetails) {
//Wait until the first zoomanim after the user has finished touch-zooming before running the animation
if (L.DomUtil.hasClass(this._map._mapPane, 'leaflet-touching')) {
if (DomUtil.hasClass(this._map._mapPane, 'leaflet-touching')) {
return;
}

Expand Down
27 changes: 15 additions & 12 deletions src/MarkerCluster.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export var MarkerCluster = L.MarkerCluster = L.Marker.extend({
options: L.Icon.prototype.options,
import { Icon, Marker } from 'leaflet/src/layer/marker';
import { LatLng, LatLngBounds } from 'leaflet/src/geo';

export var MarkerCluster = Marker.extend({
options: Icon.prototype.options,

initialize: function (group, zoom, a, b) {

L.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new L.LatLng(0, 0),
Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new LatLng(0, 0),
{ icon: this, pane: group.options.clusterPane });

this._group = group;
Expand All @@ -15,7 +18,7 @@ export var MarkerCluster = L.MarkerCluster = L.Marker.extend({
this._iconNeedsUpdate = true;
this._boundsNeedUpdate = true;

this._bounds = new L.LatLngBounds();
this._bounds = new LatLngBounds();

if (a) {
this._addChild(a);
Expand Down Expand Up @@ -77,7 +80,7 @@ export var MarkerCluster = L.MarkerCluster = L.Marker.extend({
},

getBounds: function () {
var bounds = new L.LatLngBounds();
var bounds = new LatLngBounds();
bounds.extend(this._bounds);
return bounds;
},
Expand Down Expand Up @@ -109,7 +112,7 @@ export var MarkerCluster = L.MarkerCluster = L.Marker.extend({
this._boundsNeedUpdate = true;
this._setClusterCenter(new1);

if (new1 instanceof L.MarkerCluster) {
if (new1 instanceof MarkerCluster) {
if (!isNotificationFromChild) {
this._childClusters.push(new1);
new1.__parent = this;
Expand All @@ -129,7 +132,7 @@ export var MarkerCluster = L.MarkerCluster = L.Marker.extend({

/**
* Makes sure the cluster center is set. If not, uses the child center if it is a cluster, or the marker position.
* @param child L.MarkerCluster|L.Marker that will be used as cluster center if not defined yet.
* @param child MarkerCluster|Marker that will be used as cluster center if not defined yet.
* @private
*/
_setClusterCenter: function (child) {
Expand All @@ -141,7 +144,7 @@ export var MarkerCluster = L.MarkerCluster = L.Marker.extend({

/**
* Assigns impossible bounding values so that the next extend entirely determines the new bounds.
* This method avoids having to trash the previous L.LatLngBounds object and to create a new one, which is much slower for this class.
* This method avoids having to trash the previous LatLngBounds object and to create a new one, which is much slower for this class.
* As long as the bounds are not extended, most other methods would probably fail, as they would with bounds initialized but not extended.
* @private
*/
Expand Down Expand Up @@ -202,7 +205,7 @@ export var MarkerCluster = L.MarkerCluster = L.Marker.extend({
lngSum += childLatLng.lng * childCount;
}

this._latlng = this._wLatLng = new L.LatLng(latSum / totalCount, lngSum / totalCount);
this._latlng = this._wLatLng = new LatLng(latSum / totalCount, lngSum / totalCount);

// Reset dirty flag.
this._boundsNeedUpdate = false;
Expand Down Expand Up @@ -365,11 +368,11 @@ export var MarkerCluster = L.MarkerCluster = L.Marker.extend({
},

//Run the given functions recursively to this and child clusters
// boundsToApplyTo: a L.LatLngBounds representing the bounds of what clusters to recurse in to
// boundsToApplyTo: a LatLngBounds representing the bounds of what clusters to recurse in to
// zoomLevelToStart: zoom level to start running functions (inclusive)
// zoomLevelToStop: zoom level to stop running functions (inclusive)
// runAtEveryLevel: function that takes an L.MarkerCluster as an argument that should be applied on every level
// runAtBottomLevel: function that takes an L.MarkerCluster as an argument that should be applied at only the bottom level
// runAtEveryLevel: function that takes an MarkerCluster as an argument that should be applied on every level
// runAtBottomLevel: function that takes an MarkerCluster as an argument that should be applied at only the bottom level
_recursively: function (boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel) {
var childClusters = this._childClusters,
zoom = this._zoom,
Expand Down
32 changes: 17 additions & 15 deletions src/MarkerClusterGroup.Refresh.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
/**
* Adds 1 public method to MCG and 1 to L.Marker to facilitate changing
* Adds 1 public method to MCG and 1 to Marker to facilitate changing
* markers' icon options and refreshing their icon and their parent clusters
* accordingly (case where their iconCreateFunction uses data of childMarkers
* to make up the cluster icon).
*/

import { MarkerClusterGroup } from './MarkerClusterGroup.js';
import { Marker } from 'leaflet/src/layer/marker';

L.MarkerClusterGroup.include({
MarkerClusterGroup.include({
/**
* Updates the icon of all clusters which are parents of the given marker(s).
* In singleMarkerMode, also updates the given marker(s) icon.
* @param layers L.MarkerClusterGroup|L.LayerGroup|Array(L.Marker)|Map(L.Marker)|
* L.MarkerCluster|L.Marker (optional) list of markers (or single marker) whose parent
* @param layers MarkerClusterGroup|LayerGroup|Array(Marker)|Map(Marker)|
* MarkerCluster|Marker (optional) list of markers (or single marker) whose parent
* clusters need to be updated. If not provided, retrieves all child markers of this.
* @returns {L.MarkerClusterGroup}
* @returns {MarkerClusterGroup}
*/
refreshClusters: function (layers) {
if (!layers) {
layers = this._topClusterLevel.getAllChildMarkers();
} else if (layers instanceof L.MarkerClusterGroup) {
} else if (layers instanceof MarkerClusterGroup) {
layers = layers._topClusterLevel.getAllChildMarkers();
} else if (layers instanceof L.LayerGroup) {
} else if (layers instanceof LayerGroup) {
layers = layers._layers;
} else if (layers instanceof L.MarkerCluster) {
} else if (layers instanceof MarkerCluster) {
layers = layers.getAllChildMarkers();
} else if (layers instanceof L.Marker) {
} else if (layers instanceof Marker) {
layers = [layers];
} // else: must be an Array(L.Marker)|Map(L.Marker)
} // else: must be an Array(Marker)|Map(Marker)
this._flagParentsIconsNeedUpdate(layers);
this._refreshClustersIcons();

Expand All @@ -40,7 +42,7 @@ L.MarkerClusterGroup.include({

/**
* Simply flags all parent clusters of the given markers as having a "dirty" icon.
* @param layers Array(L.Marker)|Map(L.Marker) list of markers.
* @param layers Array(Marker)|Map(Marker) list of markers.
* @private
*/
_flagParentsIconsNeedUpdate: function (layers) {
Expand All @@ -64,7 +66,7 @@ L.MarkerClusterGroup.include({
/**
* Re-draws the icon of the supplied markers.
* To be used in singleMarkerMode only.
* @param layers Array(L.Marker)|Map(L.Marker) list of markers.
* @param layers Array(Marker)|Map(Marker) list of markers.
* @private
*/
_refreshSingleMarkerModeMarkers: function (layers) {
Expand All @@ -82,18 +84,18 @@ L.MarkerClusterGroup.include({
}
});

L.Marker.include({
Marker.include({
/**
* Updates the given options in the marker's icon and refreshes the marker.
* @param options map object of icon options.
* @param directlyRefreshClusters boolean (optional) true to trigger
* MCG.refreshClustersOf() right away with this single marker.
* @returns {L.Marker}
* @returns {Marker}
*/
refreshIconOptions: function (options, directlyRefreshClusters) {
var icon = this.options.icon;

L.setOptions(icon, options);
setOptions(icon, options);

this.setIcon(icon);

Expand Down
Loading