maplair 4.0.0-alpha.1

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 (262) hide show
  1. package/CHANGELOG.md +2804 -0
  2. package/LICENSE +27 -0
  3. package/README.md +40 -0
  4. package/dist/esm/Maplair.js +71 -0
  5. package/dist/esm/Maplair.js.map +1 -0
  6. package/dist/esm/_virtual/_rolldown/runtime.js +13 -0
  7. package/dist/esm/control/AttributionControl.js +79 -0
  8. package/dist/esm/control/AttributionControl.js.map +1 -0
  9. package/dist/esm/control/Control.js +81 -0
  10. package/dist/esm/control/Control.js.map +1 -0
  11. package/dist/esm/control/LayersControl.js +220 -0
  12. package/dist/esm/control/LayersControl.js.map +1 -0
  13. package/dist/esm/control/ScaleControl.js +72 -0
  14. package/dist/esm/control/ScaleControl.js.map +1 -0
  15. package/dist/esm/control/ZoomControl.js +86 -0
  16. package/dist/esm/control/ZoomControl.js.map +1 -0
  17. package/dist/esm/core/Browser.js +31 -0
  18. package/dist/esm/core/Browser.js.map +1 -0
  19. package/dist/esm/core/Class.js +52 -0
  20. package/dist/esm/core/Class.js.map +1 -0
  21. package/dist/esm/core/Events.js +149 -0
  22. package/dist/esm/core/Events.js.map +1 -0
  23. package/dist/esm/core/Handler.js +32 -0
  24. package/dist/esm/core/Handler.js.map +1 -0
  25. package/dist/esm/core/I18n.js +36 -0
  26. package/dist/esm/core/I18n.js.map +1 -0
  27. package/dist/esm/core/Util.js +71 -0
  28. package/dist/esm/core/Util.js.map +1 -0
  29. package/dist/esm/dom/DomEvent.DoubleTap.js +74 -0
  30. package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -0
  31. package/dist/esm/dom/DomEvent.PointerEvents.js +46 -0
  32. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -0
  33. package/dist/esm/dom/DomEvent.js +135 -0
  34. package/dist/esm/dom/DomEvent.js.map +1 -0
  35. package/dist/esm/dom/DomUtil.js +107 -0
  36. package/dist/esm/dom/DomUtil.js.map +1 -0
  37. package/dist/esm/dom/Draggable.js +107 -0
  38. package/dist/esm/dom/Draggable.js.map +1 -0
  39. package/dist/esm/dom/PosAnimation.js +56 -0
  40. package/dist/esm/dom/PosAnimation.js.map +1 -0
  41. package/dist/esm/geo/LatLng.js +65 -0
  42. package/dist/esm/geo/LatLng.js.map +1 -0
  43. package/dist/esm/geo/LatLngBounds.js +114 -0
  44. package/dist/esm/geo/LatLngBounds.js.map +1 -0
  45. package/dist/esm/geo/crs/CRS.js +51 -0
  46. package/dist/esm/geo/crs/CRS.js.map +1 -0
  47. package/dist/esm/geo/crs/EPSG3395.js +16 -0
  48. package/dist/esm/geo/crs/EPSG3395.js.map +1 -0
  49. package/dist/esm/geo/crs/EPSG3857.js +19 -0
  50. package/dist/esm/geo/crs/EPSG3857.js.map +1 -0
  51. package/dist/esm/geo/crs/EPSG4326.js +13 -0
  52. package/dist/esm/geo/crs/EPSG4326.js.map +1 -0
  53. package/dist/esm/geo/crs/EarthCRS.js +14 -0
  54. package/dist/esm/geo/crs/EarthCRS.js.map +1 -0
  55. package/dist/esm/geo/crs/SimpleCRS.js +23 -0
  56. package/dist/esm/geo/crs/SimpleCRS.js.map +1 -0
  57. package/dist/esm/geo/projection/Projection.LonLat.js +19 -0
  58. package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -0
  59. package/dist/esm/geo/projection/Projection.Mercator.js +33 -0
  60. package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -0
  61. package/dist/esm/geo/projection/Projection.SphericalMercator.js +27 -0
  62. package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -0
  63. package/dist/esm/geo/projection/index.js +14 -0
  64. package/dist/esm/geo/projection/index.js.map +1 -0
  65. package/dist/esm/geometry/Bounds.js +91 -0
  66. package/dist/esm/geometry/Bounds.js.map +1 -0
  67. package/dist/esm/geometry/LineUtil.js +169 -0
  68. package/dist/esm/geometry/LineUtil.js.map +1 -0
  69. package/dist/esm/geometry/Point.js +119 -0
  70. package/dist/esm/geometry/Point.js.map +1 -0
  71. package/dist/esm/geometry/PolyUtil.js +89 -0
  72. package/dist/esm/geometry/PolyUtil.js.map +1 -0
  73. package/dist/esm/geometry/Transformation.js +34 -0
  74. package/dist/esm/geometry/Transformation.js.map +1 -0
  75. package/dist/esm/layer/BlanketOverlay.js +87 -0
  76. package/dist/esm/layer/BlanketOverlay.js.map +1 -0
  77. package/dist/esm/layer/DivOverlay.js +192 -0
  78. package/dist/esm/layer/DivOverlay.js.map +1 -0
  79. package/dist/esm/layer/FeatureGroup.js +40 -0
  80. package/dist/esm/layer/FeatureGroup.js.map +1 -0
  81. package/dist/esm/layer/GeoJSON.js +192 -0
  82. package/dist/esm/layer/GeoJSON.js.map +1 -0
  83. package/dist/esm/layer/ImageOverlay.js +140 -0
  84. package/dist/esm/layer/ImageOverlay.js.map +1 -0
  85. package/dist/esm/layer/Layer.js +119 -0
  86. package/dist/esm/layer/Layer.js.map +1 -0
  87. package/dist/esm/layer/LayerGroup.js +58 -0
  88. package/dist/esm/layer/LayerGroup.js.map +1 -0
  89. package/dist/esm/layer/Popup.js +224 -0
  90. package/dist/esm/layer/Popup.js.map +1 -0
  91. package/dist/esm/layer/SVGOverlay.js +21 -0
  92. package/dist/esm/layer/SVGOverlay.js.map +1 -0
  93. package/dist/esm/layer/Tooltip.js +228 -0
  94. package/dist/esm/layer/Tooltip.js.map +1 -0
  95. package/dist/esm/layer/VideoOverlay.js +54 -0
  96. package/dist/esm/layer/VideoOverlay.js.map +1 -0
  97. package/dist/esm/layer/marker/DefaultIcon.js +50 -0
  98. package/dist/esm/layer/marker/DefaultIcon.js.map +1 -0
  99. package/dist/esm/layer/marker/DivIcon.js +37 -0
  100. package/dist/esm/layer/marker/DivIcon.js.map +1 -0
  101. package/dist/esm/layer/marker/Icon.js +65 -0
  102. package/dist/esm/layer/marker/Icon.js.map +1 -0
  103. package/dist/esm/layer/marker/Marker.Drag.js +77 -0
  104. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -0
  105. package/dist/esm/layer/marker/Marker.js +235 -0
  106. package/dist/esm/layer/marker/Marker.js.map +1 -0
  107. package/dist/esm/layer/tile/GridLayer.js +447 -0
  108. package/dist/esm/layer/tile/GridLayer.js.map +1 -0
  109. package/dist/esm/layer/tile/TileLayer.js +139 -0
  110. package/dist/esm/layer/tile/TileLayer.js.map +1 -0
  111. package/dist/esm/layer/tile/WMSTileLayer.js +68 -0
  112. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -0
  113. package/dist/esm/layer/vector/Canvas.js +292 -0
  114. package/dist/esm/layer/vector/Canvas.js.map +1 -0
  115. package/dist/esm/layer/vector/Circle.js +35 -0
  116. package/dist/esm/layer/vector/Circle.js.map +1 -0
  117. package/dist/esm/layer/vector/CircleMarker.js +69 -0
  118. package/dist/esm/layer/vector/CircleMarker.js.map +1 -0
  119. package/dist/esm/layer/vector/Path.js +68 -0
  120. package/dist/esm/layer/vector/Path.js.map +1 -0
  121. package/dist/esm/layer/vector/Polygon.js +70 -0
  122. package/dist/esm/layer/vector/Polygon.js.map +1 -0
  123. package/dist/esm/layer/vector/Polyline.js +152 -0
  124. package/dist/esm/layer/vector/Polyline.js.map +1 -0
  125. package/dist/esm/layer/vector/Rectangle.js +25 -0
  126. package/dist/esm/layer/vector/Rectangle.js.map +1 -0
  127. package/dist/esm/layer/vector/Renderer.getRenderer.js +31 -0
  128. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -0
  129. package/dist/esm/layer/vector/Renderer.js +39 -0
  130. package/dist/esm/layer/vector/Renderer.js.map +1 -0
  131. package/dist/esm/layer/vector/SVG.js +115 -0
  132. package/dist/esm/layer/vector/SVG.js.map +1 -0
  133. package/dist/esm/map/Map.js +844 -0
  134. package/dist/esm/map/Map.js.map +1 -0
  135. package/dist/esm/map/handler/BoxZoomHandler.js +105 -0
  136. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -0
  137. package/dist/esm/map/handler/DoubleClickZoomHandler.js +27 -0
  138. package/dist/esm/map/handler/DoubleClickZoomHandler.js.map +1 -0
  139. package/dist/esm/map/handler/DragHandler.js +133 -0
  140. package/dist/esm/map/handler/DragHandler.js.map +1 -0
  141. package/dist/esm/map/handler/KeyboardHandler.js +117 -0
  142. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -0
  143. package/dist/esm/map/handler/PinchZoomHandler.js +96 -0
  144. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -0
  145. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +50 -0
  146. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -0
  147. package/dist/esm/map/handler/TapHoldHandler.js +77 -0
  148. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -0
  149. package/dist/esm/package.js +6 -0
  150. package/dist/esm/package.js.map +1 -0
  151. package/dist/images/layers.svg +1 -0
  152. package/dist/images/logo.svg +1 -0
  153. package/dist/images/marker-icon.svg +1 -0
  154. package/dist/images/marker-shadow.svg +1 -0
  155. package/dist/maplair-global-src.js +6810 -0
  156. package/dist/maplair-global-src.js.map +1 -0
  157. package/dist/maplair-global.js +12 -0
  158. package/dist/maplair-global.js.map +1 -0
  159. package/dist/maplair-src.js +6704 -0
  160. package/dist/maplair-src.js.map +1 -0
  161. package/dist/maplair.css +730 -0
  162. package/dist/maplair.js +7 -0
  163. package/dist/maplair.js.map +1 -0
  164. package/i18n/cs.json +12 -0
  165. package/i18n/de.json +12 -0
  166. package/i18n/en.json +12 -0
  167. package/i18n/es.json +12 -0
  168. package/i18n/fr.json +12 -0
  169. package/i18n/hu.json +12 -0
  170. package/i18n/it.json +12 -0
  171. package/i18n/rm.json +12 -0
  172. package/i18n/sk.json +12 -0
  173. package/i18n/sl.json +12 -0
  174. package/package.json +87 -0
  175. package/src/Maplair.js +24 -0
  176. package/src/control/AttributionControl.js +147 -0
  177. package/src/control/Control.js +174 -0
  178. package/src/control/LayersControl.js +444 -0
  179. package/src/control/ScaleControl.js +141 -0
  180. package/src/control/ZoomControl.js +147 -0
  181. package/src/control/index.js +5 -0
  182. package/src/core/Browser.js +75 -0
  183. package/src/core/Class.js +97 -0
  184. package/src/core/Events.js +334 -0
  185. package/src/core/Handler.js +59 -0
  186. package/src/core/I18n.js +76 -0
  187. package/src/core/Util.js +122 -0
  188. package/src/core/index.js +8 -0
  189. package/src/dom/DomEvent.DoubleTap.js +120 -0
  190. package/src/dom/DomEvent.PointerEvents.js +75 -0
  191. package/src/dom/DomEvent.js +282 -0
  192. package/src/dom/DomUtil.js +175 -0
  193. package/src/dom/Draggable.js +204 -0
  194. package/src/dom/PosAnimation.js +115 -0
  195. package/src/dom/index.js +9 -0
  196. package/src/geo/LatLng.js +163 -0
  197. package/src/geo/LatLngBounds.js +260 -0
  198. package/src/geo/crs/CRS.js +143 -0
  199. package/src/geo/crs/EPSG3395.js +19 -0
  200. package/src/geo/crs/EPSG3857.js +26 -0
  201. package/src/geo/crs/EPSG4326.js +21 -0
  202. package/src/geo/crs/EarthCRS.js +32 -0
  203. package/src/geo/crs/SimpleCRS.js +35 -0
  204. package/src/geo/crs/index.js +6 -0
  205. package/src/geo/index.js +7 -0
  206. package/src/geo/projection/Projection.LonLat.js +30 -0
  207. package/src/geo/projection/Projection.Mercator.js +53 -0
  208. package/src/geo/projection/Projection.SphericalMercator.js +46 -0
  209. package/src/geo/projection/index.js +26 -0
  210. package/src/geometry/Bounds.js +222 -0
  211. package/src/geometry/LineUtil.js +299 -0
  212. package/src/geometry/Point.js +239 -0
  213. package/src/geometry/PolyUtil.js +129 -0
  214. package/src/geometry/Transformation.js +67 -0
  215. package/src/geometry/index.js +8 -0
  216. package/src/images/layers.svg +1 -0
  217. package/src/images/logo.svg +1 -0
  218. package/src/images/marker-icon.svg +1 -0
  219. package/src/images/marker-shadow.svg +1 -0
  220. package/src/layer/BlanketOverlay.js +171 -0
  221. package/src/layer/DivOverlay.js +353 -0
  222. package/src/layer/FeatureGroup.js +93 -0
  223. package/src/layer/GeoJSON.js +432 -0
  224. package/src/layer/ImageOverlay.js +279 -0
  225. package/src/layer/Layer.js +273 -0
  226. package/src/layer/LayerGroup.js +129 -0
  227. package/src/layer/Popup.js +528 -0
  228. package/src/layer/SVGOverlay.js +47 -0
  229. package/src/layer/Tooltip.js +469 -0
  230. package/src/layer/VideoOverlay.js +111 -0
  231. package/src/layer/index.js +18 -0
  232. package/src/layer/marker/DefaultIcon.js +79 -0
  233. package/src/layer/marker/DivIcon.js +76 -0
  234. package/src/layer/marker/Icon.js +164 -0
  235. package/src/layer/marker/Marker.Drag.js +161 -0
  236. package/src/layer/marker/Marker.js +465 -0
  237. package/src/layer/marker/index.js +5 -0
  238. package/src/layer/tile/GridLayer.js +901 -0
  239. package/src/layer/tile/TileLayer.js +299 -0
  240. package/src/layer/tile/WMSTileLayer.js +133 -0
  241. package/src/layer/tile/index.js +3 -0
  242. package/src/layer/vector/Canvas.js +479 -0
  243. package/src/layer/vector/Circle.js +87 -0
  244. package/src/layer/vector/CircleMarker.js +110 -0
  245. package/src/layer/vector/Path.js +145 -0
  246. package/src/layer/vector/Polygon.js +160 -0
  247. package/src/layer/vector/Polyline.js +296 -0
  248. package/src/layer/vector/Rectangle.js +52 -0
  249. package/src/layer/vector/Renderer.getRenderer.js +48 -0
  250. package/src/layer/vector/Renderer.js +75 -0
  251. package/src/layer/vector/SVG.js +216 -0
  252. package/src/layer/vector/index.js +10 -0
  253. package/src/map/Map.js +1830 -0
  254. package/src/map/handler/BoxZoomHandler.js +156 -0
  255. package/src/map/handler/DoubleClickZoomHandler.js +62 -0
  256. package/src/map/handler/DragHandler.js +240 -0
  257. package/src/map/handler/KeyboardHandler.js +188 -0
  258. package/src/map/handler/PinchZoomHandler.js +157 -0
  259. package/src/map/handler/ScrollWheelZoomHandler.js +98 -0
  260. package/src/map/handler/TapHoldHandler.js +111 -0
  261. package/src/map/index.js +8 -0
  262. package/src/maplair.css +730 -0
