From 7a81a0d8fb7a81c0c37501773aa41bb68ee13f6c Mon Sep 17 00:00:00 2001 From: robson Date: Tue, 29 Jun 2021 11:15:58 -0300 Subject: [PATCH] alter changed plugins --- src/LightGallery.php | 44 +++++++++++++++++++++++++++------------ src/LightGalleryAsset.php | 14 +------------ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/LightGallery.php b/src/LightGallery.php index 27ad6fe..3b6ff31 100644 --- a/src/LightGallery.php +++ b/src/LightGallery.php @@ -2,12 +2,28 @@ namespace dynamikaweb\lightgallery; +use Yii; use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\helpers\Json; class LightGallery extends \yii\base\Widget { + const NPM_PLUGINS = [ + 'lgThumbnail' => 'plugins/thumbnail/lg-thumbnail.min.js', + 'lgZoom' => 'plugins/zoom/lg-zoom.min.js', + 'lgAutoplay' => 'plugins/autoplay/lg-autoplay.min.js', + 'lgComment' => 'plugins/comment/lg-comment.min.js', + 'lgFullscreen' => 'plugins/fullscreen/lg-fullscreen.min.js', + 'lgHash' => 'plugins/hash/lg-hash.min.js', + 'lgMediumZoom' => 'plugins/mediumZoom/lg-medium-zoom.min.js', + 'lgPager' => 'plugins/pager/lg-pager.min.js', + 'lgRelativeCaption' => 'plugins/relativeCaption/lg-relative-caption.min.js', + 'lgRotate' => 'plugins/rotate/lg-rotate.min.js', + 'lgShare' => 'plugins/share/lg-share.min.js', + 'lgVideo' => 'plugins/video/lg-video.min.js' + ]; + public $options = []; public $plugins = []; public $pluginOptions = []; @@ -18,7 +34,11 @@ class LightGallery extends \yii\base\Widget public function init() { - LightGalleryAsset::register($view); + LightGalleryAsset::register($this->view); + $bundle = Yii::$app->assetManager->getBundle('\dynamikaweb\lightgallery\LightGalleryAsset'); + foreach($this->plugins as $plugin) { + $this->view->registerJsFile('@web'.$bundle->baseUrl.'/'.self::NPM_PLUGINS[$plugin]); + } $this->registerClientScript(); } @@ -71,19 +91,17 @@ public function renderItem($item, $tag) public function registerClientScript() { - $view = $this->getView(); - $plugins = Json::encode($this->plugins); + + $id = $this->id; $pluginOptions = Json::encode($this->pluginOptions); - $js = " - var plugins = []; - var pluginsArray = $plugins; - pluginsArray.forEach(function(item){ - plugins.push(new item); - console.log(item); - }); - console.log(plugins);"; - $js .= 'lightGallery(document.getElementById("'.$this->id.'"), plugins)'; - $view->registerJs($js); + $plugins = str_replace('"','',Json::encode($this->plugins)); + $js =" + var lightGallery_pluginsOptions_$id = {$pluginOptions}; + lightGallery_pluginsOptions_$id.plugins = {$plugins}; + + lightGallery(document.getElementById('$id'), lightGallery_pluginsOptions_$id);"; + + $this->view->registerJs($js); } } \ No newline at end of file diff --git a/src/LightGalleryAsset.php b/src/LightGalleryAsset.php index f3021e4..7807a32 100644 --- a/src/LightGalleryAsset.php +++ b/src/LightGalleryAsset.php @@ -12,19 +12,7 @@ class LightGalleryAsset extends AssetBundle ]; public $js = [ - 'lightgallery.umd.js', - 'plugins/thumbnail/lg-thumbnail.umd.js', - 'plugins/zoom/lg-zoom.umd.js', - 'plugins/autoplay/lg-autoplay.umd.js', - 'plugins/comment/lg-comment.umd.js', - 'plugins/fullscreen/lg-fullscreen.umd.js', - 'plugins/hash/lg-hash.umd.js', - 'plugins/mediumZoom/lg-medium-zoom.umd.js', - 'plugins/pager/lg-pager.umd.js', - 'plugins/relativeCaption/lg-relative-caption.umd.js', - 'plugins/rotate/lg-rotate.umd.js', - 'plugins/share/lg-share.umd.js', - 'plugins/video/lg-video.umd.js', + 'lightgallery.min.js', ]; public $depends = [];