UNPKG

rc-leaflet

Version:
378 lines (377 loc) 17.7 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var leaflet_1 = __importDefault(require("leaflet")); var Array_1 = require("../../util/Array"); var throttle_1 = __importDefault(require("../../util/throttle")); var creator_1 = require("../Icon/creator"); exports.defaultOptions = { points: [], throttleThreshold: 20480, throttleDuration: 60 }; var MassLayer = /** @class */ (function (_super) { __extends(MassLayer, _super); function MassLayer(options) { var _this = _super.call(this, options) || this; _this.getOptions = function () { return _this.options; }; _this.getPoints = function () { return _this.options.points; }; _this.getIconUrl = function (point) { return point.iconUrl || _this.options.iconUrl || creator_1.defaultOptions.iconUrl; }; _this.onClick = function (e) { _this.click = _this.hover; if (_this.click) { _this.fire('click', _this.click); } }; _this.onMouseMove = function (e) { var e_1, _a; var points = _this.options.points; if (points && points.length) { var _b = e.containerPoint, x = _b.x, y = _b.y; var length_1 = points.length; var array = __spread(points).reverse(); var target = void 0; try { for (var _c = __values(array.entries()), _d = _c.next(); !_d.done; _d = _c.next()) { var _e = __read(_d.value, 2), index = _e[0], point = _e[1]; var _f = _this.getIconBox(point), top_1 = _f.top, bottom = _f.bottom, left = _f.left, right = _f.right; if (x > left && x < right && y > top_1 && y < bottom) { target = { index: length_1 - index - 1, point: point }; break; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_1) throw e_1.error; } } if (target) { leaflet_1.default.DomUtil.addClass(_this.canvas, 'leaflet-interactive'); _this.fire('mousemove', __assign({}, target, { latlng: _this._map.containerPointToLatLng(e.containerPoint) })); } else { leaflet_1.default.DomUtil.removeClass(_this.canvas, 'leaflet-interactive'); } _this.hover = target; if (_this.prevHover && (!target || _this.prevHover.point !== target.point)) { _this.fire('mouseout', _this.prevHover); } if (target && (!_this.prevHover || target.point !== _this.prevHover.point)) { _this.fire('mouseover', target); } _this.prevHover = target; } }; _this.reset = function () { var _a = _this._map.getSize(), x = _a.x, y = _a.y; if (_this.canvas.width !== x || _this.canvas.height !== y) { _this.canvas.width = x; _this.canvas.height = y; } if (!_this._map) { return; } var point = leaflet_1.default.DomUtil.getPosition(_this._map.getPanes().mapPane); leaflet_1.default.DomUtil.setPosition(_this.canvas, leaflet_1.default.point(-Math.round(point.x), -Math.round(point.y))); _this.draw(); }; _this.zoomAnim = function (e) { var scale = _this._map.getZoomScale(e.zoom, _this._map.getZoom()); var position = leaflet_1.default.DomUtil.getPosition(_this.canvas); var viewHalf = _this._map.getSize().multiplyBy(0.5); var currentCenterPoint = _this._map.project(_this._map.getCenter(), e.zoom); var destCenterPoint = _this._map.project(e.center, e.zoom); var centerOffset = destCenterPoint.subtract(currentCenterPoint); var topLeftOffset = viewHalf.multiplyBy(-scale).add(position).add(viewHalf).subtract(centerOffset); leaflet_1.default.DomUtil.setTransform(_this.canvas, topLeftOffset, scale); }; _this.onPopupClose = function () { _this.click = undefined; }; leaflet_1.default.Util.setOptions(_this, leaflet_1.default.Util.extend(exports.defaultOptions, options)); _this.canvas = leaflet_1.default.DomUtil.create('canvas', 'leaflet-zoom-animated'); _this.ctx = _this.canvas.getContext('2d'); _this.prepareIcons(); _this.setThrottledReset(_this.options); return _this; } MassLayer.prototype.setThrottledReset = function (options) { var threshold = options.throttleThreshold || exports.defaultOptions.throttleThreshold; var duration = options.throttleDuration || exports.defaultOptions.throttleDuration; this.throttledReset = options.points.length <= threshold ? this.reset : throttle_1.default(this.reset, duration); }; MassLayer.prototype.onAdd = function (map) { var _a = map.getSize(), x = _a.x, y = _a.y; this.canvas.width = x; this.canvas.height = y; map.on('click', this.onClick, this); map.on('mousemove', this.onMouseMove, this); map.on('move', this.throttledReset, this); map.on('moveend', this.reset, this); map.on('zoomanim', this.zoomAnim, this); this.on('popupclose', this.onPopupClose); this.getPane().appendChild(this.canvas); this.draw(); return this; }; MassLayer.prototype.onRemove = function (map) { map.off('click', this.onClick, this); map.off('mousemove', this.onMouseMove, this); map.off('move', this.throttledReset, this); map.off('moveend', this.reset, this); map.off('zoomanim', this.zoomAnim, this); this.off(); leaflet_1.default.DomUtil.remove(this.canvas); return this; }; MassLayer.prototype.addTo = function (map) { map.addLayer(this); return this; }; MassLayer.prototype.getLatLng = function () { return this.hover ? this.hover.point.position : this._map.getCenter(); }; MassLayer.prototype.setOptions = function (options) { this.closePopup(); leaflet_1.default.Util.setOptions(this, options); this._map.off('move', this.throttledReset, this); this.setThrottledReset(options); this._map.on('move', this.throttledReset, this); this.prepareIcons(); this.draw(); }; MassLayer.prototype.setPoints = function (points) { this.setOptions({ points: points }); }; MassLayer.prototype.setIconUrl = function (url) { this.setPopupAnchor(this.options.popupAnchor); this.options.iconUrl = url; this.prepareIcons(); this.draw(); }; MassLayer.prototype.setIconSize = function (size) { this.options.iconSize = size; this.draw(); }; MassLayer.prototype.getIconSize = function (point) { var iconSize = point.iconSize || this.options.iconSize; if (iconSize) { return leaflet_1.default.point(iconSize); } var iconUrl = this.getIconUrl(point); if (iconUrl === creator_1.defaultOptions.iconUrl) { return leaflet_1.default.point(creator_1.defaultOptions.iconSize); } var icon = this.icons[iconUrl]; return leaflet_1.default.point(icon.width, icon.height); }; MassLayer.prototype.setIconAnchor = function (anchor) { this.options.iconAnchor = anchor; this.draw(); }; MassLayer.prototype.getIconAnchor = function (point) { var iconAnchor = point.iconAnchor || this.options.iconAnchor; if (iconAnchor) { return leaflet_1.default.point(iconAnchor); } if (this.getIconUrl(point) === creator_1.defaultOptions.iconUrl) { return leaflet_1.default.point(creator_1.defaultOptions.iconAnchor); } return leaflet_1.default.point(0, 0); }; MassLayer.prototype.setPopupAnchor = function (anchor) { this.options.popupAnchor = anchor; if (this.isPopupOpen()) { this.getPopup()._updatePosition(); } }; MassLayer.prototype.getPopupAnchor = function (point) { var popupAnchor = point.popupAnchor || this.options.popupAnchor; if (popupAnchor) { return leaflet_1.default.point(popupAnchor); } if (this.getIconUrl(point) === creator_1.defaultOptions.iconUrl) { return leaflet_1.default.point(creator_1.defaultOptions.popupAnchor); } return leaflet_1.default.point(0, 0); }; MassLayer.prototype.setTooltipAnchor = function (anchor) { this.options.tooltipAnchor = anchor; if (this.isTooltipOpen()) { this.getTooltip()._updatePosition(); } }; MassLayer.prototype.getTooltipAnchor = function (point) { var tooltipAnchor = point.tooltipAnchor || this.options.tooltipAnchor; if (tooltipAnchor) { return leaflet_1.default.point(tooltipAnchor); } if (this.getIconUrl(point) === creator_1.defaultOptions.iconUrl) { return leaflet_1.default.point(creator_1.defaultOptions.tooltipAnchor); } return leaflet_1.default.point(0, 0); }; MassLayer.prototype.getIconBox = function (point) { var iconSize = this.getIconSize(point); var iconAnchor = this.getIconAnchor(point); var pixel = this._map.latLngToContainerPoint(point.position); var top = pixel.y - iconAnchor.y; var bottom = pixel.y + iconSize.y - iconAnchor.y; var left = pixel.x - iconAnchor.x; var right = pixel.x + iconSize.x - iconAnchor.x; return { top: top, bottom: bottom, left: left, right: right }; }; MassLayer.prototype._getPopupAnchor = function () { return this.click ? this.getPopupAnchor(this.click.point) : [0, 0]; }; MassLayer.prototype._getTooltipAnchor = function () { return this.hover ? this.getTooltipAnchor(this.hover.point) : [0, 0]; }; MassLayer.prototype.prepareIcons = function () { var _this = this; var points = this.options.points; this.iconsReady = Promise.resolve([]); if (points && points.length) { this.icons = {}; var urls = Array_1.distinct(points.map(this.getIconUrl)); this.iconsReady = Promise.all(urls.map(function (url) { return new Promise(function (resolve) { var image = new Image(); image.onload = resolve; image.src = url; _this.icons[url] = image; }); })); } }; MassLayer.prototype.draw = function () { return __awaiter(this, void 0, void 0, function () { var points, mapSize, points_1, points_1_1, point, _a, top_2, bottom, left, right; var e_2, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: points = this.options.points; return [4 /*yield*/, this.iconsReady]; case 1: _c.sent(); this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); if (points && points.length) { mapSize = this._map.getSize(); this.closeTooltip(); try { for (points_1 = __values(points), points_1_1 = points_1.next(); !points_1_1.done; points_1_1 = points_1.next()) { point = points_1_1.value; _a = this.getIconBox(point), top_2 = _a.top, bottom = _a.bottom, left = _a.left, right = _a.right; if (bottom <= 0 || top_2 >= mapSize.y || right <= 0 || left >= mapSize.x) { continue; } this.ctx.drawImage(this.icons[this.getIconUrl(point)], left, top_2, right - left, bottom - top_2); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (points_1_1 && !points_1_1.done && (_b = points_1.return)) _b.call(points_1); } finally { if (e_2) throw e_2.error; } } } return [2 /*return*/]; } }); }); }; return MassLayer; }(leaflet_1.default.Layer)); exports.default = MassLayer;