Skip to content

Commit

Permalink
[Map] Add the possibility to not configure map zoom/center if fit bou…
Browse files Browse the repository at this point in the history
…nds to markers
  • Loading branch information
Kocal committed Aug 11, 2024
1 parent 21595a7 commit 3acffb5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/Map/assets/dist/abstract_map_controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export type Point = {
lng: number;
};
export type MapView<Options, MarkerOptions, InfoWindowOptions> = {
center: Point;
zoom: number;
center: Point | null;
zoom: number | null;
fitBoundsToMarkers: boolean;
markers: Array<MarkerDefinition<MarkerOptions, InfoWindowOptions>>;
options: Options;
Expand Down Expand Up @@ -38,8 +38,8 @@ export default abstract class<MapOptions, Map, MarkerOptions, Marker, InfoWindow
initialize(): void;
connect(): void;
protected abstract doCreateMap({ center, zoom, options, }: {
center: Point;
zoom: number;
center: Point | null;
zoom: number | null;
options: MapOptions;
}): Map;
createMarker(definition: MarkerDefinition<MarkerOptions, InfoWindowOptions>): Marker;
Expand Down
8 changes: 4 additions & 4 deletions src/Map/assets/src/abstract_map_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Controller } from '@hotwired/stimulus';
export type Point = { lat: number; lng: number };

export type MapView<Options, MarkerOptions, InfoWindowOptions> = {
center: Point;
zoom: number;
center: Point|null;
zoom: number|null;
fitBoundsToMarkers: boolean;
markers: Array<MarkerDefinition<MarkerOptions, InfoWindowOptions>>;
options: Options;
Expand Down Expand Up @@ -93,8 +93,8 @@ export default abstract class<
zoom,
options,
}: {
center: Point;
zoom: number;
center: Point|null;
zoom: number|null;
options: MapOptions;
}): Map;

Expand Down
3 changes: 3 additions & 0 deletions src/Map/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ A map is created by calling ``new Map()``. You can configure the center, zoom, a
{
// 1. Create a new map instance
$myMap = (new Map());
// Explicitly set the center and zoom
->center(new Point(46.903354, 1.888334))
->zoom(6)
// Or automatically fit the bounds to the markers
->fitBoundsToMarkers()
;
// 2. You can add markers
Expand Down
4 changes: 2 additions & 2 deletions src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type MapOptions = Pick<LeafletMapOptions, 'center' | 'zoom'> & {
export default class extends AbstractMapController<MapOptions, typeof LeafletMap, MarkerOptions, Marker, Popup, PopupOptions> {
connect(): void;
protected doCreateMap({ center, zoom, options }: {
center: Point;
zoom: number;
center: Point | null;
zoom: number | null;
options: MapOptions;
}): LeafletMap;
protected doCreateMarker(definition: MarkerDefinition): Marker;
Expand Down
4 changes: 2 additions & 2 deletions src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class map_controller extends AbstractMapController {
doCreateMap({ center, zoom, options }) {
const map$1 = map(this.element, {
...options,
center,
zoom,
center: center === null ? undefined : center,
zoom: zoom === null ? undefined : zoom,
});
tileLayer(options.tileLayer.url, {
attribution: options.tileLayer.attribution,
Expand Down
6 changes: 3 additions & 3 deletions src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export default class extends AbstractMapController<
super.connect();
}

protected doCreateMap({ center, zoom, options }: { center: Point; zoom: number; options: MapOptions }): LeafletMap {
protected doCreateMap({ center, zoom, options }: { center: Point|null; zoom: number|null; options: MapOptions }): LeafletMap {
const map = createMap(this.element, {
...options,
center,
zoom,
center: center === null ? undefined : center,
zoom: zoom === null ? undefined : zoom,
});

createTileLayer(options.tileLayer.url, {
Expand Down
16 changes: 9 additions & 7 deletions src/Map/src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ public function addMarker(Marker $marker): self

public function toArray(): array
{
if (null === $this->center) {
throw new InvalidArgumentException('The center of the map must be set.');
}

if (null === $this->zoom) {
throw new InvalidArgumentException('The zoom of the map must be set.');
if (!$this->fitBoundsToMarkers) {
if (null === $this->center) {
throw new InvalidArgumentException('The map "center" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');
}

if (null === $this->zoom) {
throw new InvalidArgumentException('The map "zoom" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');
}
}

return [
'center' => $this->center->toArray(),
'center' => $this->center?->toArray(),
'zoom' => $this->zoom,
'fitBoundsToMarkers' => $this->fitBoundsToMarkers,
'options' => (object) ($this->options?->toArray() ?? []),
Expand Down
21 changes: 19 additions & 2 deletions src/Map/tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MapTest extends TestCase
public function testCenterValidation(): void
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage('The center of the map must be set.');
self::expectExceptionMessage('The map "center" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');

$map = new Map();
$map->toArray();
Expand All @@ -33,14 +33,31 @@ public function testCenterValidation(): void
public function testZoomValidation(): void
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage('The zoom of the map must be set.');
self::expectExceptionMessage('The map "zoom" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');

$map = new Map(
center: new Point(48.8566, 2.3522)
);
$map->toArray();
}

public function testZoomAndCenterCanBeOmittedIfFitBoundsToMarkers(): void
{
$map = new Map(
fitBoundsToMarkers: true
);

$array = $map->toArray();

self::assertSame([
'center' => null,
'zoom' => null,
'fitBoundsToMarkers' => true,
'options' => $array['options'],
'markers' => [],
], $array);
}

public function testWithMinimumConfiguration(): void
{
$map = new Map();
Expand Down

0 comments on commit 3acffb5

Please sign in to comment.