Skip to content

Commit 8ce07ac

Browse files
author
root
committed
Remove widgets
1 parent b1017c7 commit 8ce07ac

File tree

3 files changed

+152
-12
lines changed

3 files changed

+152
-12
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,39 @@ Widget::group('sidebar')->wrap(function ($content, $index, $total) {
334334
})->...;
335335
```
336336

337-
### Checking the state of a widget group
337+
### Removing widgets from a group
338+
339+
There is a couple of ways to remove widget/widgets from a group after they've been already added.
340+
341+
1) Remove one widget by its unique `id`
342+
```php
343+
$id1 = Widget::group('sidebar')->addWidget('files');
344+
$id2 = Widget::group('sidebar')->addAsyncWidget('files');
345+
Widget::group('sidebar')->removeById($id1); // There is only second widget in the group now
346+
```
347+
348+
2) Remove all widgets with specific name
349+
```php
350+
Widget::group('sidebar')->addWidget('files');
351+
Widget::group('sidebar')->addAsyncWidget('files');
352+
Widget::group('sidebar')->removeByName('files'); // Widget group is empty now
353+
```
354+
355+
3) Remove all widgets that are placed on a specific position.
356+
```php
357+
Widget::group('sidebar')->position(42)->addWidget('files');
358+
Widget::group('sidebar')->position(42)->addAsyncWidget('files');
359+
Widget::group('sidebar')->removeByPosition(42); // Widget group is empty now
360+
```
361+
362+
4) Remove all widgets at once.
363+
```php
364+
Widget::group('sidebar')->addWidget('files');
365+
Widget::group('sidebar')->addAsyncWidget('files');
366+
Widget::group('sidebar')->removeAll(); // Widget group is empty now
367+
```
368+
369+
### Checking the state of a group
338370

339371
`Widget::group('sidebar')->isEmpty(); // bool`
340372

src/WidgetGroup.php

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class WidgetGroup
4545
protected $separator = '';
4646

4747
/**
48-
* The number of widgets in the group.
48+
* Id that is going to be issued to the next widget when it's added to the group.
4949
*
5050
* @var int
5151
*/
52-
protected $count = 0;
52+
protected $nextWidgetId = 1;
5353

5454
/**
5555
* A callback that defines extra markup that wraps every widget in the group.
@@ -80,11 +80,13 @@ public function display()
8080

8181
$output = '';
8282
$index = 0;
83+
$count = $this->count();
84+
8385
foreach ($this->widgets as $position => $widgets) {
8486
foreach ($widgets as $widget) {
85-
$output .= $this->performWrap($this->displayWidget($widget), $index, $this->count);
87+
$output .= $this->performWrap($this->displayWidget($widget), $index, $count);
8688
$index++;
87-
if ($this->count !== $index) {
89+
if ($index !== $count) {
8890
$output .= $this->separator;
8991
}
9092
}
@@ -93,6 +95,59 @@ public function display()
9395
return $this->convertToViewExpression($output);
9496
}
9597

98+
/**
99+
* Remove a widget by its id.
100+
*
101+
* @param int $id
102+
*/
103+
public function removeById($id)
104+
{
105+
foreach ($this->widgets as $position => $widgets) {
106+
foreach ($widgets as $i => $widget) {
107+
if ($widget['id'] === $id) {
108+
unset($this->widgets[$position][$i]);
109+
return;
110+
}
111+
}
112+
}
113+
}
114+
115+
/**
116+
* Remove all widgets with $name from the group.
117+
*
118+
* @param string $name
119+
*/
120+
public function removeByName($name)
121+
{
122+
foreach ($this->widgets as $position => $widgets) {
123+
foreach ($widgets as $i => $widget) {
124+
if ($widget['arguments'][0] === $name) {
125+
unset($this->widgets[$position][$i]);
126+
}
127+
}
128+
}
129+
}
130+
131+
/**
132+
* Remove all widgets from $position from the group.
133+
*
134+
* @param int|string $position
135+
*/
136+
public function removeByPosition($position)
137+
{
138+
if (array_key_exists($position, $this->widgets)) {
139+
unset($this->widgets[$position]);
140+
}
141+
}
142+
143+
/**
144+
* Remove all widgets from the group.
145+
*/
146+
public function removeAll()
147+
{
148+
$this->widgets = [];
149+
}
150+
96151
/**
97152
* Set widget position.
98153
*
@@ -112,15 +167,15 @@ public function position($position)
112167
*/
113168
public function addWidget()
114169
{
115-
$this->addWidgetWithType('sync', func_get_args());
170+
return $this->addWidgetWithType('sync', func_get_args());
116171
}
117172

