diff --git a/src/Map/CHANGELOG.md b/src/Map/CHANGELOG.md index ff1e1c6c288..a6a0ee61638 100644 --- a/src/Map/CHANGELOG.md +++ b/src/Map/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 2.32 + +- Add `Map::removeAllMarkers()`, `Map::removeAllPolygons()`, `Map::removeAllPolylines()`, `Map::removeAllCircles()` and `Map::removeAllRectangles()` methods + ## 2.31 - Add `fitBoundsToMarkers` parameter to `ux_map()` Twig function diff --git a/src/Map/doc/index.rst b/src/Map/doc/index.rst index 56afd3315ca..f704d1c4af6 100644 --- a/src/Map/doc/index.rst +++ b/src/Map/doc/index.rst @@ -292,6 +292,22 @@ If you haven't stored the element instance, you can still remove them by passing $map->removeCircle('my-circle'); $map->removeRectangle('my-rectangle'); +To remove all instances of a certain element, you can use the `Map::removeAll*()` methods:: + + // Add elements + $map->addMarker($marker = new Marker(/* ... */)); + $map->addPolygon($polygon = new Polygon(/* ... */)); + $map->addPolyline($polyline = new Polyline(/* ... */)); + $map->addCircle($circle = new Circle(/* ... */)); + $map->addRectangle($rectangle = new Rectangle(/* ... */)); + + // And later, remove those elements + $map->removeAllMarkers(); + $map->removeAllPolygons(); + $map->removeAllPolylines(); + $map->removeAllCircles(); + $map->removeAllRectangles(); + Render a map ------------ diff --git a/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php b/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php index 33432cd7188..a6607418484 100644 --- a/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php +++ b/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php @@ -74,6 +74,16 @@ public static function provideTestRenderMap(): iterable ->removeMarker('marker2'), ]; + yield 'with all markers removed' => [ + 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addMarker($marker1) + ->addMarker($marker2) + ->removeAllMarkers(), + ]; + yield 'with marker remove and new ones added' => [ 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), 'map' => (new Map()) @@ -94,6 +104,16 @@ public static function provideTestRenderMap(): iterable ->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'))), ]; + yield 'with all polygons removed' => [ + 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addPolygon(new Polygon(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)])) + ->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'))) + ->removeAllPolygons(), + ]; + yield 'with polylines and infoWindows' => [ 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), 'map' => (new Map()) @@ -103,6 +123,16 @@ public static function provideTestRenderMap(): iterable ->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'))), ]; + yield 'with all polylines removed' => [ + 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addPolyline(new Polyline(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)])) + ->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polyline'))) + ->removeAllPolylines(), + ]; + yield 'with circles and infoWindows' => [ 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), 'map' => (new Map()) @@ -112,6 +142,16 @@ public static function provideTestRenderMap(): iterable ->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 1000, infoWindow: new InfoWindow(content: 'Circle'))), ]; + yield 'with all circles removed' => [ + 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addCircle(new Circle(center: new Point(48.8566, 2.3522), radius: 500, infoWindow: new InfoWindow(content: 'Circle'))) + ->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 1000, infoWindow: new InfoWindow(content: 'Circle'))) + ->removeAllCircles(), + ]; + yield 'with rectangles and infoWindows' => [ 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), 'map' => (new Map()) @@ -121,6 +161,16 @@ public static function provideTestRenderMap(): iterable ->addRectangle(new Rectangle(southWest: new Point(1.1, 2.2), northEast: new Point(3.3, 4.4), infoWindow: new InfoWindow(content: 'Rectangle'))), ]; + yield 'with all rectangles removed' => [ + 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addRectangle(new Rectangle(southWest: new Point(48.8566, 2.3522), northEast: new Point(48.8566, 2.3522), infoWindow: new InfoWindow(content: 'Rectangle'))) + ->addRectangle(new Rectangle(southWest: new Point(1.1, 2.2), northEast: new Point(3.3, 4.4), infoWindow: new InfoWindow(content: 'Rectangle'))) + ->removeAllRectangles(), + ]; + yield 'with controls enabled' => [ 'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), 'map' => (new Map()) diff --git a/src/Map/src/Bridge/Leaflet/tests/LeafletRendererTest.php b/src/Map/src/Bridge/Leaflet/tests/LeafletRendererTest.php index 39955417bb7..ff7066b3924 100644 --- a/src/Map/src/Bridge/Leaflet/tests/LeafletRendererTest.php +++ b/src/Map/src/Bridge/Leaflet/tests/LeafletRendererTest.php @@ -69,6 +69,16 @@ public static function provideTestRenderMap(): iterable ->removeMarker('marker2'), ]; + yield 'with all markers removed' => [ + 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addMarker($marker1) + ->addMarker($marker2) + ->removeAllMarkers(), + ]; + yield 'with marker remove and new ones added' => [ 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)), 'map' => (new Map()) @@ -89,6 +99,16 @@ public static function provideTestRenderMap(): iterable ->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'), id: 'polygon2')), ]; + yield 'with all polygons removed' => [ + 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addPolygon(new Polygon(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)])) + ->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'))) + ->removeAllPolygons(), + ]; + yield 'with polylines and infoWindows' => [ 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)), 'map' => (new Map()) @@ -98,6 +118,16 @@ public static function provideTestRenderMap(): iterable ->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polyline'), id: 'polyline2')), ]; + yield 'with all polylines removed' => [ + 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addPolyline(new Polyline(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)])) + ->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polyline'))) + ->removeAllPolylines(), + ]; + yield 'with circles and infoWindows' => [ 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)), 'map' => (new Map()) @@ -107,6 +137,16 @@ public static function provideTestRenderMap(): iterable ->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 500, infoWindow: new InfoWindow(content: 'Circle'), id: 'circle2')), ]; + yield 'with all circles removed' => [ + 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addCircle(new Circle(center: new Point(48.8566, 2.3522), radius: 500, infoWindow: new InfoWindow(content: 'Circle'))) + ->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 1000, infoWindow: new InfoWindow(content: 'Circle'))) + ->removeAllCircles(), + ]; + yield 'with rectangles and infoWindows' => [ 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)), 'map' => (new Map()) @@ -125,6 +165,16 @@ public static function provideTestRenderMap(): iterable )), ]; + yield 'with all rectangles removed' => [ + 'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'), + 'map' => (new Map()) + ->center(new Point(48.8566, 2.3522)) + ->zoom(12) + ->addRectangle(new Rectangle(southWest: new Point(48.8566, 2.3522), northEast: new Point(48.8566, 2.3522), infoWindow: new InfoWindow(content: 'Rectangle'))) + ->addRectangle(new Rectangle(southWest: new Point(1.1, 2.2), northEast: new Point(3.3, 4.4), infoWindow: new InfoWindow(content: 'Rectangle'))) + ->removeAllRectangles(), + ]; + yield 'markers with icons' => [ 'renderer' => new LeafletRenderer( new StimulusHelper(null), diff --git a/src/Map/src/Elements.php b/src/Map/src/Elements.php index c68876c659a..f8c75ce67f1 100644 --- a/src/Map/src/Elements.php +++ b/src/Map/src/Elements.php @@ -64,6 +64,13 @@ public function remove(Element|string $elementOrId): static return $this; } + public function removeAll(): static + { + $this->elements->removeAll($this->elements); + + return $this; + } + public function toArray(): array { foreach ($this->elements as $element) { diff --git a/src/Map/src/Map.php b/src/Map/src/Map.php index 0df52df1957..369d43fa148 100644 --- a/src/Map/src/Map.php +++ b/src/Map/src/Map.php @@ -133,6 +133,13 @@ public function removeMarker(Marker|string $markerOrId): self return $this; } + public function removeAllMarkers(): self + { + $this->markers->removeAll(); + + return $this; + } + public function addPolygon(Polygon $polygon): self { $this->polygons->add($polygon); @@ -147,6 +154,13 @@ public function removePolygon(Polygon|string $polygonOrId): self return $this; } + public function removeAllPolygons(): self + { + $this->polygons->removeAll(); + + return $this; + } + public function addPolyline(Polyline $polyline): self { $this->polylines->add($polyline); @@ -161,6 +175,13 @@ public function removePolyline(Polyline|string $polylineOrId): self return $this; } + public function removeAllPolylines(): self + { + $this->polylines->removeAll(); + + return $this; + } + public function addCircle(Circle $circle): self { $this->circles->add($circle); @@ -175,6 +196,13 @@ public function removeCircle(Circle|string $circleOrId): self return $this; } + public function removeAllCircles(): self + { + $this->circles->removeAll(); + + return $this; + } + public function addRectangle(Rectangle $rectangle): self { $this->rectangles->add($rectangle); @@ -189,6 +217,13 @@ public function removeRectangle(Rectangle|string $rectangleOrId): self return $this; } + public function removeAllRectangles(): self + { + $this->rectangles->removeAll(); + + return $this; + } + /** * @param array $extra Extra data forwarded to the JavaScript side. It can be used in your custom * Stimulus controller to benefit from greater flexibility and customization.