UNPKG

@iminside/react-yandex-maps

Version:
982 lines (981 loc) 27.7 kB
!(function (e, t) { 'object' == typeof exports && 'undefined' != typeof module ? t(exports, require('react')) : 'function' == typeof define && define.amd ? define(['exports', 'react'], t) : t(((e || self).ReactYandexMaps = {}), e.React); })(this, function (e, t) { function n(e) { return e && 'object' == typeof e && 'default' in e ? e : { default: e }; } var o = /*#__PURE__*/ n(t); function s() { return ( (s = Object.assign ? Object.assign.bind() : function (e) { for (var t = 1; t < arguments.length; t++) { var n = arguments[t]; for (var o in n) ({}).hasOwnProperty.call(n, o) && (e[o] = n[o]); } return e; }), s.apply(null, arguments) ); } const r = (e, t) => { const n = {}; for (const o in e) -1 === t.indexOf(o) && (n[o] = e[o]); return n; }, a = /*#__PURE__*/ o.default.createContext(null), c = /*#__PURE__*/ o.default.createContext(null), i = (e) => (t) => /*#__PURE__*/ o.default.createElement(c.Consumer, null, (n) => /*#__PURE__*/ o.default.createElement(e, s({ parent: n }, t)) ), l = (e = []) => { const [n, o] = t.useState(!1), s = t.useRef(e), r = (() => { const e = t.useContext(a); if (null === e) throw new Error( "Couldn't find Yandex.Maps API in the context. Make sure that hook useYMaps is inside <YMaps /> provider" ); return e; })(), c = r.getApi(); return ( t.useEffect(() => { r.load() .then(() => Promise.all(s.current.map(r.loadModule))) .then(() => o(!0)); }, []), n && c ? c : null ); }, u = () => {}, p = ['onLoad', 'onError', 'modules', 'apiLoader']; function d(e, n = !1, a = []) { return (c) => { const { width: i, height: d, modules: f = [], onLoad: m = u } = c, h = l(a.concat(f)), y = !n || !!h, b = r(c, p); return ( t.useEffect(() => (h ? m(h) : void 0), [h]), y ? /*#__PURE__*/ o.default.createElement(e, s({ ymaps: h }, b)) : /*#__PURE__*/ o.default.createElement('div', { style: { width: i, height: d }, }) ); }; } const f = 'undefined' != typeof window, m = { lang: 'ru_RU', load: '', ns: '', mode: 'release' }, h = /^on(?=[A-Z])/; function y(e) { return Object.keys(e).reduce( (t, n) => { if (h.test(n)) { const o = n.replace(h, '').toLowerCase(); t._events[o] = e[n]; } else t[n] = e[n]; return t; }, { _events: {} } ); } function b(e, t, n) { 'function' == typeof n && e.events.add(t, n); } function v(e, t, n) { 'function' == typeof n && e.events.remove(t, n); } function j(e, t, n) { Object.keys(Object.assign({}, t, n)).forEach((o) => { t[o] !== n[o] && (v(e, o, t[o]), b(e, o, n[o])); }); } const O = (e) => 'default' + e.charAt(0).toUpperCase() + e.slice(1); function E(e, t) { return void 0 !== e[t] || void 0 === e[O(t)]; } function g(e, t, n) { return (E(e, t) ? e[t] : e[O(t)]) || n; } function C(e, t, n = null) { if (e !== t) { if ( (e && ('current' in e ? (e.current = null) : 'function' == typeof e && e(null)), !t) ) return; 'current' in t ? (t.current = n) : 'function' == typeof t && t(n); } } function R(e) { const { width: t, height: n, style: o, className: s } = e; return void 0 !== o || void 0 !== s ? Object.assign({}, o && { style: o }, s && { className: s }) : { style: { width: t, height: n } }; } class _ extends o.default.Component { constructor(e) { super(e), (this.state = { error: null, errorInfo: null }); } componentDidCatch(e, t) { const { onError: n = () => {} } = this.props; n(e), this.setState({ error: e, errorInfo: t }); } render() { return this.state.error ? null : this.props.children; } } const w = (e) => ({ onError: t, ...n }) => /*#__PURE__*/ o.default.createElement( _, { onError: t }, /*#__PURE__*/ o.default.createElement(e, n) ); class P extends o.default.Component { constructor() { super(), (this.instance = null), (this.state = { instance: null }), (this._parentElement = null), (this._getRef = (e) => { this._parentElement = e; }); } componentDidMount() { (this.instance = P.mountObject( this._parentElement, this.props.ymaps.Map, this.props )), this.setState({ instance: this.instance }); } componentDidUpdate(e) { null !== this.instance && P.updateObject(this.instance, e, this.props); } componentWillUnmount() { P.unmountObject(this.instance, this.props); } render() { const e = R(this.props), t = y(this.props), n = r(t, [ '_events', 'state', 'defaultState', 'options', 'defaultOptions', 'instanceRef', 'ymaps', 'children', 'width', 'height', 'style', 'className', ]); /*#__PURE__*/ return o.default.createElement( c.Provider, { value: this.state.instance }, /*#__PURE__*/ o.default.createElement( 'div', s({ ref: this._getRef }, e, n), this.props.children ) ); } static mountObject(e, t, n) { const { instanceRef: o, _events: s } = y(n), r = new t(e, g(n, 'state'), g(n, 'options')); return Object.keys(s).forEach((e) => b(r, e, s[e])), C(null, o, r), r; } static updateObject(e, t, n) { const { _events: o, instanceRef: s } = y(n), { _events: r, instanceRef: a } = y(t); if (E(n, 'state')) { const o = g(t, 'state', {}), s = g(n, 'state', {}); o.type !== s.type && e.setType(s.type), o.behaviors !== s.behaviors && (o.behaviors && e.behaviors.disable(o.behaviors), s.behaviors && e.behaviors.enable(s.behaviors)), s.zoom && o.zoom !== s.zoom && e.setZoom(s.zoom), s.center && o.center !== s.center && e.setCenter(s.center), s.bounds && o.bounds !== s.bounds && e.setBounds(s.bounds); } if (E(n, 'options')) { const o = g(t, 'options'), s = g(n, 'options', {}); o !== s && e.options.set(s); } (g(t, 'width') === g(n, 'width') && g(t, 'height') === g(n, 'height')) || e.container.fitToViewport(), j(e, r, o), C(a, s, e); } static unmountObject(e, t) { const { instanceRef: n, _events: o } = y(t); null !== e && (Object.keys(o).forEach((t) => v(e, t, o[t])), e.destroy(), C(n)); } } const x = w(d(P, !0, ['Map'])); class M extends o.default.Component { constructor() { super(), (this.state = { instance: null }), (this._parentElement = null), (this._getRef = (e) => { this._parentElement = e; }); } componentDidMount() { (this._mounted = !0), this.props.ymaps.panorama.isSupported() && M.mountObject( this._parentElement, this.props.ymaps.panorama, this.props ).then((e) => this._mounted && this.setState({ instance: e })); } componentDidUpdate(e) { null !== this.state.instance && M.updateObject(this.state.instance, e, this.props); } componentWillUnmount() { (this._mounted = !1), M.unmountObject(this.state.instance, this.props); } render() { const e = R(this.props); /*#__PURE__*/ return o.default.createElement( 'div', s({ ref: this._getRef }, e) ); } static mountObject(e, t, n) { const { instanceRef: o, _events: s } = y(n), r = g(n, 'point'), a = g(n, 'locateOptions'), c = g(n, 'options'); return new Promise((n, i) => { t.locate(r, a).done((r) => { if (r.length > 0) { const a = new t.Player(e, r[0], c); C(null, o, a), Object.keys(s).forEach((e) => b(a, e, s[e])), n(a); } }, i); }); } static updateObject(e, t, n) { const { _events: o, instanceRef: s } = y(n), { _events: r, instanceRef: a } = y(t); if (E(n, 'options')) { const o = g(t, 'options'), s = g(n, 'options'); o !== s && e.options.set(s); } if (E(n, 'point')) { const o = g(n, 'point'), s = g(t, 'point'), r = g(n, 'locateOptions'); o !== s && e.moveTo(o, r); } j(e, r, o), C(a, s, e); } static unmountObject(e, t) { const { instanceRef: n, _events: o } = y(t); null !== e && (Object.keys(o).forEach((t) => v(e, t, o[t])), C(n)); } } const k = w( d(M, !0, [ 'panorama.isSupported', 'panorama.locate', 'panorama.createPlayer', 'panorama.Player', ]) ); class S extends o.default.Component { constructor() { super(), (this.state = { instance: null }), (this.instance = null); } componentDidMount() { const e = S.mountControl( this.props.ymaps.control[this.props.name], this.props ); (this.instance = e), this.setState({ instance: e }); } componentDidUpdate(e) { null !== this.instance && S.updateControl(this.instance, e, this.props); } componentWillUnmount() { S.unmountControl(this.instance, this.props); } render() { /*#__PURE__*/ return o.default.createElement( c.Provider, { value: this.state.instance }, this.props.children ); } static mountControl(e, t) { const { instanceRef: n, parent: o, lazy: s, _events: r } = y(t), a = new e({ data: g(t, 'data'), options: g(t, 'options'), state: g(t, 'state'), mapTypes: g(t, 'mapTypes'), lazy: s, }); if ( (Object.keys(r).forEach((e) => b(a, e, r[e])), o && o.controls && 'function' == typeof o.controls.add) ) o.controls.add(a); else { if (!o || !o.add || 'function' != typeof o.add) throw new Error(`No parent found to mount ${t.name}`); o.add(a); } return C(null, n, a), a; } static updateControl(e, t, n) { const { _events: o, instanceRef: s } = y(n), { _events: r, instanceRef: a } = y(t); if (E(n, 'options')) { const o = g(t, 'options'), s = g(n, 'options'); o !== s && e.options.set(s); } if (E(n, 'data')) { const o = g(t, 'data'), s = g(n, 'data'); o !== s && e.data.set(s); } if (E(n, 'state')) { const o = g(t, 'state'), s = g(n, 'state'); o !== s && e.state.set(s); } if (E(n, 'mapTypes')) { const o = g(t, 'mapTypes'), s = g(n, 'mapTypes'); o !== s && (e.removeAllMapTypes(), s.forEach((t) => e.addMapType(t))); } j(e, r, o), C(a, s, e); } static unmountControl(e, t) { const { instanceRef: n, parent: o, _events: s } = y(t); null !== e && (Object.keys(s).forEach((t) => v(e, t, s[t])), o.controls && 'function' == typeof o.controls.remove ? o.controls.remove(e) : o.remove && 'function' == typeof o.remove && o.remove(e), C(n)); } } var T = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'Button' }) ), !0, ['control.Button'] ) ) ), U = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'FullscreenControl' }) ), !0, ['control.FullscreenControl'] ) ) ), D = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'GeolocationControl' }) ), !0, ['control.GeolocationControl'] ) ) ), B = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'ListBox' }) ), !0, ['control.ListBox'] ) ) ), A = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'ListBoxItem' }) ), !0, ['control.ListBoxItem'] ) ) ), L = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'RouteButton' }) ), !0, ['control.RouteButton'] ) ) ), $ = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'RouteEditor' }) ), !0, ['control.RouteEditor'] ) ) ), N = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'RoutePanel' }) ), !0, ['control.RoutePanel'] ) ) ), Y = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'RulerControl' }) ), !0, ['control.RulerControl'] ) ) ), Z = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'SearchControl' }) ), !0, ['control.SearchControl'] ) ) ), z = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'TrafficControl' }) ), !0, ['control.TrafficControl'] ) ) ), G = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'TypeSelector' }) ), !0, ['control.TypeSelector'] ) ) ), I = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( S, s({}, e, { name: 'ZoomControl' }) ), !0, ['control.ZoomControl'] ) ) ); class W extends o.default.Component { constructor() { super(), (this.state = { instance: null }), (this.instance = null); } componentDidMount() { const e = W.mountObject(this.props.ymaps.Clusterer, this.props); (this.instance = e), this.setState({ instance: e }); } componentDidUpdate(e) { null !== this.state.instance && W.updateObject(this.instance, e, this.props); } componentWillUnmount() { W.unmountObject(this.instance, this.props); } render() { /*#__PURE__*/ return o.default.createElement( c.Provider, { value: this.state.instance }, this.props.children ); } static mountObject(e, t) { const { instanceRef: n, parent: o, _events: s } = y(t), r = new e(g(t, 'options')); if ( (Object.keys(s).forEach((e) => b(r, e, s[e])), o.geoObjects && 'function' == typeof o.geoObjects.add) ) o.geoObjects.add(r); else { if (!o.add || 'function' != typeof o.add) throw new Error('No parent found to mount Clusterer'); o.add(r); } return C(null, n, r), r; } static updateObject(e, t, n) { const { _events: o, instanceRef: s } = y(n), { _events: r, instanceRef: a } = y(t); if (E(n, 'options')) { const o = g(t, 'options'), s = g(n, 'options'); o !== s && e.options.set(s); } j(e, r, o), C(a, s, e); } static unmountObject(e, t) { const { instanceRef: n, parent: o, _events: s } = y(t); null !== e && (Object.keys(s).forEach((t) => v(e, t, s[t])), o.geoObjects && 'function' == typeof o.geoObjects.remove ? o.geoObjects.remove(e) : o.remove && 'function' == typeof o.remove && o.remove(e), C(n)); } } var q = w(i(d(W, !0, ['Clusterer']))); class F extends o.default.Component { constructor() { super(), (this.state = { instance: null }); } componentDidMount() { const e = F.mountObject(this.props.ymaps.ObjectManager, this.props); (this.instance = e), this.setState({ instance: e }); } componentDidUpdate(e) { null !== this.instance && F.updateObject(this.instance, e, this.props); } componentWillUnmount() { F.unmountObject(this.instance, this.props); } render() { return null; } static mountObject(e, t) { const { instanceRef: n, parent: o, _events: s } = y(t), r = g(t, 'options', {}), a = g(t, 'features', {}), c = g(t, 'filter', null), i = g(t, 'objects', {}), l = g(t, 'clusters', {}), u = new e(r); if ( (u.add(a || []), u.setFilter(c), u.objects.options.set(i), u.clusters.options.set(l), Object.keys(s).forEach((e) => b(u, e, s[e])), o.geoObjects && 'function' == typeof o.geoObjects.add) ) o.geoObjects.add(u); else { if (!o.add || 'function' != typeof o.add) throw new Error('No parent found to mount ObjectManager'); o.add(u); } return C(null, n, u), u; } static updateObject(e, t, n) { const { _events: o, instanceRef: s } = y(n), { _events: r, instanceRef: a } = y(t); if (E(n, 'options')) { const o = g(t, 'options'), s = g(n, 'options'); o !== s && e.options.set(s); } if (E(n, 'objects')) { const o = g(t, 'objects'), s = g(n, 'objects'); o !== s && e.objects.options.set(s); } if (E(n, 'clusters')) { const o = g(t, 'clusters'), s = g(n, 'clusters'); o !== s && e.clusters.options.set(s); } if (E(n, 'filter')) { const o = g(t, 'filter'), s = g(n, 'filter'); o !== s && e.setFilter(s); } if (E(n, 'features')) { const o = g(t, 'features'), s = g(n, 'features'); o !== s && (e.remove(o), e.add(s)); } j(e, r, o), C(a, s, e); } static unmountObject(e, t) { const { instanceRef: n, parent: o, _events: s } = y(t); null !== e && (Object.keys(s).forEach((t) => v(e, t, s[t])), o.geoObjects && 'function' == typeof o.geoObjects.remove ? o.geoObjects.remove(e) : o.remove && 'function' == typeof o.remove && o.remove(e), C(n)); } } var V = w(i(d(F, !0, ['ObjectManager']))); class H extends o.default.Component { constructor() { super(), (this.state = { instance: null }), (this.instance = null); } componentDidMount() { const { name: e, ymaps: t, dangerZone: n } = this.props, o = H.mountObject( n && 'function' == typeof n.modifyConstructor ? n.modifyConstructor(t[e]) : t[e], this.props ); (this.instance = o), this.setState({ instance: o }); } componentDidUpdate(e) { null !== this.instance && H.updateObject(this.instance, e, this.props); } componentWillUnmount() { H.unmountObject(this.instance, this.props); } render() { return null; } static mountObject(e, t) { const { instanceRef: n, parent: o, _events: s } = y(t), r = new e(g(t, 'geometry'), g(t, 'properties'), g(t, 'options')); if ( (Object.keys(s).forEach((e) => b(r, e, s[e])), o && o.geoObjects && 'function' == typeof o.geoObjects.add) ) o.geoObjects.add(r); else { if (!o || !o.add || 'function' != typeof o.add) throw new Error(`No parent found to mount ${t.name}`); o.add(r); } return C(null, n, r), r; } static updateObject(e, t, n) { const { _events: o, instanceRef: s } = y(n), { _events: r, instanceRef: a } = y(t); if (E(n, 'geometry')) { const o = g(t, 'geometry', {}), s = g(n, 'geometry', {}); Array.isArray(s) && s !== o ? Array.isArray(s[0]) && 'number' == typeof s[1] ? (e.geometry.setCoordinates(s[0]), e.geometry.setRadius(s[1])) : e.geometry.setCoordinates(s) : 'object' == typeof s && (s.coordinates !== o.coordinates && e.geometry.setCoordinates(s.coordinates), s.radius !== o.radius && e.geometry.setRadius(s.radius)); } if (E(n, 'properties')) { const o = g(t, 'properties'), s = g(n, 'properties'); o !== s && e.properties.set(s); } if (E(n, 'options')) { const o = g(t, 'options'), s = g(n, 'options'); o !== s && e.options.set(s); } j(e, r, o), C(a, s, e); } static unmountObject(e, t) { const { instanceRef: n, parent: o, _events: s } = y(t); null !== e && (Object.keys(s).forEach((t) => v(e, t, s[t])), o.geoObjects && 'function' == typeof o.geoObjects.remove ? o.geoObjects.remove(e) : o.remove && 'function' == typeof o.remove && o.remove(e), C(n)); } } const J = { modifyConstructor(e) { function t(t, n, o) { e.call(this, { geometry: t, properties: n }, o); } return (t.prototype = e.prototype), t; }, }; var K = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( H, s({}, e, { name: 'GeoObject', dangerZone: J }) ), !0, ['GeoObject'] ) ) ), Q = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( H, s({}, e, { name: 'Circle' }) ), !0, ['Circle'] ) ) ), X = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( H, s({}, e, { name: 'Placemark' }) ), !0, ['Placemark'] ) ) ), ee = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( H, s({}, e, { name: 'Polygon' }) ), !0, ['Polygon'] ) ) ), te = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( H, s({}, e, { name: 'Polyline' }) ), !0, ['Polyline'] ) ) ), ne = w( i( d( (e) => /*#__PURE__*/ o.default.createElement( H, s({}, e, { name: 'Rectangle' }) ), !0, ['Rectangle'] ) ) ); (e.Button = T), (e.Circle = Q), (e.Clusterer = q), (e.FullscreenControl = U), (e.GeoObject = K), (e.GeolocationControl = D), (e.ListBox = B), (e.ListBoxItem = A), (e.Map = x), (e.ObjectManager = V), (e.Panorama = k), (e.Placemark = X), (e.Polygon = ee), (e.Polyline = te), (e.Rectangle = ne), (e.RouteButton = L), (e.RouteEditor = $), (e.RoutePanel = N), (e.RulerControl = Y), (e.SearchControl = Z), (e.TrafficControl = z), (e.TypeSelector = G), (e.YMaps = (e) => { const { version: n = '2.1', enterprise: s = !1, query: r = { lang: 'ru_RU', load: '', ns: '' }, preload: c = !1, children: i, } = e, l = t.useRef( ((e) => { const { query: t = m } = e, n = Date.now().toString(32), o = t.ns || '', s = '__yandex-maps-api-onload__$$' + n, r = '__yandex-maps-api-onerror__$$' + n, a = f ? window : {}, c = {}; let i; const l = () => (o ? a[o] : i), u = () => { delete a[s], delete a[r]; }; return { load: () => { if (l()) return Promise.resolve(i); if (c[o]) return c[o]; const n = { onload: s, onerror: r, ...m, ...t }, p = Object.keys(n) .map((e) => `${e}=${n[e]}`) .join('&'), d = [ `https://${ e.enterprise ? 'enterprise.' : '' }api-maps.yandex.ru`, e.version, '?' + p, ].join('/'); return ( (c[o] = new Promise((e, t) => { (a[s] = (t) => { u(), t.ready(() => { (i = t), e(t); }); }), (a[r] = (e) => { u(), t(e); }), ((e) => new Promise((t, n) => { const o = document.createElement('script'); (o.type = 'text/javascript'), (o.onload = t), (o.onerror = n), (o.src = e), (o.async = !0), document.head.appendChild(o); }))(d).catch(a[r]); })), c[o] ); }, getApi: l, loadModule: (e) => new Promise((t, n) => { i.modules.require(e).done((n) => { n.forEach((t) => { ((e, t, n, o = !1) => { const s = 'string' == typeof t ? t.split('.') : t.slice(); let r, a = e; for (; s.length > 1; ) (r = s.shift()), a[r] || (a[r] = {}), (a = a[r]); const c = s[0]; a[c] = (!0 === o && a[c]) || n; })(i, e, t, !0); }), t(i); }, n); }), }; })({ version: n, enterprise: s, query: r, preload: c }) ); return ( t.useEffect(() => { c && l.current.load(); }, [l.current]), /*#__PURE__*/ o.default.createElement( a.Provider, { value: l.current }, i ) ); }), (e.ZoomControl = I), (e.useYMaps = l), (e.withYMaps = d); }); //# sourceMappingURL=react-yandex-maps.umd.js.map