rlayers
Version:
React Components for OpenLayers
188 lines • 8.11 kB
JavaScript
"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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var layer_1 = require("ol/layer");
var context_1 = require("../context");
var RLayerRaster_1 = __importDefault(require("./RLayerRaster"));
var RFeature_1 = __importDefault(require("../RFeature"));
var RStyle_1 = __importDefault(require("../style/RStyle"));
var debug_1 = __importDefault(require("../debug"));
/**
* A layer from a remote MBTiles vector source
*
* WARNING: Using this module requires manually installing `ol-mbtiles`
*
* `npm install ol-mbtiles`
*
* Be sure to read its own documentation for tips on how to improve
* its performance (COOP/COEP, WASM compression, preloading...)
*
* It does not provide a vector layer context for JSX-declared `RFeature`s
* and it is not compatible with RLayerVector
*
* Requires an `RMap` context
*/
var RLayerVectorMBTiles = /** @class */ (function (_super) {
__extends(RLayerVectorMBTiles, _super);
function RLayerVectorMBTiles(props) {
var _this = _super.call(this, props) || this;
_this.addon = Promise.resolve().then(function () { return __importStar(require('ol-mbtiles')); });
_this.ol = new layer_1.VectorTile({
style: RStyle_1.default.getStyle(_this.props.style),
renderBuffer: _this.props.renderBuffer
});
_this.eventSources = [_this.ol];
return _this;
}
RLayerVectorMBTiles.prototype.createSource = function () {
var _this = this;
(0, debug_1.default)('createSource start', this);
this.metadata = this.addon.then(function (mod) {
var _a, _b, _c, _d;
return mod.importMBTiles({
url: _this.props.url,
sqlWorkers: (_a = _this.props.workers) !== null && _a !== void 0 ? _a : 4,
sqlCacheSize: (_b = _this.props.sqlCacheSize) !== null && _b !== void 0 ? _b : 4096,
maxSqlPageSize: (_c = _this.props.maxSqlPageSize) !== null && _c !== void 0 ? _c : 4096,
backendType: (_d = _this.props.backend) !== null && _d !== void 0 ? _d : 'sync'
});
});
var abort = new AbortController();
this.abort = abort;
Promise.all([this.addon, this.metadata]).then(function (_a) {
var _b = __read(_a, 2), addon = _b[0], md = _b[1];
if (abort.signal.aborted) {
(0, debug_1.default)('createSource aborted', _this);
md.pool.then(function (p) { return p.close(); });
return;
}
_this.source = new addon.MBTilesVectorSource(md);
_this.eventSources = [_this.ol, _this.source];
_this.ol.setSource(_this.source);
_this.attachOldEventHandlers(_this.source);
if (_this.props.onMetadataReady)
_this.props.onMetadataReady.call(_this, md);
return _this.source;
});
};
RLayerVectorMBTiles.prototype.destroySource = function () {
(0, debug_1.default)('destroySource', this, this.abort);
if (this.source) {
this.source.dispose();
this.source = null;
if (this.ol)
this.ol.setSource(null);
}
if (this.abort) {
this.abort.abort();
this.abort = null;
}
};
RLayerVectorMBTiles.prototype.componentWillUnmount = function () {
_super.prototype.componentWillUnmount.call(this);
this.destroySource();
};
RLayerVectorMBTiles.prototype.refresh = function (prevProps) {
var _this = this;
_super.prototype.refresh.call(this, prevProps);
var handlers = Object.keys(this.props)
.filter(function (ev) { return ev.startsWith('on'); })
.reduce(function (ac, x) {
var _a;
return (__assign(__assign({}, ac), (_a = {}, _a['_' + x.toLowerCase()] = _this.props[x], _a)));
}, {});
this.ol.setProperties(handlers);
if ((prevProps === null || prevProps === void 0 ? void 0 : prevProps.style) !== this.props.style)
this.ol.setStyle(RStyle_1.default.getStyle(this.props.style));
if ((prevProps === null || prevProps === void 0 ? void 0 : prevProps.url) !== this.props.url) {
this.destroySource();
this.createSource();
}
};
RLayerVectorMBTiles.prototype.render = function () {
RFeature_1.default.initEventRelay(this.context.map);
return (react_1.default.createElement("div", { className: '_rlayers_RLayerVectorMBTiles' },
react_1.default.createElement(context_1.RContext.Provider, { value: __assign(__assign({}, this.context), { layer: this.ol, vectortilelayer: this.ol, rLayer: this }) }, this.props.children)));
};
return RLayerVectorMBTiles;
}(RLayerRaster_1.default));
exports.default = RLayerVectorMBTiles;
//# sourceMappingURL=RLayerVectorMBTiles.js.map