Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
99 changes: 45 additions & 54 deletions dist/L.Control.Locate.esm.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import { Marker, setOptions, divIcon, Control, DomUtil, extend, LayerGroup, DomEvent, Util, circle } from 'leaflet';
import { Marker, Util, DivIcon, Control, LayerGroup, DomEvent, Circle } from 'leaflet';

/*!
Copyright (c) 2016 Dominik Moritz

This file is part of the leaflet locate control. It is licensed under the MIT license.
You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
*/
const addClasses = (el, names) => {
names.split(" ").forEach((className) => {
el.classList.add(className);
});
};

const removeClasses = (el, names) => {
names.split(" ").forEach((className) => {
el.classList.remove(className);
});
};

/**
* Compatible with Circle but a true marker instead of a path
*/
const LocationMarker = Marker.extend({
initialize(latlng, options) {
setOptions(this, options);
Util.setOptions(this, options);
this._latlng = latlng;
this.createIcon();
},
Expand All @@ -41,13 +30,13 @@ const LocationMarker = Marker.extend({
["fill-opacity", opt.fillOpacity],
["opacity", opt.opacity]
]
.filter(([k,v]) => v !== undefined)
.map(([k,v]) => `${k}="${v}"`)
.filter(([k, v]) => v !== undefined)
.map(([k, v]) => `${k}="${v}"`)
.join(" ");

const icon = this._getIconSVG(opt, style);

this._locationIcon = divIcon({
this._locationIcon = new DivIcon({
className: icon.className,
html: icon.svg,
iconSize: [icon.w, icon.h]
Expand Down Expand Up @@ -78,14 +67,14 @@ const LocationMarker = Marker.extend({
},

setStyle(style) {
setOptions(this, style);
Util.setOptions(this, style);
this.createIcon();
}
});

const CompassMarker = LocationMarker.extend({
initialize(latlng, heading, options) {
setOptions(this, options);
Util.setOptions(this, options);
this._latlng = latlng;
this._heading = heading;
this.createIcon();
Expand Down Expand Up @@ -253,22 +242,29 @@ const LocateControl = Control.extend({
* This function should return an object with HtmlElement for the button (link property) and the icon (icon property).
*/
createButtonCallback(container, options) {
const link = DomUtil.create("a", "leaflet-bar-part leaflet-bar-part-single", container);
const link = document.createElement("a");
link.classList.add("leaflet-bar-part", "leaflet-bar-part-single");
link.title = options.strings.title;
link.href = "#";
link.setAttribute("role", "button");
const icon = DomUtil.create(options.iconElementTag, options.icon, link);

const icon = document.createElement(options.iconElementTag);
icon.classList.add(...options.icon.split(" "));
link.appendChild(icon);

if (options.strings.text !== undefined) {
const text = DomUtil.create(options.textElementTag, "leaflet-locate-text", link);
const text = document.createElement(options.textElementTag);
text.classList.add("leaflet-locate-text");
text.textContent = options.strings.text;
link.appendChild(text);
link.classList.add("leaflet-locate-text-active");
link.parentNode.style.display = "flex";
if (options.icon.length > 0) {
icon.classList.add("leaflet-locate-icon");
}
}

container.appendChild(link);
return { link, icon };
},
/** This event is called in case of any location error that is not a time out error. */
Expand Down Expand Up @@ -305,23 +301,24 @@ const LocateControl = Control.extend({
// set default options if nothing is set (merge one step deep)
for (const i in options) {
if (typeof this.options[i] === "object") {
extend(this.options[i], options[i]);
Object.assign(this.options[i], options[i]);
} else {
this.options[i] = options[i];
}
}

// extend the follow marker style and circle from the normal style
this.options.followMarkerStyle = extend({}, this.options.markerStyle, this.options.followMarkerStyle);
this.options.followCircleStyle = extend({}, this.options.circleStyle, this.options.followCircleStyle);
this.options.followCompassStyle = extend({}, this.options.compassStyle, this.options.followCompassStyle);
this.options.followMarkerStyle = { ...this.options.markerStyle, ...this.options.followMarkerStyle };
this.options.followCircleStyle = { ...this.options.circleStyle, ...this.options.followCircleStyle };
this.options.followCompassStyle = { ...this.options.compassStyle, ...this.options.followCompassStyle };
},

/**
* Add control to map. Returns the container for the control.
*/
onAdd(map) {
const container = DomUtil.create("div", "leaflet-control-locate leaflet-bar leaflet-control");
const container = document.createElement("div");
container.classList.add("leaflet-control-locate", "leaflet-bar", "leaflet-control");
this._container = container;
this._map = map;
this._layer = this.options.layer || new LayerGroup();
Expand All @@ -334,16 +331,12 @@ const LocateControl = Control.extend({
this._link = linkAndIcon.link;
this._icon = linkAndIcon.icon;

DomEvent.on(
this._link,
"click",
function (ev) {
DomEvent.stopPropagation(ev);
DomEvent.preventDefault(ev);
this._onClick();
},
this
).on(this._link, "dblclick", DomEvent.stopPropagation);
this._link.addEventListener("click", (ev) => {
ev.stopPropagation();
ev.preventDefault();
this._onClick();
});
this._link.addEventListener("dblclick", (ev) => ev.stopPropagation());

this._resetVariables();

Expand Down Expand Up @@ -598,7 +591,7 @@ const LocateControl = Control.extend({
const style = this._isFollowing() ? this.options.followCircleStyle : this.options.circleStyle;

if (!this._circle) {
this._circle = circle(latlng, radius, style).addTo(this._layer);
this._circle = new Circle(latlng, radius, style).addTo(this._layer);
} else {
this._circle.setLatLng(latlng).setRadius(radius).setStyle(style);
}
Expand Down Expand Up @@ -857,36 +850,34 @@ const LocateControl = Control.extend({
*/
_setClasses(state) {
if (state == "requesting") {
removeClasses(this._container, "active following");
addClasses(this._container, "requesting");
this._container.classList.remove("active", "following");
this._container.classList.add("requesting");

removeClasses(this._icon, this.options.icon);
addClasses(this._icon, this.options.iconLoading);
this._icon.classList.remove(...this.options.icon.split(" "));
this._icon.classList.add(...this.options.iconLoading.split(" "));
} else if (state == "active") {
removeClasses(this._container, "requesting following");
addClasses(this._container, "active");
this._container.classList.remove("requesting", "following");
this._container.classList.add("active");

removeClasses(this._icon, this.options.iconLoading);
addClasses(this._icon, this.options.icon);
this._icon.classList.remove(...this.options.iconLoading.split(" "));
this._icon.classList.add(...this.options.icon.split(" "));
} else if (state == "following") {
removeClasses(this._container, "requesting");
addClasses(this._container, "active following");
this._container.classList.remove("requesting");
this._container.classList.add("active", "following");

removeClasses(this._icon, this.options.iconLoading);
addClasses(this._icon, this.options.icon);
this._icon.classList.remove(...this.options.iconLoading.split(" "));
this._icon.classList.add(...this.options.icon.split(" "));
}
},

/**
* Removes all classes from button.
*/
_cleanClasses() {
DomUtil.removeClass(this._container, "requesting");
DomUtil.removeClass(this._container, "active");
DomUtil.removeClass(this._container, "following");
this._container.classList.remove("requesting", "active", "following");

removeClasses(this._icon, this.options.iconLoading);
addClasses(this._icon, this.options.icon);
this._icon.classList.remove(...this.options.iconLoading.split(" "));
this._icon.classList.add(...this.options.icon.split(" "));
},

/**
Expand Down
Loading