UNPKG

7.12 kBJavaScriptView Raw
1"use strict";
2var __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})();
15var __importStar = (this && this.__importStar) || function (mod) {
16 if (mod && mod.__esModule) return mod;
17 var result = {};
18 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
19 result["default"] = mod;
20 return result;
21};
22var __importDefault = (this && this.__importDefault) || function (mod) {
23 return (mod && mod.__esModule) ? mod : { "default": mod };
24};
25Object.defineProperty(exports, "__esModule", { value: true });
26var React = __importStar(require("react"));
27var mapbox_gl_1 = require("mapbox-gl");
28var supercluster_1 = __importDefault(require("supercluster"));
29var bbox_1 = __importDefault(require("@turf/bbox"));
30var helpers_1 = require("@turf/helpers");
31var context_1 = require("./context");
32var Cluster = (function (_super) {
33 __extends(Cluster, _super);
34 function Cluster() {
35 var _this = _super !== null && _super.apply(this, arguments) || this;
36 _this.state = {
37 superC: new supercluster_1.default({
38 radius: _this.props.radius,
39 maxZoom: _this.props.maxZoom,
40 minZoom: _this.props.minZoom,
41 extent: _this.props.extent,
42 nodeSize: _this.props.nodeSize,
43 log: _this.props.log
44 }),
45 clusterPoints: []
46 };
47 _this.featureClusterMap = new WeakMap();
48 _this.childrenChange = function (newChildren) {
49 var superC = _this.state.superC;
50 _this.featureClusterMap = new WeakMap();
51 var features = _this.childrenToFeatures(newChildren);
52 superC.load(features);
53 };
54 _this.mapChange = function (forceSetState) {
55 if (forceSetState === void 0) { forceSetState = false; }
56 var map = _this.props.map;
57 var _a = _this.state, superC = _a.superC, clusterPoints = _a.clusterPoints;
58 var zoom = map.getZoom();
59 var canvas = map.getCanvas();
60 var w = canvas.width;
61 var h = canvas.height;
62 var upLeft = map.unproject([0, 0]).toArray();
63 var upRight = map.unproject([w, 0]).toArray();
64 var downRight = map.unproject([w, h]).toArray();
65 var downLeft = map.unproject([0, h]).toArray();
66 var newPoints = superC.getClusters(bbox_1.default(helpers_1.polygon([[upLeft, upRight, downRight, downLeft, upLeft]])), Math.round(zoom));
67 if (newPoints.length !== clusterPoints.length || forceSetState) {
68 _this.setState({ clusterPoints: newPoints });
69 }
70 };
71 _this.childrenToFeatures = function (children) {
72 return children.map(function (child) {
73 var feature = _this.feature(child && child.props.coordinates);
74 _this.featureClusterMap.set(feature, child);
75 return feature;
76 });
77 };
78 _this.getLeaves = function (feature, limit, offset) {
79 var superC = _this.state.superC;
80 return superC
81 .getLeaves(feature.properties && feature.properties.cluster_id, limit || Infinity, offset)
82 .map(function (leave) { return _this.featureClusterMap.get(leave); });
83 };
84 _this.zoomToClusterBounds = function (event) {
85 var markers = Array.prototype.slice.call(event.currentTarget.children);
86 var marker = _this.findMarkerElement(event.currentTarget, event.target);
87 var index = markers.indexOf(marker);
88 var cluster = _this.state.clusterPoints[index];
89 if (!cluster.properties || !cluster.properties.cluster_id) {
90 return;
91 }
92 var children = _this.state.superC.getLeaves(cluster.properties && cluster.properties.cluster_id, Infinity);
93 var childrenBbox = bbox_1.default(helpers_1.featureCollection(children));
94 _this.props.map.fitBounds(mapbox_gl_1.LngLatBounds.convert(childrenBbox), {
95 padding: _this.props.zoomOnClickPadding
96 });
97 };
98 return _this;
99 }
100 Cluster.prototype.componentDidMount = function () {
101 var _a = this.props, children = _a.children, map = _a.map;
102 if (children) {
103 this.childrenChange(children);
104 }
105 map.on('move', this.mapChange);
106 map.on('zoom', this.mapChange);
107 this.mapChange();
108 };
109 Cluster.prototype.componentWillUnmount = function () {
110 var map = this.props.map;
111 map.off('move', this.mapChange);
112 map.off('zoom', this.mapChange);
113 };
114 Cluster.prototype.componentDidUpdate = function (prevProps) {
115 var children = prevProps.children;
116 if (children !== this.props.children && this.props.children) {
117 this.childrenChange(this.props.children);
118 this.mapChange(true);
119 }
120 };
121 Cluster.prototype.feature = function (coordinates) {
122 return {
123 type: 'Feature',
124 geometry: {
125 type: 'Point',
126 coordinates: coordinates
127 },
128 properties: {}
129 };
130 };
131 Cluster.prototype.findMarkerElement = function (target, element) {
132 if (element.parentElement === target) {
133 return element;
134 }
135 return this.findMarkerElement(target, element.parentElement);
136 };
137 Cluster.prototype.render = function () {
138 var _this = this;
139 var _a = this.props, ClusterMarkerFactory = _a.ClusterMarkerFactory, style = _a.style, className = _a.className, tabIndex = _a.tabIndex;
140 var clusterPoints = this.state.clusterPoints;
141 return (React.createElement("div", { style: style, className: className, tabIndex: tabIndex, onClick: this.props.zoomOnClick ? this.zoomToClusterBounds : undefined }, clusterPoints.map(function (feature) {
142 if (feature.properties && feature.properties.cluster) {
143 return ClusterMarkerFactory(feature.geometry.coordinates, feature.properties.point_count, _this.getLeaves.bind(_this, feature));
144 }
145 return _this.featureClusterMap.get(feature);
146 })));
147 };
148 Cluster.defaultProps = {
149 radius: 60,
150 minZoom: 0,
151 maxZoom: 16,
152 extent: 512,
153 nodeSize: 64,
154 log: false,
155 zoomOnClick: false,
156 zoomOnClickPadding: 20
157 };
158 return Cluster;
159}(React.Component));
160exports.Cluster = Cluster;
161exports.default = context_1.withMap(Cluster);
162//# sourceMappingURL=cluster.js.map
\No newline at end of file