mobility-toolbox-js 2.0.0-beta.34 → 2.0.0-beta.37

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 (195) hide show
  1. package/README.md +14 -8
  2. package/api/RealtimeAPI.d.ts +6 -6
  3. package/api/RealtimeAPI.d.ts.map +1 -1
  4. package/api/RealtimeAPI.js +170 -197
  5. package/api/RealtimeAPI.test.d.ts +2 -0
  6. package/api/RealtimeAPI.test.d.ts.map +1 -0
  7. package/api/RealtimeAPI.test.js +67 -0
  8. package/api/RoutingAPI.d.ts +6 -2
  9. package/api/RoutingAPI.d.ts.map +1 -1
  10. package/api/RoutingAPI.js +8 -38
  11. package/api/RoutingAPI.test.d.ts +2 -0
  12. package/api/RoutingAPI.test.d.ts.map +1 -0
  13. package/api/RoutingAPI.test.js +29 -0
  14. package/api/StopsAPI.d.ts +1 -1
  15. package/api/StopsAPI.d.ts.map +1 -1
  16. package/api/StopsAPI.js +8 -40
  17. package/api/StopsAPI.test.d.ts +2 -0
  18. package/api/StopsAPI.test.d.ts.map +1 -0
  19. package/api/StopsAPI.test.js +26 -0
  20. package/api/index.d.ts +3 -4
  21. package/api/index.d.ts.map +1 -1
  22. package/api/index.js +3 -10
  23. package/api/typedefs.js +1 -0
  24. package/common/api/HttpAPI.d.ts +2 -2
  25. package/common/api/HttpAPI.d.ts.map +1 -1
  26. package/common/api/HttpAPI.js +21 -48
  27. package/common/api/HttpAPI.test.d.ts +2 -0
  28. package/common/api/HttpAPI.test.d.ts.map +1 -0
  29. package/common/api/HttpAPI.test.js +54 -0
  30. package/common/api/WebSocketAPI.d.ts +8 -4
  31. package/common/api/WebSocketAPI.d.ts.map +1 -1
  32. package/common/api/WebSocketAPI.js +125 -129
  33. package/common/api/WebSocketAPI.test.d.ts +2 -0
  34. package/common/api/WebSocketAPI.test.d.ts.map +1 -0
  35. package/common/api/WebSocketAPI.test.js +380 -0
  36. package/common/controls/Control.d.ts +5 -5
  37. package/common/controls/Control.d.ts.map +1 -1
  38. package/common/controls/Control.js +44 -77
  39. package/common/controls/Control.test.d.ts +2 -0
  40. package/common/controls/Control.test.d.ts.map +1 -0
  41. package/common/controls/Control.test.js +89 -0
  42. package/common/index.js +2 -18
  43. package/common/layers/Layer.d.ts +11 -11
  44. package/common/layers/Layer.d.ts.map +1 -1
  45. package/common/layers/Layer.js +70 -104
  46. package/common/layers/Layer.test.d.ts +2 -0
  47. package/common/layers/Layer.test.d.ts.map +1 -0
  48. package/common/layers/Layer.test.js +137 -0
  49. package/common/mixins/CopyrightMixin.js +23 -52
  50. package/common/mixins/MapboxLayerMixin.js +162 -204
  51. package/common/mixins/RealtimeLayerMixin.js +580 -635
  52. package/common/mixins/StopFinderMixin.d.ts +3 -3
  53. package/common/mixins/StopFinderMixin.d.ts.map +1 -1
  54. package/common/mixins/StopFinderMixin.js +115 -157
  55. package/common/mixins/UserInteractionsLayerMixin.js +142 -175
  56. package/common/mixins/UserInteractionsLayerMixin.test.d.ts +2 -0
  57. package/common/mixins/UserInteractionsLayerMixin.test.d.ts.map +1 -0
  58. package/common/mixins/UserInteractionsLayerMixin.test.js +214 -0
  59. package/common/styles/index.js +4 -24
  60. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -1
  61. package/common/styles/realtimeDefaultStyle.js +67 -78
  62. package/common/styles/realtimeDelayStyle.js +4 -17
  63. package/common/styles/realtimeSimpleStyle.js +5 -7
  64. package/common/typedefs.js +1 -0
  65. package/common/utils/cleanStopTime.js +3 -5
  66. package/common/utils/compareDepartures.d.ts +1 -1
  67. package/common/utils/compareDepartures.d.ts.map +1 -1
  68. package/common/utils/compareDepartures.js +8 -11
  69. package/common/utils/createCanvas.js +3 -5
  70. package/common/utils/createTrackerFilters.d.ts +1 -1
  71. package/common/utils/createTrackerFilters.d.ts.map +1 -1
  72. package/common/utils/createTrackerFilters.js +22 -32
  73. package/common/utils/createTrackerFilters.test.d.ts +2 -0
  74. package/common/utils/createTrackerFilters.test.d.ts.map +1 -0
  75. package/common/utils/createTrackerFilters.test.js +79 -0
  76. package/common/utils/getLayersAsFlatArray.js +6 -8
  77. package/common/utils/getMapboxMapCopyrights.js +9 -11
  78. package/common/utils/getMapboxMapCopyrights.test.d.ts +2 -0
  79. package/common/utils/getMapboxMapCopyrights.test.d.ts.map +1 -0
  80. package/common/utils/getMapboxMapCopyrights.test.js +40 -0
  81. package/common/utils/getMapboxRender.js +12 -15
  82. package/common/utils/getMaplibreRender.js +10 -13
  83. package/common/utils/getRealtimeModeSuffix.js +2 -6
  84. package/common/utils/getUrlWithParams.js +5 -8
  85. package/common/utils/getVehiclePosition.js +20 -23
  86. package/common/utils/index.js +12 -37
  87. package/common/utils/removeDuplicate.d.ts +1 -1
  88. package/common/utils/removeDuplicate.d.ts.map +1 -1
  89. package/common/utils/removeDuplicate.js +6 -21
  90. package/common/utils/removeDuplicate.test.d.ts +2 -0
  91. package/common/utils/removeDuplicate.test.d.ts.map +1 -0
  92. package/common/utils/removeDuplicate.test.js +19 -0
  93. package/common/utils/renderTrajectories.js +31 -43
  94. package/common/utils/sortByDelay.js +4 -6
  95. package/common/utils/timeUtils.js +14 -24
  96. package/common/utils/timeUtils.test.d.ts +2 -0
  97. package/common/utils/timeUtils.test.d.ts.map +1 -0
  98. package/common/utils/timeUtils.test.js +10 -0
  99. package/common/utils/trackerConfig.js +27 -39
  100. package/common/utils/trackerConfig.test.d.ts +2 -0
  101. package/common/utils/trackerConfig.test.d.ts.map +1 -0
  102. package/common/utils/trackerConfig.test.js +23 -0
  103. package/iife.js +3 -5
  104. package/index.d.ts +4 -0
  105. package/index.js +9 -10
  106. package/mapbox/controls/CopyrightControl.d.ts +0 -1
  107. package/mapbox/controls/CopyrightControl.d.ts.map +1 -1
  108. package/mapbox/controls/CopyrightControl.js +18 -38
  109. package/mapbox/controls/index.js +1 -5
  110. package/mapbox/index.js +4 -20
  111. package/mapbox/layers/Layer.d.ts +1 -1
  112. package/mapbox/layers/Layer.d.ts.map +1 -1
  113. package/mapbox/layers/Layer.js +29 -71
  114. package/mapbox/layers/Layer.test.d.ts +2 -0
  115. package/mapbox/layers/Layer.test.d.ts.map +1 -0
  116. package/mapbox/layers/Layer.test.js +204 -0
  117. package/mapbox/layers/RealtimeLayer.d.ts +4 -4
  118. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -1
  119. package/mapbox/layers/RealtimeLayer.js +83 -125
  120. package/mapbox/layers/RealtimeLayer.test.d.ts +2 -0
  121. package/mapbox/layers/RealtimeLayer.test.d.ts.map +1 -0
  122. package/mapbox/layers/RealtimeLayer.test.js +10 -0
  123. package/mapbox/layers/index.js +2 -7
  124. package/mapbox/utils.js +19 -33
  125. package/mbt.js +59 -37
  126. package/mbt.js.map +2 -2
  127. package/mbt.min.js +10 -10
  128. package/mbt.min.js.map +2 -2
  129. package/ol/controls/CopyrightControl.js +26 -47
  130. package/ol/controls/CopyrightControl.test.d.ts +2 -0
  131. package/ol/controls/CopyrightControl.test.d.ts.map +1 -0
  132. package/ol/controls/CopyrightControl.test.js +177 -0
  133. package/ol/controls/RoutingControl.d.ts +6 -5
  134. package/ol/controls/RoutingControl.d.ts.map +1 -1
  135. package/ol/controls/RoutingControl.js +209 -270
  136. package/ol/controls/RoutingControl.test.d.ts +2 -0
  137. package/ol/controls/RoutingControl.test.d.ts.map +1 -0
  138. package/ol/controls/RoutingControl.test.js +150 -0
  139. package/ol/controls/StopFinderControl.js +9 -32
  140. package/ol/controls/StopFinderControl.test.d.ts +2 -0
  141. package/ol/controls/StopFinderControl.test.d.ts.map +1 -0
  142. package/ol/controls/StopFinderControl.test.js +49 -0
  143. package/ol/controls/index.js +3 -9
  144. package/ol/index.js +5 -21
  145. package/ol/layers/Layer.d.ts +1 -1
  146. package/ol/layers/Layer.d.ts.map +1 -1
  147. package/ol/layers/Layer.js +40 -72
  148. package/ol/layers/Layer.test.d.ts +2 -0
  149. package/ol/layers/Layer.test.d.ts.map +1 -0
  150. package/ol/layers/Layer.test.js +196 -0
  151. package/ol/layers/MapboxLayer.d.ts +7 -7
  152. package/ol/layers/MapboxLayer.d.ts.map +1 -1
  153. package/ol/layers/MapboxLayer.js +30 -66
  154. package/ol/layers/MapboxLayer.test.d.ts +2 -0
  155. package/ol/layers/MapboxLayer.test.d.ts.map +1 -0
  156. package/ol/layers/MapboxLayer.test.js +164 -0
  157. package/ol/layers/MapboxStyleLayer.d.ts +3 -3
  158. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -1
  159. package/ol/layers/MapboxStyleLayer.js +92 -135
  160. package/ol/layers/MapboxStyleLayer.test.d.ts +2 -0
  161. package/ol/layers/MapboxStyleLayer.test.d.ts.map +1 -0
  162. package/ol/layers/MapboxStyleLayer.test.js +232 -0
  163. package/ol/layers/MaplibreLayer.d.ts +1 -1
  164. package/ol/layers/MaplibreLayer.d.ts.map +1 -1
  165. package/ol/layers/MaplibreLayer.js +14 -48
  166. package/ol/layers/RealtimeLayer.d.ts +2 -2
  167. package/ol/layers/RealtimeLayer.d.ts.map +1 -1
  168. package/ol/layers/RealtimeLayer.js +111 -147
  169. package/ol/layers/RealtimeLayer.test.d.ts +2 -0
  170. package/ol/layers/RealtimeLayer.test.d.ts.map +1 -0
  171. package/ol/layers/RealtimeLayer.test.js +71 -0
  172. package/ol/layers/RoutingLayer.d.ts +2 -2
  173. package/ol/layers/RoutingLayer.d.ts.map +1 -1
  174. package/ol/layers/RoutingLayer.js +29 -61
  175. package/ol/layers/RoutingLayer.test.d.ts +2 -0
  176. package/ol/layers/RoutingLayer.test.d.ts.map +1 -0
  177. package/ol/layers/RoutingLayer.test.js +39 -0
  178. package/ol/layers/VectorLayer.d.ts +1 -1
  179. package/ol/layers/VectorLayer.d.ts.map +1 -1
  180. package/ol/layers/VectorLayer.js +14 -48
  181. package/ol/layers/VectorLayer.test.d.ts +2 -0
  182. package/ol/layers/VectorLayer.test.d.ts.map +1 -0
  183. package/ol/layers/VectorLayer.test.js +87 -0
  184. package/ol/layers/WMSLayer.d.ts +1 -1
  185. package/ol/layers/WMSLayer.d.ts.map +1 -1
  186. package/ol/layers/WMSLayer.js +34 -68
  187. package/ol/layers/WMSLayer.test.d.ts +2 -0
  188. package/ol/layers/WMSLayer.test.d.ts.map +1 -0
  189. package/ol/layers/WMSLayer.test.js +66 -0
  190. package/ol/layers/index.js +8 -19
  191. package/ol/styles/fullTrajectoryDelayStyle.js +11 -13
  192. package/ol/styles/fullTrajectoryStyle.js +16 -18
  193. package/ol/styles/index.js +2 -7
  194. package/package.json +5 -2
  195. package/setupTests.js +12 -14
