maparea 0.1.16 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +29 -26
- package/dist/index.mjs +1 -1
- package/index.ts +1 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const{PI:e}=Math,t=e/180,n=180/e,r={spherical:0,ellipsoidal:.0818191908426},i=-85.05,a=85.05,o=-180,s=180,{PI:c,sin:l,tan:u,atan:d,pow:f,exp:p,log:m,abs:h}=Math;var g=class{_p;_c;_cc;_r=new Set;constructor(e){this._p=e,this.container.style=`position: relative; overflow: hidden;`,this.container.dataset.type=`maparea`}render(){for(let e of this._r)e(this)}onRender(e,t=!1){return this._r.add(e),t||e(this),()=>{this._r.delete(e)}}getOptions(){return{...this._p,center:this.center,bounds:this.bounds}}setOptions(e){this._p={...this._p,...e},`container`in e&&delete this._c,delete this._cc,this.render()}get container(){if(!this._c){let e=document.querySelector(this._p.container);if(!e)throw Error(`missing container element`);this._c=e}return this._c}get box(){let e=this.container.getBoundingClientRect();return{x:e.left,y:e.top,w:e.width,h:e.height}}get bounds(){let e=this._p.bounds;return e?{...e}:{}}set bounds(e){this._p.bounds=e}get center(){let e=this._p.center;return e?e.slice():[0,0]}set center(e){this._p.center=e,delete this._cc,this.render()}get centerCoords(){if(!this._cc){let[e,t]=this.center;this._cc=this.toPixelCoords(e,t)}return this._cc}get zoom(){return this._p.zoom??this._p.minZoom??0}set zoom(e){let{minZoom:t,maxZoom:n}=this,r=e;e<t&&(r=t),e>n&&(r=n),this._p.zoom=r,delete this._cc,this.render()}get minZoom(){return this._p.minZoom??-1/0}set minZoom(e){this._p.minZoom=e,this.zoom<this.minZoom&&(this.zoom=this.minZoom)}get maxZoom(){return this._p.maxZoom??1/0}set maxZoom(e){this._p.maxZoom=e,this.zoom>this.maxZoom&&(this.zoom=this.maxZoom)}get projection(){return this._p.projection??`spherical`}set projection(e){this._p.projection=e,this.render()}get lang(){return this._p.lang??``}set lang(e){this._p.lang=e,this.render()}inPixelBounds(e,t){let[n,r]=this.toGeoCoords(e,t);return this.inBounds(n,r)}inBounds(e,t){let{minLat:n=i,maxLat:r=a,minLon:s=o,maxLon:c=180}=this.bounds;return e>=n&&e<=r&&t>=s&&t<=c}canMoveTo(e,t){if(!this.inBounds(e,t))return!1;let{w:n,h:r}=this.box;if(n===0||r===0)return!1;let[i,a]=this.toPixelCoords(e,t);return this.inPixelBounds(i-n/2,a-r/2)&&this.inPixelBounds(i+n/2,a+r/2)}toPixelCoords(e,n){let i=r[this.projection],a=f(2,this.zoom+7)/c,o=e*t,s=i===0?1:f((1-i*l(o))/(1+i*l(o)),i/2);return[a*(c+n*t),a*(c-m(s*u(c/4+o/2)))]}toGeoCoords(e,t){let o=r[this.projection],s=f(2,this.zoom+7)/c,l=(e/s-c)*n,u;if(o===0)u=2*(d(p(c-t/s))-c/4)*n;else{u=i;let e=a-i,n=NaN,r=0;for(;(Number.isNaN(n)||h(n-t)>.1)&&r++<30;)e/=2,n=this.toPixelCoords(u+e,l)[1],n>t&&(u+=e)}return(l<-180||l>=180)&&(l=(l+180)%360-180),(u<-90||u>=90)&&(u=(u+90)%180-90),[u,l]}};function _(e){let t=`touches`in e?e.touches[0]:e;return t?[t.clientX,t.clientY]:null}function v(e,t){return t===void 0||!(e instanceof HTMLElement)?!1:typeof t==`function`&&t(e)||typeof t==`string`&&e.closest(t)!==null}function y(e,t,n){let r=Date.now(),i=null,a=e=>{r=Date.now(),i=_(e)},o=a=>{if(v(`touches`in a?a.touches[0]?.target:a.target,n)||!i||Date.now()-r>150)return;let{box:o,centerCoords:[s,c]}=e,l=i[0]-o.x,u=i[1]-o.y,[d,f]=e.toGeoCoords(l-.5*o.w+s,u-.5*o.h+c);t({x:l,y:u,lat:d,lon:f,originalEvent:a}),i=null};return e.container.addEventListener(`mousedown`,a),e.container.addEventListener(`mouseup`,o),e.container.addEventListener(`touchstart`,a),e.container.addEventListener(`touchend`,o),()=>{e.container.removeEventListener(`mousedown`,a),e.container.removeEventListener(`mouseup`,o),e.container.removeEventListener(`touchstart`,a),e.container.removeEventListener(`touchend`,o)}}function b(){return Math.random().toString(36).slice(2)}function x(e,{id:t,className:n,inset:r=`0`}={}){let i=`${n?`.${n}`:``}${t?`[data-id="${t}"]`:``}`,a=document.querySelector(i);return a||(a=document.createElement(`div`),a.className=`${n?`${n} `:``}layer`,a.dataset.id=t??b(),a.style=`position: absolute; inset: ${r};`,e.container.append(a)),a}function S(e,t){return t===void 0?t:t instanceof Function?t(e):t}function C(e,t){return e.toFixed(t).replace(/\.(\d*[^0])0+$/,`.$1`).replace(/\.0+$/,``)}function w(e,t,{className:n,coords:r,inset:i,layer:a,content:o}){let s=a??x(e,{className:`elements`});n&&t.setAttribute(`class`,n),t.style.position=`absolute`,s.append(t),e.onRender(()=>{if(i)t.style.inset=S(e,i)??``;else if(r){let{centerCoords:[n,i],box:{w:a,h:o}}=e,[s,c]=e.toPixelCoords(...r),l=C(s-n+a/2,2),u=C(c-i+o/2,2);t.style.transform=`translate3d(${l}px, ${u}px, 0)`}if(o){let n=S(e,o)??``;t.innerHTML!==n&&(t.innerHTML=n)}})}function T(e,{onStart:t,onMove:n,onEnd:r,wheel:i}={}){let a=null,o=null,s=Date.now(),c=null,l=null,u=!1,d=!1,f=0,p=0;function m(t,r,i=100){f+=t,p+=r,a!==null&&(a-=t),o!==null&&(o-=r),!d&&!e.dataset.dragged&&(e.dataset.dragged=`true`);let l=Date.now();(f!==0||p!==0)&&l-s>=i&&(c&&cancelAnimationFrame(c),c=requestAnimationFrame(()=>{n?.(f,p),f=0,p=0,s=l,c=null}))}function h(e,t,n){a!==null&&o!==null&&m(a-e,o-t,n)}function g(e,n){l!==null&&(clearTimeout(l),d=!1,l=null),u=!0,t?.(),e!==void 0&&(a=e),n!==void 0&&(o=n),s=Date.now(),f=0,p=0}function _(t,n){u=!1,t!==void 0&&n!==void 0&&h(t,n),delete e.dataset.dragged,a=null,o=null,r?.()}e.dataset.draggable=`true`;let v=null,y=null;e.addEventListener(`mousedown`,t=>{v||(t.preventDefault(),g(t.pageX,t.pageY),v=e=>{e.preventDefault(),h(e.pageX,e.pageY)},e.addEventListener(`mousemove`,v))}),e.addEventListener(`mouseup`,t=>{v&&=(t.preventDefault(),_(t.pageX,t.pageY),e.removeEventListener(`mousemove`,v),null)}),e.addEventListener(`touchstart`,t=>{y||(t.preventDefault(),g(t.touches[0]?.pageX,t.touches[0]?.pageY),y=e=>{e.preventDefault(),h(e.touches[0]?.pageX,e.touches[0]?.pageY)},e.addEventListener(`touchmove`,y))}),e.addEventListener(`touchend`,t=>{y&&=(t.preventDefault(),_(t.touches[0]?.pageX,t.touches[0]?.pageY),e.removeEventListener(`touchmove`,y),null)}),e.addEventListener(`touchcancel`,t=>{y&&=(t.preventDefault(),_(t.touches[0]?.pageX,t.touches[0]?.pageY),e.removeEventListener(`touchmove`,y),null)}),i&&e.addEventListener(`wheel`,e=>{e.preventDefault(),u||(g(),d=!0),e.shiftKey?m(e.deltaY,e.deltaX,10):m(e.deltaX,e.deltaY,10),l!==null&&clearTimeout(l),l=setTimeout(()=>{_(),d=!1,l=null},200)})}function E(e,t){let n=0,r=0;T(e.container,{onStart(){[n,r]=e.centerCoords},onMove(t,i){let a=n+t,o=r+i,s=e.toGeoCoords(a,o);e.canMoveTo(...s)&&(e.center=s,n=a,r=o)},wheel:t?.wheel??!0})}function D(e,{storageKey:t,session:n=!1}={}){let r=n?window.sessionStorage:window.localStorage,i=t??[`maparea`,e.container.id].filter(Boolean).join(`#`),a=e.getOptions(),o=!1,s=()=>{try{r.setItem(i,JSON.stringify(e.getOptions()))}catch{}},c=()=>{try{let t=r.getItem(i);t===null?s():e.setOptions(JSON.parse(t))}catch{}},l=e=>{o=e===void 0?!o:e},u=()=>{e.setOptions(a)};c();let d=null;return e.onRender(()=>{o||(d&&=(clearTimeout(d),null),d=setTimeout(()=>{d=null,s()},350))}),{sync:c,write:s,toggle:l,reset:u}}function O(e,t){let n=new ResizeObserver(n=>{for(let r of n)if(r.contentBoxSize){if(t)if(r.contentBoxSize){let{inlineSize:e,blockSize:n}=r.contentBoxSize[0]??r.contentBoxSize;t({inlineSize:e,blockSize:n})}else{let{width:e,height:n}=r.contentRect;t({width:e,height:n})}e.render()}});return n.observe(e.container),()=>{n.unobserve(e.container)}}const k=`http://www.w3.org/2000/svg`,A={className:`shape`};function j(e,t,n){let r=x(e,n??A);r.toggleAttribute(`hidden`,t.length===0);let i=r.querySelector(`svg`);i||(i=document.createElementNS(k,`svg`),i.setAttribute(`xmlns`,k),r.append(i));let a=N(i,t.map(({id:t,coords:[n,r]})=>({id:t,coords:e.toPixelCoords(n,r)})),n);if(a){let{xMin:t,xMax:n,yMin:r,yMax:o}=a,{centerCoords:[s,c],box:l}=e,u=C(n-t,3),d=C(o-r,3),f=C(.5*l.w+t-s,2),p=C(.5*l.h+r-c,2);i.setAttribute(`viewBox`,`0 0 ${u} ${d}`),i.setAttribute(`width`,u),i.setAttribute(`height`,d),i.setAttribute(`style`,`position: absolute; transform: translate3d(${f}px, ${p}px, 0);`)}return r}function M(e){return parseFloat(window.getComputedStyle(e).strokeWidth||e.getAttribute(`stroke-width`)||`1`)}function N(e,t,n){if(t.length===0){e.innerHTML=``;return}let r=e.querySelector(`path`);r||(r=document.createElementNS(k,`path`),e.append(r));let i=t[0].coords[0],a=i,o=t[0].coords[1],s=o,c=``;for(let{coords:[e,n]}of t)i>e&&(i=e),a<e&&(a=e),o>n&&(o=n),s<n&&(s=n);i-=10,a+=10,o-=10,s+=10;let l=Array.from(e.querySelectorAll(`.marker`)),u=0;for(let{id:a,coords:[s,d]}of t){let t=C(s-i,3),f=C(d-o,3);if(c+=`${c?` L`:`M`} ${t} ${f}`,n?.markers){let n=l[u++];n||(n=document.createElementNS(k,`circle`),n.setAttribute(`class`,`marker`),n.setAttribute(`r`,String(1.5*M(r))),e.append(n)),n.setAttribute(`cx`,t),n.setAttribute(`cy`,f),n.dataset.id=a}}for(let e=u;e<l.length;e++)l[e].remove();return r.setAttribute(`d`,c),{xMin:i,xMax:a,yMin:o,yMax:s}}function P(e,t,n){let r=t.map(e=>({id:b(),coords:e}));e.onRender(()=>{j(e,r,{className:`shape`,...n})})}function F(e,t){let n=[],r=null,i={className:`shape-editor`,markers:!0},a=()=>{j(e,n,i),t?.onUpdate?.(n)};y(e,({lat:e,lon:t,originalEvent:i})=>{let o=(i.target?.closest(`.marker`))?.dataset.id;if(o&&r){clearTimeout(r),r=null,n=n.filter(({id:e})=>e!==o),a();return}let s=()=>{n.push({id:b(),coords:[e,t]}),a(),r=null};o?r=setTimeout(s,250):s()},t?.ignoreClicks),e.onRender(a)}const{floor:I,ceil:L,random:R}=Math,ee=256;function z(e,t,n){return`${t},${n},${e.zoom},${e.lang}`}function B(e){let t=e.target;t instanceof HTMLImageElement&&(t.style.opacity=``)}function V(e,t,n,{size:r,url:i,subdomains:a,retries:o=0,error:s}){let c=new Image,l=S(e,r)??256,u=0,d=(r,o)=>{if(!i)return``;if(typeof i==`function`)return i(e,t,n);let s=i.replaceAll(`{x}`,String(r)).replaceAll(`{y}`,String(o)).replaceAll(`{z}`,String(e.zoom)).replaceAll(`{lang}`,e.lang);return a&&s.includes(`{s}`)&&(s=s.replaceAll(`{s}`,a[I(a.length*R())])),s},f=t=>{let n=t.target;if(n instanceof HTMLImageElement){if(u++<o){let e=new URL(n.src);e.searchParams.set(`_t`,String(Date.now())),n.src=e.href;return}let t=S(e,s);t&&(n.dataset.src=n.src,n.src=t),c.removeEventListener(`error`,f)}};return c.width=l,c.height=l,c.src=d(t,n),c.dataset.id=z(e,t,n),c.style.position=`absolute`,c.addEventListener(`load`,B),c.addEventListener(`error`,f),c.complete||(c.style.opacity=`0`),c}function H(e){return e.querySelectorAll(`img[data-id]`)}function U(e,t){return e.querySelector(`img[data-id="${t}"]`)}function W(e,t={}){let{id:n=b(),attribution:r,attributionInset:i=`auto 0 0 auto`}=t,a=x(e,{id:n,className:`tiles`,...t}),o=x(e,{id:n,className:`tiles-attribution`,inset:i}),s=()=>{let{box:{w:n,h:i},centerCoords:[s,c]}=e,{size:l,margin:u=0}=t,d=S(e,l)??256,f=Array.isArray(u)?u[0]:u,p=Array.isArray(u)?u[1]:u,m=L((n+2*f)/d),h=L((i+2*p)/d),g=I(s/d),_=I(c/d),v=null,y=new Set,b=``;for(let r=0;r<=m;r++){let o=g+(r%2==0?-1:1)*I(r/2);for(let r=0;r<=h;r++){let l=_+(r%2==0?-1:1)*I(r/2);b=z(e,o,l),v=U(a,b),v||(v=V(e,o,l,t),a.append(v));let u=C(.5*n+o*d-s,2),f=C(.5*i+l*d-c,2);v.style.transform=`translate3d(${u}px, ${f}px, 0)`,y.add(b)}}for(let e of H(a)){let{id:t}=e.dataset;t&&!y.has(t)&&e.remove()}let x=S(e,r)??``;o.toggleAttribute(`hidden`,!x),o.innerHTML!==x&&(o.innerHTML=x)},c=e.zoom,l=null;return e.onRender(()=>{l!==null&&(clearTimeout(l),l=null),e.zoom===c?(a.style.opacity&&(a.style.opacity=``),s()):(a.style.opacity=`0`,l=setTimeout(()=>{l=null,a.style.opacity&&(a.style.opacity=``),s()},300)),c=e.zoom}),a}function G(e,t={}){let{plus:n=`➕`,minus:r=`➖`,inset:i=`0 0 auto auto`}=t,a=x(e,{className:`zoom-controls`,inset:i}),o=document.createElement(`button`);o.dataset.id=`plus`,o.innerHTML=n;let s=document.createElement(`button`);s.dataset.id=`minus`,s.innerHTML=r;let c=()=>{o.toggleAttribute(`disabled`,e.zoom+1>e.maxZoom),s.toggleAttribute(`disabled`,e.zoom-1<e.minZoom)};return y(e,({originalEvent:t})=>{let n=t.target?.closest(`button`);n&&(t.stopPropagation(),e.zoom+=n.dataset.id===`minus`?-1:1,c())}),e.onRender(c),a.append(o,s),a}function K(e){if(e.length===0)return{};let t=e[0][0],n=t,r=e[0][1],i=r;for(let[a,o]of e)t>a&&(t=a),n<a&&(n=a),r>o&&(r=o),i<o&&(i=o);return{minLat:t,maxLat:n,minLon:r,maxLon:i}}function q(e){return Array.isArray(e)&&e.length===2&&typeof e[0]==`number`&&typeof e[1]==`number`}function J(e){return q(e)?{minLat:e[0],maxLat:e[0],minLon:e[1],maxLon:e[1]}:Array.isArray(e)?K(e):e}function Y(e){let{minLat:t,maxLat:n,minLon:r,maxLon:i}=J(e);return[t===void 0||n===void 0?0:(n+t)/2,r===void 0||i===void 0?0:(i+r)/2]}const{abs:X,log2:Z,min:Q,floor:$}=Math;function te(e,t){let{box:{w:n,h:r},center:[i,a],zoom:o}=e,{minLat:s,maxLat:c,minLon:l,maxLon:u}=t,d=0,f=0;if(l!==void 0&&u!==void 0){let t=X(e.toPixelCoords(i,u)[0]-e.toPixelCoords(i,l)[0]);t!==0&&(d=Z(n/t))}if(s!==void 0&&c!==void 0){let t=X(e.toPixelCoords(s,a)[1]-e.toPixelCoords(c,a)[1]);t!==0&&(f=Z(r/t))}let p=$(Q(o+d,o+f)),m={center:Y(t)};p!==o&&(m.zoom=p),e.setOptions(m)}const ne=[.005,.018];function re(e,t=ne){let{minLat:n=0,maxLat:r=0,minLon:i=0,maxLon:a=0}=J(e),[o,s]=t;return{minLat:n-o,maxLat:r+o,minLon:i-s,maxLon:a+s}}exports.DEG=n,exports.MAX_LAT=a,exports.MAX_LON=180,exports.MIN_LAT=i,exports.MIN_LON=o,exports.MapArea=g,exports.RAD=t,exports.addClickListener=y,exports.addElement=w,exports.addMovableViewport=E,exports.addPersistence=D,exports.addResizeObserver=O,exports.addShape=P,exports.addShapeEditor=F,exports.addTiles=W,exports.addZoomControls=G,exports.eccentricityMap=r,exports.fitGeoBounds=te,exports.getCenter=Y,exports.getGeoBounds=K,exports.getId=b,exports.getLayer=x,exports.getPointerPosition=_,exports.getVicinity=re,exports.isCoordsArray=q,exports.resolveDynamic=S,exports.toGeoBounds=J,exports.toPrecision=C;
|
|
1
|
+
const{PI:e}=Math,t=e/180,n=180/e,r={spherical:0,ellipsoidal:.0818191908426},i=-85.05,a=85.05,o=-180,s=180,{PI:c,sin:l,tan:u,atan:d,pow:f,exp:p,log:m,abs:h}=Math;var g=class{_p;_c;_cc;_r=new Set;constructor(e){this._p=e,this.container.style=`position: relative; overflow: hidden;`,this.container.dataset.type=`maparea`}render(){for(let e of this._r)e(this)}onRender(e,t=!1){return this._r.add(e),t||e(this),()=>{this._r.delete(e)}}getOptions(){return{...this._p,center:this.center,bounds:this.bounds}}setOptions(e){this._p={...this._p,...e},`container`in e&&delete this._c,delete this._cc,this.render()}get container(){if(!this._c){let e=document.querySelector(this._p.container);if(!e)throw Error(`missing container element`);this._c=e}return this._c}get box(){let e=this.container.getBoundingClientRect();return{x:e.left,y:e.top,w:e.width,h:e.height}}get bounds(){let e=this._p.bounds;return e?{...e}:{}}set bounds(e){this._p.bounds=e}get center(){let e=this._p.center;return e?e.slice():[0,0]}set center(e){this._p.center=e,delete this._cc,this.render()}get centerCoords(){if(!this._cc){let[e,t]=this.center;this._cc=this.toPixelCoords(e,t)}return this._cc}get zoom(){return this._p.zoom??this._p.minZoom??0}set zoom(e){let{minZoom:t,maxZoom:n}=this,r=e;e<t&&(r=t),e>n&&(r=n),this._p.zoom=r,delete this._cc,this.render()}get minZoom(){return this._p.minZoom??-1/0}set minZoom(e){this._p.minZoom=e,this.zoom<this.minZoom&&(this.zoom=this.minZoom)}get maxZoom(){return this._p.maxZoom??1/0}set maxZoom(e){this._p.maxZoom=e,this.zoom>this.maxZoom&&(this.zoom=this.maxZoom)}get projection(){return this._p.projection??`spherical`}set projection(e){this._p.projection=e,this.render()}get lang(){return this._p.lang??``}set lang(e){this._p.lang=e,this.render()}inPixelBounds(e,t){let[n,r]=this.toGeoCoords(e,t);return this.inBounds(n,r)}inBounds(e,t){let{minLat:n=i,maxLat:r=a,minLon:s=o,maxLon:c=180}=this.bounds;return e>=n&&e<=r&&t>=s&&t<=c}canMoveTo(e,t){if(!this.inBounds(e,t))return!1;let{w:n,h:r}=this.box;if(n===0||r===0)return!1;let[i,a]=this.toPixelCoords(e,t);return this.inPixelBounds(i-n/2,a-r/2)&&this.inPixelBounds(i+n/2,a+r/2)}toPixelCoords(e,n){let i=r[this.projection],a=f(2,this.zoom+7)/c,o=e*t,s=i===0?1:f((1-i*l(o))/(1+i*l(o)),i/2);return[a*(c+n*t),a*(c-m(s*u(c/4+o/2)))]}toGeoCoords(e,t){let o=r[this.projection],s=f(2,this.zoom+7)/c,l=(e/s-c)*n,u;if(o===0)u=2*(d(p(c-t/s))-c/4)*n;else{u=i;let e=a-i,n=NaN,r=0;for(;(Number.isNaN(n)||h(n-t)>.1)&&r++<30;)e/=2,n=this.toPixelCoords(u+e,l)[1],n>t&&(u+=e)}return(l<-180||l>=180)&&(l=(l+180)%360-180),(u<-90||u>=90)&&(u=(u+90)%180-90),[u,l]}};function _(){return Math.random().toString(36).slice(2)}function v(e,{id:t,className:n,inset:r=`0`}={}){let i=`${n?`.${n}`:``}${t?`[data-id="${t}"]`:``}`,a=document.querySelector(i);return a||(a=document.createElement(`div`),a.className=`${n?`${n} `:``}layer`,a.dataset.id=t??_(),a.style=`position: absolute; inset: ${r};`,e.container.append(a)),a}function y(e,t){return t===void 0?t:t instanceof Function?t(e):t}function b(e,t){return e.toFixed(t).replace(/\.(\d*[^0])0+$/,`.$1`).replace(/\.0+$/,``)}function x(e,t,{className:n,coords:r,inset:i,layer:a,content:o}){let s=a??v(e,{className:`elements`});n&&t.setAttribute(`class`,n),t.style.position=`absolute`,s.append(t),e.onRender(()=>{if(i)t.style.inset=y(e,i)??``;else if(r){let{centerCoords:[n,i],box:{w:a,h:o}}=e,[s,c]=e.toPixelCoords(...r),l=b(s-n+a/2,2),u=b(c-i+o/2,2);t.style.transform=`translate3d(${l}px, ${u}px, 0)`}if(o){let n=y(e,o)??``;t.innerHTML!==n&&(t.innerHTML=n)}})}function S(e,t){return t===void 0||!(e instanceof HTMLElement)?!1:typeof t==`function`&&t(e)||typeof t==`string`&&e.closest(t)!==null}function C(e,{onStart:t,onMove:n,onEnd:r,wheel:i,ignore:a}={}){let o=null,s=null,c=Date.now(),l=null,u=null,d=!1,f=!1,p=0,m=0,h=0;function g(t,r){m+=t,h+=r,o!==null&&(o-=t),s!==null&&(s-=r),!f&&!e.dataset.dragged&&(e.dataset.dragged=`true`);let i=Date.now(),a=f?10:100;(m!==0||h!==0)&&i-c>=a&&(l&&cancelAnimationFrame(l),l=requestAnimationFrame(()=>{n?.(m,h),m=0,h=0,c=i,l=null}))}function _(e,t){o!==null&&s!==null&&g(o-e,s-t)}function v(e,n){u!==null&&(clearTimeout(u),f=!1,u=null),d=!0,t?.(),e!==void 0&&(o=e),n!==void 0&&(s=n),c=Date.now(),m=0,h=0}function y(t,n){d=!1,t!==void 0&&n!==void 0&&_(t,n),delete e.dataset.dragged,o=null,s=null,r?.()}e.dataset.draggable=`true`;function b(e){return!S(e.target,a)&&p===1}function x(e){p++,b(e)&&(e.preventDefault(),v(e.pageX,e.pageY))}function C(e){b(e)&&(e.preventDefault(),_(e.pageX,e.pageY))}function w(e){b(e)&&(e.preventDefault(),y(e.pageX,e.pageY)),p=0}function T(e){S(e.target,a)||(e.preventDefault(),d||(v(),f=!0),e.shiftKey?g(e.deltaY,e.deltaX):g(e.deltaX,e.deltaY),u!==null&&clearTimeout(u),u=setTimeout(()=>{y(),f=!1,u=null},200))}return e.addEventListener(`pointerdown`,x),e.addEventListener(`pointermove`,C),e.addEventListener(`pointerup`,w),e.addEventListener(`pointercancel`,w),i&&e.addEventListener(`wheel`,T),()=>{e.removeEventListener(`pointerdown`,x),e.removeEventListener(`pointermove`,C),e.removeEventListener(`pointerup`,w),e.removeEventListener(`pointercancel`,w),i&&e.removeEventListener(`wheel`,T)}}function w(e,{wheel:t=!0,ignore:n}={}){let r=0,i=0;C(e.container,{onStart(){[r,i]=e.centerCoords},onMove(t,n){let a=r+t,o=i+n,s=e.toGeoCoords(a,o);e.canMoveTo(...s)&&(e.center=s,r=a,i=o)},wheel:t,ignore:n})}function T(e,{storageKey:t,session:n=!1}={}){let r=n?window.sessionStorage:window.localStorage,i=t??[`maparea`,e.container.id].filter(Boolean).join(`#`),a=e.getOptions(),o=!1,s=()=>{try{r.setItem(i,JSON.stringify(e.getOptions()))}catch{}},c=()=>{try{let t=r.getItem(i);t===null?s():e.setOptions(JSON.parse(t))}catch{}},l=e=>{o=e===void 0?!o:e},u=()=>{e.setOptions(a)};c();let d=null;return e.onRender(()=>{o||(d&&=(clearTimeout(d),null),d=setTimeout(()=>{d=null,s()},350))}),{sync:c,write:s,toggle:l,reset:u}}function E(e,t,{ignore:n}={}){let r=null,i=null,a=Date.now(),o=e=>{r=e.clientX,i=e.clientY,a=Date.now()},s=o=>{if(S(o.target,n)||r===null||i===null||Date.now()-a>150)return;let{box:s,centerCoords:[c,l]}=e,u=r-s.x,d=i-s.y,[f,p]=e.toGeoCoords(u-.5*s.w+c,d-.5*s.h+l);t({x:u,y:d,lat:f,lon:p,originalEvent:o}),r=null,i=null};return e.container.addEventListener(`pointerdown`,o),e.container.addEventListener(`pointerup`,s),e.container.addEventListener(`pointercancel`,s),()=>{e.container.removeEventListener(`pointerdown`,o),e.container.removeEventListener(`pointerup`,s),e.container.removeEventListener(`pointercancel`,s)}}function D(e,t){let n=new ResizeObserver(n=>{for(let r of n)if(r.contentBoxSize){if(t)if(r.contentBoxSize){let{inlineSize:e,blockSize:n}=r.contentBoxSize[0]??r.contentBoxSize;t({inlineSize:e,blockSize:n})}else{let{width:e,height:n}=r.contentRect;t({width:e,height:n})}e.render()}});return n.observe(e.container),()=>{n.unobserve(e.container)}}const O=`http://www.w3.org/2000/svg`,k={className:`shape`};function A(e,t,n){let r=v(e,n??k);r.toggleAttribute(`hidden`,t.length===0);let i=r.querySelector(`svg`);i||(i=document.createElementNS(O,`svg`),i.setAttribute(`xmlns`,O),r.append(i));let a=M(i,t.map(({id:t,coords:[n,r]})=>({id:t,coords:e.toPixelCoords(n,r)})),n);if(a){let{xMin:t,xMax:n,yMin:r,yMax:o}=a,{centerCoords:[s,c],box:l}=e,u=b(n-t,3),d=b(o-r,3),f=b(.5*l.w+t-s,2),p=b(.5*l.h+r-c,2);i.setAttribute(`viewBox`,`0 0 ${u} ${d}`),i.setAttribute(`width`,u),i.setAttribute(`height`,d),i.setAttribute(`style`,`position: absolute; transform: translate3d(${f}px, ${p}px, 0);`)}return r}function j(e){return parseFloat(window.getComputedStyle(e).strokeWidth||e.getAttribute(`stroke-width`)||`1`)}function M(e,t,n){if(t.length===0){e.innerHTML=``;return}let r=e.querySelector(`path`);r||(r=document.createElementNS(O,`path`),e.append(r));let i=t[0].coords[0],a=i,o=t[0].coords[1],s=o,c=``;for(let{coords:[e,n]}of t)i>e&&(i=e),a<e&&(a=e),o>n&&(o=n),s<n&&(s=n);i-=10,a+=10,o-=10,s+=10;let l=Array.from(e.querySelectorAll(`.marker`)),u=0;for(let{id:a,coords:[s,d]}of t){let t=b(s-i,3),f=b(d-o,3);if(c+=`${c?` L`:`M`} ${t} ${f}`,n?.markers){let n=l[u++];n||(n=document.createElementNS(O,`circle`),n.setAttribute(`class`,`marker`),n.setAttribute(`r`,String(1.5*j(r))),e.append(n)),n.setAttribute(`cx`,t),n.setAttribute(`cy`,f),n.dataset.id=a}}for(let e=u;e<l.length;e++)l[e].remove();return r.setAttribute(`d`,c),{xMin:i,xMax:a,yMin:o,yMax:s}}function N(e,t,n){let r=t.map(e=>({id:_(),coords:e}));e.onRender(()=>{A(e,r,{className:`shape`,...n})})}function P(e,t){let n=[],r=null,i={className:`shape-editor`,markers:!0},a=()=>{A(e,n,i),t?.onUpdate?.(n)};E(e,({lat:e,lon:t,originalEvent:i})=>{let o=(i.target?.closest(`.marker`))?.dataset.id;if(o&&r){clearTimeout(r),r=null,n=n.filter(({id:e})=>e!==o),a();return}let s=()=>{n.push({id:_(),coords:[e,t]}),a(),r=null};o?r=setTimeout(s,250):s()},{ignore:t?.ignore}),e.onRender(a)}const{floor:F,ceil:I,random:L}=Math,ee=256;function R(e,t,n){return`${t},${n},${e.zoom},${e.lang}`}function z(e){let t=e.target;t instanceof HTMLImageElement&&(t.style.opacity=``)}function B(e,t,n,{size:r,url:i,subdomains:a,retries:o=0,error:s}){let c=new Image,l=y(e,r)??256,u=0,d=(r,o)=>{if(!i)return``;if(typeof i==`function`)return i(e,t,n);let s=i.replaceAll(`{x}`,String(r)).replaceAll(`{y}`,String(o)).replaceAll(`{z}`,String(e.zoom)).replaceAll(`{lang}`,e.lang);return a&&s.includes(`{s}`)&&(s=s.replaceAll(`{s}`,a[F(a.length*L())])),s},f=t=>{let n=t.target;if(n instanceof HTMLImageElement){if(u++<o){let e=new URL(n.src);e.searchParams.set(`_t`,String(Date.now())),n.src=e.href;return}let t=y(e,s);t&&(n.dataset.src=n.src,n.src=t),c.removeEventListener(`error`,f)}};return c.width=l,c.height=l,c.src=d(t,n),c.dataset.id=R(e,t,n),c.style.position=`absolute`,c.addEventListener(`load`,z),c.addEventListener(`error`,f),c.complete||(c.style.opacity=`0`),c}function V(e){return e.querySelectorAll(`img[data-id]`)}function H(e,t){return e.querySelector(`img[data-id="${t}"]`)}function U(e,t={}){let{id:n=_(),attribution:r,attributionInset:i=`auto 0 0 auto`}=t,a=v(e,{id:n,className:`tiles`,...t}),o=v(e,{id:n,className:`tiles-attribution`,inset:i}),s=()=>{let{box:{w:n,h:i},centerCoords:[s,c]}=e,{size:l,margin:u=0}=t,d=y(e,l)??256,f=Array.isArray(u)?u[0]:u,p=Array.isArray(u)?u[1]:u,m=I((n+2*f)/d),h=I((i+2*p)/d),g=F(s/d),_=F(c/d),v=null,x=new Set,S=``;for(let r=0;r<=m;r++){let o=g+(r%2==0?-1:1)*F(r/2);for(let r=0;r<=h;r++){let l=_+(r%2==0?-1:1)*F(r/2);S=R(e,o,l),v=H(a,S),v||(v=B(e,o,l,t),a.append(v));let u=b(.5*n+o*d-s,2),f=b(.5*i+l*d-c,2);v.style.transform=`translate3d(${u}px, ${f}px, 0)`,x.add(S)}}for(let e of V(a)){let{id:t}=e.dataset;t&&!x.has(t)&&e.remove()}let C=y(e,r)??``;o.toggleAttribute(`hidden`,!C),o.innerHTML!==C&&(o.innerHTML=C)},c=e.zoom,l=null;return e.onRender(()=>{l!==null&&(clearTimeout(l),l=null),e.zoom===c?(a.style.opacity&&(a.style.opacity=``),s()):(a.style.opacity=`0`,l=setTimeout(()=>{l=null,a.style.opacity&&(a.style.opacity=``),s()},300)),c=e.zoom}),a}function W(e,t={}){let{plus:n=`➕`,minus:r=`➖`,inset:i=`0 0 auto auto`}=t,a=v(e,{className:`zoom-controls`,inset:i}),o=document.createElement(`button`);o.dataset.id=`plus`,o.innerHTML=n;let s=document.createElement(`button`);s.dataset.id=`minus`,s.innerHTML=r;let c=()=>{o.toggleAttribute(`disabled`,e.zoom+1>e.maxZoom),s.toggleAttribute(`disabled`,e.zoom-1<e.minZoom)};return E(e,({originalEvent:t})=>{let n=t.target?.closest(`button`);n&&(t.stopPropagation(),e.zoom+=n.dataset.id===`minus`?-1:1,c())}),e.onRender(c),a.append(o,s),a}function G(e){if(e.length===0)return{};let t=e[0][0],n=t,r=e[0][1],i=r;for(let[a,o]of e)t>a&&(t=a),n<a&&(n=a),r>o&&(r=o),i<o&&(i=o);return{minLat:t,maxLat:n,minLon:r,maxLon:i}}function K(e){return Array.isArray(e)&&e.length===2&&typeof e[0]==`number`&&typeof e[1]==`number`}function q(e){return K(e)?{minLat:e[0],maxLat:e[0],minLon:e[1],maxLon:e[1]}:Array.isArray(e)?G(e):e}function J(e){let{minLat:t,maxLat:n,minLon:r,maxLon:i}=q(e);return[t===void 0||n===void 0?0:(n+t)/2,r===void 0||i===void 0?0:(i+r)/2]}const{abs:Y,log2:X,min:Z,floor:Q}=Math;function $(e,t){let{box:{w:n,h:r},center:[i,a],zoom:o}=e,{minLat:s,maxLat:c,minLon:l,maxLon:u}=t,d=0,f=0;if(l!==void 0&&u!==void 0){let t=Y(e.toPixelCoords(i,u)[0]-e.toPixelCoords(i,l)[0]);t!==0&&(d=X(n/t))}if(s!==void 0&&c!==void 0){let t=Y(e.toPixelCoords(s,a)[1]-e.toPixelCoords(c,a)[1]);t!==0&&(f=X(r/t))}let p=Q(Z(o+d,o+f)),m={center:J(t)};p!==o&&(m.zoom=p),e.setOptions(m)}const te=[.005,.018];function ne(e,t=te){let{minLat:n=0,maxLat:r=0,minLon:i=0,maxLon:a=0}=q(e),[o,s]=t;return{minLat:n-o,maxLat:r+o,minLon:i-s,maxLon:a+s}}exports.DEG=n,exports.MAX_LAT=a,exports.MAX_LON=180,exports.MIN_LAT=i,exports.MIN_LON=o,exports.MapArea=g,exports.RAD=t,exports.addElement=x,exports.addMovableViewport=w,exports.addPersistence=T,exports.addPointerListener=E,exports.addResizeObserver=D,exports.addShape=N,exports.addShapeEditor=P,exports.addTiles=U,exports.addZoomControls=W,exports.eccentricityMap=r,exports.fitGeoBounds=$,exports.getCenter=J,exports.getGeoBounds=G,exports.getId=_,exports.getLayer=v,exports.getVicinity=ne,exports.isCoordsArray=K,exports.resolveDynamic=y,exports.toGeoBounds=q,exports.toPrecision=b;
|
package/dist/index.d.ts
CHANGED
|
@@ -78,22 +78,6 @@ declare class MapArea {
|
|
|
78
78
|
toGeoCoords(x: number, y: number): GeoCoords;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
type IgnoredElement = string | ((element: HTMLElement) => boolean);
|
|
82
|
-
|
|
83
|
-
type MapAreaClickEvent = {
|
|
84
|
-
x: number;
|
|
85
|
-
y: number;
|
|
86
|
-
lat: number;
|
|
87
|
-
lon: number;
|
|
88
|
-
originalEvent: MouseEvent | TouchEvent;
|
|
89
|
-
};
|
|
90
|
-
type MapAreaClickCallback = (event: MapAreaClickEvent) => void;
|
|
91
|
-
/**
|
|
92
|
-
* Adds a click listener to the map and returns a function that removes
|
|
93
|
-
* the added listener.
|
|
94
|
-
*/
|
|
95
|
-
declare function addClickListener(map: MapArea, callback: MapAreaClickCallback, ignored?: IgnoredElement): () => void;
|
|
96
|
-
|
|
97
81
|
type Dynamic<T> = T | ((map: MapArea) => T);
|
|
98
82
|
|
|
99
83
|
type MapAreaElementOptions = {
|
|
@@ -118,15 +102,21 @@ declare function addElement(map: MapArea, element: HTMLElement | SVGSVGElement,
|
|
|
118
102
|
content
|
|
119
103
|
}: MapAreaElementOptions): void;
|
|
120
104
|
|
|
105
|
+
type IgnoredElement = string | ((element: HTMLElement) => boolean);
|
|
106
|
+
|
|
121
107
|
type MapAreaNavigationOptions = {
|
|
122
108
|
wheel?: boolean;
|
|
109
|
+
ignore?: IgnoredElement;
|
|
123
110
|
};
|
|
124
111
|
/**
|
|
125
112
|
* Enables navigation over the given map container with a mouse or touches
|
|
126
113
|
* or a scroll wheel (the latter can be disabled by setting `options.wheel`
|
|
127
114
|
* to `false`).
|
|
128
115
|
*/
|
|
129
|
-
declare function addMovableViewport(map: MapArea,
|
|
116
|
+
declare function addMovableViewport(map: MapArea, {
|
|
117
|
+
wheel,
|
|
118
|
+
ignore
|
|
119
|
+
}?: MapAreaNavigationOptions): void;
|
|
130
120
|
|
|
131
121
|
type PersistenceOptions = {
|
|
132
122
|
storageKey?: string;
|
|
@@ -151,6 +141,25 @@ declare function addPersistence(map: MapArea, {
|
|
|
151
141
|
reset: () => void;
|
|
152
142
|
};
|
|
153
143
|
|
|
144
|
+
type MapAreaClickEvent = {
|
|
145
|
+
x: number;
|
|
146
|
+
y: number;
|
|
147
|
+
lat: number;
|
|
148
|
+
lon: number;
|
|
149
|
+
originalEvent: MouseEvent | TouchEvent;
|
|
150
|
+
};
|
|
151
|
+
type MapAreaClickCallback = (event: MapAreaClickEvent) => void;
|
|
152
|
+
type MapAreaClickOptions = {
|
|
153
|
+
ignore?: IgnoredElement;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Adds a pointer event listener to the map and returns a function that
|
|
157
|
+
* removes the added listener.
|
|
158
|
+
*/
|
|
159
|
+
declare function addPointerListener(map: MapArea, callback: MapAreaClickCallback, {
|
|
160
|
+
ignore
|
|
161
|
+
}?: MapAreaClickOptions): () => void;
|
|
162
|
+
|
|
154
163
|
type MapAreaResizeEvent = {
|
|
155
164
|
inlineSize?: number;
|
|
156
165
|
blockSize?: number;
|
|
@@ -183,8 +192,8 @@ type Vertex<T> = {
|
|
|
183
192
|
type GeoVertex = Vertex<GeoCoords>;
|
|
184
193
|
|
|
185
194
|
type ShapeEditorOptions = {
|
|
186
|
-
onUpdate?: (shape: GeoVertex[]) => void;
|
|
187
|
-
|
|
195
|
+
/** Adds a shape update callback. */onUpdate?: (shape: GeoVertex[]) => void; /** Map elements to be ignored in the shape editor when clicked. */
|
|
196
|
+
ignore?: IgnoredElement;
|
|
188
197
|
};
|
|
189
198
|
/**
|
|
190
199
|
* Adds a shape editor to the given map container.
|
|
@@ -265,12 +274,6 @@ declare function getLayer(map: MapArea, {
|
|
|
265
274
|
inset
|
|
266
275
|
}?: LayerOptions): HTMLElement;
|
|
267
276
|
|
|
268
|
-
/**
|
|
269
|
-
* Returns the pointer pixel position `[x, y]` of the given mouse or
|
|
270
|
-
* touch event.
|
|
271
|
-
*/
|
|
272
|
-
declare function getPointerPosition(event: MouseEvent | TouchEvent): [number, number] | null;
|
|
273
|
-
|
|
274
277
|
/**
|
|
275
278
|
* Returns the minimal and maximal latitudes and longitudes of a region
|
|
276
279
|
* surrounding a geographic area, an array of geographic coordinates,
|
|
@@ -292,4 +295,4 @@ declare function toGeoBounds(x: GeoCoords | GeoCoords[] | GeoBounds): GeoBounds;
|
|
|
292
295
|
|
|
293
296
|
declare function toPrecision(x: number, n: number): string;
|
|
294
297
|
|
|
295
|
-
export { BoxDimensions, DEG, Dynamic, GeoBounds, GeoCoords, GeoVertex, IgnoredElement, LayerOptions, MAX_LAT, MAX_LON, MIN_LAT, MIN_LON, MapArea, MapAreaClickCallback, MapAreaClickEvent, MapAreaElementOptions, MapAreaNavigationOptions, MapAreaOptions, MapAreaResizeCallback, MapAreaResizeEvent, MapAreaTileOptions, PersistenceOptions, PixelCoords, PixelVertex, Projection, RAD, RenderCallback, ShapeEditorOptions, ShapeLayerOptions, ZoomControlOptions,
|
|
298
|
+
export { BoxDimensions, DEG, Dynamic, GeoBounds, GeoCoords, GeoVertex, IgnoredElement, LayerOptions, MAX_LAT, MAX_LON, MIN_LAT, MIN_LON, MapArea, MapAreaClickCallback, MapAreaClickEvent, MapAreaClickOptions, MapAreaElementOptions, MapAreaNavigationOptions, MapAreaOptions, MapAreaResizeCallback, MapAreaResizeEvent, MapAreaTileOptions, PersistenceOptions, PixelCoords, PixelVertex, Projection, RAD, RenderCallback, ShapeEditorOptions, ShapeLayerOptions, ZoomControlOptions, addElement, addMovableViewport, addPersistence, addPointerListener, addResizeObserver, addShape, addShapeEditor, addTiles, addZoomControls, eccentricityMap, fitGeoBounds, getCenter, getGeoBounds, getId, getLayer, getVicinity, isCoordsArray, resolveDynamic, toGeoBounds, toPrecision };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const{PI:e}=Math,t=e/180,n=180/e,r={spherical:0,ellipsoidal:.0818191908426},i=-85.05,a=85.05,o=-180,s=180,{PI:c,sin:l,tan:u,atan:d,pow:f,exp:p,log:m,abs:h}=Math;var g=class{_p;_c;_cc;_r=new Set;constructor(e){this._p=e,this.container.style=`position: relative; overflow: hidden;`,this.container.dataset.type=`maparea`}render(){for(let e of this._r)e(this)}onRender(e,t=!1){return this._r.add(e),t||e(this),()=>{this._r.delete(e)}}getOptions(){return{...this._p,center:this.center,bounds:this.bounds}}setOptions(e){this._p={...this._p,...e},`container`in e&&delete this._c,delete this._cc,this.render()}get container(){if(!this._c){let e=document.querySelector(this._p.container);if(!e)throw Error(`missing container element`);this._c=e}return this._c}get box(){let e=this.container.getBoundingClientRect();return{x:e.left,y:e.top,w:e.width,h:e.height}}get bounds(){let e=this._p.bounds;return e?{...e}:{}}set bounds(e){this._p.bounds=e}get center(){let e=this._p.center;return e?e.slice():[0,0]}set center(e){this._p.center=e,delete this._cc,this.render()}get centerCoords(){if(!this._cc){let[e,t]=this.center;this._cc=this.toPixelCoords(e,t)}return this._cc}get zoom(){return this._p.zoom??this._p.minZoom??0}set zoom(e){let{minZoom:t,maxZoom:n}=this,r=e;e<t&&(r=t),e>n&&(r=n),this._p.zoom=r,delete this._cc,this.render()}get minZoom(){return this._p.minZoom??-1/0}set minZoom(e){this._p.minZoom=e,this.zoom<this.minZoom&&(this.zoom=this.minZoom)}get maxZoom(){return this._p.maxZoom??1/0}set maxZoom(e){this._p.maxZoom=e,this.zoom>this.maxZoom&&(this.zoom=this.maxZoom)}get projection(){return this._p.projection??`spherical`}set projection(e){this._p.projection=e,this.render()}get lang(){return this._p.lang??``}set lang(e){this._p.lang=e,this.render()}inPixelBounds(e,t){let[n,r]=this.toGeoCoords(e,t);return this.inBounds(n,r)}inBounds(e,t){let{minLat:n=i,maxLat:r=a,minLon:s=o,maxLon:c=180}=this.bounds;return e>=n&&e<=r&&t>=s&&t<=c}canMoveTo(e,t){if(!this.inBounds(e,t))return!1;let{w:n,h:r}=this.box;if(n===0||r===0)return!1;let[i,a]=this.toPixelCoords(e,t);return this.inPixelBounds(i-n/2,a-r/2)&&this.inPixelBounds(i+n/2,a+r/2)}toPixelCoords(e,n){let i=r[this.projection],a=f(2,this.zoom+7)/c,o=e*t,s=i===0?1:f((1-i*l(o))/(1+i*l(o)),i/2);return[a*(c+n*t),a*(c-m(s*u(c/4+o/2)))]}toGeoCoords(e,t){let o=r[this.projection],s=f(2,this.zoom+7)/c,l=(e/s-c)*n,u;if(o===0)u=2*(d(p(c-t/s))-c/4)*n;else{u=i;let e=a-i,n=NaN,r=0;for(;(Number.isNaN(n)||h(n-t)>.1)&&r++<30;)e/=2,n=this.toPixelCoords(u+e,l)[1],n>t&&(u+=e)}return(l<-180||l>=180)&&(l=(l+180)%360-180),(u<-90||u>=90)&&(u=(u+90)%180-90),[u,l]}};function _(e){let t=`touches`in e?e.touches[0]:e;return t?[t.clientX,t.clientY]:null}function v(e,t){return t===void 0||!(e instanceof HTMLElement)?!1:typeof t==`function`&&t(e)||typeof t==`string`&&e.closest(t)!==null}function y(e,t,n){let r=Date.now(),i=null,a=e=>{r=Date.now(),i=_(e)},o=a=>{if(v(`touches`in a?a.touches[0]?.target:a.target,n)||!i||Date.now()-r>150)return;let{box:o,centerCoords:[s,c]}=e,l=i[0]-o.x,u=i[1]-o.y,[d,f]=e.toGeoCoords(l-.5*o.w+s,u-.5*o.h+c);t({x:l,y:u,lat:d,lon:f,originalEvent:a}),i=null};return e.container.addEventListener(`mousedown`,a),e.container.addEventListener(`mouseup`,o),e.container.addEventListener(`touchstart`,a),e.container.addEventListener(`touchend`,o),()=>{e.container.removeEventListener(`mousedown`,a),e.container.removeEventListener(`mouseup`,o),e.container.removeEventListener(`touchstart`,a),e.container.removeEventListener(`touchend`,o)}}function b(){return Math.random().toString(36).slice(2)}function x(e,{id:t,className:n,inset:r=`0`}={}){let i=`${n?`.${n}`:``}${t?`[data-id="${t}"]`:``}`,a=document.querySelector(i);return a||(a=document.createElement(`div`),a.className=`${n?`${n} `:``}layer`,a.dataset.id=t??b(),a.style=`position: absolute; inset: ${r};`,e.container.append(a)),a}function S(e,t){return t===void 0?t:t instanceof Function?t(e):t}function C(e,t){return e.toFixed(t).replace(/\.(\d*[^0])0+$/,`.$1`).replace(/\.0+$/,``)}function w(e,t,{className:n,coords:r,inset:i,layer:a,content:o}){let s=a??x(e,{className:`elements`});n&&t.setAttribute(`class`,n),t.style.position=`absolute`,s.append(t),e.onRender(()=>{if(i)t.style.inset=S(e,i)??``;else if(r){let{centerCoords:[n,i],box:{w:a,h:o}}=e,[s,c]=e.toPixelCoords(...r),l=C(s-n+a/2,2),u=C(c-i+o/2,2);t.style.transform=`translate3d(${l}px, ${u}px, 0)`}if(o){let n=S(e,o)??``;t.innerHTML!==n&&(t.innerHTML=n)}})}function T(e,{onStart:t,onMove:n,onEnd:r,wheel:i}={}){let a=null,o=null,s=Date.now(),c=null,l=null,u=!1,d=!1,f=0,p=0;function m(t,r,i=100){f+=t,p+=r,a!==null&&(a-=t),o!==null&&(o-=r),!d&&!e.dataset.dragged&&(e.dataset.dragged=`true`);let l=Date.now();(f!==0||p!==0)&&l-s>=i&&(c&&cancelAnimationFrame(c),c=requestAnimationFrame(()=>{n?.(f,p),f=0,p=0,s=l,c=null}))}function h(e,t,n){a!==null&&o!==null&&m(a-e,o-t,n)}function g(e,n){l!==null&&(clearTimeout(l),d=!1,l=null),u=!0,t?.(),e!==void 0&&(a=e),n!==void 0&&(o=n),s=Date.now(),f=0,p=0}function _(t,n){u=!1,t!==void 0&&n!==void 0&&h(t,n),delete e.dataset.dragged,a=null,o=null,r?.()}e.dataset.draggable=`true`;let v=null,y=null;e.addEventListener(`mousedown`,t=>{v||(t.preventDefault(),g(t.pageX,t.pageY),v=e=>{e.preventDefault(),h(e.pageX,e.pageY)},e.addEventListener(`mousemove`,v))}),e.addEventListener(`mouseup`,t=>{v&&=(t.preventDefault(),_(t.pageX,t.pageY),e.removeEventListener(`mousemove`,v),null)}),e.addEventListener(`touchstart`,t=>{y||(t.preventDefault(),g(t.touches[0]?.pageX,t.touches[0]?.pageY),y=e=>{e.preventDefault(),h(e.touches[0]?.pageX,e.touches[0]?.pageY)},e.addEventListener(`touchmove`,y))}),e.addEventListener(`touchend`,t=>{y&&=(t.preventDefault(),_(t.touches[0]?.pageX,t.touches[0]?.pageY),e.removeEventListener(`touchmove`,y),null)}),e.addEventListener(`touchcancel`,t=>{y&&=(t.preventDefault(),_(t.touches[0]?.pageX,t.touches[0]?.pageY),e.removeEventListener(`touchmove`,y),null)}),i&&e.addEventListener(`wheel`,e=>{e.preventDefault(),u||(g(),d=!0),e.shiftKey?m(e.deltaY,e.deltaX,10):m(e.deltaX,e.deltaY,10),l!==null&&clearTimeout(l),l=setTimeout(()=>{_(),d=!1,l=null},200)})}function E(e,t){let n=0,r=0;T(e.container,{onStart(){[n,r]=e.centerCoords},onMove(t,i){let a=n+t,o=r+i,s=e.toGeoCoords(a,o);e.canMoveTo(...s)&&(e.center=s,n=a,r=o)},wheel:t?.wheel??!0})}function D(e,{storageKey:t,session:n=!1}={}){let r=n?window.sessionStorage:window.localStorage,i=t??[`maparea`,e.container.id].filter(Boolean).join(`#`),a=e.getOptions(),o=!1,s=()=>{try{r.setItem(i,JSON.stringify(e.getOptions()))}catch{}},c=()=>{try{let t=r.getItem(i);t===null?s():e.setOptions(JSON.parse(t))}catch{}},l=e=>{o=e===void 0?!o:e},u=()=>{e.setOptions(a)};c();let d=null;return e.onRender(()=>{o||(d&&=(clearTimeout(d),null),d=setTimeout(()=>{d=null,s()},350))}),{sync:c,write:s,toggle:l,reset:u}}function O(e,t){let n=new ResizeObserver(n=>{for(let r of n)if(r.contentBoxSize){if(t)if(r.contentBoxSize){let{inlineSize:e,blockSize:n}=r.contentBoxSize[0]??r.contentBoxSize;t({inlineSize:e,blockSize:n})}else{let{width:e,height:n}=r.contentRect;t({width:e,height:n})}e.render()}});return n.observe(e.container),()=>{n.unobserve(e.container)}}const k=`http://www.w3.org/2000/svg`,A={className:`shape`};function j(e,t,n){let r=x(e,n??A);r.toggleAttribute(`hidden`,t.length===0);let i=r.querySelector(`svg`);i||(i=document.createElementNS(k,`svg`),i.setAttribute(`xmlns`,k),r.append(i));let a=N(i,t.map(({id:t,coords:[n,r]})=>({id:t,coords:e.toPixelCoords(n,r)})),n);if(a){let{xMin:t,xMax:n,yMin:r,yMax:o}=a,{centerCoords:[s,c],box:l}=e,u=C(n-t,3),d=C(o-r,3),f=C(.5*l.w+t-s,2),p=C(.5*l.h+r-c,2);i.setAttribute(`viewBox`,`0 0 ${u} ${d}`),i.setAttribute(`width`,u),i.setAttribute(`height`,d),i.setAttribute(`style`,`position: absolute; transform: translate3d(${f}px, ${p}px, 0);`)}return r}function M(e){return parseFloat(window.getComputedStyle(e).strokeWidth||e.getAttribute(`stroke-width`)||`1`)}function N(e,t,n){if(t.length===0){e.innerHTML=``;return}let r=e.querySelector(`path`);r||(r=document.createElementNS(k,`path`),e.append(r));let i=t[0].coords[0],a=i,o=t[0].coords[1],s=o,c=``;for(let{coords:[e,n]}of t)i>e&&(i=e),a<e&&(a=e),o>n&&(o=n),s<n&&(s=n);i-=10,a+=10,o-=10,s+=10;let l=Array.from(e.querySelectorAll(`.marker`)),u=0;for(let{id:a,coords:[s,d]}of t){let t=C(s-i,3),f=C(d-o,3);if(c+=`${c?` L`:`M`} ${t} ${f}`,n?.markers){let n=l[u++];n||(n=document.createElementNS(k,`circle`),n.setAttribute(`class`,`marker`),n.setAttribute(`r`,String(1.5*M(r))),e.append(n)),n.setAttribute(`cx`,t),n.setAttribute(`cy`,f),n.dataset.id=a}}for(let e=u;e<l.length;e++)l[e].remove();return r.setAttribute(`d`,c),{xMin:i,xMax:a,yMin:o,yMax:s}}function P(e,t,n){let r=t.map(e=>({id:b(),coords:e}));e.onRender(()=>{j(e,r,{className:`shape`,...n})})}function F(e,t){let n=[],r=null,i={className:`shape-editor`,markers:!0},a=()=>{j(e,n,i),t?.onUpdate?.(n)};y(e,({lat:e,lon:t,originalEvent:i})=>{let o=(i.target?.closest(`.marker`))?.dataset.id;if(o&&r){clearTimeout(r),r=null,n=n.filter(({id:e})=>e!==o),a();return}let s=()=>{n.push({id:b(),coords:[e,t]}),a(),r=null};o?r=setTimeout(s,250):s()},t?.ignoreClicks),e.onRender(a)}const{floor:I,ceil:L,random:R}=Math;function z(e,t,n){return`${t},${n},${e.zoom},${e.lang}`}function B(e){let t=e.target;t instanceof HTMLImageElement&&(t.style.opacity=``)}function V(e,t,n,{size:r,url:i,subdomains:a,retries:o=0,error:s}){let c=new Image,l=S(e,r)??256,u=0,d=(r,o)=>{if(!i)return``;if(typeof i==`function`)return i(e,t,n);let s=i.replaceAll(`{x}`,String(r)).replaceAll(`{y}`,String(o)).replaceAll(`{z}`,String(e.zoom)).replaceAll(`{lang}`,e.lang);return a&&s.includes(`{s}`)&&(s=s.replaceAll(`{s}`,a[I(a.length*R())])),s},f=t=>{let n=t.target;if(n instanceof HTMLImageElement){if(u++<o){let e=new URL(n.src);e.searchParams.set(`_t`,String(Date.now())),n.src=e.href;return}let t=S(e,s);t&&(n.dataset.src=n.src,n.src=t),c.removeEventListener(`error`,f)}};return c.width=l,c.height=l,c.src=d(t,n),c.dataset.id=z(e,t,n),c.style.position=`absolute`,c.addEventListener(`load`,B),c.addEventListener(`error`,f),c.complete||(c.style.opacity=`0`),c}function H(e){return e.querySelectorAll(`img[data-id]`)}function U(e,t){return e.querySelector(`img[data-id="${t}"]`)}function W(e,t={}){let{id:n=b(),attribution:r,attributionInset:i=`auto 0 0 auto`}=t,a=x(e,{id:n,className:`tiles`,...t}),o=x(e,{id:n,className:`tiles-attribution`,inset:i}),s=()=>{let{box:{w:n,h:i},centerCoords:[s,c]}=e,{size:l,margin:u=0}=t,d=S(e,l)??256,f=Array.isArray(u)?u[0]:u,p=Array.isArray(u)?u[1]:u,m=L((n+2*f)/d),h=L((i+2*p)/d),g=I(s/d),_=I(c/d),v=null,y=new Set,b=``;for(let r=0;r<=m;r++){let o=g+(r%2==0?-1:1)*I(r/2);for(let r=0;r<=h;r++){let l=_+(r%2==0?-1:1)*I(r/2);b=z(e,o,l),v=U(a,b),v||(v=V(e,o,l,t),a.append(v));let u=C(.5*n+o*d-s,2),f=C(.5*i+l*d-c,2);v.style.transform=`translate3d(${u}px, ${f}px, 0)`,y.add(b)}}for(let e of H(a)){let{id:t}=e.dataset;t&&!y.has(t)&&e.remove()}let x=S(e,r)??``;o.toggleAttribute(`hidden`,!x),o.innerHTML!==x&&(o.innerHTML=x)},c=e.zoom,l=null;return e.onRender(()=>{l!==null&&(clearTimeout(l),l=null),e.zoom===c?(a.style.opacity&&(a.style.opacity=``),s()):(a.style.opacity=`0`,l=setTimeout(()=>{l=null,a.style.opacity&&(a.style.opacity=``),s()},300)),c=e.zoom}),a}function G(e,t={}){let{plus:n=`➕`,minus:r=`➖`,inset:i=`0 0 auto auto`}=t,a=x(e,{className:`zoom-controls`,inset:i}),o=document.createElement(`button`);o.dataset.id=`plus`,o.innerHTML=n;let s=document.createElement(`button`);s.dataset.id=`minus`,s.innerHTML=r;let c=()=>{o.toggleAttribute(`disabled`,e.zoom+1>e.maxZoom),s.toggleAttribute(`disabled`,e.zoom-1<e.minZoom)};return y(e,({originalEvent:t})=>{let n=t.target?.closest(`button`);n&&(t.stopPropagation(),e.zoom+=n.dataset.id===`minus`?-1:1,c())}),e.onRender(c),a.append(o,s),a}function K(e){if(e.length===0)return{};let t=e[0][0],n=t,r=e[0][1],i=r;for(let[a,o]of e)t>a&&(t=a),n<a&&(n=a),r>o&&(r=o),i<o&&(i=o);return{minLat:t,maxLat:n,minLon:r,maxLon:i}}function q(e){return Array.isArray(e)&&e.length===2&&typeof e[0]==`number`&&typeof e[1]==`number`}function J(e){return q(e)?{minLat:e[0],maxLat:e[0],minLon:e[1],maxLon:e[1]}:Array.isArray(e)?K(e):e}function Y(e){let{minLat:t,maxLat:n,minLon:r,maxLon:i}=J(e);return[t===void 0||n===void 0?0:(n+t)/2,r===void 0||i===void 0?0:(i+r)/2]}const{abs:X,log2:Z,min:Q,floor:$}=Math;function ee(e,t){let{box:{w:n,h:r},center:[i,a],zoom:o}=e,{minLat:s,maxLat:c,minLon:l,maxLon:u}=t,d=0,f=0;if(l!==void 0&&u!==void 0){let t=X(e.toPixelCoords(i,u)[0]-e.toPixelCoords(i,l)[0]);t!==0&&(d=Z(n/t))}if(s!==void 0&&c!==void 0){let t=X(e.toPixelCoords(s,a)[1]-e.toPixelCoords(c,a)[1]);t!==0&&(f=Z(r/t))}let p=$(Q(o+d,o+f)),m={center:Y(t)};p!==o&&(m.zoom=p),e.setOptions(m)}const te=[.005,.018];function ne(e,t=te){let{minLat:n=0,maxLat:r=0,minLon:i=0,maxLon:a=0}=J(e),[o,s]=t;return{minLat:n-o,maxLat:r+o,minLon:i-s,maxLon:a+s}}export{n as DEG,a as MAX_LAT,s as MAX_LON,i as MIN_LAT,o as MIN_LON,g as MapArea,t as RAD,y as addClickListener,w as addElement,E as addMovableViewport,D as addPersistence,O as addResizeObserver,P as addShape,F as addShapeEditor,W as addTiles,G as addZoomControls,r as eccentricityMap,ee as fitGeoBounds,Y as getCenter,K as getGeoBounds,b as getId,x as getLayer,_ as getPointerPosition,ne as getVicinity,q as isCoordsArray,S as resolveDynamic,J as toGeoBounds,C as toPrecision};
|
|
1
|
+
const{PI:e}=Math,t=e/180,n=180/e,r={spherical:0,ellipsoidal:.0818191908426},i=-85.05,a=85.05,o=-180,s=180,{PI:c,sin:l,tan:u,atan:d,pow:f,exp:p,log:m,abs:h}=Math;var g=class{_p;_c;_cc;_r=new Set;constructor(e){this._p=e,this.container.style=`position: relative; overflow: hidden;`,this.container.dataset.type=`maparea`}render(){for(let e of this._r)e(this)}onRender(e,t=!1){return this._r.add(e),t||e(this),()=>{this._r.delete(e)}}getOptions(){return{...this._p,center:this.center,bounds:this.bounds}}setOptions(e){this._p={...this._p,...e},`container`in e&&delete this._c,delete this._cc,this.render()}get container(){if(!this._c){let e=document.querySelector(this._p.container);if(!e)throw Error(`missing container element`);this._c=e}return this._c}get box(){let e=this.container.getBoundingClientRect();return{x:e.left,y:e.top,w:e.width,h:e.height}}get bounds(){let e=this._p.bounds;return e?{...e}:{}}set bounds(e){this._p.bounds=e}get center(){let e=this._p.center;return e?e.slice():[0,0]}set center(e){this._p.center=e,delete this._cc,this.render()}get centerCoords(){if(!this._cc){let[e,t]=this.center;this._cc=this.toPixelCoords(e,t)}return this._cc}get zoom(){return this._p.zoom??this._p.minZoom??0}set zoom(e){let{minZoom:t,maxZoom:n}=this,r=e;e<t&&(r=t),e>n&&(r=n),this._p.zoom=r,delete this._cc,this.render()}get minZoom(){return this._p.minZoom??-1/0}set minZoom(e){this._p.minZoom=e,this.zoom<this.minZoom&&(this.zoom=this.minZoom)}get maxZoom(){return this._p.maxZoom??1/0}set maxZoom(e){this._p.maxZoom=e,this.zoom>this.maxZoom&&(this.zoom=this.maxZoom)}get projection(){return this._p.projection??`spherical`}set projection(e){this._p.projection=e,this.render()}get lang(){return this._p.lang??``}set lang(e){this._p.lang=e,this.render()}inPixelBounds(e,t){let[n,r]=this.toGeoCoords(e,t);return this.inBounds(n,r)}inBounds(e,t){let{minLat:n=i,maxLat:r=a,minLon:s=o,maxLon:c=180}=this.bounds;return e>=n&&e<=r&&t>=s&&t<=c}canMoveTo(e,t){if(!this.inBounds(e,t))return!1;let{w:n,h:r}=this.box;if(n===0||r===0)return!1;let[i,a]=this.toPixelCoords(e,t);return this.inPixelBounds(i-n/2,a-r/2)&&this.inPixelBounds(i+n/2,a+r/2)}toPixelCoords(e,n){let i=r[this.projection],a=f(2,this.zoom+7)/c,o=e*t,s=i===0?1:f((1-i*l(o))/(1+i*l(o)),i/2);return[a*(c+n*t),a*(c-m(s*u(c/4+o/2)))]}toGeoCoords(e,t){let o=r[this.projection],s=f(2,this.zoom+7)/c,l=(e/s-c)*n,u;if(o===0)u=2*(d(p(c-t/s))-c/4)*n;else{u=i;let e=a-i,n=NaN,r=0;for(;(Number.isNaN(n)||h(n-t)>.1)&&r++<30;)e/=2,n=this.toPixelCoords(u+e,l)[1],n>t&&(u+=e)}return(l<-180||l>=180)&&(l=(l+180)%360-180),(u<-90||u>=90)&&(u=(u+90)%180-90),[u,l]}};function _(){return Math.random().toString(36).slice(2)}function v(e,{id:t,className:n,inset:r=`0`}={}){let i=`${n?`.${n}`:``}${t?`[data-id="${t}"]`:``}`,a=document.querySelector(i);return a||(a=document.createElement(`div`),a.className=`${n?`${n} `:``}layer`,a.dataset.id=t??_(),a.style=`position: absolute; inset: ${r};`,e.container.append(a)),a}function y(e,t){return t===void 0?t:t instanceof Function?t(e):t}function b(e,t){return e.toFixed(t).replace(/\.(\d*[^0])0+$/,`.$1`).replace(/\.0+$/,``)}function x(e,t,{className:n,coords:r,inset:i,layer:a,content:o}){let s=a??v(e,{className:`elements`});n&&t.setAttribute(`class`,n),t.style.position=`absolute`,s.append(t),e.onRender(()=>{if(i)t.style.inset=y(e,i)??``;else if(r){let{centerCoords:[n,i],box:{w:a,h:o}}=e,[s,c]=e.toPixelCoords(...r),l=b(s-n+a/2,2),u=b(c-i+o/2,2);t.style.transform=`translate3d(${l}px, ${u}px, 0)`}if(o){let n=y(e,o)??``;t.innerHTML!==n&&(t.innerHTML=n)}})}function S(e,t){return t===void 0||!(e instanceof HTMLElement)?!1:typeof t==`function`&&t(e)||typeof t==`string`&&e.closest(t)!==null}function C(e,{onStart:t,onMove:n,onEnd:r,wheel:i,ignore:a}={}){let o=null,s=null,c=Date.now(),l=null,u=null,d=!1,f=!1,p=0,m=0,h=0;function g(t,r){m+=t,h+=r,o!==null&&(o-=t),s!==null&&(s-=r),!f&&!e.dataset.dragged&&(e.dataset.dragged=`true`);let i=Date.now(),a=f?10:100;(m!==0||h!==0)&&i-c>=a&&(l&&cancelAnimationFrame(l),l=requestAnimationFrame(()=>{n?.(m,h),m=0,h=0,c=i,l=null}))}function _(e,t){o!==null&&s!==null&&g(o-e,s-t)}function v(e,n){u!==null&&(clearTimeout(u),f=!1,u=null),d=!0,t?.(),e!==void 0&&(o=e),n!==void 0&&(s=n),c=Date.now(),m=0,h=0}function y(t,n){d=!1,t!==void 0&&n!==void 0&&_(t,n),delete e.dataset.dragged,o=null,s=null,r?.()}e.dataset.draggable=`true`;function b(e){return!S(e.target,a)&&p===1}function x(e){p++,b(e)&&(e.preventDefault(),v(e.pageX,e.pageY))}function C(e){b(e)&&(e.preventDefault(),_(e.pageX,e.pageY))}function w(e){b(e)&&(e.preventDefault(),y(e.pageX,e.pageY)),p=0}function T(e){S(e.target,a)||(e.preventDefault(),d||(v(),f=!0),e.shiftKey?g(e.deltaY,e.deltaX):g(e.deltaX,e.deltaY),u!==null&&clearTimeout(u),u=setTimeout(()=>{y(),f=!1,u=null},200))}return e.addEventListener(`pointerdown`,x),e.addEventListener(`pointermove`,C),e.addEventListener(`pointerup`,w),e.addEventListener(`pointercancel`,w),i&&e.addEventListener(`wheel`,T),()=>{e.removeEventListener(`pointerdown`,x),e.removeEventListener(`pointermove`,C),e.removeEventListener(`pointerup`,w),e.removeEventListener(`pointercancel`,w),i&&e.removeEventListener(`wheel`,T)}}function w(e,{wheel:t=!0,ignore:n}={}){let r=0,i=0;C(e.container,{onStart(){[r,i]=e.centerCoords},onMove(t,n){let a=r+t,o=i+n,s=e.toGeoCoords(a,o);e.canMoveTo(...s)&&(e.center=s,r=a,i=o)},wheel:t,ignore:n})}function T(e,{storageKey:t,session:n=!1}={}){let r=n?window.sessionStorage:window.localStorage,i=t??[`maparea`,e.container.id].filter(Boolean).join(`#`),a=e.getOptions(),o=!1,s=()=>{try{r.setItem(i,JSON.stringify(e.getOptions()))}catch{}},c=()=>{try{let t=r.getItem(i);t===null?s():e.setOptions(JSON.parse(t))}catch{}},l=e=>{o=e===void 0?!o:e},u=()=>{e.setOptions(a)};c();let d=null;return e.onRender(()=>{o||(d&&=(clearTimeout(d),null),d=setTimeout(()=>{d=null,s()},350))}),{sync:c,write:s,toggle:l,reset:u}}function E(e,t,{ignore:n}={}){let r=null,i=null,a=Date.now(),o=e=>{r=e.clientX,i=e.clientY,a=Date.now()},s=o=>{if(S(o.target,n)||r===null||i===null||Date.now()-a>150)return;let{box:s,centerCoords:[c,l]}=e,u=r-s.x,d=i-s.y,[f,p]=e.toGeoCoords(u-.5*s.w+c,d-.5*s.h+l);t({x:u,y:d,lat:f,lon:p,originalEvent:o}),r=null,i=null};return e.container.addEventListener(`pointerdown`,o),e.container.addEventListener(`pointerup`,s),e.container.addEventListener(`pointercancel`,s),()=>{e.container.removeEventListener(`pointerdown`,o),e.container.removeEventListener(`pointerup`,s),e.container.removeEventListener(`pointercancel`,s)}}function D(e,t){let n=new ResizeObserver(n=>{for(let r of n)if(r.contentBoxSize){if(t)if(r.contentBoxSize){let{inlineSize:e,blockSize:n}=r.contentBoxSize[0]??r.contentBoxSize;t({inlineSize:e,blockSize:n})}else{let{width:e,height:n}=r.contentRect;t({width:e,height:n})}e.render()}});return n.observe(e.container),()=>{n.unobserve(e.container)}}const O=`http://www.w3.org/2000/svg`,k={className:`shape`};function A(e,t,n){let r=v(e,n??k);r.toggleAttribute(`hidden`,t.length===0);let i=r.querySelector(`svg`);i||(i=document.createElementNS(O,`svg`),i.setAttribute(`xmlns`,O),r.append(i));let a=M(i,t.map(({id:t,coords:[n,r]})=>({id:t,coords:e.toPixelCoords(n,r)})),n);if(a){let{xMin:t,xMax:n,yMin:r,yMax:o}=a,{centerCoords:[s,c],box:l}=e,u=b(n-t,3),d=b(o-r,3),f=b(.5*l.w+t-s,2),p=b(.5*l.h+r-c,2);i.setAttribute(`viewBox`,`0 0 ${u} ${d}`),i.setAttribute(`width`,u),i.setAttribute(`height`,d),i.setAttribute(`style`,`position: absolute; transform: translate3d(${f}px, ${p}px, 0);`)}return r}function j(e){return parseFloat(window.getComputedStyle(e).strokeWidth||e.getAttribute(`stroke-width`)||`1`)}function M(e,t,n){if(t.length===0){e.innerHTML=``;return}let r=e.querySelector(`path`);r||(r=document.createElementNS(O,`path`),e.append(r));let i=t[0].coords[0],a=i,o=t[0].coords[1],s=o,c=``;for(let{coords:[e,n]}of t)i>e&&(i=e),a<e&&(a=e),o>n&&(o=n),s<n&&(s=n);i-=10,a+=10,o-=10,s+=10;let l=Array.from(e.querySelectorAll(`.marker`)),u=0;for(let{id:a,coords:[s,d]}of t){let t=b(s-i,3),f=b(d-o,3);if(c+=`${c?` L`:`M`} ${t} ${f}`,n?.markers){let n=l[u++];n||(n=document.createElementNS(O,`circle`),n.setAttribute(`class`,`marker`),n.setAttribute(`r`,String(1.5*j(r))),e.append(n)),n.setAttribute(`cx`,t),n.setAttribute(`cy`,f),n.dataset.id=a}}for(let e=u;e<l.length;e++)l[e].remove();return r.setAttribute(`d`,c),{xMin:i,xMax:a,yMin:o,yMax:s}}function N(e,t,n){let r=t.map(e=>({id:_(),coords:e}));e.onRender(()=>{A(e,r,{className:`shape`,...n})})}function P(e,t){let n=[],r=null,i={className:`shape-editor`,markers:!0},a=()=>{A(e,n,i),t?.onUpdate?.(n)};E(e,({lat:e,lon:t,originalEvent:i})=>{let o=(i.target?.closest(`.marker`))?.dataset.id;if(o&&r){clearTimeout(r),r=null,n=n.filter(({id:e})=>e!==o),a();return}let s=()=>{n.push({id:_(),coords:[e,t]}),a(),r=null};o?r=setTimeout(s,250):s()},{ignore:t?.ignore}),e.onRender(a)}const{floor:F,ceil:I,random:L}=Math;function R(e,t,n){return`${t},${n},${e.zoom},${e.lang}`}function z(e){let t=e.target;t instanceof HTMLImageElement&&(t.style.opacity=``)}function B(e,t,n,{size:r,url:i,subdomains:a,retries:o=0,error:s}){let c=new Image,l=y(e,r)??256,u=0,d=(r,o)=>{if(!i)return``;if(typeof i==`function`)return i(e,t,n);let s=i.replaceAll(`{x}`,String(r)).replaceAll(`{y}`,String(o)).replaceAll(`{z}`,String(e.zoom)).replaceAll(`{lang}`,e.lang);return a&&s.includes(`{s}`)&&(s=s.replaceAll(`{s}`,a[F(a.length*L())])),s},f=t=>{let n=t.target;if(n instanceof HTMLImageElement){if(u++<o){let e=new URL(n.src);e.searchParams.set(`_t`,String(Date.now())),n.src=e.href;return}let t=y(e,s);t&&(n.dataset.src=n.src,n.src=t),c.removeEventListener(`error`,f)}};return c.width=l,c.height=l,c.src=d(t,n),c.dataset.id=R(e,t,n),c.style.position=`absolute`,c.addEventListener(`load`,z),c.addEventListener(`error`,f),c.complete||(c.style.opacity=`0`),c}function V(e){return e.querySelectorAll(`img[data-id]`)}function H(e,t){return e.querySelector(`img[data-id="${t}"]`)}function U(e,t={}){let{id:n=_(),attribution:r,attributionInset:i=`auto 0 0 auto`}=t,a=v(e,{id:n,className:`tiles`,...t}),o=v(e,{id:n,className:`tiles-attribution`,inset:i}),s=()=>{let{box:{w:n,h:i},centerCoords:[s,c]}=e,{size:l,margin:u=0}=t,d=y(e,l)??256,f=Array.isArray(u)?u[0]:u,p=Array.isArray(u)?u[1]:u,m=I((n+2*f)/d),h=I((i+2*p)/d),g=F(s/d),_=F(c/d),v=null,x=new Set,S=``;for(let r=0;r<=m;r++){let o=g+(r%2==0?-1:1)*F(r/2);for(let r=0;r<=h;r++){let l=_+(r%2==0?-1:1)*F(r/2);S=R(e,o,l),v=H(a,S),v||(v=B(e,o,l,t),a.append(v));let u=b(.5*n+o*d-s,2),f=b(.5*i+l*d-c,2);v.style.transform=`translate3d(${u}px, ${f}px, 0)`,x.add(S)}}for(let e of V(a)){let{id:t}=e.dataset;t&&!x.has(t)&&e.remove()}let C=y(e,r)??``;o.toggleAttribute(`hidden`,!C),o.innerHTML!==C&&(o.innerHTML=C)},c=e.zoom,l=null;return e.onRender(()=>{l!==null&&(clearTimeout(l),l=null),e.zoom===c?(a.style.opacity&&(a.style.opacity=``),s()):(a.style.opacity=`0`,l=setTimeout(()=>{l=null,a.style.opacity&&(a.style.opacity=``),s()},300)),c=e.zoom}),a}function W(e,t={}){let{plus:n=`➕`,minus:r=`➖`,inset:i=`0 0 auto auto`}=t,a=v(e,{className:`zoom-controls`,inset:i}),o=document.createElement(`button`);o.dataset.id=`plus`,o.innerHTML=n;let s=document.createElement(`button`);s.dataset.id=`minus`,s.innerHTML=r;let c=()=>{o.toggleAttribute(`disabled`,e.zoom+1>e.maxZoom),s.toggleAttribute(`disabled`,e.zoom-1<e.minZoom)};return E(e,({originalEvent:t})=>{let n=t.target?.closest(`button`);n&&(t.stopPropagation(),e.zoom+=n.dataset.id===`minus`?-1:1,c())}),e.onRender(c),a.append(o,s),a}function G(e){if(e.length===0)return{};let t=e[0][0],n=t,r=e[0][1],i=r;for(let[a,o]of e)t>a&&(t=a),n<a&&(n=a),r>o&&(r=o),i<o&&(i=o);return{minLat:t,maxLat:n,minLon:r,maxLon:i}}function K(e){return Array.isArray(e)&&e.length===2&&typeof e[0]==`number`&&typeof e[1]==`number`}function q(e){return K(e)?{minLat:e[0],maxLat:e[0],minLon:e[1],maxLon:e[1]}:Array.isArray(e)?G(e):e}function J(e){let{minLat:t,maxLat:n,minLon:r,maxLon:i}=q(e);return[t===void 0||n===void 0?0:(n+t)/2,r===void 0||i===void 0?0:(i+r)/2]}const{abs:Y,log2:X,min:Z,floor:Q}=Math;function $(e,t){let{box:{w:n,h:r},center:[i,a],zoom:o}=e,{minLat:s,maxLat:c,minLon:l,maxLon:u}=t,d=0,f=0;if(l!==void 0&&u!==void 0){let t=Y(e.toPixelCoords(i,u)[0]-e.toPixelCoords(i,l)[0]);t!==0&&(d=X(n/t))}if(s!==void 0&&c!==void 0){let t=Y(e.toPixelCoords(s,a)[1]-e.toPixelCoords(c,a)[1]);t!==0&&(f=X(r/t))}let p=Q(Z(o+d,o+f)),m={center:J(t)};p!==o&&(m.zoom=p),e.setOptions(m)}const ee=[.005,.018];function te(e,t=ee){let{minLat:n=0,maxLat:r=0,minLon:i=0,maxLon:a=0}=q(e),[o,s]=t;return{minLat:n-o,maxLat:r+o,minLon:i-s,maxLon:a+s}}export{n as DEG,a as MAX_LAT,s as MAX_LON,i as MIN_LAT,o as MIN_LON,g as MapArea,t as RAD,x as addElement,w as addMovableViewport,T as addPersistence,E as addPointerListener,D as addResizeObserver,N as addShape,P as addShapeEditor,U as addTiles,W as addZoomControls,r as eccentricityMap,$ as fitGeoBounds,J as getCenter,G as getGeoBounds,_ as getId,v as getLayer,te as getVicinity,K as isCoordsArray,y as resolveDynamic,q as toGeoBounds,b as toPrecision};
|
package/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export * from "./src/MapArea/const.ts";
|
|
2
2
|
export * from "./src/MapArea/index.ts";
|
|
3
|
-
export * from "./src/plugins/addClickListener.ts";
|
|
4
3
|
export * from "./src/plugins/addElement.ts";
|
|
5
4
|
export * from "./src/plugins/addMovableViewport.ts";
|
|
6
5
|
export * from "./src/plugins/addPersistence.ts";
|
|
6
|
+
export * from "./src/plugins/addPointerListener.ts";
|
|
7
7
|
export * from "./src/plugins/addResizeObserver.ts";
|
|
8
8
|
export * from "./src/plugins/addShape.ts";
|
|
9
9
|
export * from "./src/plugins/addShapeEditor.ts";
|
|
@@ -25,7 +25,6 @@ export * from "./src/utils/getCenter.ts";
|
|
|
25
25
|
export * from "./src/utils/getGeoBounds.ts";
|
|
26
26
|
export * from "./src/utils/getId.ts";
|
|
27
27
|
export * from "./src/utils/getLayer.ts";
|
|
28
|
-
export * from "./src/utils/getPointerPosition.ts";
|
|
29
28
|
export * from "./src/utils/getVicinity.ts";
|
|
30
29
|
export * from "./src/utils/isCoordsArray.ts";
|
|
31
30
|
export * from "./src/utils/resolveDynamic.ts";
|