mobility-toolbox-js 2.0.0 → 2.0.1-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/api/RoutingAPI.js +15 -0
  2. package/api/RoutingAPI.test.js +25 -0
  3. package/api/StopsAPI.js +12 -0
  4. package/api/StopsAPI.test.js +22 -0
  5. package/api/TralisAPI.js +359 -0
  6. package/api/TralisAPI.test.js +67 -0
  7. package/api/{tralis/TralisAPIUtils.js → TralisAPIUtils.js} +2 -32
  8. package/api/index.js +3 -3
  9. package/{ol/README.md → api/typedefs.js} +0 -0
  10. package/common/Tracker.js +14 -118
  11. package/common/api/HttpAPI.js +30 -0
  12. package/common/api/HttpAPI.test.js +50 -0
  13. package/common/api/WebSocketAPI.js +175 -0
  14. package/{api/tralis/WebSocketConnector.test.js → common/api/WebSocketAPI.test.js} +100 -145
  15. package/common/controls/Control.js +26 -91
  16. package/common/controls/Control.test.js +32 -43
  17. package/common/index.js +4 -0
  18. package/common/layers/Layer.js +53 -244
  19. package/common/layers/Layer.test.js +185 -244
  20. package/common/mixins/CopyrightMixin.js +20 -44
  21. package/common/mixins/SearchMixin.js +100 -166
  22. package/common/mixins/TralisLayerMixin.js +443 -894
  23. package/common/styles/index.js +4 -4
  24. package/common/styles/trackerDefaultStyle.js +39 -175
  25. package/common/styles/trackerDelayStyle.js +2 -11
  26. package/common/styles/trackerSimpleStyle.js +4 -8
  27. package/common/trackerConfig.js +61 -99
  28. package/common/trackerConfig.test.js +15 -17
  29. package/common/typedefs.js +0 -23
  30. package/common/utils/createTrackerFilters.js +10 -41
  31. package/common/utils/createTrackerFilters.test.js +40 -56
  32. package/common/utils/getMapboxMapCopyrights.js +3 -16
  33. package/common/utils/getMapboxMapCopyrights.test.js +32 -39
  34. package/common/utils/getMapboxStyleUrl.js +3 -13
  35. package/common/utils/getVehiclePosition.js +3 -33
  36. package/common/utils/index.js +5 -6
  37. package/common/utils/removeDuplicate.js +3 -17
  38. package/common/utils/removeDuplicate.test.js +17 -20
  39. package/common/utils/sortByDelay.js +2 -7
  40. package/common/utils/timeUtils.js +8 -32
  41. package/common/utils/timeUtils.test.js +7 -13
  42. package/index.js +8 -2
  43. package/mapbox/controls/CopyrightControl.js +9 -38
  44. package/mapbox/controls/index.js +1 -0
  45. package/mapbox/index.js +4 -3
  46. package/mapbox/layers/Layer.js +15 -76
  47. package/mapbox/layers/Layer.test.js +81 -101
  48. package/mapbox/layers/TralisLayer.js +46 -193
  49. package/mapbox/layers/TralisLayer.test.js +12 -14
  50. package/mapbox/layers/index.js +2 -0
  51. package/mapbox/utils.js +7 -21
  52. package/mbt.js +50444 -0
  53. package/mbt.js.map +7 -0
  54. package/mbt.min.js +1005 -0
  55. package/mbt.min.js.map +7 -0
  56. package/ol/controls/CopyrightControl.js +8 -46
  57. package/ol/controls/CopyrightControl.test.js +75 -121
  58. package/ol/controls/RoutingControl.js +167 -532
  59. package/ol/controls/RoutingControl.test.js +99 -164
  60. package/ol/controls/StopFinderControl.js +3 -31
  61. package/ol/controls/StopFinderControl.test.js +18 -29
  62. package/ol/controls/index.js +3 -0
  63. package/ol/index.js +5 -13
  64. package/ol/layers/Layer.js +23 -128
  65. package/ol/layers/Layer.test.js +79 -102
  66. package/ol/layers/MapboxLayer.js +62 -237
  67. package/ol/layers/MapboxLayer.test.js +58 -84
  68. package/ol/layers/MapboxStyleLayer.js +38 -268
  69. package/ol/layers/MapboxStyleLayer.test.js +97 -128
  70. package/ol/layers/MaplibreLayer.js +46 -187
  71. package/ol/layers/RoutingLayer.js +21 -51
  72. package/ol/layers/RoutingLayer.test.js +15 -24
  73. package/ol/layers/TralisLayer.js +102 -276
  74. package/ol/layers/TralisLayer.test.js +32 -50
  75. package/ol/layers/VectorLayer.js +3 -24
  76. package/ol/layers/VectorLayer.test.js +34 -45
  77. package/ol/layers/WMSLayer.js +15 -57
  78. package/ol/layers/WMSLayer.test.js +35 -43
  79. package/ol/layers/index.js +8 -0
  80. package/ol/styles/fullTrajectoryDelayStyle.js +11 -15
  81. package/ol/styles/fullTrajectoryStyle.js +17 -25
  82. package/ol/styles/index.js +2 -2
  83. package/package.json +35 -62
  84. package/api/routing/RoutingAPI.js +0 -44
  85. package/api/routing/RoutingAPI.test.js +0 -41
  86. package/api/stops/StopsAPI.js +0 -41
  87. package/api/stops/StopsAPI.test.js +0 -34
  88. package/api/tralis/TralisAPI.js +0 -731
  89. package/api/tralis/TralisAPI.test.js +0 -75
  90. package/api/tralis/WebSocketConnector.js +0 -338
  91. package/api/tralis/typedefs.js +0 -81
  92. package/common/api/api.js +0 -64
  93. package/common/api/api.test.js +0 -68
  94. package/index.js.map +0 -1
  95. package/module.js +0 -23
  96. package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  97. package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  98. package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  99. package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  100. package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  101. package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  102. package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
