1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/2amigos/yii2-gallery-widget |
4
|
|
|
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC |
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dosamigos\gallery; |
9
|
|
|
|
10
|
|
|
use yii\helpers\Html; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Carousel BlueImp Gallery Widget |
15
|
|
|
* |
16
|
|
|
* @author Alexander Kochetov <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Carousel extends Gallery |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var bool whether to json encode items or not. If using Json to encode items, we will be able to render |
22
|
|
|
* different |
23
|
|
|
*/ |
24
|
|
|
public $json = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
3 |
|
public function init() |
30
|
|
|
{ |
31
|
3 |
|
parent::init(); |
32
|
3 |
|
Html::addCssClass($this->templateOptions, 'blueimp-gallery-carousel'); |
33
|
3 |
|
$this->clientOptions['carousel'] = true; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
3 |
|
public function renderItems() |
40
|
|
|
{ |
41
|
3 |
|
if ($this->json) { |
42
|
3 |
|
return null; |
43
|
|
|
} |
44
|
3 |
|
return parent::renderItems(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string the carousel template |
49
|
|
|
*/ |
50
|
3 |
|
public function renderTemplate() |
51
|
|
|
{ |
52
|
3 |
|
$template[] = '<div class="slides"></div>'; |
|
|
|
|
53
|
3 |
|
$template[] = '<h3 class="title"></h3>'; |
54
|
3 |
|
$template[] = '<a class="prev">‹</a>'; |
55
|
3 |
|
$template[] = '<a class="next">›</a>'; |
56
|
3 |
|
$template[] = '<a class="play-pause"></a>'; |
57
|
3 |
|
$template[] = '<ol class="indicator"></ol>'; |
58
|
|
|
|
59
|
3 |
|
return Html::tag('div', implode("\n", $template), $this->templateOptions); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
3 |
|
public function registerClientScript() |
66
|
|
|
{ |
67
|
3 |
|
$view = $this->getView(); |
68
|
3 |
|
GalleryAsset::register($view); |
69
|
3 |
|
DosamigosAsset::register($view); |
70
|
|
|
|
71
|
3 |
|
$id = $this->options['id']; |
72
|
3 |
|
$options = Json::encode($this->clientOptions); |
73
|
3 |
|
$items = $this->json ? Json::encode($this->items) : "$('#$id').find('a.gallery-item').hide()"; |
74
|
3 |
|
$js = "blueimp_galleries = ( typeof blueimp_galleries != 'undefined' && blueimp_galleries instanceof Array ) ? blueimp_galleries : [];"; |
75
|
3 |
|
$js .= "blueimp_galleries['$id']=blueimp.Gallery($items, $options);"; |
76
|
3 |
|
$view->registerJs($js); |
77
|
3 |
|
} |
78
|
|
|
} |
79
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.