@iminside/react-yandex-maps
Version:
Yandex.Maps API bindings for React
921 lines (920 loc) • 24.4 kB
JavaScript
import e, {
useContext as t,
useState as n,
useRef as o,
useEffect as s,
} from 'react';
function r() {
return (
(r = 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;
}),
r.apply(null, arguments)
);
}
const a = (e, t) => {
const n = {};
for (const o in e) -1 === t.indexOf(o) && (n[o] = e[o]);
return n;
},
c = /*#__PURE__*/ e.createContext(null),
i = /*#__PURE__*/ e.createContext(null),
l = (t) => (n) =>
/*#__PURE__*/ e.createElement(i.Consumer, null, (o) =>
/*#__PURE__*/ e.createElement(t, r({ parent: o }, n))
),
p = (e = []) => {
const [r, a] = n(!1),
i = o(e),
l = (() => {
const e = t(c);
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;
})(),
p = l.getApi();
return (
s(() => {
l.load()
.then(() => Promise.all(i.current.map(l.loadModule)))
.then(() => a(!0));
}, []),
r && p ? p : null
);
},
u = () => {},
m = ['onLoad', 'onError', 'modules', 'apiLoader'];
function d(t, n = !1, o = []) {
return (c) => {
const { width: i, height: l, modules: d = [], onLoad: f = u } = c,
h = p(o.concat(d)),
y = !n || !!h,
b = a(c, m);
return (
s(() => (h ? f(h) : void 0), [h]),
y
? /*#__PURE__*/ e.createElement(t, r({ ymaps: h }, b))
: /*#__PURE__*/ e.createElement('div', {
style: { width: i, height: l },
})
);
};
}
const f = 'undefined' != typeof window,
h = { lang: 'ru_RU', load: '', ns: '', mode: 'release' },
y = (t) => {
const {
version: n = '2.1',
enterprise: r = !1,
query: a = { lang: 'ru_RU', load: '', ns: '' },
preload: i = !1,
children: l,
} = t,
p = o(
((e) => {
const { query: t = h } = 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),
p = () => {
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, ...h, ...t },
u = Object.keys(n)
.map((e) => `${e}=${n[e]}`)
.join('&'),
m = [
`https://${
e.enterprise ? 'enterprise.' : ''
}api-maps.yandex.ru`,
e.version,
'?' + u,
].join('/');
return (
(c[o] = new Promise((e, t) => {
(a[s] = (t) => {
p(),
t.ready(() => {
(i = t), e(t);
});
}),
(a[r] = (e) => {
p(), 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);
}))(m).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: r, query: a, preload: i })
);
return (
s(() => {
i && p.current.load();
}, [p.current]),
/*#__PURE__*/ e.createElement(c.Provider, { value: p.current }, l)
);
},
b = /^on(?=[A-Z])/;
function v(e) {
return Object.keys(e).reduce(
(t, n) => {
if (b.test(n)) {
const o = n.replace(b, '').toLowerCase();
t._events[o] = e[n];
} else t[n] = e[n];
return t;
},
{ _events: {} }
);
}
function j(e, t, n) {
'function' == typeof n && e.events.add(t, n);
}
function O(e, t, n) {
'function' == typeof n && e.events.remove(t, n);
}
function E(e, t, n) {
Object.keys(Object.assign({}, t, n)).forEach((o) => {
t[o] !== n[o] && (O(e, o, t[o]), j(e, o, n[o]));
});
}
const g = (e) => 'default' + e.charAt(0).toUpperCase() + e.slice(1);
function C(e, t) {
return void 0 !== e[t] || void 0 === e[g(t)];
}
function _(e, t, n) {
return (C(e, t) ? e[t] : e[g(t)]) || n;
}
function R(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 w(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 P extends e.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 x =
(t) =>
({ onError: n, ...o }) =>
/*#__PURE__*/ e.createElement(
P,
{ onError: n },
/*#__PURE__*/ e.createElement(t, o)
);
class M extends e.Component {
constructor() {
super(),
(this.instance = null),
(this.state = { instance: null }),
(this._parentElement = null),
(this._getRef = (e) => {
this._parentElement = e;
});
}
componentDidMount() {
(this.instance = M.mountObject(
this._parentElement,
this.props.ymaps.Map,
this.props
)),
this.setState({ instance: this.instance });
}
componentDidUpdate(e) {
null !== this.instance && M.updateObject(this.instance, e, this.props);
}
componentWillUnmount() {
M.unmountObject(this.instance, this.props);
}
render() {
const t = w(this.props),
n = v(this.props),
o = a(n, [
'_events',
'state',
'defaultState',
'options',
'defaultOptions',
'instanceRef',
'ymaps',
'children',
'width',
'height',
'style',
'className',
]);
/*#__PURE__*/ return e.createElement(
i.Provider,
{ value: this.state.instance },
/*#__PURE__*/ e.createElement(
'div',
r({ ref: this._getRef }, t, o),
this.props.children
)
);
}
static mountObject(e, t, n) {
const { instanceRef: o, _events: s } = v(n),
r = new t(e, _(n, 'state'), _(n, 'options'));
return Object.keys(s).forEach((e) => j(r, e, s[e])), R(null, o, r), r;
}
static updateObject(e, t, n) {
const { _events: o, instanceRef: s } = v(n),
{ _events: r, instanceRef: a } = v(t);
if (C(n, 'state')) {
const o = _(t, 'state', {}),
s = _(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 (C(n, 'options')) {
const o = _(t, 'options'),
s = _(n, 'options', {});
o !== s && e.options.set(s);
}
(_(t, 'width') === _(n, 'width') && _(t, 'height') === _(n, 'height')) ||
e.container.fitToViewport(),
E(e, r, o),
R(a, s, e);
}
static unmountObject(e, t) {
const { instanceRef: n, _events: o } = v(t);
null !== e &&
(Object.keys(o).forEach((t) => O(e, t, o[t])), e.destroy(), R(n));
}
}
const k = x(d(M, !0, ['Map']));
class S extends e.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() &&
S.mountObject(
this._parentElement,
this.props.ymaps.panorama,
this.props
).then((e) => this._mounted && this.setState({ instance: e }));
}
componentDidUpdate(e) {
null !== this.state.instance &&
S.updateObject(this.state.instance, e, this.props);
}
componentWillUnmount() {
(this._mounted = !1), S.unmountObject(this.state.instance, this.props);
}
render() {
const t = w(this.props);
/*#__PURE__*/ return e.createElement('div', r({ ref: this._getRef }, t));
}
static mountObject(e, t, n) {
const { instanceRef: o, _events: s } = v(n),
r = _(n, 'point'),
a = _(n, 'locateOptions'),
c = _(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);
R(null, o, a), Object.keys(s).forEach((e) => j(a, e, s[e])), n(a);
}
}, i);
});
}
static updateObject(e, t, n) {
const { _events: o, instanceRef: s } = v(n),
{ _events: r, instanceRef: a } = v(t);
if (C(n, 'options')) {
const o = _(t, 'options'),
s = _(n, 'options');
o !== s && e.options.set(s);
}
if (C(n, 'point')) {
const o = _(n, 'point'),
s = _(t, 'point'),
r = _(n, 'locateOptions');
o !== s && e.moveTo(o, r);
}
E(e, r, o), R(a, s, e);
}
static unmountObject(e, t) {
const { instanceRef: n, _events: o } = v(t);
null !== e && (Object.keys(o).forEach((t) => O(e, t, o[t])), R(n));
}
}
const T = x(
d(S, !0, [
'panorama.isSupported',
'panorama.locate',
'panorama.createPlayer',
'panorama.Player',
])
);
class U extends e.Component {
constructor() {
super(), (this.state = { instance: null }), (this.instance = null);
}
componentDidMount() {
const e = U.mountControl(
this.props.ymaps.control[this.props.name],
this.props
);
(this.instance = e), this.setState({ instance: e });
}
componentDidUpdate(e) {
null !== this.instance && U.updateControl(this.instance, e, this.props);
}
componentWillUnmount() {
U.unmountControl(this.instance, this.props);
}
render() {
/*#__PURE__*/ return e.createElement(
i.Provider,
{ value: this.state.instance },
this.props.children
);
}
static mountControl(e, t) {
const { instanceRef: n, parent: o, lazy: s, _events: r } = v(t),
a = new e({
data: _(t, 'data'),
options: _(t, 'options'),
state: _(t, 'state'),
mapTypes: _(t, 'mapTypes'),
lazy: s,
});
if (
(Object.keys(r).forEach((e) => j(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 R(null, n, a), a;
}
static updateControl(e, t, n) {
const { _events: o, instanceRef: s } = v(n),
{ _events: r, instanceRef: a } = v(t);
if (C(n, 'options')) {
const o = _(t, 'options'),
s = _(n, 'options');
o !== s && e.options.set(s);
}
if (C(n, 'data')) {
const o = _(t, 'data'),
s = _(n, 'data');
o !== s && e.data.set(s);
}
if (C(n, 'state')) {
const o = _(t, 'state'),
s = _(n, 'state');
o !== s && e.state.set(s);
}
if (C(n, 'mapTypes')) {
const o = _(t, 'mapTypes'),
s = _(n, 'mapTypes');
o !== s && (e.removeAllMapTypes(), s.forEach((t) => e.addMapType(t)));
}
E(e, r, o), R(a, s, e);
}
static unmountControl(e, t) {
const { instanceRef: n, parent: o, _events: s } = v(t);
null !== e &&
(Object.keys(s).forEach((t) => O(e, t, s[t])),
o.controls && 'function' == typeof o.controls.remove
? o.controls.remove(e)
: o.remove && 'function' == typeof o.remove && o.remove(e),
R(n));
}
}
var D = x(
l(
d(
(t) => /*#__PURE__*/ e.createElement(U, r({}, t, { name: 'Button' })),
!0,
['control.Button']
)
)
),
B = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(
U,
r({}, t, { name: 'FullscreenControl' })
),
!0,
['control.FullscreenControl']
)
)
),
A = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(
U,
r({}, t, { name: 'GeolocationControl' })
),
!0,
['control.GeolocationControl']
)
)
),
L = x(
l(
d(
(t) => /*#__PURE__*/ e.createElement(U, r({}, t, { name: 'ListBox' })),
!0,
['control.ListBox']
)
)
),
$ = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'ListBoxItem' })),
!0,
['control.ListBoxItem']
)
)
),
N = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'RouteButton' })),
!0,
['control.RouteButton']
)
)
),
Z = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'RouteEditor' })),
!0,
['control.RouteEditor']
)
)
),
z = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'RoutePanel' })),
!0,
['control.RoutePanel']
)
)
),
G = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'RulerControl' })),
!0,
['control.RulerControl']
)
)
),
I = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'SearchControl' })),
!0,
['control.SearchControl']
)
)
),
W = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(
U,
r({}, t, { name: 'TrafficControl' })
),
!0,
['control.TrafficControl']
)
)
),
F = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'TypeSelector' })),
!0,
['control.TypeSelector']
)
)
),
q = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(U, r({}, t, { name: 'ZoomControl' })),
!0,
['control.ZoomControl']
)
)
);
class Y extends e.Component {
constructor() {
super(), (this.state = { instance: null }), (this.instance = null);
}
componentDidMount() {
const e = Y.mountObject(this.props.ymaps.Clusterer, this.props);
(this.instance = e), this.setState({ instance: e });
}
componentDidUpdate(e) {
null !== this.state.instance &&
Y.updateObject(this.instance, e, this.props);
}
componentWillUnmount() {
Y.unmountObject(this.instance, this.props);
}
render() {
/*#__PURE__*/ return e.createElement(
i.Provider,
{ value: this.state.instance },
this.props.children
);
}
static mountObject(e, t) {
const { instanceRef: n, parent: o, _events: s } = v(t),
r = new e(_(t, 'options'));
if (
(Object.keys(s).forEach((e) => j(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 R(null, n, r), r;
}
static updateObject(e, t, n) {
const { _events: o, instanceRef: s } = v(n),
{ _events: r, instanceRef: a } = v(t);
if (C(n, 'options')) {
const o = _(t, 'options'),
s = _(n, 'options');
o !== s && e.options.set(s);
}
E(e, r, o), R(a, s, e);
}
static unmountObject(e, t) {
const { instanceRef: n, parent: o, _events: s } = v(t);
null !== e &&
(Object.keys(s).forEach((t) => O(e, t, s[t])),
o.geoObjects && 'function' == typeof o.geoObjects.remove
? o.geoObjects.remove(e)
: o.remove && 'function' == typeof o.remove && o.remove(e),
R(n));
}
}
var V = x(l(d(Y, !0, ['Clusterer'])));
class H extends e.Component {
constructor() {
super(), (this.state = { instance: null });
}
componentDidMount() {
const e = H.mountObject(this.props.ymaps.ObjectManager, this.props);
(this.instance = e), this.setState({ instance: e });
}
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 } = v(t),
r = _(t, 'options', {}),
a = _(t, 'features', {}),
c = _(t, 'filter', null),
i = _(t, 'objects', {}),
l = _(t, 'clusters', {}),
p = new e(r);
if (
(p.add(a || []),
p.setFilter(c),
p.objects.options.set(i),
p.clusters.options.set(l),
Object.keys(s).forEach((e) => j(p, e, s[e])),
o.geoObjects && 'function' == typeof o.geoObjects.add)
)
o.geoObjects.add(p);
else {
if (!o.add || 'function' != typeof o.add)
throw new Error('No parent found to mount ObjectManager');
o.add(p);
}
return R(null, n, p), p;
}
static updateObject(e, t, n) {
const { _events: o, instanceRef: s } = v(n),
{ _events: r, instanceRef: a } = v(t);
if (C(n, 'options')) {
const o = _(t, 'options'),
s = _(n, 'options');
o !== s && e.options.set(s);
}
if (C(n, 'objects')) {
const o = _(t, 'objects'),
s = _(n, 'objects');
o !== s && e.objects.options.set(s);
}
if (C(n, 'clusters')) {
const o = _(t, 'clusters'),
s = _(n, 'clusters');
o !== s && e.clusters.options.set(s);
}
if (C(n, 'filter')) {
const o = _(t, 'filter'),
s = _(n, 'filter');
o !== s && e.setFilter(s);
}
if (C(n, 'features')) {
const o = _(t, 'features'),
s = _(n, 'features');
o !== s && (e.remove(o), e.add(s));
}
E(e, r, o), R(a, s, e);
}
static unmountObject(e, t) {
const { instanceRef: n, parent: o, _events: s } = v(t);
null !== e &&
(Object.keys(s).forEach((t) => O(e, t, s[t])),
o.geoObjects && 'function' == typeof o.geoObjects.remove
? o.geoObjects.remove(e)
: o.remove && 'function' == typeof o.remove && o.remove(e),
R(n));
}
}
var J = x(l(d(H, !0, ['ObjectManager'])));
class K extends e.Component {
constructor() {
super(), (this.state = { instance: null }), (this.instance = null);
}
componentDidMount() {
const { name: e, ymaps: t, dangerZone: n } = this.props,
o = K.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 && K.updateObject(this.instance, e, this.props);
}
componentWillUnmount() {
K.unmountObject(this.instance, this.props);
}
render() {
return null;
}
static mountObject(e, t) {
const { instanceRef: n, parent: o, _events: s } = v(t),
r = new e(_(t, 'geometry'), _(t, 'properties'), _(t, 'options'));
if (
(Object.keys(s).forEach((e) => j(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 R(null, n, r), r;
}
static updateObject(e, t, n) {
const { _events: o, instanceRef: s } = v(n),
{ _events: r, instanceRef: a } = v(t);
if (C(n, 'geometry')) {
const o = _(t, 'geometry', {}),
s = _(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 (C(n, 'properties')) {
const o = _(t, 'properties'),
s = _(n, 'properties');
o !== s && e.properties.set(s);
}
if (C(n, 'options')) {
const o = _(t, 'options'),
s = _(n, 'options');
o !== s && e.options.set(s);
}
E(e, r, o), R(a, s, e);
}
static unmountObject(e, t) {
const { instanceRef: n, parent: o, _events: s } = v(t);
null !== e &&
(Object.keys(s).forEach((t) => O(e, t, s[t])),
o.geoObjects && 'function' == typeof o.geoObjects.remove
? o.geoObjects.remove(e)
: o.remove && 'function' == typeof o.remove && o.remove(e),
R(n));
}
}
const Q = {
modifyConstructor(e) {
function t(t, n, o) {
e.call(this, { geometry: t, properties: n }, o);
}
return (t.prototype = e.prototype), t;
},
};
var X = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(
K,
r({}, t, { name: 'GeoObject', dangerZone: Q })
),
!0,
['GeoObject']
)
)
),
ee = x(
l(
d(
(t) => /*#__PURE__*/ e.createElement(K, r({}, t, { name: 'Circle' })),
!0,
['Circle']
)
)
),
te = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(K, r({}, t, { name: 'Placemark' })),
!0,
['Placemark']
)
)
),
ne = x(
l(
d(
(t) => /*#__PURE__*/ e.createElement(K, r({}, t, { name: 'Polygon' })),
!0,
['Polygon']
)
)
),
oe = x(
l(
d(
(t) => /*#__PURE__*/ e.createElement(K, r({}, t, { name: 'Polyline' })),
!0,
['Polyline']
)
)
),
se = x(
l(
d(
(t) =>
/*#__PURE__*/ e.createElement(K, r({}, t, { name: 'Rectangle' })),
!0,
['Rectangle']
)
)
);
export {
D as Button,
ee as Circle,
V as Clusterer,
B as FullscreenControl,
X as GeoObject,
A as GeolocationControl,
L as ListBox,
$ as ListBoxItem,
k as Map,
J as ObjectManager,
T as Panorama,
te as Placemark,
ne as Polygon,
oe as Polyline,
se as Rectangle,
N as RouteButton,
Z as RouteEditor,
z as RoutePanel,
G as RulerControl,
I as SearchControl,
W as TrafficControl,
F as TypeSelector,
y as YMaps,
q as ZoomControl,
p as useYMaps,
d as withYMaps,
};
//# sourceMappingURL=react-yandex-maps.esm.js.map