Skip to content

Commit

Permalink
loaded asset inline
Browse files Browse the repository at this point in the history
  • Loading branch information
simialbi committed Oct 4, 2021
1 parent c247402 commit 0a742f1
Show file tree
Hide file tree
Showing 4 changed files with 4,407 additions and 9 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"require": {
"php": ">=7.4",
"yiisoft/yii2": "^2.0.0",
"simialbi/yii2-simialbi-base": ">=0.10.2 <1.0 | ^1.0.0",
"npm-asset/hotwired--turbo": "^7.0.0-beta.4"
"simialbi/yii2-simialbi-base": ">=0.10.2 <1.0 | ^1.0.0"
},
"require-dev": {
"yiisoft/yii2-coding-standards": "~2.0",
Expand Down
37 changes: 35 additions & 2 deletions src/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace simialbi\yii2\turbo;

use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;
Expand Down Expand Up @@ -76,16 +77,48 @@ public function init(): void
$this->options['target'] = $this->target;
}

echo Html::beginTag('turbo-frame', $this->options);
if ($this->getRequestTurboFrame() === $this->options['id']) {
if (isset($this->options['src'])) {
unset($this->options['src']);
}
$view = $this->getView();
$view->clear();
$view->beginPage();
echo Html::beginTag('turbo-frame', $this->options);
$view->head();
$view->beginBody();
} else {
echo Html::beginTag('turbo-frame', $this->options);
}
}

/**
* {@inheritDoc}
*/
public function run()
{
TurboAsset::register($this->view);
if ($this->getRequestTurboFrame() === $this->options['id']) {
$view = $this->getView();
$view->endBody();
$view->endPage(true);
echo Html::endTag('turbo-frame');
Yii::$app->end();

return;
}

TurboAsset::register($this->view);
echo Html::endTag('turbo-frame');
}

/**
* Check if turbo is needed
* @return string|null
*/
protected function getRequestTurboFrame(): ?string
{
$headers = Yii::$app->getRequest()->getHeaders();

return $headers->get('Turbo-Frame');
}
}
12 changes: 7 additions & 5 deletions src/TurboAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace simialbi\yii2\turbo;

use simialbi\yii2\web\AssetBundle;
use yii\web\View;

/**
* The turbo asset bundle
Expand All @@ -16,22 +17,23 @@ class TurboAsset extends AssetBundle
/**
* @inheritdoc
*/
public $sourcePath = '@npm/hotwired--turbo/dist';
public $js = [
'js/turbo.es2017-umd.js'
];

/**
* @inheritdoc
*/
public $js = [
'turbo.es2017-umd.js'
public $jsOptions = [
'position' => View::POS_HEAD
];

/**
* @inheritdoc
*/
public $publishOptions = [
'only' => [
'turbo.es2017-umd.js',
'turbo.es2017-umd.js.map'
'turbo.es2017-umd.js'
]
];
}
Loading

0 comments on commit 0a742f1

Please sign in to comment.