lcdp-map
Version:
A Leaflet wrapper library for Appsmith with custom VLU functions
2 lines • 16 kB
JavaScript
/*! For license information please see index.umd.js.LICENSE.txt */
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.lcdp_map=e():t.lcdp_map=e()}(this,(()=>(()=>{"use strict";var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)}function n(){n=function(){return e};var t,e={},o=Object.prototype,a=o.hasOwnProperty,i=Object.defineProperty||function(t,e,r){t[e]=r.value},c="function"==typeof Symbol?Symbol:{},u=c.iterator||"@@iterator",s=c.asyncIterator||"@@asyncIterator",l=c.toStringTag||"@@toStringTag";function h(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{h({},"")}catch(t){h=function(t,e,r){return t[e]=r}}function p(t,e,r,n){var o=e&&e.prototype instanceof b?e:b,a=Object.create(o.prototype),c=new A(n||[]);return i(a,"_invoke",{value:M(t,r,c)}),a}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=p;var d="suspendedStart",v="suspendedYield",y="executing",g="completed",m={};function b(){}function w(){}function k(){}var x={};h(x,u,(function(){return this}));var L=Object.getPrototypeOf,j=L&&L(L(T([])));j&&j!==o&&a.call(j,u)&&(x=j);var O=k.prototype=b.prototype=Object.create(x);function E(t){["next","throw","return"].forEach((function(e){h(t,e,(function(t){return this._invoke(e,t)}))}))}function P(t,e){function n(o,i,c,u){var s=f(t[o],t,i);if("throw"!==s.type){var l=s.arg,h=l.value;return h&&"object"==r(h)&&a.call(h,"__await")?e.resolve(h.__await).then((function(t){n("next",t,c,u)}),(function(t){n("throw",t,c,u)})):e.resolve(h).then((function(t){l.value=t,c(l)}),(function(t){return n("throw",t,c,u)}))}u(s.arg)}var o;i(this,"_invoke",{value:function(t,r){function a(){return new e((function(e,o){n(t,r,e,o)}))}return o=o?o.then(a,a):a()}})}function M(e,r,n){var o=d;return function(a,i){if(o===y)throw Error("Generator is already running");if(o===g){if("throw"===a)throw i;return{value:t,done:!0}}for(n.method=a,n.arg=i;;){var c=n.delegate;if(c){var u=_(c,n);if(u){if(u===m)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===d)throw o=g,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=y;var s=f(e,r,n);if("normal"===s.type){if(o=n.done?g:v,s.arg===m)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=g,n.method="throw",n.arg=s.arg)}}}function _(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,_(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var a=f(o,e.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,m;var i=a.arg;return i?i.done?(r[e.resultName]=i.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,m):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,m)}function C(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function S(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function A(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(C,this),this.reset(!0)}function T(e){if(e||""===e){var n=e[u];if(n)return n.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(a.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return i.next=i}}throw new TypeError(r(e)+" is not iterable")}return w.prototype=k,i(O,"constructor",{value:k,configurable:!0}),i(k,"constructor",{value:w,configurable:!0}),w.displayName=h(k,l,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===w||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,k):(t.__proto__=k,h(t,l,"GeneratorFunction")),t.prototype=Object.create(O),t},e.awrap=function(t){return{__await:t}},E(P.prototype),h(P.prototype,s,(function(){return this})),e.AsyncIterator=P,e.async=function(t,r,n,o,a){void 0===a&&(a=Promise);var i=new P(p(t,r,n,o),a);return e.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},E(O),h(O,l,"Generator"),h(O,u,(function(){return this})),h(O,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=T,A.prototype={constructor:A,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(S),!e)for(var r in this)"t"===r.charAt(0)&&a.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(n,o){return c.type="throw",c.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],c=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=a.call(i,"catchLoc"),s=a.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&a.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o.finallyLoc,m):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),m},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),m}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;S(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:T(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),m}},e}function o(t,e,r,n,o,a,i){try{var c=t[a](i),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,a){var i=t.apply(e,r);function c(t){o(i,n,a,c,u,"next",t)}function u(t){o(i,n,a,c,u,"throw",t)}c(void 0)}))}}function i(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function c(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?i(Object(r),!0).forEach((function(e){u(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):i(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function u(t,e,r){return(e=l(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function s(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,l(n.key),n)}}function l(t){var e=function(t){if("object"!=r(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,"string");if("object"!=r(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==r(e)?e:e+""}t.d(e,{default:()=>h});const h=function(){return t=function t(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.map=L.map(e,r),this.markers=[],this.polylines=[],this.currentLocation=null,L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",{attribution:"© OpenStreetMap contributors"}).addTo(this.map)},e=[{key:"createMarker",value:function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"",n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"",o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:"fa-map-marker-alt",a=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"#003C71",i=arguments.length>6&&void 0!==arguments[6]?arguments[6]:{},u=L.divIcon({html:'\n <span class="fa-stack fa-lg" style="font-size: 20px;">\n <i class="fa-regular fa-circle fa-stack-2x" style="color: '.concat(a,'; opacity: 0.8;"></i>\n <i class="').concat(o,' fa-stack-1x" style="color: ').concat(a,';"></i>\n </span>\n '),className:"custom-marker-icon",iconSize:[40,40],iconAnchor:[20,20],popupAnchor:[0,-20]}),s=c(c({},i.marker),{},{icon:u}),l=c(c({},i.popup),{},{className:"custom-popup"}),h='\n <div class="popup-content">\n <h3 class="popup-title">'.concat(r,'</h3>\n <div class="popup-body">').concat(n,"</div>\n </div>\n ");return L.marker([t,e],s).addTo(this.map).bindPopup(h,l).openPopup()}},{key:"addMarker",value:function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"",n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"",o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:"fa-solid fa-location-dot",a=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"#003C71",i=arguments.length>6&&void 0!==arguments[6]?arguments[6]:{},c=this.createMarker(t,e,r,n,o,a,i);return this.markers.push(c),c}},{key:"updateCurrentLocation",value:function(t,e){var r=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"Current location",o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"Current location";return this.currentLocation&&(this.currentLocation.remove(),this.markers=this.markers.filter((function(t){return t!==r.currentLocation}))),this.currentLocation=this.createMarker(t,e,n,o,"fa-solid fa-location-dot","#FF0000"),this.markers.push(this.currentLocation),this.currentLocation}},{key:"getCurrentLocation",value:(l=a(n().mark((function t(){var e,r,o,a;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(navigator.geolocation){t.next=3;break}return console.error("Geolocation is not supported by this browser."),t.abrupt("return");case 3:return t.prev=3,t.next=6,new Promise((function(t,e){navigator.geolocation.getCurrentPosition(t,e,{enableHighAccuracy:!0,timeout:5e3,maximumAge:0})}));case 6:e=t.sent,r=e.coords,o=r.latitude,a=r.longitude,this.updateCurrentLocation(o,a),this.map.setView([o,a],13),t.next=15;break;case 12:t.prev=12,t.t0=t.catch(3),console.error("Error getting current location:",t.t0);case 15:case"end":return t.stop()}}),t,this,[[3,12]])}))),function(){return l.apply(this,arguments)})},{key:"calculateDistance",value:function(t,e,r,n){var o=this.toRad(r-t),a=this.toRad(n-e),i=this.toRad(t),c=this.toRad(r),u=Math.sin(o/2)*Math.sin(o/2)+Math.cos(i)*Math.cos(c)*Math.sin(a/2)*Math.sin(a/2),s=2*Math.atan2(Math.sqrt(u),Math.sqrt(1-u))*6371;return Math.round(100*s)/100}},{key:"toRad",value:function(t){return t*Math.PI/180}},{key:"drawRoute",value:(u=a(n().mark((function t(e,r,o,a){var i,u,s,l,h,p,f=arguments;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=c({color:"#003C71",weight:3,opacity:.8,dashArray:null},f.length>4&&void 0!==f[4]?f[4]:{}),t.prev=2,t.next=5,fetch("https://router.project-osrm.org/route/v1/driving/".concat(r,",").concat(e,";").concat(a,",").concat(o,"?overview=full&geometries=geojson"));case 5:return u=t.sent,t.next=8,u.json();case 8:if("Ok"===(s=t.sent).code&&s.routes.length){t.next=11;break}throw new Error("Could not find a route");case 11:return l=s.routes[0].geometry.coordinates,h=l.map((function(t){return[t[1],t[0]]})),this.clearRoutes(),p=L.polyline(h,i).addTo(this.map),this.polylines.push(p),this.map.fitBounds(p.getBounds(),{padding:[50,50]}),t.abrupt("return",p);case 20:return t.prev=20,t.t0=t.catch(2),console.error("Error drawing route:",t.t0),t.abrupt("return",null);case 24:case"end":return t.stop()}}),t,this,[[2,20]])}))),function(t,e,r,n){return u.apply(this,arguments)})},{key:"current_to_marker",value:(i=a(n().mark((function t(e,r){var o,a,i,u,s,l,h,p,f,d,v=arguments;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=c({color:"#003C71",weight:3,opacity:.8,dashArray:null},v.length>2&&void 0!==v[2]?v[2]:{}),t.prev=2,t.next=5,new Promise((function(t,e){navigator.geolocation.getCurrentPosition(t,e,{enableHighAccuracy:!0,timeout:5e3,maximumAge:0})}));case 5:return a=t.sent,i=a.coords,u=i.latitude,s=i.longitude,t.next=9,fetch("https://router.project-osrm.org/route/v1/driving/".concat(s,",").concat(u,";").concat(r,",").concat(e,"?overview=full&geometries=geojson"));case 9:return l=t.sent,t.next=12,l.json();case 12:if("Ok"===(h=t.sent).code&&h.routes.length){t.next=15;break}throw new Error("Could not find a route");case 15:return p=h.routes[0].geometry.coordinates,f=p.map((function(t){return[t[1],t[0]]})),this.clearRoutes(),d=L.polyline(f,o).addTo(this.map),this.polylines.push(d),this.map.fitBounds(d.getBounds(),{padding:[50,50]}),t.abrupt("return",d);case 24:return t.prev=24,t.t0=t.catch(2),console.error("Error drawing route:",t.t0),t.abrupt("return",null);case 28:case"end":return t.stop()}}),t,this,[[2,24]])}))),function(t,e){return i.apply(this,arguments)})},{key:"clearRoutes",value:function(){this.polylines.forEach((function(t){t.remove()})),this.polylines=[]}},{key:"clearMarkers",value:function(){this.markers.forEach((function(t){t.remove()})),this.markers=[]}},{key:"searchAddress",value:(o=a(n().mark((function t(e){var r,o;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,fetch("https://nominatim.openstreetmap.org/search?q=".concat(encodeURIComponent(e),"&format=json&addressdetails=1"));case 3:return r=t.sent,t.next=6,r.json();case 6:if(!((o=t.sent)&&o.length>0)){t.next=9;break}return t.abrupt("return",{lat:parseFloat(o[0].lat),lng:parseFloat(o[0].lon)});case 9:return t.abrupt("return",null);case 12:return t.prev=12,t.t0=t.catch(0),console.error("Error searching address:",t.t0),t.abrupt("return",null);case 16:case"end":return t.stop()}}),t,null,[[0,12]])}))),function(t){return o.apply(this,arguments)})},{key:"searchAndMarkAddress",value:(r=a(n().mark((function t(e){var r,o,a,i=arguments;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=i.length>1&&void 0!==i[1]?i[1]:{},t.next=3,this.searchAddress(e);case 3:if(!(o=t.sent)){t.next=8;break}return a=this.createMarker(o.lat,o.lng,e,"",r.iconClass||"fa-map-marker-alt",r.iconColor||"#003C71",r),this.map.setView([o.lat,o.lng],16),t.abrupt("return",a);case 8:return t.abrupt("return",null);case 9:case"end":return t.stop()}}),t,this)}))),function(t){return r.apply(this,arguments)})}],e&&s(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e,r,o,i,u,l}();return e.default})()));