1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | }
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | var __importStar = (this && this.__importStar) || function (mod) {
|
27 | if (mod && mod.__esModule) return mod;
|
28 | var result = {};
|
29 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
30 | result["default"] = mod;
|
31 | return result;
|
32 | };
|
33 | Object.defineProperty(exports, "__esModule", { value: true });
|
34 | var React = __importStar(require("react"));
|
35 | var uid_1 = require("./util/uid");
|
36 | function layerMouseTouchEvents(WrappedComponent) {
|
37 | return (function (_super) {
|
38 | __extends(EnhancedLayer, _super);
|
39 | function EnhancedLayer() {
|
40 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
41 | _this.hover = [];
|
42 | _this.draggedChildren = undefined;
|
43 | _this.id = _this.props.id || "layer-" + uid_1.generateID();
|
44 | _this.getChildren = function () {
|
45 | return []
|
46 | .concat(_this.props.children)
|
47 | .filter(function (el) {
|
48 | return typeof el !== 'undefined';
|
49 | });
|
50 | };
|
51 | _this.getChildFromId = function (children, id) { return children[id]; };
|
52 | _this.areFeaturesDraggable = function (children, featureIds) {
|
53 | if (featureIds === void 0) { featureIds = _this.hover; }
|
54 | return !!featureIds
|
55 | .map(function (id) {
|
56 | return _this.getChildFromId(children, id)
|
57 | ? _this.getChildFromId(children, id).props.draggable
|
58 | : false;
|
59 | })
|
60 | .filter(Boolean).length;
|
61 | };
|
62 | _this.onClick = function (evt) {
|
63 | var features = evt.features;
|
64 | var children = _this.getChildren();
|
65 | var map = _this.props.map;
|
66 | if (features) {
|
67 | features.forEach(function (feature) {
|
68 | var id = feature.properties.id;
|
69 | if (children) {
|
70 | var child = _this.getChildFromId(children, id);
|
71 | var onClick = child && child.props.onClick;
|
72 | if (onClick) {
|
73 | onClick(__assign({}, evt, { feature: feature, map: map }));
|
74 | }
|
75 | }
|
76 | });
|
77 | }
|
78 | };
|
79 | _this.onMouseEnter = function (evt) {
|
80 | var children = _this.getChildren();
|
81 | var map = _this.props.map;
|
82 | _this.hover = [];
|
83 | evt.features.forEach(function (feature) {
|
84 | var id = feature.properties.id;
|
85 | var child = _this.getChildFromId(children, id);
|
86 | _this.hover.push(id);
|
87 | var onMouseEnter = child && child.props.onMouseEnter;
|
88 | if (onMouseEnter) {
|
89 | onMouseEnter(__assign({}, evt, { feature: feature, map: map }));
|
90 | }
|
91 | });
|
92 | if (_this.areFeaturesDraggable(children)) {
|
93 | map.dragPan.disable();
|
94 | }
|
95 | };
|
96 | _this.onMouseLeave = function (evt) {
|
97 | var children = _this.getChildren();
|
98 | var map = _this.props.map;
|
99 | if (_this.areFeaturesDraggable(children)) {
|
100 | map.dragPan.enable();
|
101 | }
|
102 | _this.hover.forEach(function (id) {
|
103 | var child = _this.getChildFromId(children, id);
|
104 | var onMouseLeave = child && child.props.onMouseLeave;
|
105 | if (onMouseLeave) {
|
106 | onMouseLeave(__assign({}, evt, { map: map }));
|
107 | }
|
108 | });
|
109 | if (!_this.draggedChildren) {
|
110 | _this.hover = [];
|
111 | }
|
112 | };
|
113 | _this.onMouseDown = function () {
|
114 | if (_this.hover.length) {
|
115 | _this.onFeatureDown('mousedown');
|
116 | }
|
117 | };
|
118 | _this.onTouchStart = function (evt) {
|
119 | _this.hover = evt.features.map(function (feature) { return feature.properties.id; });
|
120 | if (_this.hover.length) {
|
121 | _this.onFeatureDown('touchstart');
|
122 | }
|
123 | };
|
124 | _this.onFeatureDown = function (startEvent) {
|
125 | var moveEvent = startEvent === 'mousedown' ? 'mousemove' : 'touchmove';
|
126 | var endEvent = startEvent === 'mousedown' ? 'mouseup' : 'touchend';
|
127 | var map = _this.props.map;
|
128 | map.once(moveEvent, _this.onFeatureDragStart);
|
129 | map.on(moveEvent, _this.onFeatureDrag);
|
130 | map.once(endEvent, function (evt) {
|
131 | map.off(moveEvent, _this.onFeatureDragStart);
|
132 | map.off(moveEvent, _this.onFeatureDrag);
|
133 | _this.onFeatureDragEnd(evt);
|
134 | });
|
135 | };
|
136 | _this.onFeatureDragStart = function (evt) {
|
137 | var map = _this.props.map;
|
138 | var children = _this.getChildren();
|
139 | _this.hover.forEach(function (id) {
|
140 | var child = _this.getChildFromId(children, id);
|
141 | if (child && !child.props.draggable) {
|
142 | return;
|
143 | }
|
144 | var onDragStart = child && child.props.onDragStart;
|
145 | if (onDragStart) {
|
146 | onDragStart(__assign({}, evt, { map: map }));
|
147 | }
|
148 | });
|
149 | };
|
150 | _this.onFeatureDrag = function (evt) {
|
151 | var children = _this.getChildren();
|
152 | var map = _this.props.map;
|
153 | var _a = evt.lngLat, lng = _a.lng, lat = _a.lat;
|
154 | _this.draggedChildren = [];
|
155 | _this.hover.forEach(function (id) {
|
156 | var child = _this.getChildFromId(children, id);
|
157 | var onDrag = child && child.props.onDrag;
|
158 | if (child && child.props.draggable) {
|
159 | _this.draggedChildren.push(React.cloneElement(child, {
|
160 | coordinates: [lng, lat]
|
161 | }));
|
162 | if (onDrag) {
|
163 | onDrag(__assign({}, evt, { map: map }));
|
164 | }
|
165 | }
|
166 | });
|
167 | _this.forceUpdate();
|
168 | };
|
169 | _this.onFeatureDragEnd = function (evt) {
|
170 | var map = _this.props.map;
|
171 | var children = _this.getChildren();
|
172 | _this.hover.forEach(function (id) {
|
173 | var child = _this.getChildFromId(children, id);
|
174 | var onDragEnd = child && child.props.onDragEnd;
|
175 | if (onDragEnd && child.props.draggable && _this.draggedChildren) {
|
176 | onDragEnd(__assign({}, evt, { map: map }));
|
177 | }
|
178 | });
|
179 | _this.draggedChildren = undefined;
|
180 | };
|
181 | return _this;
|
182 | }
|
183 | EnhancedLayer.prototype.componentDidMount = function () {
|
184 | var map = this.props.map;
|
185 | map.on('click', this.id, this.onClick);
|
186 | map.on('mouseenter', this.id, this.onMouseEnter);
|
187 | map.on('mouseleave', this.id, this.onMouseLeave);
|
188 | map.on('mousedown', this.id, this.onMouseDown);
|
189 | map.on('touchstart', this.id, this.onTouchStart);
|
190 | };
|
191 | EnhancedLayer.prototype.componentWillUnmount = function () {
|
192 | var map = this.props.map;
|
193 | map.off('click', this.onClick);
|
194 | map.off('mouseenter', this.onMouseEnter);
|
195 | map.off('mouseleave', this.onMouseLeave);
|
196 | map.off('mousedown', this.onMouseDown);
|
197 | map.off('touchstart', this.onTouchStart);
|
198 | };
|
199 | EnhancedLayer.prototype.render = function () {
|
200 | return (React.createElement(WrappedComponent, __assign({}, this.props, { id: this.id, map: this.props.map, draggedChildren: this.draggedChildren })));
|
201 | };
|
202 | return EnhancedLayer;
|
203 | }(React.Component));
|
204 | }
|
205 | exports.layerMouseTouchEvents = layerMouseTouchEvents;
|
206 | exports.default = layerMouseTouchEvents;
|
207 |
|
\ | No newline at end of file |