Skip to content

Commit

Permalink
New widgetGroup->wrap() method
Browse files Browse the repository at this point in the history
  • Loading branch information
arrilot committed Jun 6, 2017
1 parent 21d9a31 commit b1017c7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,16 @@ equals

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

You can also set a separator to display between widgets in a group.
`Widget::group('sidebar')->setSeparator('<hr>');`
You can set a separator that will be display between widgets in a group.
`Widget::group('sidebar')->setSeparator('<hr>')->...;`

You can also wrap each widget in a group using `wrap` method like that.
```php
Widget::group('sidebar')->wrap(function ($content, $index, $total) {
// $total is a total number of widgets in a group.
return "<div class='widget-{$index}'>{$content}</div>";
})->...;
```

### Checking the state of a widget group

Expand Down
51 changes: 46 additions & 5 deletions src/WidgetGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class WidgetGroup
*/
protected $count = 0;

/**
* A callback that defines extra markup that wraps every widget in the group.
*
* @var callable
*/
protected $wrapCallback;

/**
* @param $name
* @param ApplicationWrapperContract $app
Expand All @@ -72,12 +79,12 @@ public function display()
ksort($this->widgets);

$output = '';
$count = 0;
$index = 0;
foreach ($this->widgets as $position => $widgets) {
foreach ($widgets as $widget) {
$count++;
$output .= $this->displayWidget($widget);
if ($this->count !== $count) {
$output .= $this->performWrap($this->displayWidget($widget), $index, $this->count);
$index++;
if ($this->count !== $index) {
$output .= $this->separator;
}
}
Expand Down Expand Up @@ -119,7 +126,7 @@ public function addAsyncWidget()
/**
* Getter for position.
*
* @return array
* @return int
*/
public function getPosition()
{
Expand All @@ -140,6 +147,20 @@ public function setSeparator($separator)
return $this;
}

/**
* Setter for $this->wrapCallback.
*
* @param callable $callable
*
* @return $this
*/
public function wrap(callable $callable)
{
$this->wrapCallback = $callable;

return $this;
}

/**
* Check if there are any widgets in the group.
*
Expand Down Expand Up @@ -219,4 +240,24 @@ protected function resetPosition()
{
$this->position = 100;
}

/**
* Wraps widget content in a special markup defined by $this->wrap().
*
* @param string $content
* @param int $index
* @param int $total
*
* @return string
*/
protected function performWrap($content, $index, $total)
{
if (is_null($this->wrapCallback)) {
return $content;
}

$callback = $this->wrapCallback;

return $callback($content, $index, $total);
}
}
23 changes: 23 additions & 0 deletions tests/WidgetGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ public function testSeparator()
'</div>', $output);
}

public function testWrap()
{
$this->widgetGroup->addWidget('Slider', ['slides' => 5]);
$this->widgetGroup->addAsyncWidget('Slider');

$output = $this->widgetGroup->wrap(function($content, $index, $count) {
return "<div class='widget widget-{$index}-{$count}'>{$content}</div>";
})->display();

$this->assertEquals(
'<div class=\'widget widget-0-2\'>Slider was executed with $slides = 5 foo: bar</div>'.
'<div class=\'widget widget-1-2\'><div id="arrilot-widget-container-2" style="display:inline" class="arrilot-widget-container">Placeholder here!'.
'<script type="text/javascript">'.
'var widgetTimer2 = setInterval(function() {'.
'if (window.$) {'.
"$('#arrilot-widget-container-2').load('".$this->ajaxUrl('Slider', [], 2)."');".
'clearInterval(widgetTimer2);'.
'}'.
'}, 100);'.
'</script>'.
'</div></div>', $output);
}

public function testIsEmpty()
{
$this->assertTrue($this->widgetGroup->isEmpty());
Expand Down

0 comments on commit b1017c7

Please sign in to comment.