Skip to content

Commit 7faf302

Browse files
committed
[Map] Prefer abstract "dispatchEvent" instead
1 parent f32b8bc commit 7faf302

13 files changed

+59
-48
lines changed

src/Map/assets/dist/abstract_map_controller.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default abstract class<MapOptions, Map, MarkerOptions, Marker, InfoWindow
3535
protected map: Map;
3636
protected markers: Array<Marker>;
3737
protected infoWindows: Array<InfoWindow>;
38+
protected additionalEventPayload: Record<string, unknown>;
3839
connect(): void;
3940
protected abstract doCreateMap({ center, zoom, options, }: {
4041
center: Point;
@@ -52,6 +53,5 @@ export default abstract class<MapOptions, Map, MarkerOptions, Marker, InfoWindow
5253
marker: Marker;
5354
}): InfoWindow;
5455
protected abstract doFitBoundsToMarkers(): void;
55-
protected abstract augmentEventPayload(payload: Record<string, unknown>): Record<string, unknown>;
56-
private dispatchEvent;
56+
protected abstract dispatchEvent(name: string, payload: Record<string, unknown>): void;
5757
}

src/Map/assets/dist/abstract_map_controller.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class default_1 extends Controller {
55
super(...arguments);
66
this.markers = [];
77
this.infoWindows = [];
8+
this.additionalEventPayload = {};
89
}
910
connect() {
1011
const { center, zoom, options, markers, fitBoundsToMarkers } = this.viewValue;
@@ -34,12 +35,6 @@ class default_1 extends Controller {
3435
this.infoWindows.push(infoWindow);
3536
return infoWindow;
3637
}
37-
dispatchEvent(name, payload = {}) {
38-
this.dispatch(name, {
39-
prefix: 'ux:map',
40-
detail: this.augmentEventPayload(payload),
41-
});
42-
}
4338
}
4439
default_1.values = {
4540
providerOptions: Object,

src/Map/assets/src/abstract_map_controller.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export default abstract class<
6666
protected markers: Array<Marker> = [];
6767
protected infoWindows: Array<InfoWindow> = [];
6868

69+
protected additionalEventPayload: Record<string, unknown> = {};
70+
6971
connect() {
7072
const { center, zoom, options, markers, fitBoundsToMarkers } = this.viewValue;
7173

@@ -134,12 +136,5 @@ export default abstract class<
134136

135137
protected abstract doFitBoundsToMarkers(): void;
136138

137-
protected abstract augmentEventPayload(payload: Record<string, unknown>): Record<string, unknown>;
138-
139-
private dispatchEvent(name: string, payload: Record<string, unknown> = {}): void {
140-
this.dispatch(name, {
141-
prefix: 'ux:map',
142-
detail: this.augmentEventPayload(payload),
143-
});
144-
}
139+
protected abstract dispatchEvent(name: string, payload: Record<string, unknown>): void;
145140
}

src/Map/assets/test/abstract_map_controller.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ import { Application } from '@hotwired/stimulus';
22
import { getByTestId, waitFor } from '@testing-library/dom';
33
import { clearDOM, mountDOM } from '@symfony/stimulus-testing';
44
import AbstractMapController from '../src/abstract_map_controller.ts';
5+
import * as L from 'leaflet';
56

67
class MyMapController extends AbstractMapController {
8+
protected dispatchEvent(name: string, payload: Record<string, unknown> = {}): void {
9+
this.dispatch(name, {
10+
prefix: 'ux:map',
11+
detail: payload,
12+
});
13+
}
14+
715
doCreateMap({ center, zoom, options }) {
816
return { map: 'map', center, zoom, options };
917
}

src/Map/src/Bridge/Google/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ $map->options($googleOptions);
8484

8585
Below are some common or advanced use cases when using a map.
8686

87-
> **Note**:
88-
> As the UX Map component is new, there are not too many examples for the moment.
89-
9087
### Customize the marker
9188

9289
A common use case is to customize the marker. You can listen to the `ux:map:marker:before-create` event to customize the marker before it is created.
@@ -96,7 +93,7 @@ Assuming you have a map with a custom controller:
9693
{{ render_map(map, {'data-controller': 'my-map' }) }}
9794
```
9895

99-
You can create a controller to customize the marker before it is created:
96+
You can create a Stimulus controller to customize the markers before they are created:
10097
```js
10198
// assets/controllers/my_map_controller.js
10299
import {Controller} from "@hotwired/stimulus";
@@ -128,7 +125,7 @@ export default class extends Controller
128125
// 2. To use a custom glyph for the marker
129126
const pinElement = new google.maps.marker.PinElement({
130127
// Note: instead of using an hardcoded URL, you can use the `extra` parameter from `new Marker()` (PHP) and access it here with `definition.extra`.
131-
glyph: new URL(String('https://maps.gstatic.com/mapfiles/place_api/icons/v2/museum_pinlet.svg')),
128+
glyph: new URL('https://maps.gstatic.com/mapfiles/place_api/icons/v2/museum_pinlet.svg'),
132129
glyphColor: "white",
133130
});
134131
definition.rawOptions = {

src/Map/src/Bridge/Google/assets/dist/map_controller.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class extends AbstractMapController<MapOptions, google.maps.Map,
99
};
1010
providerOptionsValue: Pick<LoaderOptions, 'apiKey' | 'id' | 'language' | 'region' | 'nonce' | 'retries' | 'url' | 'version' | 'libraries'>;
1111
connect(): Promise<void>;
12+
protected dispatchEvent(name: string, payload?: Record<string, unknown>): void;
1213
protected doCreateMap({ center, zoom, options, }: {
1314
center: Point;
1415
zoom: number;
@@ -22,6 +23,5 @@ export default class extends AbstractMapController<MapOptions, google.maps.Map,
2223
private createTextOrElement;
2324
private closeInfoWindowsExcept;
2425
protected doFitBoundsToMarkers(): void;
25-
protected augmentEventPayload(payload: Record<string, unknown>): Record<string, unknown>;
2626
}
2727
export {};

src/Map/src/Bridge/Google/assets/dist/map_controller.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ class default_1 extends AbstractMapController {
2222
}
2323
super.connect();
2424
}
25+
dispatchEvent(name, payload = {}) {
26+
this.dispatch(name, {
27+
prefix: 'ux:map',
28+
detail: {
29+
...payload,
30+
google: _google,
31+
},
32+
});
33+
}
2534
doCreateMap({ center, zoom, options, }) {
2635
options.zoomControl = typeof options.zoomControlOptions !== 'undefined';
2736
options.mapTypeControl = typeof options.mapTypeControlOptions !== 'undefined';
@@ -104,10 +113,6 @@ class default_1 extends AbstractMapController {
104113
});
105114
this.map.fitBounds(bounds);
106115
}
107-
augmentEventPayload(payload) {
108-
payload.google = _google;
109-
return payload;
110-
}
111116
}
112117
default_1.values = {
113118
providerOptions: Object,

src/Map/src/Bridge/Google/assets/src/map_controller.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ export default class extends AbstractMapController<
7575
super.connect();
7676
}
7777

