Skip to content

Commit

Permalink
Add loadOnAnyRequest flag for HtmlInline widget
Browse files Browse the repository at this point in the history
  • Loading branch information
digitv committed Jan 18, 2018
1 parent 5a4434b commit 40a43de
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
18 changes: 13 additions & 5 deletions Yii2Live.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use digitv\yii2live\components\Response;
use digitv\yii2live\components\View;
use digitv\yii2live\widgets\BaseLiveWidget;
use digitv\yii2live\widgets\HtmlInline;
use digitv\yii2live\yii2sockets\YiiNodeSocketFrameLoader;
use Yii;
use yii\base\Application;
Expand Down Expand Up @@ -137,26 +138,33 @@ public function isLiveRequest() {
*/
public function setWidgetData($widget) {
$widgetId = $widget instanceof BaseLiveWidget ? $widget->id : $widget->owner->id;
/** @var BaseLiveWidget|HtmlInline $widgetBase */
$widgetBase = $widget instanceof BaseLiveWidget ? $widget : $widget->owner;
$data = $widget->getWidgetLiveData();
$required = isset($widgetBase->loadOnAnyRequest) && $widgetBase->loadOnAnyRequest;
/** @var Response $response */
$response = Yii::$app->response;
if($required) {
$response->livePageWidgetsRequired[] = $widgetId;
}
if($widget->widgetType === WidgetBehavior::LIVE_WIDGET_TYPE_COMMANDS && !empty($data['data']) && is_array($data['data'])) {
$response->liveCommands = ArrayHelper::merge($response->liveCommands, $data['data']);
}
$response->livePageWidgets[$widgetId] = $data;
//Send response immediately for this context
if($this->getContextType() === static::CONTEXT_TYPE_EXACT && $this->getContextId() === $widgetId) {
$response->livePageWidgets = [
$widgetId => $data,
];
foreach ($response->livePageWidgets as $_widgetId => $_widgetData) {
if($_widgetId !== $widgetId && !in_array($_widgetId, $response->livePageWidgetsRequired)) {
unset($response->livePageWidgets[$_widgetId]);
}
}
/** @var View $view */
$view = Yii::$app->view;
$response->clearOutputBuffers();
$response->content = $view->renderFile($view->liveLayoutFile, [ 'content' => '' ]);
$response->data = [];
$response->send();
Yii::$app->end();
} else {
$response->livePageWidgets[$widgetId] = $data;
}
}

Expand Down
2 changes: 2 additions & 0 deletions components/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class Response extends \yii\web\Response
{
public $livePageWidgets = [];
/** @var array Not removable widgets */
public $livePageWidgetsRequired = [];

public $liveCommands = [];

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "digitv/yii2live",
"version": "1.1.7",
"version": "1.1.8",
"description": "Yii2 Live package",
"keywords": ["yii2", "ajax", "live", "node.js"],
"type": "yii2-extension",
Expand Down
2 changes: 2 additions & 0 deletions widgets/HtmlInline.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class HtmlInline extends Widget
public $liveRequestMethod;
/** @var bool Enable replace animation on AJAX request */
public $liveReplaceAnimation;
/** @var bool Do not remove widget response for Yii2Live::CONTEXT_TYPE_EXACT with different id */
public $loadOnAnyRequest = false;

/**
* @inheritdoc
Expand Down

0 comments on commit 40a43de

Please sign in to comment.