aio-map 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -1
- package/index.js +215 -1
- package/package.json +1 -3
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1 +1,215 @@
|
|
|
1
|
-
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
11
|
+
import { divIcon } from "leaflet";
|
|
12
|
+
import * as ReactDOMServer from 'react-dom/server';
|
|
13
|
+
import { createContext, isValidElement, useContext, useEffect, useRef, useState } from "react";
|
|
14
|
+
import { Circle, FeatureGroup, LayersControl, MapContainer, Marker, Polyline, Rectangle, TileLayer, useMapEvents } from "react-leaflet";
|
|
15
|
+
import AIOInput from "aio-input";
|
|
16
|
+
import 'leaflet/dist/leaflet.css';
|
|
17
|
+
import './index.css';
|
|
18
|
+
const MAPCTX = createContext({});
|
|
19
|
+
const AIMap = (props) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const { value = [35.699939, 51.338497], getSearchResult, mapRef } = props;
|
|
22
|
+
const [map, setMap] = useState(null);
|
|
23
|
+
const [zoom, setZoom] = useState(((_a = props.zoom) === null || _a === void 0 ? void 0 : _a.value) || 14);
|
|
24
|
+
const lockChangeZoomState = useRef(false);
|
|
25
|
+
function changeZoom(zoom) {
|
|
26
|
+
if (lockChangeZoomState.current === true) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
setZoom(zoom);
|
|
30
|
+
}
|
|
31
|
+
if (mapRef) {
|
|
32
|
+
mapRef.current = map;
|
|
33
|
+
}
|
|
34
|
+
if (props.actionsRef) {
|
|
35
|
+
props.actionsRef.current = {
|
|
36
|
+
flyTo: (p) => {
|
|
37
|
+
lockChangeZoomState.current = true;
|
|
38
|
+
map.flyTo([p.lat, p.lng], p.zoom);
|
|
39
|
+
const onMoveEnd = () => {
|
|
40
|
+
map.off("moveend", onMoveEnd);
|
|
41
|
+
lockChangeZoomState.current = false;
|
|
42
|
+
changeZoom(p.zoom);
|
|
43
|
+
p.callback();
|
|
44
|
+
};
|
|
45
|
+
map.on("moveend", onMoveEnd);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const [pos, setPos] = useState(value);
|
|
50
|
+
const moveTimeout = useRef(undefined);
|
|
51
|
+
function move(pos) {
|
|
52
|
+
setPos(pos);
|
|
53
|
+
if (props.onChange) {
|
|
54
|
+
clearTimeout(moveTimeout.current);
|
|
55
|
+
moveTimeout.current = setTimeout(() => { if (props.onChange) {
|
|
56
|
+
props.onChange(pos);
|
|
57
|
+
} }, 600);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function getDefaultMarkerIcon() {
|
|
61
|
+
return _jsxs("div", { className: 'marker-icon', children: [_jsx("div", { className: 'marker-icon-circle' }), _jsx("div", { className: 'marker-icon-arrow' })] });
|
|
62
|
+
}
|
|
63
|
+
function getContext() { return { pos, setMap, rootProps: props, move, getDefaultMarkerIcon, changeZoom, zoom }; }
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
if (map !== null) {
|
|
66
|
+
map.setView(value, zoom, { animate: false });
|
|
67
|
+
}
|
|
68
|
+
setPos(value);
|
|
69
|
+
}, [value[0] + '-' + value[1] + '-' + zoom]);
|
|
70
|
+
const attrs = AddToAttrs(props.attrs, { className: 'ai-map' });
|
|
71
|
+
return (_jsx(MAPCTX.Provider, { value: getContext(), children: _jsxs("div", Object.assign({}, attrs, { children: [!!getSearchResult && _jsx(MapHeader, {}), _jsx(MapBody, {}), _jsx(MapFooter, {})] })) }));
|
|
72
|
+
};
|
|
73
|
+
export default AIMap;
|
|
74
|
+
const MapBody = () => {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
const { rootProps, pos, setMap, getDefaultMarkerIcon, zoom } = useContext(MAPCTX);
|
|
77
|
+
const { mapStyle, dragging = true, children, shapes = [], marker, markers = [], whenReady } = rootProps;
|
|
78
|
+
const defaultStyle = { width: '100%', height: '100%' };
|
|
79
|
+
return (_jsxs(MapContainer, { center: pos, style: Object.assign(Object.assign({}, defaultStyle), mapStyle), zoom: zoom, scrollWheelZoom: ((_a = rootProps.zoom) === null || _a === void 0 ? void 0 : _a.wheel) ? 'center' : undefined, zoomControl: ((_b = rootProps.zoom) === null || _b === void 0 ? void 0 : _b.control) !== false, attributionControl: true, dragging: dragging, ref: setMap, whenReady: whenReady, children: [_jsx(TileLayer, { url: "https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png" }), _jsx(MapEvents, {}), marker !== false && _jsx(MapMarker, { pos: pos, html: isValidElement(marker) ? marker : getDefaultMarkerIcon() }, 'main-marker'), markers.map((marker, i) => _jsx(MapMarker, { pos: marker.pos, html: marker.html, eventHandlers: marker.eventHandlers }, `marker-${i}`)), shapes.map((o, i) => _jsx(MapShape, { shape: o }, i)), _jsx(MapLayers, {}), children] }));
|
|
80
|
+
};
|
|
81
|
+
const MagnifyIcon = () => {
|
|
82
|
+
return (_jsx("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", width: 24, height: 24, children: _jsx("path", { d: "M9.5,3A6.5,6.5 0 0,1 16,9.5C16,11.11 15.41,12.59 14.44,13.73L14.71,14H15.5L20.5,19L19,20.5L14,15.5V14.71L13.73,14.44C12.59,15.41 11.11,16 9.5,16A6.5,6.5 0 0,1 3,9.5A6.5,6.5 0 0,1 9.5,3M9.5,5C7,5 5,7 5,9.5C5,12 7,14 9.5,14C12,14 14,12 14,9.5C14,7 12,5 9.5,5Z" }) }));
|
|
83
|
+
};
|
|
84
|
+
const MapHeader = () => {
|
|
85
|
+
const { rootProps } = useContext(MAPCTX);
|
|
86
|
+
const { getSearchResult, onSearch } = rootProps;
|
|
87
|
+
const [searchValue, setSearchValue] = useState('');
|
|
88
|
+
const [searchResult, setSerachResult] = useState([]);
|
|
89
|
+
const timeout = useRef(undefined);
|
|
90
|
+
function changeSearch(searchValue) {
|
|
91
|
+
setSearchValue(searchValue);
|
|
92
|
+
clearTimeout(timeout.current);
|
|
93
|
+
timeout.current = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
if (getSearchResult) {
|
|
95
|
+
const res = yield getSearchResult(searchValue);
|
|
96
|
+
if (Array.isArray(res)) {
|
|
97
|
+
setSerachResult(res);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}), 1200);
|
|
101
|
+
}
|
|
102
|
+
return (_jsx("div", { className: "ai-map-header", children: _jsx(AIOInput, { type: 'text', value: searchValue, options: (searchResult || []), before: _jsx(MagnifyIcon, {}), onChange: (searchValue) => changeSearch(searchValue), option: {
|
|
103
|
+
onClick: (option) => { if (onSearch) {
|
|
104
|
+
onSearch(option);
|
|
105
|
+
} }
|
|
106
|
+
} }) }));
|
|
107
|
+
};
|
|
108
|
+
const MapLayers = () => {
|
|
109
|
+
const { rootProps } = useContext(MAPCTX);
|
|
110
|
+
const { layers } = rootProps;
|
|
111
|
+
if (!layers) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
const { position = 'topright', items = [] } = layers;
|
|
115
|
+
return (_jsx(LayersControl, { position: position, children: items.map((o, i) => {
|
|
116
|
+
const { shapes = [], markers = [], active = true } = o;
|
|
117
|
+
return (_jsx(LayersControl.Overlay, { name: o.name, checked: active, children: _jsxs(FeatureGroup, { children: [markers.map((marker, j) => _jsx(MapMarker, { pos: marker.pos, html: marker.html, eventHandlers: marker.eventHandlers }, j)), shapes.map((shape, k) => _jsx(MapShape, { shape: shape }, k))] }) }, i));
|
|
118
|
+
}) }));
|
|
119
|
+
};
|
|
120
|
+
const MapShape = ({ shape }) => {
|
|
121
|
+
const { style = {}, type } = shape, { stroke = {}, fill = {} } = style;
|
|
122
|
+
const { width = 4, dash, color: strokeColor = 'orange' } = stroke;
|
|
123
|
+
const { color: fillColor = 'orange', opacity = 0.3 } = fill;
|
|
124
|
+
const pathOptions = { fillColor: fillColor, color: strokeColor, fillOpacity: opacity, weight: width, dashArray: dash };
|
|
125
|
+
if (type === 'circle') {
|
|
126
|
+
const { center, radius = 100 } = shape;
|
|
127
|
+
return (_jsx(Circle, { center: center, pathOptions: pathOptions, radius: radius }));
|
|
128
|
+
}
|
|
129
|
+
else if (type === 'rect') {
|
|
130
|
+
return (_jsx(Rectangle, { bounds: shape.points, pathOptions: pathOptions }));
|
|
131
|
+
}
|
|
132
|
+
else if (type === 'polyline') {
|
|
133
|
+
return (_jsx(Polyline, { positions: shape.points, pathOptions: pathOptions }));
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
};
|
|
137
|
+
const MapFooter = () => {
|
|
138
|
+
const { rootProps, pos } = useContext(MAPCTX);
|
|
139
|
+
const { submitText = 'Submit', onSubmit, footer } = rootProps;
|
|
140
|
+
if (!onSubmit && !footer) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
return (_jsxs("div", { className: "ai-map-footer", children: [!!onSubmit && _jsx("button", { type: 'button', onClick: () => onSubmit(pos), children: submitText }), _jsx("div", { className: "ai-map-footer-html", children: footer || null })] }));
|
|
144
|
+
};
|
|
145
|
+
const MapMarker = ({ pos, html, eventHandlers }) => {
|
|
146
|
+
const { getDefaultMarkerIcon } = useContext(MAPCTX);
|
|
147
|
+
function getHtmlIcon(html) {
|
|
148
|
+
return divIcon({
|
|
149
|
+
html: JSXToHTML(html),
|
|
150
|
+
className: '',
|
|
151
|
+
iconSize: [32, 32],
|
|
152
|
+
iconAnchor: [16, 32], // point of the icon which will correspond to marker's location
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
html = html || getDefaultMarkerIcon();
|
|
156
|
+
let props = { position: pos };
|
|
157
|
+
if (html) {
|
|
158
|
+
props.icon = getHtmlIcon(html);
|
|
159
|
+
}
|
|
160
|
+
return _jsx(Marker, Object.assign({}, props, { animate: false, eventHandlers: eventHandlers }));
|
|
161
|
+
};
|
|
162
|
+
function MapEvents() {
|
|
163
|
+
const { rootProps, move, changeZoom } = useContext(MAPCTX);
|
|
164
|
+
useMapEvents({
|
|
165
|
+
click: () => rootProps.onClick ? rootProps.onClick() : undefined,
|
|
166
|
+
move: (e) => {
|
|
167
|
+
if (rootProps.dragging === false) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
let { lat, lng } = e.target.getCenter();
|
|
171
|
+
move([lat, lng]);
|
|
172
|
+
},
|
|
173
|
+
zoom: (e) => {
|
|
174
|
+
changeZoom(e.target._zoom);
|
|
175
|
+
},
|
|
176
|
+
locationfound: (location) => {
|
|
177
|
+
console.log('location found:', location);
|
|
178
|
+
},
|
|
179
|
+
moveend: (e) => {
|
|
180
|
+
if (rootProps.onMoveEnd) {
|
|
181
|
+
rootProps.onMoveEnd(e);
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
movestart: () => {
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
function JSXToHTML(jsx) {
|
|
190
|
+
return ReactDOMServer.renderToStaticMarkup(jsx);
|
|
191
|
+
}
|
|
192
|
+
function classListToString(classes) {
|
|
193
|
+
let list = [];
|
|
194
|
+
const getClasses = (cls) => {
|
|
195
|
+
if (Array.isArray(cls)) {
|
|
196
|
+
return cls.filter((o) => !!o && typeof o === 'string');
|
|
197
|
+
}
|
|
198
|
+
if (typeof cls === 'string') {
|
|
199
|
+
return cls.split(' ');
|
|
200
|
+
}
|
|
201
|
+
return [];
|
|
202
|
+
};
|
|
203
|
+
for (let i = 0; i < classes.length; i++) {
|
|
204
|
+
const cls = classes[i];
|
|
205
|
+
list = [...list, ...getClasses(cls)];
|
|
206
|
+
}
|
|
207
|
+
return list.length ? list.join(' ') : '';
|
|
208
|
+
}
|
|
209
|
+
function AddToAttrs(attrs, p) {
|
|
210
|
+
attrs = attrs || {};
|
|
211
|
+
let { style } = p;
|
|
212
|
+
const className = classListToString([attrs.className, p.className]);
|
|
213
|
+
let newStyle = Object.assign(Object.assign({}, attrs.style), style);
|
|
214
|
+
return Object.assign(Object.assign(Object.assign({}, attrs), { className, style: newStyle }), p.attrs);
|
|
215
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aio-map",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"main": "index.js",
|
|
@@ -12,8 +12,6 @@
|
|
|
12
12
|
"url": "git+https://github.com/mohammadFeiz/aio-map.git"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"jquery": "^3.6.1",
|
|
16
|
-
"aio-utils": "latest",
|
|
17
15
|
"leaflet": "^1.9.4",
|
|
18
16
|
"react-leaflet": "^4.2.1",
|
|
19
17
|
"aio-input":"latest"
|