118173
/**
119174
* Add an async widget to the group.
120175
*/
121176
public function addAsyncWidget()
122177
{
123-
$this->addWidgetWithType('async', func_get_args());
178+
return $this->addWidgetWithType('async', func_get_args());
124179
}
125180

126181
/**
@@ -195,27 +250,31 @@ public function count()
195250

196251
return $count;
197252
}
198-
253+
199254
/**
200255
* Add a widget with a given type to the array.
201256
*
202257
* @param string $type
203-
* @param array $arguments
258+
* @param array $arguments
259+
* @return int
204260
*/
205261
protected function addWidgetWithType($type, array $arguments = [])
206262
{
207263
if (!isset($this->widgets[$this->position])) {
208264
$this->widgets[$this->position] = [];
209265
}
210266

267+
$id = $this->nextWidgetId;
211268
$this->widgets[$this->position][] = [
269+
'id' => $id,
212270
'arguments' => $arguments,
213271
'type' => $type,
214272
];
215273

216-
$this->count++;
217-
218274
$this->resetPosition();
275+
$this->nextWidgetId++;
276+
277+
return $id;
219278
}
220279

221280
/**

tests/WidgetGroupTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,64 @@ public function testCount()
140140
$this->assertSame(0, $this->widgetGroup->count());
141141

142142
$this->widgetGroup->addWidget('Slider');
143-
144143
$this->assertSame(1, $this->widgetGroup->count());
145144

146145
$this->widgetGroup->position(50)->addWidget('Slider');
146+
$this->assertSame(2, $this->widgetGroup->count());
147147

148+
$this->widgetGroup->position(50)->addWidget('Slider');
149+
$this->assertSame(3, $this->widgetGroup->count());
150+
}
151+
152+
public function testRemoveById()
153+
{
154+
$id1 = $this->widgetGroup->addWidget('Slider');
155+
$id2 = $this->widgetGroup->addWidget('Slider');
148156
$this->assertSame(2, $this->widgetGroup->count());
149157

158+
$this->widgetGroup->removeById($id1);
159+
$this->assertSame(1, $this->widgetGroup->count());
160+
161+
$this->widgetGroup->removeById($id2);
162+
$this->assertSame(0, $this->widgetGroup->count());
163+
}
164+
165+
public function testRemoveByName()
166+
{
167+
$this->widgetGroup->addWidget('Slider');
168+
$this->widgetGroup->addWidget('TestDefaultSlider');
169+
$this->widgetGroup->addWidget('Slider');
170+
$this->assertSame(3, $this->widgetGroup->count());
171+
172+
$this->widgetGroup->removeByName('Slider');
173+
$this->assertSame(1, $this->widgetGroup->count());
174+
175+
$this->widgetGroup->removeByName('TestDefaultSlider');
176+
$this->assertSame(0, $this->widgetGroup->count());
177+
}
178+
179+
public function testRemoveByPosition()
180+
{
181+
$this->widgetGroup->position(60)->addWidget('Slider');
182+
$this->widgetGroup->position(50)->addWidget('TestDefaultSlider');
150183
$this->widgetGroup->position(50)->addWidget('Slider');
184+
$this->assertSame(3, $this->widgetGroup->count());
185+
186+
$this->widgetGroup->removeByPosition(50);
187+
$this->assertSame(1, $this->widgetGroup->count());
151188

189+
$this->widgetGroup->removeByPosition(60);
190+
$this->assertSame(0, $this->widgetGroup->count());
191+
}
192+
193+
public function testRemoveAll()
194+
{
195+
$this->widgetGroup->addWidget('Slider');
196+
$this->widgetGroup->addWidget('TestDefaultSlider');
197+
$this->widgetGroup->addWidget('Slider');
152198
$this->assertSame(3, $this->widgetGroup->count());
199+
200+
$this->widgetGroup->removeAll();
201+
$this->assertSame(0, $this->widgetGroup->count());
153202
}
154203
}

0 commit comments

Comments
 (0)