UNPKG

vue-yandex-maps

Version:
3 lines (2 loc) 7.19 kB
import{defineComponent as e,ref as t,inject as o,provide as a,watch as n,h as s,computed as d,onMounted as r,onBeforeUnmount as l,Teleport as c}from"vue";const i=Symbol();const p=new class{events;ymapReady;scriptIsNotAttached;pluginInstalledGlobal;constructor(){this.events={},this.ymapReady=!1,this.scriptIsNotAttached=!0,this.pluginInstalledGlobal=!1}$on(e,t){return this.events[e]||(this.events[e]=[]),this.events[e].push(t),()=>{this.events[e]=this.events[e].filter((e=>t!==e))}}$emit(e){const t=this.events[e];t&&t.forEach((e=>e()))}};function u(e){return new Promise(((t,o)=>{if(window.ymaps)return p.ymapReady||(p.ymapReady=!0,p.$emit("scriptIsLoaded")),t(!0);if(document.getElementById("vue-yandex-maps-script"))return void p.$on("scriptIsLoaded",t);const a=document.createElement("SCRIPT"),{apiKey:n="",lang:s="ru_RU",version:d="2.1",coordorder:r="latlong",debug:l=!1,enterprise:c=!1}=e,i=`https://${c?"enterprise.":""}api-maps.yandex.ru/${d}/?${`lang=${s}${n&&`&apikey=${n}`}&mode=${l?"debug":"release"}&coordorder=${r}`}`;a.setAttribute("src",i),a.setAttribute("async",""),a.setAttribute("defer",""),a.setAttribute("id","vue-yandex-maps-script"),document.head.appendChild(a),p.scriptIsNotAttached=!1,a.onload=(e=>()=>{ymaps.ready((()=>{p.ymapReady=!0,p.$emit("scriptIsLoaded"),e(!0)}))})(t),a.onerror=o}))}const m=e=>Array.isArray(e)?e.map(m):+e,y=["actionbegin","actionbreak","actionend","actiontick","actiontickcomplete","balloonclose","balloonopen","boundschange","click","contextmenu","dblclick","destroy","hintclose","hintopen","marginchange","mousedown","mouseenter","mouseleave","mousemove","mouseup","multitouchend","multitouchmove","multitouchstart","optionschange","sizechange","typechange","wheel"],b=["balloonclose","balloonopen","beforedrag","beforedragstart","click","contextmenu","dblclick","drag","dragend","dragstart","editorstatechange","geometrychange","hintclose","hintopen","mapchange","mousedown","mouseenter","mouseleave","mousemove","mouseup","multitouchend","multitouchmove","multitouchstart","optionschange","overlaychange","parentchange","propertieschange","wheel"];function h(e,t=!1){let o,a,n=[],s=[];return{addGeoObject:(a,s)=>{n.push(t?s:a),clearTimeout(o),o=window.setTimeout((()=>{e(n,"add"),n=[]}))},deleteGeoObject:(o,n)=>{s.push(t?n:o),clearTimeout(a),a=window.setTimeout((()=>{e(s,"remove"),s=[]}))}}}var g=e({name:"YandexMap",props:{coordinates:{type:Array,required:!0},zoom:{type:Number,default:10},bounds:{type:Array,default:null},behaviors:{type:Array,default:null},controls:{type:Array,default:null},detailedControls:{type:Object,default:null},events:{type:Array,default:()=>["click"],validator:e=>e.every((e=>y.includes(e)))},mapType:{type:String,default:"map"},settings:{type:Object,default:()=>({})},panOptions:{type:Object,default:void 0},options:{type:Object,default:()=>({})}},emits:[...y,"geo-objects-updated","created"],setup(e,{emit:d,slots:r,expose:l}){const c=t(!1),m=p.pluginInstalledGlobal&&o("pluginOptions")||{};let y;const b=`yandexMap${Math.round(1e5*Math.random())}`,{addGeoObject:g,deleteGeoObject:j}=h(((e,t)=>{y&&e.length&&(e.forEach((e=>y?.geoObjects[t](e))),d("geo-objects-updated",y.geoObjects))}));a(i,{addGeoObject:g,deleteGeoObject:j});const v=()=>{if(c.value=!0,y=new ymaps.Map(b,{center:e.coordinates,zoom:e.zoom,bounds:e.bounds,behaviors:e.behaviors,controls:e.controls,type:`yandex#${e.mapType}`},e.options),e.events.forEach((e=>y?.events?.add(e,(t=>d(e,t))))),e.detailedControls){Object.keys(e.detailedControls).forEach((t=>{y?.controls.remove(t),y?.controls.add(t,e.detailedControls?.[t])}))}d("created",y)};if(p.scriptIsNotAttached){u({...m,...e.settings})}return p.ymapReady?ymaps.ready(v):p.$on("scriptIsLoaded",v),l(y),n((()=>e.coordinates),(t=>y?.panTo(t,e.panOptions))),n((()=>e.zoom),(e=>y?.setZoom(e))),n((()=>e.bounds),(e=>y?.setBounds(e))),n((()=>e.mapType),(e=>y?.setType(e))),()=>s("section",{class:"yandex-container","data-test":"map"},[s("div",{id:b,style:"min-height: 100%;"}),c.value&&s("div",{style:"display: none;"},[r.default?.()])])}}),j=e({name:"YandexMarker",props:{coordinates:{type:Array,required:!0},markerId:{type:[String,Number],required:!0},properties:{type:Object,default:()=>({})},options:{type:Object,default:null},type:{type:String,default:"Point"},radius:{type:Number,default:null},events:{type:Array,default:()=>["click"],validator:e=>e.every((e=>b.includes(e)))}},emits:[...b],setup(e,{emit:p,slots:u,expose:y}){const{addGeoObject:b,deleteGeoObject:h}=o(i)||{},g=d((()=>e.coordinates.map(m))),j=t(!1),v={geometry:{type:e.type,coordinates:g.value,radius:e.radius},properties:{...e.properties,markerId:e.markerId}},O=u.component?.().length?ymaps.templateLayoutFactory.createClass(`<div id="balloon-${e.markerId}" class="yandex-balloon"><div>`,{build(){O.superclass.build.call(this),j.value=!0},clear(){j.value=!1,O.superclass.clear.call(this)}}):null,f={balloonContentLayout:O,...e.options},G=new ymaps.GeoObject(v,f);e.events.forEach((e=>G.events?.add(e,(t=>p(e,t))))),a("marker",G);const x={...v,options:f};return r((()=>{b(G,x)})),l((()=>{h(G,x)})),n((()=>e.coordinates),(e=>{G.geometry?.setCoordinates?.(e)})),y(G),u.component?.().length?()=>j.value&&s(c,{to:`#balloon-${e.markerId}`},[u.component?.()]):()=>u.default?.()}}),v=e({name:"YandexGeoObjectCollection",props:{options:{type:Object,default:()=>({})}},emits:["geo-objects-updated"],setup(e,{emit:t,expose:n,slots:s}){const d=new ymaps.GeoObjectCollection({},e.options),{addGeoObject:c,deleteGeoObject:p}=o(i)||{},{addGeoObject:u,deleteGeoObject:m}=h(((e,o)=>{d&&e.length&&(e.forEach((e=>d[o](e))),t("geo-objects-updated",d))}));return a(i,{addGeoObject:u,deleteGeoObject:m}),r((()=>{c(d)})),l((()=>{p(d)})),n(d),()=>s.default?.()}}),O=e({name:"YandexClusterer",props:{options:{type:Object,default:()=>({})}},emits:["geo-objects-updated"],setup(e,{emit:t,expose:n,slots:s}){const d=new ymaps.Clusterer(e.options),{addGeoObject:c,deleteGeoObject:p}=o(i)||{},{addGeoObject:u,deleteGeoObject:m}=h(((e,o)=>{d&&e.length&&(d[o](e),t("geo-objects-updated",d))}));return a(i,{addGeoObject:u,deleteGeoObject:m}),r((()=>{c(d)})),l((()=>{p(d)})),n(d),()=>s.default?.()}}),f=e({name:"YandexObjectManager",props:{options:{type:Object,default:()=>({})}},emits:["geo-objects-updated"],setup(e,{emit:t,expose:n,slots:s}){const d=new ymaps.ObjectManager(e.options),{addGeoObject:c,deleteGeoObject:p}=o(i)||{},{addGeoObject:u,deleteGeoObject:m}=h(((e,o)=>{if(!d||!e.length)return;const a=e.map((e=>({type:"Feature",id:e.properties.markerId,geometry:e.geometry,properties:e.properties,options:e.options})));d[o](a),t("geo-objects-updated",d)}),!0);return a(i,{addGeoObject:u,deleteGeoObject:m}),r((()=>{c(d)})),l((()=>{p(d)})),n(d),()=>s.default?.()}});let G;const x=u,k=g,I=j,$=v,w=O,A=f;var C={install:(e,t)=>{G||(G=!0,p.pluginInstalledGlobal=!0,e.provide("pluginOptions",t),e.component("YandexMap",g),e.component("YandexMarker",j),e.component("YandexCollection",v),e.component("YandexClusterer",O),e.component("YandexObjectManager",f))}};export{w as YandexClusterer,$ as YandexCollection,k as YandexMap,I as YandexMarker,A as YandexObjectManager,C as default,x as loadYmap}; //# sourceMappingURL=vue-yandex-maps.umd.js.map