/
Picture.php
237 lines (206 loc) · 6.49 KB
/
Picture.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
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
<?php
namespace Concrete\Core\Html\Object;
use HtmlObject\Element;
use HtmlObject\Image;
class Picture extends Element
{
/**
* Default element.
*
* @var string
*/
protected $element = 'picture';
/**
* Whether the element is self closing.
*
* @var bool
*/
protected $isSelfClosing = false;
/**
* Default element for nested children.
*
* @var string
*/
protected $defaultChild = 'source';
////////////////////////////////////////////////////////////////////
//////////////////////////// CORE METHODS //////////////////////////
////////////////////////////////////////////////////////////////////
public function __construct(array $sources = [], $fallbackSrc, $attributes = [], $lazyLoadNative = false, $lazyLoadJavaScript = false)
{
$this->sources($sources, $lazyLoadJavaScript);
if ($lazyLoadJavaScript) {
$this->noscriptFallback($fallbackSrc, $lazyLoadNative);
}
$this->fallback($fallbackSrc, $lazyLoadNative, $lazyLoadJavaScript);
}
/**
* Static alias for constructor.
*
* @param array $sources
* @param string|null $fallbackSrc
* @param array $attributes
* @param bool|null $lazyLoadNative
* @param bool|null $lazyLoadJavaScript
*
* @return \Concrete\Core\Html\Object\Picture
*/
public static function create($sources = [], $fallbackSrc = false, $attributes = [], $lazyLoadNative = false, $lazyLoadJavaScript = false)
{
return new static($sources, $fallbackSrc, $attributes, $lazyLoadNative, $lazyLoadJavaScript);
}
////////////////////////////////////////////////////////////////////
/////////////////////////////// CHILDREN ///////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Create the source elements for the picture element.
*
* Example output:
* <!--[if IE 9]><video style='display: none;'><![endif]-->
* <source srcset="..." media="(min-width: ...)">
* <source srcset="..." media="(min-width: ...)">
* <source srcset="...">
* <!--[if IE 9]></video><![endif]-->
* or
* <!--[if IE 9]><video style='display: none;'><![endif]-->
* <source data-srcset="..." media="(min-width: ...)">
* <source data-srcset="..." media="(min-width: ...)">
* <source data-srcset="...">
* <!--[if IE 9]></video><![endif]-->
*
* @param array $sources an array of image thumbnail file paths
* @param bool $lazyLoadJavaScript if true, the source image thumbnail file path is set to a "data-srcset" attribute instead of "srcset"
*
* @return \Concrete\Core\Html\Object\Picture
*/
public function sources($sources, $lazyLoadJavaScript = false)
{
$this->nest("<!--[if IE 9]><video style='display: none;'><![endif]-->");
foreach ($sources as $source) {
$path = $source['src'];
$width = $source['width'];
$source = Source::create();
if ($lazyLoadJavaScript) {
$source->dataSrcset($path);
} else {
$source->srcset($path);
}
if ($width != 0) {
$source->media("(min-width: $width)");
}
$this->setChild($source);
}
$this->nest('<!--[if IE 9]></video><![endif]-->');
return $this;
}
/**
* Create an img element wrapped in "<noscript></noscript>". The image will be displayed if JavaScript is disabled.
*
* Example output:
* <noscript>
* <img src="...">
* </noscript>
* or
* <noscript>
* <img src="..." loading="lazy">
* </noscript>
*
* @param string $src the file path of an image
* @param bool $lazyLoadNative if true, the image "loading" attribute is set to "lazy"
*
* @return \Concrete\Core\Html\Object\Picture
*/
public function noscriptFallback($src, $lazyLoadNative = false)
{
$this->nest('<noscript>');
$img = Image::create();
$img->src($src);
if ($lazyLoadNative) {
$img->loading('lazy');
}
$this->setChild($img);
$this->nest('</noscript>');
return $this;
}
/**
* Create the img element fallback for the picture element.
*
* Example output:
* <img src="...">
* or
* <img src="..." loading="lazy">
* or
* <img data-src="..." loading="lazy">
* or
* <img data-src="...">
*
* @param string $src the file path of an image
* @param bool $lazyLoadNative if true, the image "loading" attribute is set to "lazy"
* @param bool $lazyLoadJavaScript if true, the image path is set to a "data-src" attribute
*/
public function fallback($src, $lazyLoadNative = false, $lazyLoadJavaScript = false)
{
$img = Image::create();
$img->src($src);
if ($lazyLoadNative) {
$img->loading('lazy');
}
if ($lazyLoadJavaScript) {
$img->src(false);
$img->dataSrc($src);
}
$this->setChild($img);
}
/**
* Set the image fallback and noscript image fallback "alt" attribute value.
*
* @param string $alt
*/
public function alt($alt)
{
foreach ($this->getChildren() as $child) {
if ($child instanceof Image) {
$child->alt($alt);
}
}
}
/**
* Set the image fallback and noscript image fallback "title" attribute value.
*
* @param string $title
*/
public function title($title)
{
foreach ($this->getChildren() as $child) {
if ($child instanceof Image) {
$child->title($title);
}
}
}
/**
* Add one or more CSS classes to the image and noscript image fallback.
*
* @param string $classes a string of space separated CSS classes
*/
public function addClass($classes)
{
foreach ($this->getChildren() as $child) {
if ($child instanceof Image) {
$child->addClass($classes);
}
}
}
/**
* Set an attribute.
*
* @param string $attribute
* @param string|null $value
*/
public function setAttribute($attribute, $value = null)
{
foreach ($this->getChildren() as $child) {
if ($child instanceof Image) {
$child->setAttribute($attribute, $value);
}
}
}
}