@@ -0,0 +1,89 @@
1
+ import Control from './Control';
2
+ describe('Control', () => {
3
+ test('should be activated by default', () => {
4
+ const control = new Control();
5
+ expect(control.active).toBe(true);
6
+ });
7
+ test('should not be activated if set to false in the options', () => {
8
+ const control = new Control({ active: false });
9
+ expect(control.active).toBe(false);
10
+ });
11
+ test('should call activate/deactivate when active is set to true/false', () => {
12
+ const control = new Control();
13
+ const spy1 = jest.spyOn(control, 'activate');
14
+ const spy2 = jest.spyOn(control, 'deactivate');
15
+ const spy3 = jest.spyOn(control, 'render');
16
+ control.active = false;
17
+ expect(spy1).toBeCalledTimes(0);
18
+ expect(spy2).toBeCalledTimes(1);
19
+ expect(spy3).toBeCalledTimes(1);
20
+ control.active = true;
21
+ expect(spy1).toBeCalledTimes(1);
22
+ expect(spy2).toBeCalledTimes(2);
23
+ expect(spy3).toBeCalledTimes(2);
24
+ });
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');
28
+ const control = new Control({
29
+ element,
30
+ });
31
+ const spy1 = jest.spyOn(control, 'activate');
32
+ const spy2 = jest.spyOn(control, 'deactivate');
33
+ const spy3 = jest.spyOn(control, 'render');
34
+ control.map = {
35
+ getContainer() {
36
+ return target;
37
+ },
38
+ };
39
+ expect(target.childNodes[0]).toBe(element);
40
+ expect(spy1).toBeCalledTimes(1);
41
+ expect(spy2).toBeCalledTimes(2);
42
+ expect(spy3).toBeCalledTimes(1);
43
+ control.detachFromMap();
44
+ expect(target.childNodes[0]).toBe();
45
+ expect(spy1).toBeCalledTimes(1);
46
+ expect(spy2).toBeCalledTimes(3);
47
+ expect(spy3).toBeCalledTimes(2);
48
+ });
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');
52
+ const control = new Control({
53
+ target,
54
+ element,
55
+ });
56
+ const spy1 = jest.spyOn(control, 'activate');
57
+ const spy2 = jest.spyOn(control, 'deactivate');
58
+ const spy3 = jest.spyOn(control, 'render');
59
+ control.map = {};
60
+ expect(target.childNodes[0]).toBe(element);
61
+ expect(spy1).toBeCalledTimes(1);
62
+ expect(spy2).toBeCalledTimes(2);
63
+ expect(spy3).toBeCalledTimes(1);
64
+ control.detachFromMap();
65
+ expect(target.childNodes[0]).toBe();
66
+ expect(spy1).toBeCalledTimes(1);
67
+ expect(spy2).toBeCalledTimes(3);
68
+ expect(spy3).toBeCalledTimes(2);
69
+ });
70
+ test('set a custom render method', () => {
71
+ const spy = jest.fn();
72
+ // eslint-disable-next-line no-unused-vars
73
+ const control = new Control({
74
+ render: spy,
75
+ });
76
+ expect(spy).toBeCalledTimes(1);
77
+ });
78
+ test('pass function params to custom render method', () => {
79
+ const spy = jest.fn();
80
+ // eslint-disable-next-line no-unused-vars
81
+ const control = new Control({
82
+ render: spy,
83
+ });
84
+ control.render('foo', 'bar');
85
+ expect(spy).toBeCalledTimes(2);
86
+ expect(spy.mock.calls[1][0]).toBe('foo');
87
+ expect(spy.mock.calls[1][1]).toBe('bar');
88
+ });
89
+ });
package/common/index.js CHANGED
@@ -1,18 +1,2 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./utils"), exports);
18
- __exportStar(require("./styles"), exports);
1
+ export * from './utils';
2
+ export * from './styles';
@@ -31,19 +31,19 @@ export default class Layer extends BaseObject {
31
31
  * @param {Object} [options.properties={}] Application-specific layer properties.
32
32
  */
33
33
  constructor(options?: {
34
- key?: string;
35
- name?: string;
36
- copyrights?: string[];
37
- children?: Array<Layer>;
38
- visible?: boolean;
39
- disabled?: boolean;
40
- hitTolerance?: number;
41
- properties?: any;
34
+ key?: string | undefined;
35
+ name?: string | undefined;
36
+ copyrights?: string[] | undefined;
37
+ children?: Layer[] | undefined;
38
+ visible?: boolean | undefined;
39
+ disabled?: boolean | undefined;
40
+ hitTolerance?: number | undefined;
41
+ properties?: Object | undefined;
42
42
  });
43
43
  visible: boolean;
44
44
  group: any;
45
- copyrights: string[];
46
- children: Layer[];
45
+ copyrights: string[] | undefined;
46
+ children: Layer[] | undefined;
47
47
  /**
48
48
  * Define layer's properties that needs custom get and set.
49
49
  *
@@ -70,7 +70,7 @@ export default class Layer extends BaseObject {
70
70
  * @param {Object} options Some options. See child classes to see which are supported.
71
71
  * @return {Promise<FeatureInfo>} An empty response.
72
72
  */
73
- getFeatureInfoAtCoordinate(coordinate: any, options: any): Promise<FeatureInfo>;
73
+ getFeatureInfoAtCoordinate(coordinate: any, options: Object): Promise<FeatureInfo>;
74
74
  /**
75
75
  * Return the an array containing all the descendants of the layer in a flat array. Including the current layer.
76
76
  */
@@ -1 +1 @@
1
- {"version":3,"file":"Layer.d.ts","sourceRoot":"","sources":["../../../src/common/layers/Layer.js"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;GAiBG;AACH;IACE;;;;;;;;;;;;OAYG;IACH;QAT4B,GAAG,GAApB,MAAM;QACW,IAAI,GAArB,MAAM;QACa,UAAU,GAA7B,MAAM,EAAE;QACe,QAAQ,GAA/B,MAAM,KAAK,CAAC;QACM,OAAO,GAAzB,OAAO;QACW,QAAQ,GAA1B,OAAO;QACU,YAAY,GAA7B,MAAM;QACW,UAAU;OA+BrC;IAvBC,iBAAuE;IAEvE,WAA0B;IAE1B,qBAAoC;IAEpC,kBAAgC;IAmBlC;;;;OAIG;IACH,qCA0HC;IAED;;;;OAIG;IACH,4BAUC;IARC,cAAc;IACd,SAAc;IAShB;;OAEG;IAEH,sBAGC;IAED;;;;;;;OAOG;IAEH,2DAHY,QAAQ,WAAW,CAAC,CAiB/B;IAED;;OAEG;IACH,cAEC;CACF"}
1
+ {"version":3,"file":"Layer.d.ts","sourceRoot":"","sources":["../../../src/common/layers/Layer.js"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;GAiBG;AACH;IACE;;;;;;;;;;;;OAYG;IACH;QAT4B,GAAG;QACH,IAAI;QACF,UAAU;QACN,QAAQ;QACb,OAAO;QACP,QAAQ;QACT,YAAY;QACZ,UAAU;OA+BrC;IAvBC,iBAAuE;IAEvE,WAA0B;IAE1B,iCAAoC;IAEpC,8BAAgC;IAmBlC;;;;OAIG;IACH,qCA0HC;IAED;;;;OAIG;IACH,4BAUC;IARC,cAAc;IACd,SAAc;IAShB;;OAEG;IAEH,sBAGC;IAED;;;;;;;OAOG;IAEH,qDAJW,MAAM,GACL,QAAQ,WAAW,CAAC,CAiB/B;IAED;;OAEG;IACH,cAEC;CACF"}
@@ -1,34 +1,6 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- var Object_1 = require("ol/Object");
30
- var uuid_1 = require("uuid");
31
- var getLayersAsFlatArray_1 = require("../utils/getLayersAsFlatArray");
1
+ import BaseObject from 'ol/Object';
2
+ import { v4 as uuid } from 'uuid';
3
+ import getLayersAsFlatArray from '../utils/getLayersAsFlatArray';
32
4
  /**
33
5
  * A class representing a layer to display on map.
34
6
  *
@@ -47,8 +19,7 @@ var getLayersAsFlatArray_1 = require("../utils/getLayersAsFlatArray");
47
19
  * @classproperty {Object} properties - Custom properties.
48
20
  * @classproperty {ol/Map~Map|mapboxgl.Map} map - The map where the layer is displayed.
49
21
  */
50
- var Layer = /** @class */ (function (_super) {
51
- __extends(Layer, _super);
22
+ export default class Layer extends BaseObject {
52
23
  /**
53
24
  * Constructor
54
25
  *
@@ -62,40 +33,37 @@ var Layer = /** @class */ (function (_super) {
62
33
  * @param {number} [options.hitTolerance=5] Hit-detection tolerance in css pixels. Pixels inside the radius around the given position will be checked for features.
63
34
  * @param {Object} [options.properties={}] Application-specific layer properties.
64
35
  */
65
- function Layer(options) {
66
- if (options === void 0) { options = {}; }
67
- var _this = _super.call(this) || this;
68
- _this.defineProperties(options);
69
- _this.setProperties(options.properties);
70
- _this.visible = options.visible === undefined ? true : !!options.visible;
71
- _this.group = options.group;
72
- _this.copyrights = options.copyrights;
73
- _this.children = options.children;
36
+ constructor(options = {}) {
37
+ super();
38
+ this.defineProperties(options);
39
+ this.setProperties(options.properties);
40
+ this.visible = options.visible === undefined ? true : !!options.visible;
41
+ this.group = options.group;
42
+ this.copyrights = options.copyrights;
43
+ this.children = options.children;
74
44
  // Listen for group visiblity change
75
45
  // if a layer from a group is newly visible we hide the others.
76
- _this.on("change:visible:group", function (evt) {
46
+ this.on(`change:visible:group`, (evt) => {
77
47
  // We hide layer of the same group
78
- if (_this.group === evt.target.group &&
79
- _this !== evt.target &&
80
- _this.visible) {
81
- _this.visible = false;
48
+ if (this.group === evt.target.group &&
49
+ this !== evt.target &&
50
+ this.visible) {
51
+ this.visible = false;
82
52
  // Propagate event to parent
83
53
  }
84
- else if (_this.children) {
85
- _this.children.forEach(function (child) { return child.dispatchEvent(evt); });
54
+ else if (this.children) {
55
+ this.children.forEach((child) => child.dispatchEvent(evt));
86
56
  }
87
57
  });
88
- return _this;
89
58
  }
90
59
  /**
91
60
  * Define layer's properties that needs custom get and set.
92
61
  *
93
62
  * @ignore
94
63
  */
95
- Layer.prototype.defineProperties = function (options) {
96
- var _this = this;
97
- var _a = __assign({}, options), name = _a.name, key = _a.key, properties = _a.properties, hitTolerance = _a.hitTolerance;
98
- var uid = (0, uuid_1.v4)();
64
+ defineProperties(options) {
65
+ const { name, key, properties, hitTolerance } = Object.assign({}, options);
66
+ const uid = uuid();
99
67
  Object.defineProperties(this, {
100
68
  /* Layer's information properties */
101
69
  name: {
@@ -105,18 +73,18 @@ var Layer = /** @class */ (function (_super) {
105
73
  value: key || uid,
106
74
  },
107
75
  group: {
108
- get: function () { return _this.get('group'); },
109
- set: function (newGroup) {
110
- _this.set('group', newGroup);
76
+ get: () => this.get('group'),
77
+ set: (newGroup) => {
78
+ this.set('group', newGroup);
111
79
  },
112
80
  },
113
81
  copyrights: {
114
- get: function () { return _this.get('copyrights'); },
115
- set: function (newCopyrights) {
116
- var arrValue = newCopyrights && !Array.isArray(newCopyrights)
82
+ get: () => this.get('copyrights'),
83
+ set: (newCopyrights) => {
84
+ const arrValue = newCopyrights && !Array.isArray(newCopyrights)
117
85
  ? [newCopyrights]
118
86
  : newCopyrights;
119
- _this.set('copyrights', arrValue || []);
87
+ this.set('copyrights', arrValue || []);
120
88
  },
121
89
  },
122
90
  // options is used for clone function.
@@ -128,47 +96,47 @@ var Layer = /** @class */ (function (_super) {
128
96
  },
129
97
  /* Layer's state properties */
130
98
  visible: {
131
- get: function () { return _this.get('visible'); },
132
- set: function (newVisible) {
133
- if (newVisible === _this.visible) {
99
+ get: () => this.get('visible'),
100
+ set: (newVisible) => {
101
+ if (newVisible === this.visible) {
134
102
  return;
135
103
  }
136
- _this.set('visible', newVisible);
137
- if (_this.visible) {
138
- if (_this.parent && !_this.parent.visible) {
139
- _this.parent.visible = true;
104
+ this.set('visible', newVisible);
105
+ if (this.visible) {
106
+ if (this.parent && !this.parent.visible) {
107
+ this.parent.visible = true;
140
108
  }
141
- if (_this.children && _this.children.find(function (child) { return child.group; })) {
142
- var child = _this.children.find(function (childd) { return !!childd.group; });
109
+ if (this.children && this.children.find((child) => child.group)) {
110
+ const child = this.children.find((childd) => !!childd.group);
143
111
  // Make visible only radioGroup layers
144
112
  child.visible = true;
145
113
  }
146
114
  // Warn the same group that a new layer is visible
147
- if (_this.parent && _this.group) {
115
+ if (this.parent && this.group) {
148
116
  // We search for the higher parent then it will dispatch to all the tree.
149
- var higherParent = _this.parent;
117
+ let higherParent = this.parent;
150
118
  while (higherParent.parent) {
151
119
  higherParent = higherParent.parent;
152
120
  }
153
121
  higherParent.dispatchEvent({
154
- type: "change:visible:group",
155
- target: _this,
122
+ type: `change:visible:group`,
123
+ target: this,
156
124
  });
157
125
  }
158
126
  }
159
- else if (!_this.visible) {
160
- if (_this.parent &&
161
- _this.parent.visible &&
162
- !_this.parent.children.find(function (child) { return child.visible; })) {
163
- _this.parent.visible = false;
127
+ else if (!this.visible) {
128
+ if (this.parent &&
129
+ this.parent.visible &&
130
+ !this.parent.children.find((child) => child.visible)) {
131
+ this.parent.visible = false;
164
132
  }
165
133
  }
166
134
  },
167
135
  },
168
136
  disabled: {
169
- get: function () { return _this.get('disabled'); },
170
- set: function (newValue) {
171
- _this.set('disabled', newValue);
137
+ get: () => this.get('disabled'),
138
+ set: (newValue) => {
139
+ this.set('disabled', newValue);
172
140
  },
173
141
  },
174
142
  /* Layer's hierarchy properties */
@@ -177,19 +145,19 @@ var Layer = /** @class */ (function (_super) {
177
145
  writable: true,
178
146
  },
179
147
  children: {
180
- get: function () { return _this.get('children') || []; },
181
- set: function (newValue) {
182
- (_this.children || []).forEach(function (child) {
148
+ get: () => this.get('children') || [],
149
+ set: (newValue) => {
150
+ (this.children || []).forEach((child) => {
183
151
  // eslint-disable-next-line no-param-reassign
184
152
  child.parent = null;
185
153
  });
186
154
  if (Array.isArray(newValue)) {
187
- newValue.forEach(function (child) {
155
+ newValue.forEach((child) => {
188
156
  // eslint-disable-next-line no-param-reassign
189
- child.parent = _this;
157
+ child.parent = this;
190
158
  });
191
159
  }
192
- _this.set('children', newValue || []);
160
+ this.set('children', newValue || []);
193
161
  },
194
162
  },
195
163
  /* Layer's query properties */
@@ -199,33 +167,33 @@ var Layer = /** @class */ (function (_super) {
199
167
  },
200
168
  /* Custom app specific properties */
201
169
  properties: {
202
- value: __assign({}, (properties || {})),
170
+ value: Object.assign({}, (properties || {})),
203
171
  },
204
172
  });
205
- };
173
+ }
206
174
  /**
207
175
  * Initialize the layer with the map passed in parameters.
208
176
  *
209
177
  * @param {ol/Map~Map|mapboxgl.Map} map A map.
210
178
  */
211
- Layer.prototype.attachToMap = function (map) {
179
+ attachToMap(map) {
212
180
  this.detachFromMap();
213
181
  /** @ignore */
214
182
  this.map = map;
215
183
  if (this.children) {
216
- this.children.forEach(function (child) {
184
+ this.children.forEach((child) => {
217
185
  child.attachToMap(map);
218
186
  });
219
187
  }
220
- };
188
+ }
221
189
  /**
222
190
  * Terminate what was initialized in init function. Remove layer, events...
223
191
  */
224
192
  // eslint-disable-next-line class-methods-use-this
225
- Layer.prototype.detachFromMap = function () {
193
+ detachFromMap() {
226
194
  /** @ignore */
227
195
  this.map = null;
228
- };
196
+ }
229
197
  /**
230
198
  * Request feature information for a given coordinate.
231
199
  * This function must be implemented by inheriting layers.
@@ -235,7 +203,7 @@ var Layer = /** @class */ (function (_super) {
235
203
  * @return {Promise<FeatureInfo>} An empty response.
236
204
  */
237
205
  // eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars
238
- Layer.prototype.getFeatureInfoAtCoordinate = function (coordinate, options) {
206
+ getFeatureInfoAtCoordinate(coordinate, options) {
239
207
  // eslint-disable-next-line no-console
240
208
  console.error('getFeatureInfoAtCoordinate must be implemented by inheriting layers', this.key);
241
209
  // This layer returns no feature info.
@@ -243,15 +211,13 @@ var Layer = /** @class */ (function (_super) {
243
211
  return Promise.resolve({
244
212
  layer: this,
245
213
  features: [],
246
- coordinate: coordinate,
214
+ coordinate,
247
215
  });
248
- };
216
+ }
249
217
  /**
250
218
  * Return the an array containing all the descendants of the layer in a flat array. Including the current layer.
251
219
  */
252
- Layer.prototype.flat = function () {
253
- return (0, getLayersAsFlatArray_1.default)(this);
254
- };
255
- return Layer;
256
- }(Object_1.default));
257
- exports.default = Layer;
220
+ flat() {
221
+ return getLayersAsFlatArray(this);
222
+ }
223
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Layer.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Layer.test.d.ts","sourceRoot":"","sources":["../../../src/common/layers/Layer.test.js"],"names":[],"mappings":""}
@@ -0,0 +1,137 @@
1
+ import VectorLayer from 'ol/layer/Vector';
2
+ import VectorSource from 'ol/source/Vector';
3
+ import Layer from './Layer';
4
+ const olLayer = new VectorLayer({ source: new VectorSource() });
5
+ describe('Layer', () => {
6
+ test('should initialize.', () => {
7
+ const layer = new Layer({ name: 'Layer', olLayer });
8
+ expect(layer).toBeInstanceOf(Layer);
9
+ });
10
+ test('should define default properties.', () => {
11
+ const options = {
12
+ name: 'Layer',
13
+ key: 'Layerkey',
14
+ olLayer,
15
+ };
16
+ const layer = new Layer(options);
17
+ expect(layer).toBeInstanceOf(Layer);
18
+ expect(layer.options).toEqual(options);
19
+ expect(layer.name).toEqual(options.name);
20
+ expect(layer.key).toEqual('Layerkey');
21
+ expect(layer.hitTolerance).toBe(5);
22
+ expect(layer.copyrights).toEqual([]);
23
+ expect(layer.visible).toBe(true);
24
+ expect(layer.properties).toEqual({});
25
+ expect(layer.map).toBe(undefined);
26
+ expect(layer.group).toBe(undefined);
27
+ });
28
+ test('should be visible by default.', () => {
29
+ const layer = new Layer();
30
+ expect(layer.visible).toBe(true);
31
+ });
32
+ test('should be hidden using constructor.', () => {
33
+ const layer = new Layer({ visible: false });
34
+ expect(layer.visible).toBe(false);
35
+ });
36
+ test('should be hidden using setter.', () => {
37
+ const layer = new Layer();
38
+ layer.visible = false;
39
+ expect(layer.visible).toBe(false);
40
+ });
41
+ test('should visibility stay unchanged', () => {
42
+ const layer = new Layer();
43
+ layer.visible = false;
44
+ expect(layer.visible).toBe(false);
45
+ });
46
+ test('should return its name.', () => {
47
+ const layer = new Layer({ name: 'Layer' });
48
+ expect(layer.name).toEqual('Layer');
49
+ });
50
+ test('should called detachFromMap on initialization.', () => {
51
+ const layer = new Layer();
52
+ const spy = jest.spyOn(layer, 'detachFromMap');
53
+ layer.attachToMap();
54
+ expect(spy).toHaveBeenCalledTimes(1);
55
+ });
56
+ test('should properties correctly set and get.', () => {
57
+ const layer = new Layer({
58
+ name: 'Layer',
59
+ olLayer,
60
+ properties: {
61
+ abc: 'foo',
62
+ },
63
+ });
64
+ expect(layer).toBeInstanceOf(Layer);
65
+ expect(layer.get('abc')).toEqual('foo');
66
+ layer.set('abc', 'bar');
67
+ expect(layer.get('abc')).toEqual('bar');
68
+ });
69
+ test('should set children', () => {
70
+ const layer = new Layer({
71
+ children: [new Layer(), new Layer()],
72
+ });
73
+ expect(layer.children.length).toBe(2);
74
+ expect(layer.children[0].parent).toBe(layer);
75
+ expect(layer.children[1].parent).toBe(layer);
76
+ });
77
+ test('should initialize copyrights property.', () => {
78
+ const layer = new Layer({
79
+ name: 'Layer',
80
+ olLayer,
81
+ copyrights: ['&copy: copyright', 'another copyright'],
82
+ });
83
+ expect(layer.copyrights[0]).toEqual('&copy: copyright');
84
+ });
85
+ test('should set and get copyright property.', () => {
86
+ const layer = new Layer({
87
+ name: 'Layer',
88
+ olLayer,
89
+ });
90
+ expect(layer).toBeInstanceOf(Layer);
91
+ expect(layer.copyright).toEqual(undefined);
92
+ layer.copyright = '&copy; OSM Contributors';
93
+ expect(layer.copyright).toEqual('&copy; OSM Contributors');
94
+ });
95
+ describe('#attachToMap()', () => {
96
+ test('should set map.', () => {
97
+ const layer = new Layer({
98
+ name: 'Layer',
99
+ olLayer,
100
+ });
101
+ expect(layer.map).toBe(undefined);
102
+ const obj = {};
103
+ layer.attachToMap(obj);
104
+ expect(layer.map).toBe(obj);
105
+ });
106
+ test('should call terminate.', () => {
107
+ const layer = new Layer({
108
+ name: 'Layer',
109
+ olLayer,
110
+ });
111
+ const spy = jest.spyOn(layer, 'detachFromMap');
112
+ layer.attachToMap();
113
+ expect(spy).toHaveBeenCalledTimes(1);
114
+ });
115
+ });
116
+ describe('#getFeatureInfoAtCoordinate()', () => {
117
+ test('return an empty fetaureInfo object and display an error message', (done) => {
118
+ // eslint-disable-next-line no-console
119
+ console.error = jest.fn();
120
+ const layer = new Layer({
121
+ name: 'Layer',
122
+ olLayer,
123
+ });
124
+ const coord = [0, 0];
125
+ layer.getFeatureInfoAtCoordinate(coord).then((featureInfo) => {
126
+ expect(featureInfo.features).toEqual([]);
127
+ expect(featureInfo.layer).toEqual(layer);
128
+ expect(featureInfo.coordinate).toEqual(coord);
129
+ // eslint-disable-next-line no-console
130
+ expect(console.error).toHaveBeenCalledTimes(1);
131
+ done();
132
+ // eslint-disable-next-line no-console
133
+ console.error.mockRestore();
134
+ });
135
+ });
136
+ });
137
+ });