@@ -0,0 +1,282 @@
1
+ import {Point} from '../geometry/Point.js';
2
+ import * as Util from '../core/Util.js';
3
+ import Browser from '../core/Browser.js';
4
+ import {addDoubleTapListener, removeDoubleTapListener} from './DomEvent.DoubleTap.js';
5
+ import {getScale} from './DomUtil.js';
6
+
7
+ import * as PointerEvents from './DomEvent.PointerEvents.js';
8
+ export {PointerEvents};
9
+
10
+ /*
11
+ * @namespace DomEvent
12
+ * Utility functions to work with the [DOM events](https://developer.mozilla.org/docs/Web/API/Event), used by Maplair internally.
13
+ */
14
+
15
+ // Inspired by John Resig, Dean Edwards and YUI addEvent implementations.
16
+
17
+ // @function on(el: HTMLElement, types: String, fn: Function, context?: Object): this
18
+ // Adds a listener function (`fn`) to a particular DOM event type of the
19
+ // element `el`. You can optionally specify the context of the listener
20
+ // (object the `this` keyword will point to). You can also pass several
21
+ // space-separated types (e.g. `'click dblclick'`).
22
+
23
+ // @alternative
24
+ // @function on(el: HTMLElement, eventMap: Object, context?: Object): this
25
+ // Adds a set of type/listener pairs, e.g. `{click: onClick, pointermove: onPointerMove}`
26
+ export function on(obj, types, fn, context) {
27
+
28
+ if (types && typeof types === 'object') {
29
+ for (const [type, listener] of Object.entries(types)) {
30
+ addOne(obj, type, listener, fn);
31
+ }
32
+ } else {
33
+ for (const type of Util.splitWords(types)) {
34
+ addOne(obj, type, fn, context);
35
+ }
36
+ }
37
+
38
+ return this;
39
+ }
40
+
41
+ const eventsKey = '_maplair_events';
42
+
43
+ // @function off(el: HTMLElement, types: String, fn: Function, context?: Object): this
44
+ // Removes a previously added listener function.
45
+ // Note that if you passed a custom context to on, you must pass the same
46
+ // context to `off` in order to remove the listener.
47
+
48
+ // @alternative
49
+ // @function off(el: HTMLElement, eventMap: Object, context?: Object): this
50
+ // Removes a set of type/listener pairs, e.g. `{click: onClick, pointermove: onPointerMove}`
51
+
52
+ // @alternative
53
+ // @function off(el: HTMLElement, types: String): this
54
+ // Removes all previously added listeners of given types.
55
+
56
+ // @alternative
57
+ // @function off(el: HTMLElement): this
58
+ // Removes all previously added listeners from given HTMLElement
59
+ export function off(obj, types, fn, context) {
60
+
61
+ if (arguments.length === 1) {
62
+ batchRemove(obj);
63
+ delete obj[eventsKey];
64
+
65
+ } else if (types && typeof types === 'object') {
66
+ for (const [type, listener] of Object.entries(types)) {
67
+ removeOne(obj, type, listener, fn);
68
+ }
69
+
70
+ } else {
71
+ types = Util.splitWords(types);
72
+
73
+ if (arguments.length === 2) {
74
+ batchRemove(obj, type => types.includes(type));
75
+ } else {
76
+ for (const type of types) {
77
+ removeOne(obj, type, fn, context);
78
+ }
79
+ }
80
+ }
81
+
82
+ return this;
83
+ }
84
+
85
+ const DIGIT_RE = /\d/;
86
+
87
+ function batchRemove(obj, filterFn) {
88
+ for (const id of Object.keys(obj[eventsKey] ?? {})) {
89
+ const type = id.split(DIGIT_RE)[0];
90
+ if (!filterFn || filterFn(type)) {
91
+ removeOne(obj, type, null, null, id);
92
+ }
93
+ }
94
+ }
95
+
96
+ const pointerSubst = {
97
+ pointerenter: 'pointerover',
98
+ pointerleave: 'pointerout',
99
+ wheel: typeof window === 'undefined' ? false : !('onwheel' in window) && 'mousewheel'
100
+ };
101
+
102
+ function addOne(obj, type, fn, context) {
103
+ const id = type + Util.stamp(fn) + (context ? `_${Util.stamp(context)}` : '');
104
+
105
+ if (obj[eventsKey] && obj[eventsKey][id]) { return this; }
106
+
107
+ let handler = function (e) {
108
+ return fn.call(context || obj, e);
109
+ };
110
+
111
+ const originalHandler = handler;
112
+
113
+ if (Browser.touch && (type === 'dblclick')) {
114
+ handler = addDoubleTapListener(obj, handler);
115
+
116
+ } else if ('addEventListener' in obj) {
117
+
118
+ if (type === 'wheel' || type === 'mousewheel') {
119
+ obj.addEventListener(pointerSubst[type] || type, handler, {passive: false});
120
+ } else if (type === 'pointerenter' || type === 'pointerleave') {
121
+ handler = function (e) {
122
+ if (isExternalTarget(obj, e)) {
123
+ originalHandler(e);
124
+ }
125
+ };
126
+ obj.addEventListener(pointerSubst[type], handler, false);
127
+
128
+ } else {
129
+ obj.addEventListener(type, originalHandler, false);
130
+ }
131
+
132
+ } else {
133
+ obj.attachEvent(`on${type}`, handler);
134
+ }
135
+
136
+ obj[eventsKey] ??= {};
137
+ obj[eventsKey][id] = handler;
138
+ }
139
+
140
+ function removeOne(obj, type, fn, context, id) {
141
+ id ??= type + Util.stamp(fn) + (context ? `_${Util.stamp(context)}` : '');
142
+ const handler = obj[eventsKey] && obj[eventsKey][id];
143
+
144
+ if (!handler) { return this; }
145
+
146
+ if (Browser.touch && (type === 'dblclick')) {
147
+ removeDoubleTapListener(obj, handler);
148
+
149
+ } else if ('removeEventListener' in obj) {
150
+
151
+ obj.removeEventListener(pointerSubst[type] || type, handler, false);
152
+
153
+ } else {
154
+ obj.detachEvent(`on${type}`, handler);
155
+ }
156
+
157
+ obj[eventsKey][id] = null;
158
+ }
159
+
160
+ // @function stopPropagation(ev: DOMEvent): this
161
+ // Stop the given event from propagation to parent elements. Used inside the listener functions:
162
+ // ```js
163
+ // DomEvent.on(div, 'click', DomEvent.stopPropagation);
164
+ // ```
165
+ export function stopPropagation(e) {
166
+
167
+ // feature detection rather than `instanceof Event`, which fails for events from other windows (e.g. iframes)
168
+ if (e.stopPropagation) {
169
+ e.stopPropagation();
170
+ } else if (e.originalEvent) { // In case of Maplair event.
171
+ e.originalEvent._stopped = true;
172
+ } else {
173
+ // custom Maplair event
174
+ console.debug('Unsupported event', e);
175
+ }
176
+
177
+ return this;
178
+ }
179
+
180
+ // @function disableScrollPropagation(el: HTMLElement): this
181
+ // Adds `stopPropagation` to the element's `'wheel'` events (plus browser variants).
182
+ export function disableScrollPropagation(el) {
183
+ addOne(el, 'wheel', stopPropagation);
184
+ return this;
185
+ }
186
+
187
+ // @function disableClickPropagation(el: HTMLElement): this
188
+ // Adds `stopPropagation` to the element's `'dblclick'`, `'contextmenu'`
189
+ // and `'pointerdown'` events (plus browser variants).
190
+ // For `'click'` events, prevents the map from handling the click
191
+ // while still allowing the native DOM click event to propagate normally.
192
+ export function disableClickPropagation(el) {
193
+ on(el, 'pointerdown dblclick contextmenu', stopPropagation);
194
+ el['_maplair_disable_click'] = true;
195
+ return this;
196
+ }
197
+
198
+ // @function preventDefault(ev: DOMEvent): this
199
+ // Prevents the default action of the DOM Event `ev` from happening (such as
200
+ // following a link in the href of the a element, or doing a POST request
201
+ // with page reload when a `<form>` is submitted).
202
+ // Use it inside listener functions.
203
+ export function preventDefault(e) {
204
+ e.preventDefault?.();
205
+ return this;
206
+ }
207
+
208
+ // @function stop(ev: DOMEvent): this
209
+ // Does `stopPropagation` and `preventDefault` at the same time.
210
+ export function stop(e) {
211
+ preventDefault(e);
212
+ stopPropagation(e);
213
+ return this;
214
+ }
215
+
216
+ // @function getPropagationPath(ev: DOMEvent): Array
217
+ // Returns an array containing the `HTMLElement`s that the given DOM event
218
+ // should propagate to (if not stopped).
219
+ export function getPropagationPath(ev) {
220
+ return ev.composedPath();
221
+ }
222
+
223
+
224
+ // @function getPointerPosition(ev: DOMEvent, container?: HTMLElement): Point
225
+ // Gets normalized pointer position from a DOM event relative to the
226
+ // `container` (border excluded) or to the whole page if not specified.
227
+ export function getPointerPosition(e, container) {
228
+ if (!container) {
229
+ return new Point(e.clientX, e.clientY);
230
+ }
231
+
232
+ const scale = getScale(container),
233
+ offset = scale.boundingClientRect; // left and top values are in page scale (like the event clientX/Y)
234
+
235
+ return new Point(
236
+ // offset.left/top values are in page scale (like clientX/Y),
237
+ // whereas clientLeft/Top (border width) values are the original values (before CSS scale applies).
238
+ (e.clientX - offset.left) / scale.x - container.clientLeft,
239
+ (e.clientY - offset.top) / scale.y - container.clientTop
240
+ );
241
+ }
242
+
243
+ // @function getWheelPxFactor(): Number
244
+ // Gets the wheel pixel factor based on the devicePixelRatio
245
+ export function getWheelPxFactor() {
246
+ // We need double the scroll pixels (see #7403 and #4538) for all Browsers
247
+ // except OSX (Mac) -> 3x, Chrome running on Linux 1x
248
+ const ratio = window.devicePixelRatio;
249
+ return Browser.linux && Browser.chrome ? ratio :
250
+ Browser.mac ? ratio * 3 :
251
+ ratio > 0 ? 2 * ratio : 1;
252
+ }
253
+
254
+ // @function getWheelDelta(ev: DOMEvent): Number
255
+ // Gets normalized wheel delta from a wheel DOM event, in vertical
256
+ // pixels scrolled (negative if scrolling down).
257
+ // Events from pointing devices without precise scrolling are mapped to
258
+ // a best guess of 60 pixels.
259
+ export function getWheelDelta(e) {
260
+ return (e.deltaY && e.deltaMode === 0) ? -e.deltaY / getWheelPxFactor() : // Pixels
261
+ (e.deltaY && e.deltaMode === 1) ? -e.deltaY * 20 : // Lines
262
+ (e.deltaY && e.deltaMode === 2) ? -e.deltaY * 60 : // Pages
263
+ (e.deltaX || e.deltaZ) ? 0 : // Skip horizontal/depth wheel events
264
+ 0;
265
+ }
266
+
267
+ // check if element really left/entered the event target (for pointerenter/pointerleave)
268
+ export function isExternalTarget(el, e) {
269
+
270
+ let related = e.relatedTarget;
271
+
272
+ if (!related) { return true; }
273
+
274
+ try {
275
+ while (related && (related !== el)) {
276
+ related = related.parentNode;
277
+ }
278
+ } catch (err) {
279
+ return false;
280
+ }
281
+ return (related !== el);
282
+ }
@@ -0,0 +1,175 @@
1
+ import * as DomEvent from './DomEvent.js';
2
+ import {Point} from '../geometry/Point.js';
3
+
4
+ /*
5
+ * @namespace DomUtil
6
+ *
7
+ * Utility functions to work with the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model)
8
+ * tree, used by Maplair internally.
9
+ *
10
+ * Most functions expecting or returning a `HTMLElement` also work for
11
+ * SVG elements. The only difference is that classes refer to CSS classes
12
+ * in HTML and SVG classes in SVG.
13
+ */
14
+
15
+ // @function get(id: String|HTMLElement): HTMLElement
16
+ // Returns an element given its DOM id, or returns the element itself
17
+ // if it was passed directly.
18
+ export function get(id) {
19
+ return typeof id === 'string' ? document.getElementById(id) : id;
20
+ }
21
+
22
+ // @function create(tagName: String, className?: String, container?: HTMLElement): HTMLElement
23
+ // Creates an HTML element with `tagName`, sets its class to `className`, and optionally appends it to `container` element.
24
+ export function create(tagName, className, container) {
25
+ const el = document.createElement(tagName);
26
+ el.className = className ?? '';
27
+
28
+ container?.appendChild(el);
29
+ return el;
30
+ }
31
+
32
+ // @function toFront(el: HTMLElement)
33
+ // Makes `el` the last child of its parent, so it renders in front of the other children.
34
+ export function toFront(el) {
35
+ const parent = el.parentNode;
36
+ if (parent && parent.lastChild !== el) {
37
+ parent.appendChild(el);
38
+ }
39
+ }
40
+
41
+ // @function toBack(el: HTMLElement)
42
+ // Makes `el` the first child of its parent, so it renders behind the other children.
43
+ export function toBack(el) {
44
+ const parent = el.parentNode;
45
+ if (parent && parent.firstChild !== el) {
46
+ parent.insertBefore(el, parent.firstChild);
47
+ }
48
+ }
49
+
50
+ // @function setTransform(el: HTMLElement, offset: Point, scale?: Number)
51
+ // Resets the 3D CSS transform of `el` so it is translated by `offset` pixels
52
+ // and optionally scaled by `scale`. Does not have an effect if the
53
+ // browser doesn't support 3D CSS transforms.
54
+ export function setTransform(el, offset, scale) {
55
+ const pos = offset ?? new Point(0, 0);
56
+
57
+ // same format as browsers serialize it, so that unchanged positions can be skipped
58
+ const transform = `translate3d(${pos.x}px, ${pos.y}px, 0px)${scale ? ` scale(${scale})` : ''}`;
59
+ if (el.style.transform !== transform) {
60
+ el.style.transform = transform;
61
+ }
62
+ }
63
+
64
+ const positions = new WeakMap();
65
+
66
+ // @function setPosition(el: HTMLElement, position: Point)
67
+ // Sets the position of `el` to coordinates specified by `position`,
68
+ // using CSS translate or top/left positioning depending on the browser
69
+ // (used by Maplair internally to position its layers).
70
+ export function setPosition(el, point) {
71
+ positions.set(el, point);
72
+ setTransform(el, point);
73
+ }
74
+
75
+ // @function getPosition(el: HTMLElement): Point
76
+ // Returns the coordinates of an element previously positioned with setPosition.
77
+ export function getPosition(el) {
78
+ // this method is only used for elements previously positioned using setPosition,
79
+ // so it's safe to cache the position for performance
80
+ return positions.get(el) ?? new Point(0, 0);
81
+ }
82
+
83
+ const documentStyle = typeof document === 'undefined' ? {} : document.documentElement.style;
84
+ // Safari still needs a vendor prefix, we need to detect with property name is supported.
85
+ const userSelectProp = ['userSelect', 'WebkitUserSelect'].find(prop => prop in documentStyle);
86
+ let prevUserSelect;
87
+
88
+ // @function disableTextSelection()
89
+ // Prevents the user from selecting text in the document. Used internally
90
+ // by Maplair to override the behaviour of any click-and-drag interaction on
91
+ // the map. Affects drag interactions on the whole document.
92
+ export function disableTextSelection() {
93
+ const value = documentStyle[userSelectProp];
94
+
95
+ if (value === 'none') {
96
+ return;
97
+ }
98
+
99
+ prevUserSelect = value;
100
+ documentStyle[userSelectProp] = 'none';
101
+ }
102
+
103
+ // @function enableTextSelection()
104
+ // Cancels the effects of a previous [`DomUtil.disableTextSelection`](#domutil-disabletextselection).
105
+ export function enableTextSelection() {
106
+ if (typeof prevUserSelect === 'undefined') {
107
+ return;
108
+ }
109
+
110
+ documentStyle[userSelectProp] = prevUserSelect;
111
+ prevUserSelect = undefined;
112
+ }
113
+
114
+ // @function disableImageDrag()
115
+ // Prevents the user from generating `dragstart` DOM events, usually generated when the user drags an image.
116
+ export function disableImageDrag() {
117
+ DomEvent.on(window, 'dragstart', DomEvent.preventDefault);
118
+ }
119
+
120
+ // @function enableImageDrag()
121
+ // Cancels the effects of a previous [`DomUtil.disableImageDrag`](#domutil-disableimagedrag).
122
+ export function enableImageDrag() {
123
+ DomEvent.off(window, 'dragstart', DomEvent.preventDefault);
124
+ }
125
+
126
+ let _outlineElement, _outlineStyle;
127
+ // @function preventOutline(el: HTMLElement)
128
+ // Makes the [outline](https://developer.mozilla.org/docs/Web/CSS/outline)
129
+ // of the element `el` invisible. Used internally by Maplair to prevent
130
+ // focusable elements from displaying an outline when the user performs a
131
+ // drag interaction on them.
132
+ export function preventOutline(element) {
133
+ while (element.tabIndex === -1) {
134
+ element = element.parentNode;
135
+ }
136
+ if (!element.style) { return; }
137
+ restoreOutline();
138
+ _outlineElement = element;
139
+ _outlineStyle = element.style.outlineStyle;
140
+ element.style.outlineStyle = 'none';
141
+ DomEvent.on(window, 'keydown', restoreOutline);
142
+ }
143
+
144
+ // @function restoreOutline()
145
+ // Cancels the effects of a previous [`DomUtil.preventOutline`](#domutil-preventoutline).
146
+ export function restoreOutline() {
147
+ if (!_outlineElement) { return; }
148
+ _outlineElement.style.outlineStyle = _outlineStyle;
149
+ _outlineElement = undefined;
150
+ _outlineStyle = undefined;
151
+ DomEvent.off(window, 'keydown', restoreOutline);
152
+ }
153
+
154
+ // @function getSizedParentNode(el: HTMLElement): HTMLElement
155
+ // Finds the closest parent node which size (width and height) is not null.
156
+ export function getSizedParentNode(element) {
157
+ do {
158
+ element = element.parentNode;
159
+ } while ((!element.offsetWidth || !element.offsetHeight) && element !== document.body);
160
+ return element;
161
+ }
162
+
163
+ // @function getScale(el: HTMLElement): Object
164
+ // Computes the CSS scale currently applied on the element.
165
+ // Returns an object with `x` and `y` members as horizontal and vertical scales respectively,
166
+ // and `boundingClientRect` as the result of [`getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect).
167
+ export function getScale(element) {
168
+ const rect = element.getBoundingClientRect();
169
+
170
+ return {
171
+ x: rect.width / element.offsetWidth || 1,
172
+ y: rect.height / element.offsetHeight || 1,
173
+ boundingClientRect: rect
174
+ };
175
+ }
@@ -0,0 +1,204 @@
1
+ import {Evented} from '../core/Events.js';
2
+ import * as DomEvent from './DomEvent.js';
3
+ import * as DomUtil from './DomUtil.js';
4
+ import * as Util from '../core/Util.js';
5
+ import {Point} from '../geometry/Point.js';
6
+ import * as PointerEvents from './DomEvent.PointerEvents.js';
7
+
8
+ /*
9
+ * @class Draggable
10
+ * @inherits Evented
11
+ *
12
+ * A class for making DOM elements draggable.
13
+ * Used internally for map and marker dragging. Works on any DOM element
14
+ *
15
+ * @example
16
+ * ```js
17
+ * const draggable = new Draggable(elementToDrag);
18
+ * draggable.enable();
19
+ * ```
20
+ */
21
+
22
+ export class Draggable extends Evented {
23
+
24
+ static {
25
+ this.setDefaultOptions({
26
+ // @section
27
+ // @aka Draggable options
28
+ // @option clickTolerance: Number = 3
29
+ // The max number of pixels a user can shift the pointer during a click
30
+ // for it to be considered a valid click (as opposed to a pointer drag).
31
+ clickTolerance: 3
32
+ });
33
+ }
34
+
35
+ // @constructor Draggable(el: HTMLElement, dragHandle?: HTMLElement, preventOutline?: Boolean, options?: Draggable options)
36
+ // Creates a `Draggable` object for moving `el` when you start dragging the `dragHandle` element (equals `el` itself by default).
37
+ constructor(element, dragStartTarget, preventOutline, options) {
38
+ super();
39
+ Util.setOptions(this, options);
40
+
41
+ this._element = element;
42
+ this._dragStartTarget = dragStartTarget ?? element;
43
+ this._preventOutline = preventOutline;
44
+
45
+ this.callInitHooks(Draggable);
46
+ }
47
+
48
+ // @method enable()
49
+ // Enables the dragging ability
50
+ enable() {
51
+ if (this._enabled) { return; }
52
+
53
+ DomEvent.on(this._dragStartTarget, 'pointerdown', this._onDown, this);
54
+
55
+ this._enabled = true;
56
+ }
57
+
58
+ // @method disable()
59
+ // Disables the dragging ability
60
+ disable() {
61
+ if (!this._enabled) { return; }
62
+
63
+ // If we're currently dragging this draggable,
64
+ // disabling it counts as first ending the drag.
65
+ if (Draggable._dragging === this) {
66
+ this.finishDrag(true);
67
+ }
68
+
69
+ DomEvent.off(this._dragStartTarget, 'pointerdown', this._onDown, this);
70
+
71
+ this._enabled = false;
72
+ this._moved = false;
73
+ }
74
+
75
+ _onDown(e) {
76
+ this._moved = false;
77
+
78
+ if (this._element.classList.contains('maplair-zoom-anim')) { return; }
79
+
80
+ if (PointerEvents.getPointers().length !== 1) {
81
+ // Finish dragging to avoid conflict with touchZoom
82
+ if (Draggable._dragging === this) {
83
+ this.finishDrag();
84
+ }
85
+ return;
86
+ }
87
+
88
+ if (Draggable._dragging || e.shiftKey || (e.button !== 0 && e.pointerType !== 'touch')) { return; }
89
+ Draggable._dragging = this; // Prevent dragging multiple objects at once.
90
+
91
+ if (this._preventOutline) {
92
+ DomUtil.preventOutline(this._element);
93
+ }
94
+
95
+ DomUtil.disableImageDrag();
96
+ DomUtil.disableTextSelection();
97
+
98
+ if (this._moving) { return; }
99
+
100
+ // @event down: Event
101
+ // Fired when a drag is about to start.
102
+ this.fire('down');
103
+
104
+ const sizedParent = DomUtil.getSizedParentNode(this._element);
105
+
106
+ this._startPoint = new Point(e.clientX, e.clientY);
107
+ this._startPos = DomUtil.getPosition(this._element);
108
+
109
+ // Cache the scale, so that we can continuously compensate for it during drag (_onMove).
110
+ this._parentScale = DomUtil.getScale(sizedParent);
111
+
112
+ DomEvent.on(this._dragStartTarget, 'pointermove', this._onMove, this);
113
+ DomEvent.on(this._dragStartTarget, 'pointerup pointercancel', this._onUp, this);
114
+ }
115
+
116
+ _onMove(e) {
117
+ if (PointerEvents.getPointers().length > 1) {
118
+ this._moved = true;
119
+ return;
120
+ }
121
+
122
+ const offset = new Point(e.clientX, e.clientY)._subtract(this._startPoint);
123
+
124
+ if (!offset.x && !offset.y) { return; }
125
+ if (Math.abs(offset.x) + Math.abs(offset.y) < this.options.clickTolerance) { return; }
126
+
127
+ // We assume that the parent container's position, border and scale do not change for the duration of the drag.
128
+ // Therefore there is no need to account for the position and border (they are eliminated by the subtraction)
129
+ // and we can use the cached value for the scale.
130
+ offset.x /= this._parentScale.x;
131
+ offset.y /= this._parentScale.y;
132
+
133
+ if (e.cancelable) {
134
+ e.preventDefault();
135
+ }
136
+
137
+ if (!this._moved) {
138
+ // @event dragstart: Event
139
+ // Fired when a drag starts
140
+ this.fire('dragstart');
141
+
142
+ this._moved = true;
143
+
144
+ this._element.ownerDocument.body.classList.add('maplair-dragging');
145
+
146
+ this._lastTarget = e.target ?? e.srcElement;
147
+ this._lastTarget.classList.add('maplair-drag-target');
148
+ }
149
+
150
+ this._newPos = this._startPos.add(offset);
151
+ this._moving = true;
152
+
153
+ this._lastEvent = e;
154
+ this._updatePosition();
155
+ }
156
+
157
+ _updatePosition() {
158
+ const e = {originalEvent: this._lastEvent};
159
+
160
+ // @event predrag: Event
161
+ // Fired continuously during dragging *before* each corresponding
162
+ // update of the element's position.
163
+ this.fire('predrag', e);
164
+ DomUtil.setPosition(this._element, this._newPos);
165
+
166
+ // @event drag: Event
167
+ // Fired continuously during dragging.
168
+ this.fire('drag', e);
169
+ }
170
+
171
+ _onUp() {
172
+ this.finishDrag();
173
+ }
174
+
175
+ finishDrag(noInertia) {
176
+ this._element.ownerDocument.body.classList.remove('maplair-dragging');
177
+
178
+ if (this._lastTarget) {
179
+ this._lastTarget.classList.remove('maplair-drag-target');
180
+ this._lastTarget = null;
181
+ }
182
+
183
+ DomEvent.off(this._dragStartTarget, 'pointermove', this._onMove, this);
184
+ DomEvent.off(this._dragStartTarget, 'pointerup pointercancel', this._onUp, this);
185
+
186
+ DomUtil.enableImageDrag();
187
+ DomUtil.enableTextSelection();
188
+
189
+ const fireDragend = this._moved && this._moving;
190
+
191
+ this._moving = false;
192
+ Draggable._dragging = false;
193
+
194
+ if (fireDragend) {
195
+ // @event dragend: DragEndEvent
196
+ // Fired when the drag ends.
197
+ this.fire('dragend', {
198
+ noInertia,
199
+ distance: this._newPos.distanceTo(this._startPos)
200
+ });
201
+ }
202
+ }
203
+
204
+ }