Merge pull request #37 from julianrutten/master (fix #36)

fix: clientOptions=false skips registerJs
This commit is contained in:
simialbi 2022-03-10 23:46:37 +01:00 committed by GitHub
commit b8f12cddd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 6 deletions

View File

@ -3,7 +3,7 @@ Yii Framework 2 bootstrap5 extension Change Log
2.0.3 under development 2.0.3 under development
----------------------- -----------------------
- Enh #36: `BootstrapWidgetTrait::$clientOptions = false` disables registerJs in `BootstrapWidgetTrait` (julianrutten)
- Enh #33: Updated russian translations (WinterSilence) - Enh #33: Updated russian translations (WinterSilence)
- Enh #28: Added translations (simialbi) - Enh #28: Added translations (simialbi)
- Enh #24: Accept `Breadcrumbs::$homeLink = false` to omit "Home" link (fetus-hina) - Enh #24: Accept `Breadcrumbs::$homeLink = false` to omit "Home" link (fetus-hina)

View File

@ -35,10 +35,11 @@ use yii\helpers\Json;
trait BootstrapWidgetTrait trait BootstrapWidgetTrait
{ {
/** /**
* @var array the options for the underlying Bootstrap JS plugin. * @var array|bool the options for the underlying Bootstrap JS plugin.
* Please refer to the corresponding Bootstrap plugin Web page for possible options. * Please refer to the corresponding Bootstrap plugin Web page for possible options.
* For example, [this page](http://getbootstrap.com/javascript/#modals) shows * For example, [this page](http://getbootstrap.com/javascript/#modals) shows
* how to use the "Modal" plugin and the supported options (e.g. "remote"). * how to use the "Modal" plugin and the supported options (e.g. "remote").
* If this property is false, registerJs will not be called on the view to initialize the module.
*/ */
public $clientOptions = []; public $clientOptions = [];
/** /**
@ -76,9 +77,11 @@ trait BootstrapWidgetTrait
$id = $this->options['id']; $id = $this->options['id'];
$options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions); if($this->clientOptions !== false){
$js = "jQuery('#$id').$name($options);"; $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
$view->registerJs($js); $js = "jQuery('#$id').$name($options);";
$view->registerJs($js);
}
$this->registerClientEvents(); $this->registerClientEvents();
} }

View File

@ -142,4 +142,51 @@ HTML;
$this->assertContainsWithoutLE("jQuery('#w0').toast();", $options); $this->assertContainsWithoutLE("jQuery('#w0').toast();", $options);
} }
/**
* @see https://github.com/yiisoft/yii2-bootstrap5/issues/36
*/
public function testWidgetNoInitialization()
{
Toast::$counter = 0;
ob_start();
$toast = Toast::begin([
'title' => 'Toast title',
'clientOptions' => false,
'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']]
]);
echo 'test';
Toast::end();
$out = ob_get_clean();
$this->assertFalse($toast->clientOptions);
$this->assertArrayNotHasKey(View::POS_READY,Yii::$app->view->js);
}
/**
*
* @see https://github.com/yiisoft/yii2-bootstrap5/issues/36
*/
public function testWidgetInitializationTrue()
{
Toast::$counter = 0;
ob_start();
$toast = Toast::begin([
'title' => 'Toast title',
'clientOptions' => true,
'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']]
]);
echo 'test';
Toast::end();
$out = ob_get_clean();
$this->assertTrue($toast->clientOptions);
$this->assertArrayHasKey(View::POS_READY,Yii::$app->view->js);
$js = Yii::$app->view->js[View::POS_READY];
$this->assertInternalType(IsType::TYPE_ARRAY, $js);
$options = array_shift($js);
$this->assertContainsWithoutLE("jQuery('#w0').toast(true);", $options);
}
} }