1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: MetalGuardian |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace metalguardian\fotorama; |
7
|
|
|
|
8
|
|
|
use Yii; |
9
|
|
|
use yii\base\Widget; |
10
|
|
|
use yii\helpers\Html; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
use yii\web\View; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Fotorama |
16
|
|
|
* |
17
|
|
|
* @package metalguardian\fotorama |
18
|
|
|
*/ |
19
|
|
|
class Fotorama extends Widget |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Choose fotorama version from CDNJS |
24
|
|
|
* true - use current version from CDN |
25
|
|
|
* '4.5.0' - use custom version |
26
|
|
|
* |
27
|
|
|
* @var bool | string |
28
|
|
|
*/ |
29
|
|
|
public static $useCDN = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Current Fotorama widget version |
33
|
|
|
*/ |
34
|
|
|
const VERSION = '4.6.3'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Widget options |
38
|
|
|
* |
39
|
|
|
* ```php |
40
|
|
|
* 'options' => [ |
41
|
|
|
* 'width' => '50%', // Number or String. Stage container width in pixels or percents. |
42
|
|
|
* 'minwidth' => '40%', // Number or String. Stage container minimum width in pixels or percents. |
43
|
|
|
* 'maxwidth' => 300, // Number or String. Stage container maximum width in pixels or percents. |
44
|
|
|
* 'height' => 500, // Number or String. Stage container height in pixels or percents. |
45
|
|
|
* 'minheight' => '500px', // Number or String. Stage container minimum height in pixels or percents. |
46
|
|
|
* 'maxheight' => '100px', // Number or String. Stage container maximum height in pixels or percents. |
47
|
|
|
* 'ratio' => 800/600, // Number or String. Width divided by height. Recommended if you’re using percentage width. |
48
|
|
|
* 'margin' => 10, // Number. Horizontal margins for frames in pixels. |
49
|
|
|
* 'glimpse' => '100px', // Number or String. Glimpse size of nearby frames in pixels or percents. |
50
|
|
|
* 'nav' => 'thumbs', // Boolean or String. Navigation style: 'dots' (iPhone-style dots), 'thumbs' (Thumbnails), false (Nothing) |
51
|
|
|
* 'navposition' => 'bottom', // String. Navigation block position relative to stage: 'bottom' or 'top'. |
52
|
|
|
* 'thumbwidth' => 100, // Number. Tumbnail width in pixels. |
53
|
|
|
* 'thumbheight' => 100, // Number. Tumbnail height in pixels. |
54
|
|
|
* 'thumbmargin' => 10, // Number. Size of thumbnail margins. |
55
|
|
|
* 'thumbborderwidth' => 5, // Number. Border width of the active thumbnail. |
56
|
|
|
* 'allowfullscreen' => true, // Boolean or String. Allows fullscreen: false (Default), true, 'native' |
57
|
|
|
* 'fit' => 'cover', // String. How to fit an image into a fotorama: 'contain' (Default), 'cover', 'scaledown', 'none' |
58
|
|
|
* 'transition' => 'slide', // String. Defines what transition to use: 'slide' (Default), 'crossfade', 'dissolve' |
59
|
|
|
* 'transitionduration' => 3000, // Number. Animation length in milliseconds. |
60
|
|
|
* 'captions' => true, // Boolean. Captions visibility. |
61
|
|
|
* 'hash' => false, // Boolean. Hash in urls |
62
|
|
|
* 'startindex' => 3, // Number or String. Index or id of the frame that will be shown upon initialization of the fotorama. |
63
|
|
|
* 'loop' => true, // Boolean. Enables loop. |
64
|
|
|
* 'autoplay' => true, // Boolean or Number. Enables slideshow. Turn it on with true or any interval in milliseconds. |
65
|
|
|
* 'stopautoplayontouch' => '', // Boolean. Stops slideshow at any user action with the fotorama. |
66
|
|
|
* 'keyboard' => true, // Boolean. Enables keyboard navigation. |
67
|
|
|
* 'arrows' => true, // Boolean. Turns on navigation arrows over the frames. |
68
|
|
|
* 'click' => true, // Boolean. Moving between frames by clicking. |
69
|
|
|
* 'swipe' => true, // Boolean. Moving between frames by swiping. |
70
|
|
|
* 'trackpad' => true, // Boolean. Enables trackpad support and horizontal mouse wheel as well. |
71
|
|
|
* 'shuffle' => true, // Boolean. Shuffles frames at launch. |
72
|
|
|
* 'direction' => 'ltr', // String. Sets the frames direction: 'ltr' or 'rtl'. |
73
|
|
|
* 'shadows' => true, // Boolean. Enables shadows. |
74
|
|
|
* ], |
75
|
|
|
* ``` |
76
|
|
|
* |
77
|
|
|
* @var array {@link http://fotorama.io/customize/ Fotorama options} |
78
|
|
|
*/ |
79
|
|
|
public $options = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add items as js objects |
83
|
|
|
* |
84
|
|
|
* ```php |
85
|
|
|
* 'items' => [ |
86
|
|
|
* [ |
87
|
|
|
* 'img' => '1.jpg', |
88
|
|
|
* 'thumb' => '1-thumb.jpg', |
89
|
|
|
* 'full' => '1-full.jpg', // Separate image for the fullscreen mode. |
90
|
|
|
* 'video' => 'http://youtu.be/C3lWwBslWqg', // Youtube, Vimeo or custom iframe URL |
91
|
|
|
* 'id' => 'one', // Custom anchor is used with the hash:true option. |
92
|
|
|
* 'caption' => 'The item caption', |
93
|
|
|
* 'html' => new \yii\web\JsExpression('jQuery("selector")'), // ...or '<div>123</div>'. Custom HTML inside the frame. |
94
|
|
|
* 'fit' => 'cover', // Override the global fit option. |
95
|
|
|
* 'any' => 'Any data relative to the frame you want to store', |
96
|
|
|
* ], |
97
|
|
|
* ], |
98
|
|
|
* ``` |
99
|
|
|
* |
100
|
|
|
* @var array {@link http://fotorama.io/customize/initialization/#frame-object item options} |
101
|
|
|
*/ |
102
|
|
|
public $items = []; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Loader (spinner) object config |
106
|
|
|
* |
107
|
|
|
* ```php |
108
|
|
|
* 'spinner' => [ |
109
|
|
|
* 'lines' => 13, // The number of lines to draw |
110
|
|
|
* 'length' => 20, // The length of each line |
111
|
|
|
* 'width' => 10, // The line thickness |
112
|
|
|
* 'radius' => 30, // The radius of the inner circle |
113
|
|
|
* 'corners' => 1, // Corner roundness (0..1) |
114
|
|
|
* 'rotate' => 0, // The rotation offset |
115
|
|
|
* 'direction' => 1, // 1: clockwise, -1: counterclockwise |
116
|
|
|
* 'color' => '#000', // #rgb or #rrggbb or array of colors |
117
|
|
|
* 'speed' => 1, // Rounds per second |
118
|
|
|
* 'trail' => 60, // Afterglow percentage |
119
|
|
|
* 'shadow' => false, // Whether to render a shadow |
120
|
|
|
* 'hwaccel' => false, // Whether to use hardware acceleration |
121
|
|
|
* 'zIndex' => 1000, // The z-index (defaults to 2000000000) |
122
|
|
|
* 'top' => '50%', // Top position relative to parent |
123
|
|
|
* 'left' => '50%' // Left position relative to parent |
124
|
|
|
* ], |
125
|
|
|
* ``` |
126
|
|
|
* |
127
|
|
|
* @var array {@link http://fgnass.github.io/spin.js/ spin.js options} |
128
|
|
|
*/ |
129
|
|
|
public $spinner = []; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Using of html data-params, if false - js object initialization |
133
|
|
|
* |
134
|
|
|
* @var bool |
135
|
|
|
*/ |
136
|
|
|
public $useHtmlData = true; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Container tag name |
140
|
|
|
* |
141
|
|
|
* @var string |
142
|
|
|
*/ |
143
|
|
|
public $tagName = 'div'; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Html options of the container tag |
147
|
|
|
* |
148
|
|
|
* @var array |
149
|
|
|
*/ |
150
|
|
|
public $htmlOptions = []; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Setup default Fotorama widget options |
154
|
|
|
* {@link http://fotorama.io/customize/options/#defaults} |
155
|
|
|
* |
156
|
|
|
* @param array $options |
157
|
|
|
*/ |
158
|
|
|
public static function setDefaults($options = []) |
159
|
|
|
{ |
160
|
|
|
$options = empty($options) ? '{}' : Json::encode($options); |
161
|
|
|
$view = Yii::$app->getView(); |
162
|
|
|
$js = <<<EOD |
163
|
|
|
fotoramaDefaults = {$options}; |
164
|
|
|
EOD; |
165
|
|
|
$view->registerJs($js, View::POS_HEAD, 'fotorama-defaults'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Initializes the widget. |
170
|
|
|
* This renders the open tag. |
171
|
|
|
*/ |
172
|
|
|
public function init() |
173
|
|
|
{ |
174
|
|
|
parent::init(); |
175
|
|
|
|
176
|
|
|
if (empty($this->htmlOptions['id'])) { |
177
|
|
|
$this->htmlOptions['id'] = $this->id; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($this->useHtmlData) { |
181
|
|
|
if (isset($this->options['data'])) { |
182
|
|
|
$this->items = (array)$this->options['data']; |
183
|
|
|
unset($this->options['data']); |
184
|
|
|
} |
185
|
|
|
if (isset($this->options['spinner'])) { |
186
|
|
|
$this->spinner = (array)$this->options['spinner']; |
187
|
|
|
unset($this->options['spinner']); |
188
|
|
|
} |
189
|
|
|
foreach ($this->options as $option => $value) { |
190
|
|
|
$this->htmlOptions['data-' . $option] = $value; |
191
|
|
|
} |
192
|
|
|
$this->options = []; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->htmlOptions['data-auto'] = 'false'; // disable auto init |
196
|
|
|
|
197
|
|
|
if (!empty($this->items)) { |
198
|
|
|
$this->options['data'] = $this->items; |
199
|
|
|
} |
200
|
|
|
if (!empty($this->spinner)) { |
201
|
|
|
$this->options['spinner'] = $this->spinner; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
echo Html::beginTag($this->tagName, $this->htmlOptions) . "\n"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Runs the widget. |
209
|
|
|
* This registers the necessary javascript code and renders the close tag. |
210
|
|
|
*/ |
211
|
|
|
public function run() |
212
|
|
|
{ |
213
|
|
|
parent::run(); |
214
|
|
|
|
215
|
|
|
echo Html::endTag($this->tagName); |
216
|
|
|
|
217
|
|
|
$this->registerJs(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param int $position |
222
|
|
|
* @param null $key |
223
|
|
|
*/ |
224
|
|
|
public function registerJs($position = View::POS_READY, $key = null) |
225
|
|
|
{ |
226
|
|
|
$view = $this->getView(); |
227
|
|
|
|
228
|
|
|
FotoramaAsset::register($view)->version = self::$useCDN; |
229
|
|
|
|
230
|
|
|
$options = empty($this->options) ? '' : Json::encode($this->options); |
231
|
|
|
$id = $this->htmlOptions['id']; |
232
|
|
|
$js = <<<EOD |
233
|
|
|
jQuery("#{$id}").fotorama({$options}); |
234
|
|
|
EOD; |
235
|
|
|
$view->registerJs($js, $position, $key); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|