Skip to content

Commit b1017c7

Browse files
committed
New widgetGroup->wrap() method
1 parent 21d9a31 commit b1017c7

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,16 @@ equals
323323

324324
`Widget::group('sidebar')->position(100)->addWidget('files');`
325325

326-
You can also set a separator to display between widgets in a group.
327-
`Widget::group('sidebar')->setSeparator('<hr>');`
326+
You can set a separator that will be display between widgets in a group.
327+
`Widget::group('sidebar')->setSeparator('<hr>')->...;`
328+
329+
You can also wrap each widget in a group using `wrap` method like that.
330+
```php
331+
Widget::group('sidebar')->wrap(function ($content, $index, $total) {
332+
// $total is a total number of widgets in a group.
333+
return "<div class='widget-{$index}'>{$content}</div>";
334+
})->...;
335+
```
328336

329337
### Checking the state of a widget group
330338

src/WidgetGroup.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ class WidgetGroup
5151
*/
5252
protected $count = 0;
5353

54+
/**
55+
* A callback that defines extra markup that wraps every widget in the group.
56+
*
57+
* @var callable
58+
*/
59+
protected $wrapCallback;
60+
5461
/**
5562
* @param $name
5663
* @param ApplicationWrapperContract $app
@@ -72,12 +79,12 @@ public function display()
7279
ksort($this->widgets);
7380

7481
$output = '';
75-
$count = 0;
82+
$index = 0;
7683
foreach ($this->widgets as $position => $widgets) {
7784
foreach ($widgets as $widget) {
78-
$count++;
79-
$output .= $this->displayWidget($widget);
80-
if ($this->count !== $count) {
85+
$output .= $this->performWrap($this->displayWidget($widget), $index, $this->count);
86+
$index++;
87+
if ($this->count !== $index) {
8188
$output .= $this->separator;
8289
}
8390
}
@@ -119,7 +126,7 @@ public function addAsyncWidget()
119126
/**
120127
* Getter for position.
121128
*
122-
* @return array
129+
* @return int
123130
*/
124131
public function getPosition()
125132
{
@@ -140,6 +147,20 @@ public function setSeparator($separator)
140147
return $this;
141148
}
142149

150+
/**
151+
* Setter for $this->wrapCallback.
152+
*
153+
* @param callable $callable
154+
*
155+
* @return $this
156+
*/
157+
public function wrap(callable $callable)
158+
{
159+
$this->wrapCallback = $callable;
160+
161+
return $this;
162+
}
163+
143164
/**
144165
* Check if there are any widgets in the group.
145166
*
@@ -219,4 +240,24 @@ protected function resetPosition()
219240
{
220241
$this->position = 100;
221242
}
243+
244+
/**
245+
* Wraps widget content in a special markup defined by $this->wrap().
246+
*
247+
* @param string $content
248+
* @param int $index
249+
* @param int $total
250+
*
251+
* @return string
252+
*/
253+
protected function performWrap($content, $index, $total)
254+
{
255+
if (is_null($this->wrapCallback)) {
256+
return $content;
257+
}
258+
259+
$callback = $this->wrapCallback;
260+
261+
return $callback($content, $index, $total);
262+
}
222263
}

tests/WidgetGroupTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ public function testSeparator()
9494
'</div>', $output);
9595
}
9696

97+
public function testWrap()
98+
{
99+
$this->widgetGroup->addWidget('Slider', ['slides' => 5]);
100+
$this->widgetGroup->addAsyncWidget('Slider');
101+
102+
$output = $this->widgetGroup->wrap(function($content, $index, $count) {
103+
return "<div class='widget widget-{$index}-{$count}'>{$content}</div>";
104+
})->display();
105+
106+
$this->assertEquals(
107+
'<div class=\'widget widget-0-2\'>Slider was executed with $slides = 5 foo: bar</div>'.
108+
'<div class=\'widget widget-1-2\'><div id="arrilot-widget-container-2" style="display:inline" class="arrilot-widget-container">Placeholder here!'.
109+
'<script type="text/javascript">'.
110+
'var widgetTimer2 = setInterval(function() {'.
111+
'if (window.$) {'.
112+
"$('#arrilot-widget-container-2').load('".$this->ajaxUrl('Slider', [], 2)."');".
113+
'clearInterval(widgetTimer2);'.
114+
'}'.
115+
'}, 100);'.
116+
'</script>'.
117+
'</div></div>', $output);
118+
}
119+
97120
public function testIsEmpty()
98121
{
99122
$this->assertTrue($this->widgetGroup->isEmpty());

0 commit comments

Comments
 (0)