Skip to content

Commit 81119d6

Browse files
committed
container improvements
1 parent 10387da commit 81119d6

File tree

6 files changed

+79
-32
lines changed

6 files changed

+79
-32
lines changed

spec/Arrilot/Widgets/Factories/WidgetFactorySpec.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Widgets\TestDefaultSlider;
88
use App\Widgets\TestMyClass;
99
use App\Widgets\TestRepeatableFeed;
10+
use App\Widgets\TestWidgetWithCustomContainer;
1011
use App\Widgets\TestWidgetWithDIInRun;
1112
use App\Widgets\TestWidgetWithParamsInRun;
1213
use Arrilot\Widgets\Misc\LaravelApplicationWrapper;
@@ -178,7 +179,7 @@ public function it_can_run_multiple_widgets(LaravelApplicationWrapper $wrapper)
178179
);
179180
}
180181

181-
public function it_can_run_async_widget(LaravelApplicationWrapper $wrapper)
182+
public function it_can_run_reloadable_widget(LaravelApplicationWrapper $wrapper)
182183
{
183184
$config = [];
184185
$params = [$config];
@@ -196,6 +197,24 @@ public function it_can_run_async_widget(LaravelApplicationWrapper $wrapper)
196197
);
197198
}
198199

200+
public function it_can_run_widget_with_custom_container(LaravelApplicationWrapper $wrapper)
201+
{
202+
$config = [];
203+
$params = [$config];
204+
205+
$wrapper->csrf_token()->willReturn('token_stub');
206+
$wrapper->call(Argument::any(), Argument::any())->willReturn(
207+
call_user_func_array([new TestWidgetWithCustomContainer([]), 'run'], [])
208+
);
209+
210+
$this->testWidgetWithCustomContainer($config)
211+
->shouldReturn(
212+
'<p id="arrilot-widget-container-1" data-id="123">Dummy Content'.
213+
'<script type="text/javascript">setTimeout( function() { $(\'#arrilot-widget-container-1\').load(\'/arrilot/load-widget\', '.$this->mockProduceJavascriptData('TestWidgetWithCustomContainer', $params).') }, 10000)</script>'.
214+
'</p>'
215+
);
216+
}
217+
199218
public function it_can_cache_widgets(LaravelApplicationWrapper $wrapper)
200219
{
201220
$wrapper->call(Argument::any(), Argument::any())->willReturn(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Widgets;
4+
5+
use Arrilot\Widgets\AbstractWidget;
6+
7+
class TestWidgetWithCustomContainer extends AbstractWidget
8+
{
9+
public $reloadTimeout = 10;
10+
11+
public function run()
12+
{
13+
return 'Dummy Content';
14+
}
15+
16+
public function placeholder()
17+
{
18+
return 'Placeholder here!';
19+
}
20+
21+
/**
22+
* Async and reloadable widgets are wrapped in container.
23+
* You can customize it by overwriting this method.
24+
*
25+
* @return array
26+
*/
27+
public function container()
28+
{
29+
return [
30+
'element' => 'p',
31+
'attributes' => 'data-id="123"',
32+
];
33+
}
34+
}

spec/Dummies/TestWidgetWithCustomCssClass.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/AbstractWidget.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ abstract class AbstractWidget
66
{
77
/**
88
* The number of seconds before each reload.
9+
* False means no reload at all.
910
*
10-
* @var int|float
11+
* @var int|float|bool
1112
*/
12-
public $reloadTimeout;
13+
public $reloadTimeout = false;
1314

1415
/**
1516
* The number of minutes before cache expires.
@@ -19,14 +20,6 @@ abstract class AbstractWidget
1920
*/
2021
public $cacheTime = false;
2122

22-
/**
23-
* The css class or classes that are applied to a special container (div)
24-
* that wraps all widget content.
25-
*
26-
* @var string
27-
*/
28-
public $cssClassForWrapper = 'arrilot-widget-container';
29-
3023
/**
3124
* Constructor.
3225
*
@@ -45,6 +38,7 @@ public function __construct($config)
4538

4639
/**
4740
* Placeholder for async widget.
41+
* You can customize it by overwriting this method.
4842
*
4943
* @return string
5044
*/
@@ -53,6 +47,20 @@ public function placeholder()
5347
return '';
5448
}
5549

50+
/**
51+
* Async and reloadable widgets are wrapped in container.
52+
* You can customize it by overwriting this method.
53+
*
54+
* @return array
55+
*/
56+
public function container()
57+
{
58+
return [
59+
'element' => 'div',
60+
'attributes' => 'style="display:inline" class="arrilot-widget-container"',
61+
];
62+
}
63+
5664
/**
5765
* Cache key that is used if caching is enabled.
5866
*

src/Factories/AbstractWidgetFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ protected function wrapContentInContainer($content)
165165
return $content;
166166
}
167167

168-
return '<div id="'.$this->javascriptFactory->getContainerId().'" style="display:inline" class="'.$this->widget->cssClassForWrapper.'">'.$content.'</div>';
168+
$container = $this->widget->container();
169+
if (empty($container['element'])) {
170+
$container['element'] = 'div';
171+
}
172+
173+
return '<'.$container['element'].' id="'.$this->javascriptFactory->getContainerId().'" '.$container['attributes'].'>'.$content.'</'.$container['element'].'>';
169174
}
170175
}

src/Factories/WidgetFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function run()
3434

3535
if ($timeout = $this->getReloadTimeout()) {
3636
$content .= $this->javascriptFactory->getReloader($timeout);
37+
3738
return $this->wrapContentInContainer($content);
3839
}
3940

0 commit comments

Comments
 (0)