leaflet-defaulticon-compatibility
Version:
Retrieve all Leaflet Default Icon options from CSS, in particular all icon images URL's, to improve compatibility with bundlers and frameworks that modify URL's in CSS.
2 lines (1 loc) • 1.66 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("leaflet")):"function"==typeof define&&define.amd?define(["leaflet"],e):e(t.L)}(this,function(c){"use strict";function e(t,e){var n,i,o=c.DomUtil.create("div",t,document.body),r=function(t,e){var n=/url\(['"]?([^"']*?)['"]?\)/gi,i=[],o=n.exec(t);for(;o;)i.push(e?(r=o[1]).substr(r.lastIndexOf("/")+1):o[1]),o=n.exec(t);var r;return i}((i=p(n=o,"background-image"))&&"none"!==i?i:p(n,"cursor"),e),l=f(o,"width"),s=f(o,"height"),a=f(o,"margin-left"),u=f(o,"margin-top");return o.parentNode.removeChild(o),{Url:r[0],RetinaUrl:r[1],Size:[l,s],Anchor:[-a,-u]}}function n(t){var e=c.DomUtil.create("div",t,document.body),n=f(e,"margin-left"),i=f(e,"margin-top");return e.parentNode.removeChild(e),{Anchor:[n,i]}}function f(t,e){return parseInt(p(t,e),10)}function p(t,e){return c.DomUtil.getStyle(t,e)||c.DomUtil.getStyle(t,e.replace(/-(\w)/g,function(t,e){return e.toUpperCase()}))}c.Icon.Default.mergeOptions({iconUrl:null,iconRetinaUrl:null,shadowUrl:null,iconSize:null,iconAnchor:null,popupAnchor:null,tooltipAnchor:null,shadowSize:null,classNamePrefix:"leaflet-default-icon-"}),c.Icon.Default.include({_needsInit:!0,_getIconUrl:function(t){var e=this.options.imagePath||c.Icon.Default.imagePath||"";return this._needsInit&&this._initializeOptions(e),e+c.Icon.prototype._getIconUrl.call(this,t)},_initializeOptions:function(t){this._setOptions("icon",e,t),this._setOptions("shadow",e,t),this._setOptions("popup",n),this._setOptions("tooltip",n),this._needsInit=!1},_setOptions:function(t,e,n){var i=this.options,o=e(i.classNamePrefix+t,n);for(var r in o)i[t+r]=i[t+r]||o[r]}})});