-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOverlay.php
154 lines (126 loc) · 3.81 KB
/
Overlay.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
<?php
namespace demogorgorn\uikit;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
/**
* Create an image overlay, which comes in different styles.
*
* Thumb mode example:
*
* ```php
* echo Overlay::widget([
* 'image' => Yii::$app->request->getBaseUrl() . '/theme/logo.png',
* 'overlayContent' => 'Oleg',
* 'tagName' => 'a',
* //'options' => ['href' => '#'], // overlay options
* 'enableCaptionMode' => false,
* 'enableThumbMode' => true,
* 'thumbOptions' => [
* 'url' => '#',
* ],
* ]);
* ```
* Please note, that in thumb mode the defined in tagName option tag will be replaced by 'div'.
* Also, if you want to specify an url for thumb - define the thumbOptions and url option especially.
*
* Non-thumb mode:
*
* ```php
* echo Overlay::widget([
* 'image' => Yii::$app->request->getBaseUrl() . '/theme/logo.png',
* 'overlayContent' => 'Oleg',
* 'tagName' => 'a',
* 'options' => ['href' => '#', 'class' => 'someclass'],
* 'enableCaptionMode' => false,
* ]);
* ```
*
* Feel free to use anything (e.g. another widget) in the overlayContent.
*
* @see http://www.getuikit.com/docs/overlay.html
* @author Oleg Martemjanov <[email protected]>
* @since 2.0
*/
class Overlay extends Widget
{
/**
* @var string the tag to be used. an <a> or <div> element.
* Note that the 'div' tag will be used instead of defined if the Thumb mode is enabled.
*/
public $tagName = 'a';
/**
* @var bool determines to use the full overlay or partial caption mode.
*/
public $enableCaptionMode = false;
/**
* @var string overlay content.
*/
public $overlayContent;
/**
* @var string image url.
*/
public $image;
/**
* @var bool enable thumb mode.
*/
public $enableThumbMode = false;
/**
* @var bool, optional, array of thumb options.
* - url: string, the link for the thumb.
*
* The rest of the options will be rendered as the HTML attributes of the 'a' tag.
*/
public $thumbOptions = [];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
if ($this->enableThumbMode) {
Html::addCssClass($this->thumbOptions, 'uk-thumbnail');
if (isset($this->thumbOptions['url'])) {
$url = ArrayHelper::remove($this->thumbOptions, 'url', '');
$this->thumbOptions = array_merge([
'href' => $url,
], $this->thumbOptions);
}
echo Html::beginTag('a', $this->thumbOptions);
}
Html::addCssClass($this->options, 'uk-overlay');
echo Html::beginTag($this->enableThumbMode ? 'div' : $this->tagName, $this->options) . "\n";
}
/**
* Renders the widget.
*/
public function run()
{
echo "\n" . $this->renderImage();
echo "\n" . $this->renderOverlayContent();
echo "\n" . Html::endTag($this->enableThumbMode ? 'div' : $this->tagName);
if ($this->enableThumbMode)
echo Html::endTag('a');
$this->registerAsset();
}
/**
* Renders the image.
*/
protected function renderImage()
{
if ($this->image == null)
throw new InvalidConfigException("Option 'image' can't be empty!");
return Html::img($this->image, []);
}
/**
* Renders the overlay.
*/
protected function renderOverlayContent()
{
$class = $this->enableCaptionMode ? 'uk-overlay-caption' : 'uk-overlay-area-content';
$overlay = Html::tag('div', $this->overlayContent, ['class' => $class]);
if ($this->enableCaptionMode)
return $overlay;
return Html::tag('div', $overlay, ['class' => 'uk-overlay-area']);
}
}