* @author Qiang Xue * @author Paul Klimov */ trait BootstrapWidgetTrait { /** * @var array|bool 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://getbootstrap.com/javascript/#modals) shows * 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 = []; /** * @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://getbootstrap.com/javascript/#modals) shows * how to use the "Modal" plugin and the supported events (e.g. "shown"). */ public $clientEvents = []; /** * 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(); } } /** * Registers a specific Bootstrap plugin and the related events * @param string $name the name of the Bootstrap plugin */ protected function registerPlugin(string $name) { $view = $this->getView(); BootstrapPluginAsset::register($view); $id = $this->options['id']; if ($this->clientOptions !== false) { $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions); $js = "jQuery('#$id').$name($options);"; $view->registerJs($js); } $this->registerClientEvents(); } /** * 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) { $js[] = "jQuery('#$id').on('$event', $handler);"; } $this->getView()->registerJs(implode("\n", $js)); } } }