From fff5e34b9aa1edad7ddae216156d87af00e6eb13 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 13 Oct 2022 15:22:59 +0300 Subject: [PATCH 1/2] Add fools protection to BootstrapWidgetTrait --- src/BootstrapWidgetTrait.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/BootstrapWidgetTrait.php b/src/BootstrapWidgetTrait.php index 5c3ce9e..1d9ea91 100644 --- a/src/BootstrapWidgetTrait.php +++ b/src/BootstrapWidgetTrait.php @@ -27,10 +27,6 @@ * ``` * * This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict. - * - * @author Antonio Ramirez - * @author Qiang Xue - * @author Paul Klimov */ trait BootstrapWidgetTrait { @@ -100,7 +96,13 @@ protected function registerPlugin(string $name) $name = ucfirst($name); $id = $this->options['id']; $options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions); - $view->registerJs("(new bootstrap.$name('#$id', $options));"); + $view->registerJs(<< 0) { + (new bootstrap.$name('#$id', $options)); +} +JS + ); } $this->registerClientEvents(); @@ -114,10 +116,14 @@ protected function registerClientEvents() { if (!empty($this->clientEvents)) { $id = $this->options['id']; - $js = []; + $js = [ + "var bs5pluginElement = document.getElementById('$id');", + "if (bs5pluginElement.lenght > 0) {", + ]; foreach ($this->clientEvents as $event => $handler) { - $js[] = "document.getElementById('$id').addEventListener('$event', $handler);"; + $js[] = " bs5pluginElement.addEventListener('$event', $handler);"; } + $js[] = "}"; $this->getView()->registerJs(implode("\n", $js)); } } From 762824be70af8a662d910d3f109b4c97016cf0dd Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 13 Oct 2022 15:29:53 +0300 Subject: [PATCH 2/2] Update BootstrapWidgetTrait.php --- src/BootstrapWidgetTrait.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/BootstrapWidgetTrait.php b/src/BootstrapWidgetTrait.php index 1d9ea91..61199e4 100644 --- a/src/BootstrapWidgetTrait.php +++ b/src/BootstrapWidgetTrait.php @@ -96,7 +96,8 @@ protected function registerPlugin(string $name) $name = ucfirst($name); $id = $this->options['id']; $options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions); - $view->registerJs(<<registerJs( +<< 0) { (new bootstrap.$name('#$id', $options));