adt-js-components 1.4.0 → 1.5.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,7 +22,9 @@
22
22
  "scrollparent": "^2.0.1",
23
23
  "tinymce": "^5.7.0",
24
24
  "tinymce-i18n": "^20.12.25",
25
- "nette.ajax.js": "^2.3.0"
25
+ "nette.ajax.js": "^2.3.0",
26
+ "leaflet": "^1.9.4",
27
+ "leaflet.markercluster": "^1.5.3"
26
28
  },
27
29
  "peerDependencies": {
28
30
  "jquery": "^3.4.1"
@@ -71,6 +71,9 @@ const initAjaxSelect = () => {
71
71
  const initInputClear = () => {
72
72
  init('input-clear', 'InputClear');
73
73
  };
74
+ const initMap = () => {
75
+ init('map', 'Map');
76
+ };
74
77
 
75
78
  const getFirstDiffCharIndex = (a, b) => {
76
79
  const longerLength = Math.max(a.length, b.length);
@@ -102,5 +105,6 @@ export default {
102
105
  initTinyMCE,
103
106
  initAjaxSelect,
104
107
  initInputClear,
108
+ initMap,
105
109
  loadScssModule,
106
110
  };
@@ -0,0 +1,123 @@
1
+ import L from 'leaflet';
2
+ import 'leaflet/dist/leaflet.css';
3
+
4
+ import "leaflet.markercluster";
5
+ import "leaflet.markercluster/dist/MarkerCluster.css";
6
+ import "leaflet.markercluster/dist/MarkerCluster.Default.css";
7
+
8
+ let siteKey;
9
+ const mapInstances = new WeakMap();
10
+
11
+ async function run(options) {
12
+ siteKey = options.siteKey;
13
+
14
+ function applyEventHandlers(el) {
15
+ const { position, zoom, hideControl = false, markers = [], callback } = JSON.parse(el.dataset.adtMap);
16
+
17
+ if (mapInstances.has(el)) {
18
+ mapInstances.get(el).remove();
19
+ mapInstances.delete(el);
20
+ }
21
+
22
+ const map = L.map(el);
23
+ mapInstances.set(el, map);
24
+
25
+ L.tileLayer("https://api.mapy.cz/v1/maptiles/basic/256/{z}/{x}/{y}?apikey=" + siteKey, {
26
+ minZoom: 0,
27
+ maxZoom: 19,
28
+ attribution: '<a href="https://api.mapy.cz/copyright" target="_blank">&copy; Seznam.cz a.s. a další</a>',
29
+ }).addTo(map);
30
+ const LogoControl = L.Control.extend({
31
+ options: {
32
+ position: 'bottomleft',
33
+ },
34
+ onAdd: function (map) {
35
+ const container = L.DomUtil.create('div');
36
+ const link = L.DomUtil.create('a', '', container);
37
+ link.setAttribute('href', 'http://mapy.cz/');
38
+ link.setAttribute('target', '_blank');
39
+ link.innerHTML = '<img src="https://api.mapy.cz/img/api/logo.svg" />';
40
+ L.DomEvent.disableClickPropagation(link);
41
+ return container;
42
+ },
43
+ });
44
+ new LogoControl().addTo(map);
45
+
46
+ if (position.length) {
47
+ map.setView(position, zoom);
48
+ }
49
+
50
+ if (hideControl) {
51
+ map.scrollWheelZoom.disable();
52
+ map.zoomControl.remove();
53
+ map.keyboard.disable();
54
+ map.dragging.disable();
55
+ }
56
+
57
+ const markerOptions = {
58
+ icon: L.icon({
59
+ iconUrl: '//' + window.location.hostname + '/images/mapmarker.png',
60
+ }),
61
+ };
62
+ if (markers.length) {
63
+ const markerPositions = [];
64
+ const cluster = L.markerClusterGroup({
65
+ disableClusteringAtZoom: map.getMaxZoom()
66
+ });
67
+ for (const marker of markers) {
68
+ const mapMarker = L.marker(marker.position, markerOptions);
69
+ if (marker.handler) {
70
+ mapMarker.on('click', marker.handler);
71
+ }
72
+ if (marker.popup) {
73
+ mapMarker.bindPopup(marker.popup)
74
+ }
75
+ if (!marker.excludeFromBoundary) {
76
+ markerPositions.push(marker.position);
77
+ }
78
+ cluster.addLayer(mapMarker);
79
+ }
80
+ if (!position.length) {
81
+ map.fitBounds(markerPositions);
82
+ }
83
+ map.addLayer(cluster);
84
+ } else {
85
+ L.marker(position, markerOptions).addTo(map);
86
+ }
87
+
88
+ if (callback) {
89
+ map.on('zoomend', window[callback]);
90
+ map.on('moveend', window[callback]);
91
+ map.on('dragend', window[callback]);
92
+ }
93
+ }
94
+
95
+ const observer = new MutationObserver(mutations => {
96
+ mutations.forEach(mutation => {
97
+ if (mutation.type === "childList") {
98
+ mutation.addedNodes.forEach(node => {
99
+ if (node.nodeType === 1 && node.hasAttribute("data-adt-map")) {
100
+ handleMapElement(node);
101
+ }
102
+ });
103
+ }
104
+
105
+ if (mutation.type === "attributes" && mutation.attributeName === "data-adt-map") {
106
+ applyEventHandlers(mutation.target);
107
+ }
108
+ });
109
+ });
110
+
111
+ observer.observe(document.body, {
112
+ childList: true,
113
+ subtree: true,
114
+ attributes: true,
115
+ attributeFilter: ["data-adt-map"]
116
+ });
117
+
118
+ document.querySelectorAll('[data-adt-map]').forEach(function(el) {
119
+ applyEventHandlers(el);
120
+ });
121
+ }
122
+
123
+ export default { run }
@@ -17,7 +17,6 @@ function run(options) {
17
17
  if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
18
18
  $(mutation.addedNodes).each(function() {
19
19
  if (this.nodeType === Node.ELEMENT_NODE) {
20
- console.log(this);
21
20
  if (this.hasAttribute('data-adt-select2')) {
22
21
  applyEventHandlers(this);
23
22
  }