/** * Registers a specific Materialize plugin and the related events. * @param string $name the name of the Materialize plugin * @param string|null $selector the name of the selector the plugin shall be attached to * @uses [yii\helper\BaseJson::encode()](http://www.yiiframework.com/doc-2.0/yii-helpers-basejson.html#encode()-detail) * to encode the [[clientOptions]] * @uses [[MaterializePluginAsset::register()]] * @uses [[registerClientEvents()]] */ protected function registerPlugin($name, $selector = null) { $view = $this->getView(); MaterializePluginAsset::register($view); $id = $this->options['id']; if (is_null($selector)) { $selector = '#' . $id; } if ($this->clientOptions !== false) { $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions); $js = "jQuery('{$selector}').{$name}({$options});"; $view->registerJs($js); } $this->registerClientEvents(); }
/** * Renders the widget and registers the plugin asset. * * @return string the result of widget execution to be outputted. * @uses [[renderItems()]] * @uses MaterializePluginAsset * @see MaterializePluginAsset|MaterializePluginAsset * @see \macgyer\yii2materializecss\lib\MaterializeWidgetTrait|MaterializeWidgetTrait */ public function run() { MaterializePluginAsset::register($this->getView()); $this->registerClientEvents(); return $this->renderItems($this->items, $this->options); }
/** * Registers the JS for the toggle button. * @param string $targetId the ID of the toggled navigation. */ protected function registerToggleButtonScript($targetId) { $view = $this->getView(); MaterializePluginAsset::register($view); $selector = '#' . $this->options['id'] . ' .button-collapse'; $js = "var sideNav = jQuery('#{$this->containerOptions['id']} > ul').clone();"; $js .= "sideNav.removeClass().addClass('side-nav').attr('id', '{$targetId}').appendTo('body');"; $js .= "jQuery('{$selector}').sideNav();"; $view->registerJs($js); }