* @author Qiang Xue * @since 2.0 */ class Widget extends \yii\base\Widget { /** * @var boolean whether to use the responsive version of Bootstrap. */ public static $responsive = true; /** * @var array the HTML attributes for the widget container tag. */ public $options = array(); /** * @var array the options for the underlying Bootstrap JS plugin. * Please refer to the corresponding Bootstrap plugin Web page for possible options. * For example, [this page](http://twitter.github.io/bootstrap/javascript.html#modals) shows * how to use the "Modal" plugin and the supported options (e.g. "remote"). */ public $clientOptions = array(); /** * @var array the event handlers for the underlying Bootstrap JS plugin. * Please refer to the corresponding Bootstrap plugin Web page for possible events. * For example, [this page](http://twitter.github.io/bootstrap/javascript.html#modals) shows * how to use the "Modal" plugin and the supported events (e.g. "shown"). */ public $clientEvents = array(); /** * Initializes the widget. * This method will register the bootstrap asset bundle. If you override this method, * make sure you call the parent implementation first. */ public function init() { parent::init(); if (!isset($this->options['id'])) { $this->options['id'] = $this->getId(); } } /** * Registers a specific Bootstrap plugin and the related events * @param string $name the name of the Bootstrap plugin */ protected function registerPlugin($name) { $id = $this->options['id']; $view = $this->getView(); if (self::$responsive) { ResponsiveAsset::register($view); } else { BootstrapAsset::register($view); } /** @var \yii\web\AssetBundle $assetClass */ $assetClass = 'yii\bootstrap\\' . ucfirst($name); $assetClass::register($view); if ($this->clientOptions !== false) { $options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); $js = "jQuery('#$id').$name($options);"; $view->registerJs($js); } if (!empty($this->clientEvents)) { $js = array(); foreach ($this->clientEvents as $event => $handler) { $js[] = "jQuery('#$id').on('$event', $handler);"; } $view->registerJs(implode("\n", $js)); } } }