@@ -1,21 +1,18 @@
1
- import Control from './Control';
2
-
3
- describe('Control', () => {
4
- test('should be activated by default', () => {
1
+ import Control from "./Control";
2
+ describe("Control", () => {
3
+ test("should be activated by default", () => {
5
4
  const control = new Control();
6
5
  expect(control.active).toBe(true);
7
6
  });
8
-
9
- test('should not be activated if set to false in the options', () => {
7
+ test("should not be activated if set to false in the options", () => {
10
8
  const control = new Control({ active: false });
11
9
  expect(control.active).toBe(false);
12
10
  });
13
-
14
- test('should call activate/deactivate when active is set to true/false', () => {
11
+ test("should call activate/deactivate when active is set to true/false", () => {
15
12
  const control = new Control();
16
- const spy1 = jest.spyOn(control, 'activate');
17
- const spy2 = jest.spyOn(control, 'deactivate');
18
- const spy3 = jest.spyOn(control, 'render');
13
+ const spy1 = jest.spyOn(control, "activate");
14
+ const spy2 = jest.spyOn(control, "deactivate");
15
+ const spy3 = jest.spyOn(control, "render");
19
16
  control.active = false;
20
17
  expect(spy1).toBeCalledTimes(0);
21
18
  expect(spy2).toBeCalledTimes(1);
@@ -25,74 +22,66 @@ describe('Control', () => {
25
22
  expect(spy2).toBeCalledTimes(2);
26
23
  expect(spy3).toBeCalledTimes(2);
27
24
  });
28
-
29
- test('should append/remove the element to the map container when map is set', () => {
30
- const element = document.createElement('div');
31
- const target = document.createElement('div');
25
+ test("should append/remove the element to the map container when map is set", () => {
26
+ const element = document.createElement("div");
27
+ const target = document.createElement("div");
32
28
  const control = new Control({
33
- element,
29
+ element
34
30
  });
35
- const spy1 = jest.spyOn(control, 'activate');
36
- const spy2 = jest.spyOn(control, 'deactivate');
37
- const spy3 = jest.spyOn(control, 'render');
31
+ const spy1 = jest.spyOn(control, "activate");
32
+ const spy2 = jest.spyOn(control, "deactivate");
33
+ const spy3 = jest.spyOn(control, "render");
38
34
  control.map = {
39
35
  getContainer() {
40
36
  return target;
41
- },
37
+ }
42
38
  };
43
39
  expect(target.childNodes[0]).toBe(element);
44
40
  expect(spy1).toBeCalledTimes(1);
45
41
  expect(spy2).toBeCalledTimes(2);
46
42
  expect(spy3).toBeCalledTimes(1);
47
-
48
- control.map = null;
43
+ control.detachFromMap();
49
44
  expect(target.childNodes[0]).toBe();
50
45
  expect(spy1).toBeCalledTimes(1);
51
46
  expect(spy2).toBeCalledTimes(3);
52
47
  expect(spy3).toBeCalledTimes(2);
53
48
  });
54
-
55
- test('should append/remove the element to the target property when map is set', () => {
56
- const element = document.createElement('div');
57
- const target = document.createElement('div');
49
+ test("should append/remove the element to the target property when map is set", () => {
50
+ const element = document.createElement("div");
51
+ const target = document.createElement("div");
58
52
  const control = new Control({
59
53
  target,
60
- element,
54
+ element
61
55
  });
62
- const spy1 = jest.spyOn(control, 'activate');
63
- const spy2 = jest.spyOn(control, 'deactivate');
64
- const spy3 = jest.spyOn(control, 'render');
56
+ const spy1 = jest.spyOn(control, "activate");
57
+ const spy2 = jest.spyOn(control, "deactivate");
58
+ const spy3 = jest.spyOn(control, "render");
65
59
  control.map = {};
66
60
  expect(target.childNodes[0]).toBe(element);
67
61
  expect(spy1).toBeCalledTimes(1);
68
62
  expect(spy2).toBeCalledTimes(2);
69
63
  expect(spy3).toBeCalledTimes(1);
70
-
71
- control.map = null;
64
+ control.detachFromMap();
72
65
  expect(target.childNodes[0]).toBe();
73
66
  expect(spy1).toBeCalledTimes(1);
74
67
  expect(spy2).toBeCalledTimes(3);
75
68
  expect(spy3).toBeCalledTimes(2);
76
69
  });
77
-
78
- test('set a custom render method', () => {
70
+ test("set a custom render method", () => {
79
71
  const spy = jest.fn();
80
- // eslint-disable-next-line no-unused-vars
81
72
  const control = new Control({
82
- render: spy,
73
+ render: spy
83
74
  });
84
75
  expect(spy).toBeCalledTimes(1);
85
76
  });
86
-
87
- test('pass function params to custom render method', () => {
77
+ test("pass function params to custom render method", () => {
88
78
  const spy = jest.fn();
89
- // eslint-disable-next-line no-unused-vars
90
79
  const control = new Control({
91
- render: spy,
80
+ render: spy
92
81
  });
93
- control.render('foo', 'bar');
82
+ control.render("foo", "bar");
94
83
  expect(spy).toBeCalledTimes(2);
95
- expect(spy.mock.calls[1][0]).toBe('foo');
96
- expect(spy.mock.calls[1][1]).toBe('bar');
84
+ expect(spy.mock.calls[1][0]).toBe("foo");
85
+ expect(spy.mock.calls[1][1]).toBe("bar");
97
86
  });
98
87
  });
@@ -0,0 +1,4 @@
1
+ export { default as Tracker } from "./Tracker";
2
+ export * as trackerConfig from "./trackerConfig";
3
+ export * from "./utils";
4
+ export * from "./styles";
@@ -1,78 +1,22 @@
1
- import Observable from 'ol/Observable';
2
- import { v4 as uuid } from 'uuid';
3
-
4
- /**
5
- * A class representing a layer to display on map.
6
- *
7
- * @example
8
- * const layer = new Layer({
9
- * name: 'myLayer',
10
- * });
11
- *
12
- * @classproperty {string} name - Name of the layer
13
- * @classproperty {string} key - Identifier of the layer. Must be unique.
14
- * @classproperty {string[]} copyrights - Array of copyrights.
15
- * @classproperty {boolean} isBaseLayer - Define if the layer is a base layer. Read-only.
16
- * @classproperty {boolean} isQueryable - Define if the layer can be queried. If false, it will set isHoverActive and isClickActive to false. Read-only.
17
- * @classproperty {boolean} isClickActive - If true feature information will be queried on user click event. See inherited layers for more informations. Read-only.
18
- * @classproperty {boolean} isHoverActive - If true feature information will be queried on pointer move event. See inherited layers for more informations. Read-only.
19
- * @classproperty {boolean} isMobilityLayer - Custom property for duck typing since `instanceof` is not working when the instance was created on different bundles. Read-only.
20
- * @classproperty {Layer[]} children - List of children.
21
- * @classproperty {boolean} visible - Define if the layer is visible or not.
22
- * @classproperty {number} hitTolerance - Hit-detection tolerance in css pixels. Pixels inside the radius around the given position will be checked for features.
23
- * @classproperty {Object} properties - Custom properties.
24
- * @classproperty {ol/Map~Map|mapboxgl.Map} map - The map where the layer is displayed.
25
- */
1
+ import Observable from "ol/Observable";
2
+ import { v4 as uuid } from "uuid";
26
3
  export default class Layer extends Observable {
27
- /**
28
- * Constructor
29
- *
30
- * @param {Object} options
31
- * @param {string} [options.name=uuid()] Layer name. Default use a generated uuid.
32
- * @param {string} [options.key=uuid().toLowerCase()] Layer key, will use options.name.toLowerCase() if not specified.
33
- * @param {string[]} [options.copyrights=undefined] Array of copyrights.
34
- * @param {Array<Layer>} [options.children=[]] Sublayers.
35
- * @param {Object} [options.properties={}] Application-specific layer properties.
36
- * @param {boolean} [options.visible=true] If true this layer is visible on the map.
37
- * @param {boolean} [options.isBaseLayer=false] If true this layer is a baseLayer.
38
- * @param {boolean} [options.isQueryable=true] Define if the layer can be queried. If false, it will also set isHoverActive and isClickActive to false. Read-only.
39
- * @param {boolean} [options.isClickActive=true] If true feature information will be queried on click event. See inherited layers for more informations. Read-only.
40
- * @param {boolean} [options.isHoverActive=true] If true feature information will be queried on pointer move event. See inherited layers for more informations. Read-only.
41
- * @param {number} [options.hitTolerance=5] Hit-detection tolerance in css pixels. Pixels inside the radius around the given position will be checked for features.
42
- */
43
4
  constructor(options = {}) {
44
5
  super();
45
6
  this.defineProperties(options);
46
-
47
- // Add mouse event callbacks
48
7
  const { onClick, onHover } = options;
49
-
50
8
  if (onHover) {
51
9
  this.onHover(onHover);
52
10
  }
53
-
54
11
  if (onClick) {
55
12
  this.onClick(onClick);
56
13
  }
57
-
58
- // This if is very important if you remove it you break the copyright control.
59
14
  if (options.copyrights) {
60
- /** @ignore */
61
15
  this.copyrights = options.copyrights;
62
16
  }
63
-
64
- /** @ignore */
65
17
  this.onUserClickCallback = this.onUserClickCallback.bind(this);
66
-
67
- /** @ignore */
68
18
  this.onUserMoveCallback = this.onUserMoveCallback.bind(this);
69
19
  }
70
-
71
- /**
72
- * Define layer's properties.
73
- *
74
- * @ignore
75
- */
76
20
  defineProperties(options) {
77
21
  const {
78
22
  name,
@@ -84,321 +28,186 @@ export default class Layer extends Observable {
84
28
  isQueryable,
85
29
  isClickActive,
86
30
  isHoverActive,
87
- hitTolerance,
31
+ hitTolerance
88
32
  } = {
89
33
  isQueryable: true,
90
34
  isClickActive: true,
91
35
  isHoverActive: true,
92
- ...options,
36
+ ...options
93
37
  };
94
38
  const uid = uuid();
95
39
  const dfltName = name || uid;
96
40
  Object.defineProperties(this, {
97
- // options is used for clone function.
98
41
  options: {
99
- value: options,
42
+ value: options
100
43
  },
101
44
  name: {
102
- value: dfltName,
45
+ value: dfltName
103
46
  },
104
47
  key: {
105
- value: key || dfltName.toLowerCase(),
48
+ value: key || dfltName.toLowerCase()
106
49
  },
107
50
  isBaseLayer: {
108
- value: !!isBaseLayer,
51
+ value: !!isBaseLayer
109
52
  },
110
53
  isQueryable: {
111
54
  value: !!isQueryable,
112
- writable: true,
55
+ writable: true
113
56
  },
114
57
  isClickActive: {
115
58
  value: !!isQueryable && !!isClickActive,
116
- writable: true,
59
+ writable: true
117
60
  },
118
61
  isHoverActive: {
119
62
  value: !!isQueryable && !!isHoverActive,
120
- writable: true,
63
+ writable: true
121
64
  },
122
65
  hitTolerance: {
123
66
  value: hitTolerance || 5,
124
- writable: true,
67
+ writable: true
125
68
  },
126
- // Custom property for duck typing since `instanceof` is not working
127
- // when the instance was created on different bundles.
128
69
  isMobilityLayer: {
129
- value: true,
70
+ value: true
130
71
  },
131
72
  children: {
132
73
  value: children || [],
133
- writable: true,
74
+ writable: true
134
75
  },
135
76
  copyrights: {
136
- get: () => this.get('copyrights'),
77
+ get: () => this.get("copyrights"),
137
78
  set: (newCopyrights) => {
138
- const arrValue = !Array.isArray(newCopyrights)
139
- ? [newCopyrights]
140
- : newCopyrights;
141
- this.set('copyrights', arrValue);
142
- },
79
+ const arrValue = !Array.isArray(newCopyrights) ? [newCopyrights] : newCopyrights;
80
+ this.set("copyrights", arrValue);
81
+ }
143
82
  },
144
83
  visible: {
145
- value: visible === undefined ? true : visible,
146
- writable: true,
84
+ value: visible === void 0 ? true : visible,
85
+ writable: true
147
86
  },
148
87
  properties: {
149
- value: { ...(properties || {}) },
88
+ value: { ...properties || {} }
150
89
  },
151
90
  map: {
152
- writable: true,
91
+ writable: true
153
92
  },
154
- /**
155
- * Callback function when a user click on a feature.
156
- */
157
93
  clickCallbacks: {
158
- value: [],
94
+ value: []
159
95
  },
160
- /**
161
- * Callback function when a user hover on a feature.
162
- */
163
96
  hoverCallbacks: {
164
- value: [],
165
- },
97
+ value: []
98
+ }
166
99
  });
167
100
  }
168
-
169
- /**
170
- * Initialize the layer with the map passed in parameters.
171
- *
172
- * @param {ol/Map~Map|mapboxgl.Map} map A map.
173
- */
174
- init(map) {
175
- this.terminate();
176
- /** @ignore */
101
+ attachToMap(map) {
102
+ this.detachFromMap();
177
103
  this.map = map;
178
104
  }
179
-
180
- /**
181
- * Terminate what was initialized in init function. Remove layer, events...
182
- */
183
- // eslint-disable-next-line class-methods-use-this
184
- terminate() {}
185
-
186
- /**
187
- * Get a layer property.
188
- *
189
- * @param {string} name Property name.
190
- * @return {property} Property
191
- */
105
+ detachFromMap() {
106
+ }
192
107
  get(name) {
193
108
  return this.properties[name];
194
109
  }
195
-
196
- /**
197
- * Set a layer property.
198
- *
199
- * @param {string} name Property name.
200
- * @param {string} value Value.
201
- */
202
110
  set(name, value) {
203
111
  if (value !== this.properties[name]) {
204
112
  this.properties[name] = value;
205
113
  this.dispatchEvent({
206
114
  type: `change:${name}`,
207
- target: this,
115
+ target: this
208
116
  });
209
117
  }
210
118
  }
211
-
212
- /**
213
- * Change the visibility of the layer
214
- *
215
- * @param {boolean} visible Defines the visibility of the layer
216
- * @param {boolean} [stopPropagationDown]
217
- * @param {boolean} [stopPropagationUp]
218
- * @param {boolean} [stopPropagationSiblings]
219
- */
220
- setVisible(
221
- visible,
222
- stopPropagationDown = false,
223
- stopPropagationUp = false,
224
- stopPropagationSiblings = false,
225
- ) {
119
+ setVisible(visible, stopPropagationDown = false, stopPropagationUp = false, stopPropagationSiblings = false) {
226
120
  if (visible === this.visible) {
227
121
  return;
228
122
  }
229
-
230
- /** @ignore */
231
123
  this.visible = visible;
232
-
233
124
  this.dispatchEvent({
234
- type: 'change:visible',
125
+ type: "change:visible",
235
126
  target: this,
236
127
  stopPropagationDown,
237
128
  stopPropagationUp,
238
- stopPropagationSiblings,
129
+ stopPropagationSiblings
239
130
  });
240
131
  }
241
-
242
- /**
243
- * Returns an array with visible child layers
244
- *
245
- * @return {Layer[]} Visible children
246
- */
247
132
  getVisibleChildren() {
248
133
  return this.children.filter((child) => child.visible);
249
134
  }
250
-
251
- /**
252
- * Checks whether the layer has child layers with visible set to True
253
- *
254
- * @return {boolean} True if the layer has visible child layers
255
- * @deprecated
256
- */
257
135
  hasVisibleChildren() {
258
136
  return !!this.children.find((child) => child.visible === true);
259
137
  }
260
-
261
- /**
262
- * Request feature information for a given coordinate.
263
- * This function must be implemented by inheriting layers.
264
- *
265
- * @param {ol/coordinate~Coordinate} coordinate Coordinate.
266
- * @param {Object} options Some options. See child classes to see which are supported.
267
- * @return {Promise<FeatureInfo>} An empty response.
268
- */
269
- // eslint-disable-next-line no-unused-vars
270
138
  getFeatureInfoAtCoordinate(coordinate, options) {
271
- // eslint-disable-next-line no-console
272
- console.error(
273
- 'getFeatureInfoAtCoordinate must be implemented by inheriting layers',
274
- this.key,
275
- );
276
-
277
- // No response so we modify the properties accordingly, to avoid spaming the console.
139
+ console.error("getFeatureInfoAtCoordinate must be implemented by inheriting layers", this.key);
278
140
  this.isQueryable = false;
279
- // this.isClickActive = false;
280
- // this.isHoverActive = false;
281
-
282
- // This layer returns no feature info.
283
- // The function is implemented by inheriting layers.
284
141
  return Promise.resolve({
285
142
  layer: this,
286
143
  features: [],
287
- coordinate,
144
+ coordinate
288
145
  });
289
146
  }
290
-
291
- /**
292
- * Listens to click events on the layer.
293
- * @param {function} callback Callback function, called with the clicked
294
- * features,
295
- * the layer instance and the click event.
296
- */
297
147
  onClick(callback) {
298
- if (typeof callback === 'function') {
148
+ if (typeof callback === "function") {
299
149
  if (!this.clickCallbacks.includes(callback)) {
300
150
  this.clickCallbacks.push(callback);
301
151
  }
302
152
  } else {
303
- throw new Error('onClick callback must be of type function:', callback);
153
+ throw new Error("onClick callback must be of type function:", callback);
304
154
  }
305
155
  }
306
-
307
- /**
308
- * Unlistens to click events on the layer.
309
- * @param {function} callback Callback function, called with the clicked
310
- * features,
311
- * the layer instance and the click event.
312
- */
313
156
  unClick(callback) {
314
- if (typeof callback === 'function') {
157
+ if (typeof callback === "function") {
315
158
  const idx = this.clickCallbacks.indexOf(callback);
316
159
  if (idx > -1) {
317
160
  this.clickCallbacks.splice(idx, 1);
318
161
  }
319
162
  }
320
163
  }
321
-
322
- /**
323
- * Function triggered when the user click the map.
324
- * @private
325
- */
326
164
  onUserClickCallback(evt) {
327
165
  const emptyFeatureInfo = {
328
166
  features: [],
329
167
  layer: this,
330
168
  coordinate: evt.coordinate,
331
- event: evt,
169
+ event: evt
332
170
  };
333
-
334
171
  if (!this.isClickActive || !this.clickCallbacks.length) {
335
172
  return Promise.resolve(emptyFeatureInfo);
336
173
  }
337
-
338
- return this.getFeatureInfoAtCoordinate(evt.coordinate)
339
- .then((featureInfo) => {
340
- const { features, layer, coordinate } = featureInfo;
341
- this.clickCallbacks.forEach((callback) =>
342
- callback(features, layer, coordinate),
343
- );
344
- return featureInfo;
345
- })
346
- .catch(() => emptyFeatureInfo);
174
+ return this.getFeatureInfoAtCoordinate(evt.coordinate).then((featureInfo) => {
175
+ const { features, layer, coordinate } = featureInfo;
176
+ this.clickCallbacks.forEach((callback) => callback(features, layer, coordinate));
177
+ return featureInfo;
178
+ }).catch(() => emptyFeatureInfo);
347
179
  }
348
-
349
- /**
350
- * Listens to hover events on the layer.
351
- * @param {function} callback Callback function, called with the clicked
352
- * features, the layer instance and the click event.
353
- */
354
180
  onHover(callback) {
355
- if (typeof callback === 'function') {
181
+ if (typeof callback === "function") {
356
182
  if (!this.hoverCallbacks.includes(callback)) {
357
183
  this.hoverCallbacks.push(callback);
358
184
  }
359
185
  } else {
360
- throw new Error('callback must be of type function.');
186
+ throw new Error("callback must be of type function.");
361
187
  }
362
188
  }
363
-
364
- /**
365
- * Unlistens to hover events on the layer.
366
- * @param {function} callback Callback function, called with the hovered
367
- * features, the layer instance and the click event.
368
- */
369
189
  unHover(callback) {
370
- if (typeof callback === 'function') {
190
+ if (typeof callback === "function") {
371
191
  const idx = this.hoverCallbacks.indexOf(callback);
372
192
  if (idx > -1) {
373
193
  this.hoverCallbacks.splice(idx, 1);
374
194
  }
375
195
  }
376
196
  }
377
-
378
- /**
379
- * Function triggered when the user move the cursor.
380
- * @private
381
- */
382
197
  onUserMoveCallback(evt) {
383
198
  const emptyFeatureInfo = {
384
199
  features: [],
385
200
  layer: this,
386
201
  coordinate: evt.coordinate,
387
- event: evt,
202
+ event: evt
388
203
  };
389
-
390
204
  if (!this.isHoverActive || !this.hoverCallbacks.length) {
391
205
  return Promise.resolve(emptyFeatureInfo);
392
206
  }
393
-
394
- return this.getFeatureInfoAtCoordinate(evt.coordinate)
395
- .then((featureInfo) => {
396
- const { features, layer, coordinate } = featureInfo;
397
- this.hoverCallbacks.forEach((callback) =>
398
- callback(features, layer, coordinate),
399
- );
400
- return featureInfo;
401
- })
402
- .catch(() => emptyFeatureInfo);
207
+ return this.getFeatureInfoAtCoordinate(evt.coordinate).then((featureInfo) => {
208
+ const { features, layer, coordinate } = featureInfo;
209
+ this.hoverCallbacks.forEach((callback) => callback(features, layer, coordinate));
210
+ return featureInfo;
211
+ }).catch(() => emptyFeatureInfo);
403
212
  }
404
213
  }