forked from 2amigos/yii2-gallery-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gallery.php
173 lines (160 loc) · 5.43 KB
/
Gallery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* @copyright Copyright (c) 2013 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\gallery;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
/**
* Gallery renders a BlueImp Gallery items
*
* @author Antonio Ramirez <[email protected]>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\gallery
*/
class Gallery extends Widget
{
/**
* @var array the HTML attributes for the links container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var boolean whether to write html template of Gallery on initialization.
* Useful when you want multiple galleries on page without code repeating.
* Need to specify id of container through clientOptions though.
*/
public $renderTemplate = true;
/**
* @var array the HTML attributes for the lightbox container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $templateOptions = [];
/**
* @var array the options for the BlueImp Gallery plugin.
* Please refer to the BlueImp Gallery plugin Web page for possible options.
* @see https://github.com/blueimp/Gallery/blob/master/README.md#setup
*/
public $clientOptions = [];
/**
* @var array the event handlers for the underlying Bootstrap Switch 3 input JS plugin.
* Please refer to the [BlueImp Gallery plugin](https://github.com/blueimp/Gallery/blob/master/README.md#event-callbacks)
* for information about their callbacks.
*/
public $clientEvents = [];
/**
* @var array The array of items that compound the gallery. The syntax is as follows:
*
* - src: string, the image to display
* - url: string, the image to display on the lightbox. If none found, will display `src`
* - options: HTML attributes of the link
*/
public $items = array();
/**
* @var bool whether to display the controls on initialization
*/
public $showControls = true;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
$this->templateOptions['id'] = ArrayHelper::getValue($this->templateOptions, 'id', 'blueimp-gallery');
Html::addCssClass($this->templateOptions, 'blueimp-gallery');
if ($this->showControls) {
Html::addCssClass($this->templateOptions, 'blueimp-gallery-controls');
}
foreach($this->clientEvents as $key => $event) {
if(!($event instanceof JsExpression)) {
$this->clientOptions[$key] = new JsExpression($event);
}
}
}
/**
* @inheritdoc
*/
public function run()
{
if (empty($this->items)) {
return null;
}
echo $this->renderItems();
if ($this->renderTemplate) {
echo $this->renderTemplate();
}
$this->registerClientScript();
}
/**
* @return string the items that are need to be rendered.
*/
public function renderItems()
{
$items = [];
foreach ($this->items as $item) {
$items[] = $this->renderItem($item);
}
return Html::tag('div', implode("\n", array_filter($items)), $this->options);
}
/**
* @param mixed $item
* @return null|string the item to render
*/
public function renderItem($item)
{
if (is_string($item)) {
return Html::a(Html::img($item), $item, ['class' => 'gallery-item']);
}
$src = ArrayHelper::getValue($item, 'src');
if ($src === null) {
return null;
}
$url = ArrayHelper::getValue($item, 'url', $src);
$options = ArrayHelper::getValue($item, 'options', []);
Html::addCssClass($options, 'gallery-item');
return Html::a(Html::img($src), $url, $options);
}
/**
* Renders the template to display the images on a lightbox
* @return string the template
*/
public function renderTemplate()
{
$template[] = '<div class="slides"></div>';
$template[] = '<h3 class="title"></h3>';
$template[] = '<a class="prev">‹</a>';
$template[] = '<a class="next">›</a>';
$template[] = '<a class="close">×</a>';
$template[] = '<a class="play-pause"></a>';
$template[] = '<ol class="indicator"></ol>';
return Html::tag('div', implode("\n", $template), $this->templateOptions);
}
/**
* Registers the client script required for the plugin
*/
public function registerClientScript()
{
$view = $this->getView();
GalleryAsset::register($view);
$id = $this->options['id'];
$options = Json::encode($this->clientOptions);
$js = "dosamigos.gallery.registerLightBoxHandlers('#$id a', $options);";
$view->registerJs($js);
if (!empty($this->clientEvents)) {
$js = [];
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('$id').on('$event', $handler);";
}
$view->registerJs(implode("\n", $js));
}
}
}