Skip to content

Commit

Permalink
alter changed plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
robson committed Jun 29, 2021
1 parent 2781f98 commit 7a81a0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
44 changes: 31 additions & 13 deletions src/LightGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
}

}
14 changes: 1 addition & 13 deletions src/LightGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit 7a81a0d

Please sign in to comment.