2021-02-10 05:04:59 +08:00
|
|
|
<?php
|
2021-08-07 05:38:53 +08:00
|
|
|
/**
|
|
|
|
* @link http://www.yiiframework.com/
|
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
|
|
* @license http://www.yiiframework.com/license/
|
|
|
|
*/
|
2021-02-10 05:04:59 +08:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace yii\bootstrap5;
|
|
|
|
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
use yii\helpers\Json;
|
|
|
|
|
|
|
|
/**
|
2022-08-25 19:26:48 +08:00
|
|
|
* BootstrapWidgetTrait is the trait, which provides basic for all Bootstrap widgets features.
|
2021-02-10 05:04:59 +08:00
|
|
|
*
|
|
|
|
* Note: class, which uses this trait must declare public field named `options` with the array default value:
|
|
|
|
*
|
|
|
|
* ```php
|
|
|
|
* class MyWidget extends \yii\base\Widget
|
|
|
|
* {
|
|
|
|
* use BootstrapWidgetTrait;
|
|
|
|
*
|
|
|
|
* public $options = [];
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict.
|
|
|
|
*
|
|
|
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
|
|
|
*/
|
|
|
|
trait BootstrapWidgetTrait
|
|
|
|
{
|
|
|
|
/**
|
2022-08-25 19:26:48 +08:00
|
|
|
* @var array|false the options for the underlying Bootstrap JS plugin/component.
|
|
|
|
* Please refer to the corresponding Bootstrap plugin/component Web page for possible options.
|
|
|
|
* For example, [this page](https://getbootstrap.com/docs/5.1/components/modal/#options) shows
|
|
|
|
* how to use the "Modal" component and the supported options (e.g. "backdrop").
|
|
|
|
* If this property is false, `registerJs()` will not be called on the view to initialize the module.
|
2021-02-10 05:04:59 +08:00
|
|
|
*/
|
2021-08-04 14:03:41 +08:00
|
|
|
public $clientOptions = [];
|
2021-02-10 05:04:59 +08:00
|
|
|
/**
|
|
|
|
* @var array the event handlers for the underlying Bootstrap JS plugin.
|
|
|
|
* Please refer to the corresponding Bootstrap plugin Web page for possible events.
|
2022-08-25 19:26:48 +08:00
|
|
|
* For example, [this page](https://getbootstrap.com/docs/5.1/components/modal/#events) shows
|
|
|
|
* how to use the "Modal" plugin and the supported events (e.g. "shown.bs.modal").
|
2021-02-10 05:04:59 +08:00
|
|
|
*/
|
2021-08-04 14:03:41 +08:00
|
|
|
public $clientEvents = [];
|
2021-02-10 05:04:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the widget.
|
|
|
|
* This method will register the bootstrap asset bundle. If you override this method,
|
|
|
|
* make sure you call the parent implementation first.
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
parent::init();
|
|
|
|
if (!isset($this->options['id'])) {
|
|
|
|
$this->options['id'] = $this->getId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-25 19:26:48 +08:00
|
|
|
* Registers a specific Bootstrap plugin/component and the related events.
|
|
|
|
*
|
2021-02-10 05:04:59 +08:00
|
|
|
* @param string $name the name of the Bootstrap plugin
|
|
|
|
*/
|
|
|
|
protected function registerPlugin(string $name)
|
|
|
|
{
|
2022-08-25 19:26:48 +08:00
|
|
|
/**
|
|
|
|
* @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
|
|
|
|
*/
|
|
|
|
$jsPlugins = [
|
|
|
|
'alert',
|
|
|
|
'button',
|
|
|
|
'carousel',
|
|
|
|
'collapse',
|
|
|
|
'dropdown',
|
|
|
|
'modal',
|
|
|
|
'offcanvas',
|
|
|
|
'popover',
|
|
|
|
'scrollspy',
|
|
|
|
'tab',
|
|
|
|
'toast',
|
|
|
|
'tooltip'
|
|
|
|
];
|
|
|
|
if (in_array($name, $jsPlugins, true)) {
|
|
|
|
$view = $this->getView();
|
|
|
|
BootstrapPluginAsset::register($view);
|
|
|
|
// 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
|
|
|
|
if (
|
|
|
|
$this->clientOptions !== false
|
|
|
|
|| !empty($this->clientOptions)
|
|
|
|
|| in_array($name, ['popover', 'toast', 'tooltip'], true)
|
|
|
|
) {
|
|
|
|
$name = ucfirst($name);
|
|
|
|
$id = $this->options['id'];
|
|
|
|
$options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
|
|
|
|
$view->registerJs("(new bootstrap.$name('#$id', $options));");
|
|
|
|
}
|
2021-02-10 05:04:59 +08:00
|
|
|
|
2022-08-25 19:26:48 +08:00
|
|
|
$this->registerClientEvents();
|
2022-03-09 21:04:26 +08:00
|
|
|
}
|
2021-02-10 05:04:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers JS event handlers that are listed in [[clientEvents]].
|
|
|
|
*/
|
|
|
|
protected function registerClientEvents()
|
|
|
|
{
|
|
|
|
if (!empty($this->clientEvents)) {
|
|
|
|
$id = $this->options['id'];
|
|
|
|
$js = [];
|
|
|
|
foreach ($this->clientEvents as $event => $handler) {
|
2022-08-25 19:26:48 +08:00
|
|
|
$js[] = "document.getElementById('$id').addEventListener('$event', $handler);";
|
2021-02-10 05:04:59 +08:00
|
|
|
}
|
|
|
|
$this->getView()->registerJs(implode("\n", $js));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|