UNPKG

dtuappsmithmap

Version:

A Leaflet wrapper library for Appsmith with custom DTU functions

2 lines 16.6 kB
/*! For license information please see index.umd.js.LICENSE.txt */ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("leaflet")):"function"==typeof define&&define.amd?define(["leaflet"],e):"object"==typeof exports?exports.DTUAppsmithMap=e(require("leaflet")):t.DTUAppsmithMap=e(t.L)}(this,(t=>(()=>{"use strict";var e={167:e=>{e.exports=t}},r={};function n(t){var o=r[t];if(void 0!==o)return o.exports;var i=r[t]={exports:{}};return e[t](i,i.exports,n),i.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var o={};n.d(o,{default:()=>d});var i=n(167),a=n.n(i);function c(t){return c="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},c(t)}function u(){u=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",s=i.asyncIterator||"@@asyncIterator",l=i.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 f(t,e,r,n){var i=e&&e.prototype instanceof b?e:b,a=Object.create(i.prototype),c=new M(n||[]);return o(a,"_invoke",{value:P(t,r,c)}),a}function p(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=f;var v="suspendedStart",y="suspendedYield",d="executing",g="completed",m={};function b(){}function w(){}function k(){}var x={};h(x,a,(function(){return this}));var L=Object.getPrototypeOf,O=L&&L(L(S([])));O&&O!==r&&n.call(O,a)&&(x=O);var j=k.prototype=b.prototype=Object.create(x);function D(t){["next","throw","return"].forEach((function(e){h(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,e){function r(o,i,a,u){var s=p(t[o],t,i);if("throw"!==s.type){var l=s.arg,h=l.value;return h&&"object"==c(h)&&n.call(h,"__await")?e.resolve(h.__await).then((function(t){r("next",t,a,u)}),(function(t){r("throw",t,a,u)})):e.resolve(h).then((function(t){l.value=t,a(l)}),(function(t){return r("throw",t,a,u)}))}u(s.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new e((function(e,o){r(t,n,e,o)}))}return i=i?i.then(o,o):o()}})}function P(e,r,n){var o=v;return function(i,a){if(o===d)throw Error("Generator is already running");if(o===g){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var u=C(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===v)throw o=g,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var s=p(e,r,n);if("normal"===s.type){if(o=n.done?g:y,s.arg===m)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=g,n.method="throw",n.arg=s.arg)}}}function C(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,C(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var i=p(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,m;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,m):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,m)}function _(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 T(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function M(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function S(e){if(e||""===e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(n.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(c(e)+" is not iterable")}return w.prototype=k,o(j,"constructor",{value:k,configurable:!0}),o(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(j),t},e.awrap=function(t){return{__await:t}},D(E.prototype),h(E.prototype,s,(function(){return this})),e.AsyncIterator=E,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new E(f(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},D(j),h(j,l,"Generator"),h(j,a,(function(){return this})),h(j,"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=S,M.prototype={constructor:M,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(T),!e)for(var r in this)"t"===r.charAt(0)&&n.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 o(n,o){return c.type="throw",c.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(u&&s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,m):this.complete(a)},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),T(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;T(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:S(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),m}},e}function s(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function l(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var i=t.apply(e,r);function a(t){s(i,n,o,a,c,"next",t)}function c(t){s(i,n,o,a,c,"throw",t)}a(void 0)}))}}function h(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 f(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?h(Object(r),!0).forEach((function(e){p(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):h(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function p(t,e,r){return(e=y(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function v(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,y(n.key),n)}}function y(t){var e=function(t){if("object"!=c(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=c(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==c(e)?e:e+""}const d=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=null,this.markers=[],this.polylines=[],this.currentLocation=null,this.init(e,r)},e=[{key:"init",value:function(t,e){this.map=a().map(t,e),a().tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",{attribution:"© OpenStreetMap contributors"}).addTo(this.map)}},{key:"taoDiaDiem",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",i=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"#003C71",c=arguments.length>6&&void 0!==arguments[6]?arguments[6]:{},u=a().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(i,'; opacity: 0.8;"></i>\n <i class="').concat(o,' fa-stack-1x" style="color: ').concat(i,';"></i>\n </span>\n '),className:"custom-marker-icon",iconSize:[40,40],iconAnchor:[20,20],popupAnchor:[0,-20]}),s=f(f({},c.marker),{},{icon:u}),l=f(f({},c.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 "),p=a().marker([t,e],s).addTo(this.map).bindPopup(h,l).openPopup();return this.markers.push(p),p}},{key:"viTriHienTai",value:function(t,e,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"",o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:"fa-map-marker-alt",i=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"#003C71",a=arguments.length>6&&void 0!==arguments[6]?arguments[6]:{},c=this.taoDiaDiem(t,e,r,n,o,i,a);return this.map.setView([t,e],this.map.getZoom()),c}},{key:"tinhKhoangCach",value:function(t,e,r,n){var o=this._toRad(r-t),i=this._toRad(n-e),a=Math.sin(o/2)*Math.sin(o/2)+Math.cos(this._toRad(t))*Math.cos(this._toRad(r))*Math.sin(i/2)*Math.sin(i/2),c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a))*6371;return Math.round(100*c)/100}},{key:"_toRad",value:function(t){return t*Math.PI/180}},{key:"tinhKhoangCachMarker",value:function(t,e){var r=t.getLatLng(),n=e.getLatLng();return this.tinhKhoangCach(r.lat,r.lng,n.lat,n.lng)}},{key:"veDuongDi",value:(i=l(u().mark((function t(e,r,n,o){var i,c,s,l,h,p,v=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=f({color:"#003C71",weight:3,opacity:.8,dashArray:null},v.length>4&&void 0!==v[4]?v[4]:{}),t.prev=2,t.next=5,fetch("https://router.project-osrm.org/route/v1/driving/".concat(r,",").concat(e,";").concat(o,",").concat(n,"?overview=full&geometries=geojson"));case 5:return c=t.sent,t.next=8,c.json();case 8:if("Ok"===(s=t.sent).code&&s.routes.length){t.next=11;break}throw new Error("Không thể tìm được đường đi");case 11:return l=s.routes[0].geometry.coordinates,h=l.map((function(t){return[t[1],t[0]]})),this.xoaDuongDi(),p=a().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("Lỗi khi vẽ đường:",t.t0),t.abrupt("return",null);case 24:case"end":return t.stop()}}),t,this,[[2,20]])}))),function(t,e,r,n){return i.apply(this,arguments)})},{key:"veDuongDiMarker",value:(o=l(u().mark((function t(e,r){var n,o,i,a=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=a.length>2&&void 0!==a[2]?a[2]:{},o=e.getLatLng(),i=r.getLatLng(),t.next=5,this.veDuongDi(o.lat,o.lng,i.lat,i.lng,n);case 5:return t.abrupt("return",t.sent);case 6:case"end":return t.stop()}}),t,this)}))),function(t,e){return o.apply(this,arguments)})},{key:"xoaDuongDi",value:function(){this.polylines.forEach((function(t){t.remove()})),this.polylines=[]}},{key:"capNhatViTriHienTai",value:function(t,e){var r=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"Vị trí hiện tại",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.taoDiaDiem(t,e,n,o,"fa-solid fa-location-dot","#FF0000"),this.currentLocation}},{key:"tinhKhoangCachDenDiem",value:function(t,e){if(!this.currentLocation)return console.warn("Chưa có vị trí hiện t���i"),null;var r=this.currentLocation.getLatLng();return this.tinhKhoangCach(r.lat,r.lng,t,e)}},{key:"tinhKhoangCachDenCacDiem",value:function(){var t=this;return this.currentLocation?this.markers.filter((function(e){return e!==t.currentLocation})).map((function(e){var r,n=e.getLatLng(),o=t.tinhKhoangCachDenDiem(n.lat,n.lng),i=(null===(r=e.getPopup())||void 0===r||null===(r=r.getContent())||void 0===r||null===(r=r.match(/<h3[^>]*>(.*?)<\/h3>/))||void 0===r?void 0:r[1])||"Không có tiêu đề";return{marker:e,title:i,distance:o}})).sort((function(t,e){return t.distance-e.distance})):(console.warn("Chưa có vị trí hiện tại"),[])}},{key:"veVungTron",value:function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1e3,n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"#003C71",o=function(t,e){var r=t.replace("#",""),n=parseInt(r.substring(0,2),16),o=parseInt(r.substring(2,4),16),i=parseInt(r.substring(4,6),16);return"rgba(".concat(n,", ").concat(o,", ").concat(i,", ").concat(e,")")},i=f({stroke:!0,color:n,weight:2,opacity:1,fillColor:n,fillOpacity:.2},arguments.length>4&&void 0!==arguments[4]?arguments[4]:{}),c=a().circle([t,e],f(f({radius:r},i),{},{gradient:!0,fillGradient:{radial:!0,colors:[{offset:0,color:o(n,.1)},{offset:.5,color:o(n,.2)},{offset:1,color:o(n,.3)}]}})).addTo(this.map);return this.circles||(this.circles=[]),this.circles.push(c),this.map.fitBounds(c.getBounds(),{padding:[50,50]}),c}},{key:"xoaVungTron",value:function(){this.circles&&(this.circles.forEach((function(t){t.remove()})),this.circles=[])}},{key:"timKiemDiaChi",value:(n=l(u().mark((function t(e){var r,n;return u().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(!((n=t.sent)&&n.length>0)){t.next=9;break}return t.abrupt("return",{lat:parseFloat(n[0].lat),lng:parseFloat(n[0].lon)});case 9:return t.abrupt("return",null);case 12:return t.prev=12,t.t0=t.catch(0),console.error("Lỗi khi tìm kiếm địa chỉ:",t.t0),t.abrupt("return",null);case 16:case"end":return t.stop()}}),t,null,[[0,12]])}))),function(t){return n.apply(this,arguments)})},{key:"timVaDanhDau",value:(r=l(u().mark((function t(e){var r,n,o,i=arguments;return u().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.timKiemDiaChi(e);case 3:if(!(n=t.sent)){t.next=8;break}return o=this.taoDiaDiem(n.lat,n.lng,e,"",r.iconClass||"fa-map-marker-alt",r.iconColor||"#003C71",r),this.map.setView([n.lat,n.lng],16),t.abrupt("return",o);case 8:return t.abrupt("return",null);case 9:case"end":return t.stop()}}),t,this)}))),function(t){return r.apply(this,arguments)})}],e&&v(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e,r,n,o,i}();return o.default})()));