rlayers
Version:
React Components for OpenLayers
308 lines • 12.7 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 __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RlayersBase = exports.handlersSymbol = void 0;
var react_1 = __importDefault(require("react"));
var context_1 = require("./context");
var debug_1 = __importDefault(require("./debug"));
exports.handlersSymbol = '_rlayers_handlers';
var RlayersBase = /** @class */ (function (_super) {
__extends(RlayersBase, _super);
function RlayersBase() {
return _super !== null && _super.apply(this, arguments) || this;
}
RlayersBase.getOLObject = function (prop, ol) {
var handlers = ol.get(prop);
if (handlers === undefined) {
handlers = {};
ol.set(prop, handlers);
}
return handlers;
};
Object.defineProperty(RlayersBase.prototype, "handlers", {
get: function () {
return RlayersBase.getOLObject(exports.handlersSymbol, this.ol);
},
enumerable: false,
configurable: true
});
/**
* Get the lowercase names of the currently installed handlers
*/
RlayersBase.prototype.getCurrentEvents = function () {
var _this = this;
return Object.keys(this.props)
.filter(function (p) { return p.startsWith('on'); })
.map(function (ev) { return ({ event: ev.toLowerCase().slice(2), prop: ev }); })
.reduce(function (a, x) {
var _a;
return (__assign(__assign({}, a), (_a = {}, _a[x.event] = _this.props[x.prop], _a)));
}, {});
};
/**
* Get the uppercase name of this event
*/
RlayersBase.prototype.getHandlerProp = function (event) {
var e_1, _a;
try {
for (var _b = __values(Object.keys(this.props)), _c = _b.next(); !_c.done; _c = _b.next()) {
var p = _c.value;
if (p.toLowerCase() === 'on' + event)
return p;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
RlayersBase.prototype.incrementHandlers = function (ev) {
return;
};
RlayersBase.prototype.decrementHandlers = function (ev) {
return;
};
RlayersBase.prototype.attachEventHandlers = function () {
var e_2, _a;
var _this = this;
var _b;
var handlers = this.handlers;
var handlersList = Object.keys(handlers !== null && handlers !== void 0 ? handlers : {});
var eventSources = (_b = this.eventSources) !== null && _b !== void 0 ? _b : [this.ol];
var newEvents = this.getCurrentEvents();
var newEventsList = Object.keys(newEvents);
var eventsToCheck = newEventsList.concat(handlersList.filter(function (ev) { return !newEventsList.includes(ev); }));
var _loop_1 = function (p) {
var e_3, _c, e_4, _d;
if (handlers[p] !== undefined && newEvents[p] === undefined) {
(0, debug_1.default)('removing previously installed handler', this_1, p, handlers[p]);
try {
for (var eventSources_1 = (e_3 = void 0, __values(eventSources)), eventSources_1_1 = eventSources_1.next(); !eventSources_1_1.done; eventSources_1_1 = eventSources_1.next()) {
var source = eventSources_1_1.value;
source.un(p, handlers[p]);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (eventSources_1_1 && !eventSources_1_1.done && (_c = eventSources_1.return)) _c.call(eventSources_1);
}
finally { if (e_3) throw e_3.error; }
}
handlers[p] = undefined;
this_1.decrementHandlers(p);
}
if (handlers[p] === undefined && newEvents[p] !== undefined) {
(0, debug_1.default)('installing handler', this_1, p, newEvents[p]);
var prop_1 = this_1.getHandlerProp(p);
if (!prop_1)
throw new Error('Internal error');
handlers[p] = function (e) {
(0, debug_1.default)('handling event', e, _this, _this.props[prop_1]);
return _this.props[prop_1].call(_this, e);
};
try {
for (var eventSources_2 = (e_4 = void 0, __values(eventSources)), eventSources_2_1 = eventSources_2.next(); !eventSources_2_1.done; eventSources_2_1 = eventSources_2.next()) {
var source = eventSources_2_1.value;
source.on(p, handlers[p]);
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (eventSources_2_1 && !eventSources_2_1.done && (_d = eventSources_2.return)) _d.call(eventSources_2);
}
finally { if (e_4) throw e_4.error; }
}
this_1.incrementHandlers(p);
}
};
var this_1 = this;
try {
for (var eventsToCheck_1 = __values(eventsToCheck), eventsToCheck_1_1 = eventsToCheck_1.next(); !eventsToCheck_1_1.done; eventsToCheck_1_1 = eventsToCheck_1.next()) {
var p = eventsToCheck_1_1.value;
_loop_1(p);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (eventsToCheck_1_1 && !eventsToCheck_1_1.done && (_a = eventsToCheck_1.return)) _a.call(eventsToCheck_1);
}
finally { if (e_2) throw e_2.error; }
}
};
// Used when replacing a source
RlayersBase.prototype.attachOldEventHandlers = function (newSource) {
var e_5, _a;
var handlers = this.handlers;
var events = this.getCurrentEvents();
try {
for (var _b = __values(Object.keys(events)), _c = _b.next(); !_c.done; _c = _b.next()) {
var e = _c.value;
if (events[e]) {
(0, debug_1.default)('reinstalling existing handler', this, e, events[e]);
newSource.on(e, handlers[e]);
}
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_5) throw e_5.error; }
}
};
RlayersBase.prototype.refresh = function (prevProps) {
this.attachEventHandlers();
};
/**
* Programmatically add an event handler to an RLayers component.
*
* @param {string} ev OpenLayers event
* @param {Handler} cb Callback
*/
RlayersBase.prototype.on = function (ev, cb) {
this.ol.on(ev, cb);
this.incrementHandlers(ev);
};
/**
* Programmatically add an event handler to an RLayers component.
*
* Although public, use of this method is discouraged as it lacks
* any safety against calling un on a method that has not been
* registered.
*
* @param {string} ev OpenLayers event
* @param {Handler} cb Callback
*/
RlayersBase.prototype.un = function (ev, cb) {
this.decrementHandlers(ev);
this.ol.un(ev, cb);
};
RlayersBase.prototype.componentDidMount = function () {
(0, debug_1.default)('didMount', this);
this.refresh();
};
/* istanbul ignore next */
RlayersBase.prototype.propsDiff = function (prev) {
var e_6, _a;
if (this.props === null || prev === null) {
if (this.props !== prev) {
(0, debug_1.default)('null props differ', this.props, prev);
return true;
}
return false;
}
try {
for (var _b = __values(Object.keys(this.props)), _c = _b.next(); !_c.done; _c = _b.next()) {
var k = _c.value;
if (this.props[k] !== prev[k]) {
(0, debug_1.default)('because of', k, this.props[k], prev[k]);
return true;
}
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_6) throw e_6.error; }
}
return false;
};
RlayersBase.prototype.componentDidUpdate = function (prevProps, prev, snap) {
if (this.props !== prevProps) {
(0, debug_1.default)('willRefresh', this, prevProps, this.props);
this.refresh(prevProps);
}
};
RlayersBase.prototype.componentWillUnmount = function () {
var e_7, _a, e_8, _b;
var _c;
var handlers = this.handlers;
(0, debug_1.default)('willUnmount', this, handlers);
var eventSources = (_c = this.eventSources) !== null && _c !== void 0 ? _c : [this.ol];
try {
for (var _d = __values(Object.keys(handlers !== null && handlers !== void 0 ? handlers : {})), _e = _d.next(); !_e.done; _e = _d.next()) {
var h = _e.value;
(0, debug_1.default)('cleaning up handler', this, h, handlers[h]);
if (handlers[h]) {
try {
for (var eventSources_3 = (e_8 = void 0, __values(eventSources)), eventSources_3_1 = eventSources_3.next(); !eventSources_3_1.done; eventSources_3_1 = eventSources_3.next()) {
var source = eventSources_3_1.value;
source.un(h, handlers[h]);
}
}
catch (e_8_1) { e_8 = { error: e_8_1 }; }
finally {
try {
if (eventSources_3_1 && !eventSources_3_1.done && (_b = eventSources_3.return)) _b.call(eventSources_3);
}
finally { if (e_8) throw e_8.error; }
}
handlers[h] = undefined;
this.decrementHandlers(h);
}
}
}
catch (e_7_1) { e_7 = { error: e_7_1 }; }
finally {
try {
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
}
finally { if (e_7) throw e_7.error; }
}
};
RlayersBase.prototype.render = function () {
return null;
};
RlayersBase.contextType = context_1.RContext;
return RlayersBase;
}(react_1.default.PureComponent));
exports.RlayersBase = RlayersBase;
//# sourceMappingURL=REvent.js.map