-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSwatImagePreviewDisplay.php
More file actions
331 lines (289 loc) · 8.62 KB
/
Copy pathSwatImagePreviewDisplay.php
File metadata and controls
331 lines (289 loc) · 8.62 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* Image preview display control
*
* This control displays an image and uses a lightbox-like effect to display
* another image when the first image is clicked.
*
* @package Swat
* @copyright 2005-2016 silverorange
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
*/
class SwatImagePreviewDisplay extends SwatImageDisplay
{
// {{{ public properties
/**
* Preview Image
*
* The src attribute in the XHTML img tag.
*
* @var string
*/
public $preview_image;
/**
* Optional array of values to substitute into the preview image property
*
* Uses vsprintf() syntax, for example:
*
* <code>
* $my_image->preview_image = 'mydir/%s.%s';
* $my_image->preview_image_values = array('myfilename', 'ext');
* </code>
*
* @var array
*/
public $preview_image_values = array();
/**
* Preview Image height
*
* The height attribute in the XHTML img tag.
*
* @var integer
*/
public $preview_height = null;
/**
* Preview Image width
*
* The width attribute in the XHTML img tag.
*
* @var integer
*/
public $preview_width = null;
/**
* Whether or not to show a resize icon next to the image
*
* @var boolean
*/
public $show_icon = true;
/**
* Whether or not to visibly display the title below the image
*
* By default no visible title is displayed.
*
* The visible title is only displayed if JavaScript is enabled. The
* default title is "View Larger Image", but this may be changed by setting
* the {@link SwatImageDisplay::$title} property of this image preview
* display.
*
* @var boolean
*/
public $show_title = false;
/**
* The href attribute in the XHTML anchor tag
*
* If JavaScript is not enabled, the image preview display will link to
* this location
*
* Optionally uses vsprintf() syntax, for example:
* <code>
* $renderer->link = 'MySection/MyPage/%s?id=%s';
* </code>
*
* @var string
*
* @see SwatImagePreviewDisplay::$link_value
*/
public $link;
/**
* A value or array of values to substitute into the link
*
* The value property may be specified either as an array of values or as
* a single value. If an array is passed, a call to vsprintf() is done
* on the {@link SwatImageLinkCellRenderer::$link} property. If the value
* is a string a single sprintf() call is made.
*
* @var mixed
*
* @see SwatImagePreviewDisplay::$link
*/
public $link_value = null;
/**
* Optional container width (default is the image width + padding)
*/
public $container_width = null;
/**
* Optional container height (default is the image height + padding)
*/
public $container_height = null;
/**
* Optional title to display above the large image when the preview is
* opened
*
* @var string
*/
public $preview_title = null;
/**
* Only show the preview image when the preview area is smaller or equal to
* the original area.
*
* @var boolean
*/
public $show_preview_when_smaller = false;
/**
* Close text
*
* @var string
*/
public $close_text = null;
// }}}
// {{{ public function __construct()
/**
* Creates a new image preview display
*
* @param string $id a non-visible unique id for this widget.
*
* @see SwatWidget::__construct()
*/
public function __construct($id = null)
{
parent::__construct($id);
$this->requires_id = true;
$this->title = Swat::_('View Larger Image');
}
// }}}
// {{{ public function display()
/**
* Displays this image
*/
public function display()
{
if (!$this->visible) {
return;
}
if (!$this->isPreviewDisplayable()) {
parent::display();
} else {
if ($this->link !== null) {
$tag = new SwatHtmlTag('a');
if ($this->link_value === null) {
$tag->href = $this->link;
} elseif (is_array($this->link_value)) {
$tag->href = vsprintf($this->link, $this->link_value);
} else {
$tag->href = sprintf($this->link, $this->link_value);
}
} else {
$tag = new SwatHtmlTag('span');
}
$tag->id = $this->id . '_wrapper';
$tag->title = $this->title;
if ($this->show_icon) {
$tag->class = 'swat-image-preview-display-link';
} else {
$tag->class = 'swat-image-preview-display-link-plain';
}
$tag->open();
parent::display();
$tag->close();
Swat::displayInlineJavaScript($this->getInlineJavaScript());
}
}
// }}}
// {{{ protected function isPreviewDisplayable()
/**
* Checks whether the preview exists, and whether it should be displayed.
*
* @return boolean True if the preview should be displayed, false if it
* should not.
*/
protected function isPreviewDisplayable()
{
$image_area = $this->width * $this->height;
$preview_area = $this->preview_width * $this->preview_height;
$difference = ($preview_area - $image_area) / $image_area;
return $this->preview_image != '' &&
($this->show_preview_when_smaller || $difference >= 0.2);
}
// }}}
// {{{ protected function getJavaScriptClass()
/**
* Gets the name of the JavaScript class to instantiate for this image
* preview display
*
* Sub-classes of this class may want to return a sub-class of the default
* JavaScript image preview class.
*
* @return string the name of the JavaScript class to instantiate for this
* image preview display. Defaults to
* 'SwatImagePreviewDisplay'.
*/
protected function getJavaScriptClass()
{
return 'SwatImagePreviewDisplay';
}
// }}}
// {{{ protected function getInlineJavaScript()
/**
* Gets inline JavaScript required by this image preview.
*
* @return string inline JavaScript needed by this widget.
*/
protected function getInlineJavaScript()
{
static $shown = false;
if (!$shown) {
$javascript = $this->getInlineJavaScriptTranslations();
$shown = true;
} else {
$javascript = '';
}
$javascript .= sprintf(
"var %s = new %s(\n" . "%s, %s, %s, %s, %s, %s);\n",
$this->id,
$this->getJavaScriptClass(),
SwatString::quoteJavaScriptString($this->id),
SwatString::quoteJavaScriptString($this->preview_image),
intval($this->preview_width),
intval($this->preview_height),
$this->show_title ? 'true' : 'false',
SwatString::quoteJavaScriptString($this->preview_title)
);
if ($this->container_width !== null) {
$javascript .= sprintf(
"%s.width = %s;",
$this->id,
(int) $this->container_width
);
}
if ($this->container_height !== null) {
$javascript .= sprintf(
"%s.height = %s;",
$this->id,
(int) $this->container_height
);
}
return $javascript;
}
// }}}
// {{{ protected function getInlineJavaScriptTranslations()
/**
* Gets translatable string resources for the JavaScript object for
* this widget
*
* @return string translatable JavaScript string resources for this widget.
*/
protected function getInlineJavaScriptTranslations()
{
$close_text =
$this->close_text === null ? Swat::_('Close') : $this->close_text;
return sprintf(
"SwatImagePreviewDisplay.close_text = '%s';\n",
$close_text
);
}
// }}}
// {{{ protected function getCSSClassNames()
/**
* Gets the array of CSS classes that are applied to this image display
*
* @return array the array of CSS classes that are applied to this image
* display.
*/
protected function getCSSClassNames()
{
$classes = array('swat-image-preview-display');
$classes = array_merge($classes, parent::getCSSClassNames());
return $classes;
}
// }}}
}