aboutsummaryrefslogtreecommitdiffstats
path: root/src/js
diff options
context:
space:
mode:
authorJakub Hampl <kopomir@gmail.com>2019-06-17 15:03:53 +0100
committerJakub Hampl <kopomir@gmail.com>2019-06-17 15:03:53 +0100
commit804f47381a82d7a2c969ff5b53b81cbf0e0eda43 (patch)
treee46c7fe2e8f6eb0efaff99b8f335001661a76d92 /src/js
parent4417163537dfd644b2c7ab70afa46c18bbd63b0f (diff)
Adds fancier event decoding
Diffstat (limited to 'src/js')
-rw-r--r--src/js/main.js86
1 files changed, 43 insertions, 43 deletions
diff --git a/src/js/main.js b/src/js/main.js
index afc5ebc..e5112fb 100644
--- a/src/js/main.js
+++ b/src/js/main.js
@@ -151,49 +151,49 @@ export function registerCustomElement(settings) {
addEventListener(type, fn, ...args) {
if (this._map) {
- var wrapped;
- if (
- [
- "mousedown",
- "mouseup",
- "mouseover",
- "mousemove",
- "click",
- "dblclick",
- "mouseout",
- "contextmenu",
- "zoom",
- "zoomstart",
- "zoomend",
- "rotate",
- "rotatestart",
- "rotateend"
- ].includes(type)
- ) {
- wrapped = e => {
- e.features = this._map.queryRenderedFeatures(e.point, {
- layers: this.eventFeaturesLayers,
- filter: this.eventFeaturesFilter
- });
- return fn(e);
- };
- } else if (["touchend", "touchmove", "touchcancel"].includes(type)) {
- wrapped = e => {
- e.features = this._map.queryRenderedFeatures([e.point], {
- layers: this.eventFeaturesLayers,
- filter: this.eventFeaturesFilter
- });
- e.perPointFeatures = e.points.map(point =>
- this._map.queryRenderedFeatures(point, {
- layers: this.eventFeaturesLayers,
- filter: this.eventFeaturesFilter
- })
- );
- return fn(e);
- };
- } else {
- wrapped = fn;
- }
+ const wrapped = e =>
+ fn(
+ new Proxy(e, {
+ has: (obj, prop) =>
+ prop in obj ||
+ (prop === "features" && obj.point) ||
+ (prop === "perPointFeatures" && obj.points) ||
+ (
+ (prop.slice(0, 2) === "is" || prop.slice(0, 3) === "get") &&
+ prop in this._map &&
+ typeof this._map[prop] === "function"
+ )
+ ,
+ get: (obj, prop) => {
+ if (prop in obj) {
+ return obj[prop];
+ } else if (prop === "features" && obj.point) {
+ return this._map.queryRenderedFeatures(obj.point, {
+ layers: this.eventFeaturesLayers,
+ filter: this.eventFeaturesFilter
+ });
+ } else if (prop === "perPointFeatures" && obj.points) {
+ return obj.points.map(point =>
+ this._map.queryRenderedFeatures(point, {
+ layers: this.eventFeaturesLayers,
+ filter: this.eventFeaturesFilter
+ })
+ );
+ } else if (
+ (prop.slice(0, 2) === "is" || prop.slice(0, 3) === "get") &&
+ prop in this._map &&
+ typeof this._map[prop] === "function"
+ ) {
+ try {
+ return this._map[prop]();
+ } catch (_) {
+ return undefined;
+ }
+ }
+ return undefined;
+ }
+ })
+ );
this._eventListenerMap.set(fn, wrapped);
return this._map.on(type, wrapped);
} else {