-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightboxedImage.php
118 lines (107 loc) · 3.06 KB
/
LightboxedImage.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
<?php
namespace d1soft\widgets;
use Yii;
use yii\helpers\BaseHtml;
use yii\helpers\ArrayHelper;
use yii\helpers\JsonHelper;
use d1soft\widgets\LightboxedImageAsset;
/**
* LightboxedImage widget render image with lightbox.
* It's wrapper for lightbox on javascript coded by Lokesh Dhakar
* @see http://lokeshdhakar.com/projects/lightbox2/
* ```php
* LiteboxedImage::widget([
* 'options' => [
* 'src' => '/path/to/image.jpg',
* 'lightboxId' => 'lightBox',
* 'lightboxClass' => 'lightBoxClass',
* 'lightboxStyle' => 'max-height: 350px',
* 'imageId' => 'imageId',
* 'imageClass' => 'imageClass',
* 'imageStyle' => 'max-width: 250px',
* 'title' => 'Image title',
* 'alt' => 'Some image',
* ],
* 'clientOptions' => [
* 'resizeDuration' => 200,
* 'wrapAround' => true
* ]
* ]);
* ```
*
* @author Lokesh Dhakar <[email protected]>
* @author d1soft <[email protected]>
*/
class LightboxedImage extends \yii\base\Widget
{
/**
* @var array widget options list
*
* Possible options:
* @var string 'src' image source path
* @var string 'alt' alternate image specified text
* @var string 'title' image title in lightbox
* @var string 'lightboxid' lightbox id
* @var string 'lightboxClass' lightbox class
* @var string 'lightboxStyle' lightbox inline styles
* @var string 'imageId' image container id
* @var string 'imageClass' image container class
* @var string 'imageStyle' image container inline style
*/
public $options = [];
/**
* @var array client options
* @see http://lokeshdhakar.com/projects/lightbox2/#options
*/
public $clientOptions;
public function init()
{
$view = $this->getView();
LightboxedImageAsset::register($view);
$options = Json::encode($this->clientOptions);
$clientScript = "lightbox.option($options)";
$view->registerJs($clientScript);
parent::init();
}
/**
* {@inheritdoc}
*/
public function run()
{
$this->prepareOptions();
$image = BaseHtml::tag('img', '', [
'id' => $this->options['imageId'],
'src' => $this->options['src'],
'class' => $this->options['imageClass'],
'style' => $this->options['imageStyle']
]);
return BaseHtml::tag('a', $image, [
'data' => $this->options['data'],
'href' => $this->options['src'],
'class' => $this->options['lightboxClass'],
'style' => $this->options['lightboxStyle']
]);
}
/**
* Prepare widget options
*
* @param array $options widget options
* @return void
*/
private function prepareOptions()
{
$id = $this->getId();
if(!isset($this->options['lightboxId'])){
$this->options['lightboxId'] = $id;
}
if(isset($this->options['title'])){
$this->options['data']['title'] = $options['title'];
unset($this->options['title']);
}
if(isset($this->options['alt'])){
$this->options['data']['alt'] = $options['alt'];
unset($this->options['alt']);
}
$this->options['data']['lightbox'] = $id;
}
}