78+
protected dispatchEvent(name: string, payload: Record<string, unknown> = {}): void {
79+
this.dispatch(name, {
80+
prefix: 'ux:map',
81+
detail: {
82+
...payload,
83+
google: _google,
84+
},
85+
});
86+
}
87+
7888
protected doCreateMap({
7989
center,
8090
zoom,
@@ -197,10 +207,4 @@ export default class extends AbstractMapController<
197207

198208
this.map.fitBounds(bounds);
199209
}
200-
201-
protected augmentEventPayload(payload: Record<string, unknown>): Record<string, unknown> {
202-
payload.google = _google;
203-
204-
return payload;
205-
}
206210
}

src/Map/src/Bridge/Google/src/Renderer/GoogleRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838
private ?string $url = null,
3939
private ?string $version = null,
4040
/**
41-
* @var array<'core' | 'maps' | 'places' | 'geocoding' | 'routes' | 'marker' | 'geometry' | 'elevation' | 'streetView' | 'journeySharing' | 'drawing' | 'visualization'>
41+
* @var array<'core'|'maps'|'places'|'geocoding'|'routes'|'marker'|'geometry'|'elevation'|'streetView'|'journeySharing'|'drawing'|'visualization'>
4242
*/
4343
private array $libraries = [],
4444
) {

src/Map/src/Bridge/Leaflet/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ $map->options($leafletOptions);
4141

4242
Below are some common or advanced use cases when using a map.
4343

44-
> **Note**:
45-
> As the UX Map component is new, there are not too many examples for the moment.
46-
4744
### Customize the marker
4845

4946
A common use case is to customize the marker. You can listen to the `ux:map:marker:before-create` event to customize the marker before it is created.
@@ -53,7 +50,7 @@ Assuming you have a map with a custom controller:
5350
{{ render_map(map, {'data-controller': 'my-map' }) }}
5451
```
5552

56-
You can create a controller to customize the marker before it is created:
53+
You can create a Stimulus controller to customize the markers before they are created:
5754
```js
5855
// assets/controllers/my_map_controller.js
5956
import {Controller} from "@hotwired/stimulus";

src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type MapOptions = Pick<LeafletMapOptions, 'center' | 'zoom'> & {
1212
};
1313
export default class extends AbstractMapController<MapOptions, typeof L.Map, MarkerOptions, typeof L.Marker, typeof L.Popup, PopupOptions> {
1414
connect(): void;
15+
protected dispatchEvent(name: string, payload?: Record<string, unknown>): void;
1516
protected doCreateMap({ center, zoom, options }: {
1617
center: Point;
1718
zoom: number;
@@ -23,6 +24,5 @@ export default class extends AbstractMapController<MapOptions, typeof L.Map, Mar
2324
marker: L.Marker;
2425
}): L.Popup;
2526
protected doFitBoundsToMarkers(): void;
26-
protected augmentEventPayload(payload: Record<string, unknown>): Record<string, unknown>;
2727
}
2828
export {};

src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ class map_controller extends AbstractMapController {
1313
});
1414
super.connect();
1515
}
16+
dispatchEvent(name, payload = {}) {
17+
this.dispatch(name, {
18+
prefix: 'ux:map',
19+
detail: {
20+
...payload,
21+
leaflet: L,
22+
},
23+
});
24+
}
1625
doCreateMap({ center, zoom, options }) {
1726
const map = L.map(this.element, {
1827
...options,
@@ -54,10 +63,6 @@ class map_controller extends AbstractMapController {
5463
return [position.lat, position.lng];
5564
}));
5665
}
57-
augmentEventPayload(payload) {
58-
payload.L = L;
59-
return payload;
60-
}
6166
}
6267

6368
export { map_controller as default };

src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ export default class extends AbstractMapController<
2424
popupAnchor: [0, -41],
2525
className: '',
2626
});
27+
2728
super.connect();
2829
}
2930

31+
protected dispatchEvent(name: string, payload: Record<string, unknown> = {}): void {
32+
this.dispatch(name, {
33+
prefix: 'ux:map',
34+
detail: {
35+
...payload,
36+
leaflet: L,
37+
},
38+
});
39+
}
40+
3041
protected doCreateMap({ center, zoom, options }: { center: Point; zoom: number; options: MapOptions }): L.Map {
3142
const map = L.map(this.element, {
3243
...options,
@@ -88,10 +99,4 @@ export default class extends AbstractMapController<
8899
})
89100
);
90101
}
91-
92-
protected augmentEventPayload(payload: Record<string, unknown>): Record<string, unknown> {
93-
payload.L = L;
94-
95-
return payload;
96-
}
97102
}

0 commit comments

Comments
 (0)