1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TStyle class file. |
4
|
|
|
* |
5
|
|
|
* @author Qiang Xue <[email protected]> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
8
|
|
|
* @package Prado\Web\UI\WebControls |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Prado\Web\UI\WebControls; |
12
|
|
|
|
13
|
|
|
use Prado\TPropertyValue; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* TStyle class |
17
|
|
|
* |
18
|
|
|
* TStyle encapsulates the CSS style applied to a control. |
19
|
|
|
* |
20
|
|
|
* @author Qiang Xue <[email protected]> |
21
|
|
|
* @package Prado\Web\UI\WebControls |
22
|
|
|
* @since 3.0 |
23
|
|
|
*/ |
24
|
|
|
class TStyle extends \Prado\TComponent |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array storage of CSS fields |
28
|
|
|
*/ |
29
|
|
|
protected $_fields = []; |
30
|
|
|
/** |
31
|
|
|
* @var TFont font object |
32
|
|
|
*/ |
33
|
|
|
protected $_font; |
34
|
|
|
/** |
35
|
|
|
* @var string CSS class name |
36
|
|
|
*/ |
37
|
|
|
protected $_class; |
38
|
|
|
/** |
39
|
|
|
* @var string CSS style string (those not represented by specific fields of TStyle) |
40
|
|
|
*/ |
41
|
|
|
protected $_customStyle; |
42
|
|
|
/** |
43
|
|
|
* @var string display style |
44
|
|
|
*/ |
45
|
|
|
protected $_displayStyle = 'Fixed'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns an array with the names of all variables of this object that should NOT be serialized |
49
|
|
|
* because their value is the default one or useless to be cached for the next page loads. |
50
|
|
|
* Reimplement in derived classes to add new variables, but remember to also to call the parent |
51
|
|
|
* implementation first. |
52
|
|
|
* @param array $exprops by reference |
53
|
|
|
*/ |
54
|
|
|
protected function _getZappableSleepProps(&$exprops) |
55
|
|
|
{ |
56
|
|
|
parent::_getZappableSleepProps($exprops); |
57
|
|
|
if ($this->_fields === []) { |
58
|
|
|
$exprops[] = "\0*\0_fields"; |
59
|
|
|
} |
60
|
|
|
if ($this->_font === null) { |
61
|
|
|
$exprops[] = "\0*\0_font"; |
62
|
|
|
} |
63
|
|
|
if ($this->_class === null) { |
64
|
|
|
$exprops[] = "\0*\0_class"; |
65
|
|
|
} |
66
|
|
|
if ($this->_customStyle === null) { |
67
|
|
|
$exprops[] = "\0*\0_customStyle"; |
68
|
|
|
} |
69
|
|
|
if ($this->_displayStyle === 'Fixed') { |
70
|
|
|
$exprops[] = "\0*\0_displayStyle"; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor. |
76
|
|
|
* @param TStyle $style style to copy from |
77
|
|
|
*/ |
78
|
1 |
|
public function __construct($style = null) |
79
|
|
|
{ |
80
|
1 |
|
parent::__construct(); |
81
|
1 |
|
if ($style !== null) { |
82
|
|
|
$this->copyFrom($style); |
83
|
|
|
} |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Need to clone the font object. |
88
|
|
|
*/ |
89
|
|
|
public function __clone() |
90
|
|
|
{ |
91
|
|
|
if ($this->_font !== null) { |
92
|
|
|
$this->_font = clone($this->_font); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string the background color of the control |
98
|
|
|
*/ |
99
|
|
|
public function getBackColor() |
100
|
|
|
{ |
101
|
|
|
return $this->_fields['background-color'] ?? ''; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $value the background color of the control |
106
|
|
|
*/ |
107
|
|
|
public function setBackColor($value) |
108
|
|
|
{ |
109
|
|
|
if (trim($value) === '') { |
110
|
|
|
unset($this->_fields['background-color']); |
111
|
|
|
} else { |
112
|
|
|
$this->_fields['background-color'] = $value; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string the border color of the control |
118
|
|
|
*/ |
119
|
|
|
public function getBorderColor() |
120
|
|
|
{ |
121
|
|
|
return $this->_fields['border-color'] ?? ''; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $value the border color of the control |
126
|
|
|
*/ |
127
|
|
|
public function setBorderColor($value) |
128
|
|
|
{ |
129
|
|
|
if (trim($value) === '') { |
130
|
|
|
unset($this->_fields['border-color']); |
131
|
|
|
} else { |
132
|
|
|
$this->_fields['border-color'] = $value; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string the border style of the control |
138
|
|
|
*/ |
139
|
|
|
public function getBorderStyle() |
140
|
|
|
{ |
141
|
|
|
return $this->_fields['border-style'] ?? ''; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Sets the border style of the control. |
146
|
|
|
* @param string $value the border style of the control |
147
|
|
|
*/ |
148
|
|
|
public function setBorderStyle($value) |
149
|
|
|
{ |
150
|
|
|
if (trim($value) === '') { |
151
|
|
|
unset($this->_fields['border-style']); |
152
|
|
|
} else { |
153
|
|
|
$this->_fields['border-style'] = $value; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string the border width of the control |
159
|
|
|
*/ |
160
|
|
|
public function getBorderWidth() |
161
|
|
|
{ |
162
|
|
|
return $this->_fields['border-width'] ?? ''; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $value the border width of the control |
167
|
|
|
*/ |
168
|
|
|
public function setBorderWidth($value) |
169
|
|
|
{ |
170
|
|
|
if (trim($value) === '') { |
171
|
|
|
unset($this->_fields['border-width']); |
172
|
|
|
} else { |
173
|
|
|
$this->_fields['border-width'] = $value; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string the border radius of the panel |
179
|
|
|
* @since 4.2.0 |
180
|
|
|
*/ |
181
|
|
|
public function getBorderRadius() |
182
|
|
|
{ |
183
|
|
|
return $this->_fields['border-radius'] ?? ''; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $value the border radius of the panel |
188
|
|
|
* @since 4.2.0 |
189
|
|
|
*/ |
190
|
|
|
public function setBorderRadius($value) |
191
|
|
|
{ |
192
|
|
|
if (trim($value) === '') { |
193
|
|
|
unset($this->_fields['border-radius']); |
194
|
|
|
} else { |
195
|
|
|
$this->_fields['border-radius'] = $value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return string the CSS class of the control |
201
|
|
|
*/ |
202
|
|
|
public function getCssClass() |
203
|
|
|
{ |
204
|
|
|
return $this->_class === null ? '' : $this->_class; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return bool true if CSS is set or empty. |
209
|
|
|
*/ |
210
|
|
|
public function hasCssClass() |
211
|
|
|
{ |
212
|
|
|
return ($this->_class !== null); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $value the name of the CSS class of the control |
217
|
|
|
*/ |
218
|
|
|
public function setCssClass($value) |
219
|
|
|
{ |
220
|
|
|
$this->_class = $value; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return TFont the font of the control |
225
|
|
|
*/ |
226
|
|
|
public function getFont() |
227
|
|
|
{ |
228
|
|
|
if ($this->_font === null) { |
229
|
|
|
$this->_font = new TFont; |
230
|
|
|
} |
231
|
|
|
return $this->_font; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return bool true if font is set. |
236
|
|
|
*/ |
237
|
|
|
public function hasFont() |
238
|
|
|
{ |
239
|
|
|
return $this->_font !== null; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param TDisplayStyle $value control display style, default is TDisplayStyle::Fixed |
244
|
|
|
*/ |
245
|
|
|
public function setDisplayStyle($value) |
246
|
|
|
{ |
247
|
|
|
$this->_displayStyle = TPropertyValue::ensureEnum($value, 'Prado\\Web\\UI\\WebControls\\TDisplayStyle'); |
248
|
|
|
switch ($this->_displayStyle) { |
249
|
|
|
case TDisplayStyle::None: |
250
|
|
|
$this->_fields['display'] = 'none'; |
251
|
|
|
break; |
252
|
|
|
case TDisplayStyle::Dynamic: |
253
|
|
|
$this->_fields['display'] = ''; //remove the display property |
254
|
|
|
break; |
255
|
|
|
case TDisplayStyle::Fixed: |
256
|
|
|
$this->_fields['visibility'] = 'visible'; |
257
|
|
|
break; |
258
|
|
|
case TDisplayStyle::Hidden: |
259
|
|
|
$this->_fields['visibility'] = 'hidden'; |
260
|
|
|
break; |
261
|
1 |
|
} |
262
|
|
|
} |
263
|
1 |
|
|
264
|
|
|
/** |
265
|
|
|
* @return TDisplayStyle display style |
266
|
1 |
|
*/ |
267
|
|
|
public function getDisplayStyle() |
268
|
1 |
|
{ |
269
|
|
|
return $this->_displayStyle; |
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return string the foreground color of the control |
274
|
|
|
*/ |
275
|
|
|
public function getForeColor() |
276
|
|
|
{ |
277
|
|
|
return $this->_fields['color'] ?? ''; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param string $value the foreground color of the control |
282
|
|
|
*/ |
283
|
|
|
public function setForeColor($value) |
284
|
|
|
{ |
285
|
|
|
if (trim($value) === '') { |
286
|
|
|
unset($this->_fields['color']); |
287
|
|
|
} else { |
288
|
|
|
$this->_fields['color'] = $value; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return string the height of the control |
294
|
|
|
*/ |
295
|
|
|
public function getHeight() |
296
|
|
|
{ |
297
|
|
|
return $this->_fields['height'] ?? ''; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param string $value the height of the control |
302
|
|
|
*/ |
303
|
|
|
public function setHeight($value) |
304
|
|
|
{ |
305
|
|
|
if (trim($value) === '') { |
306
|
|
|
unset($this->_fields['height']); |
307
|
|
|
} else { |
308
|
|
|
$this->_fields['height'] = $value; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return string the custom style of the control |
314
|
|
|
*/ |
315
|
|
|
public function getCustomStyle() |
316
|
|
|
{ |
317
|
|
|
return $this->_customStyle === null ? '' : $this->_customStyle; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Sets custom style fields from a string. |
322
|
|
|
* Custom style fields will be overwritten by style fields explicitly defined. |
323
|
|
|
* @param string $value the custom style of the control |
324
|
|
|
*/ |
325
|
|
|
public function setCustomStyle($value) |
326
|
|
|
{ |
327
|
|
|
$this->_customStyle = $value; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param mixed $name |
332
|
|
|
* @return string a single style field value set via {@link setStyleField}. Defaults to empty string. |
333
|
|
|
*/ |
334
|
|
|
public function getStyleField($name) |
335
|
|
|
{ |
336
|
|
|
return $this->_fields[$name] ?? ''; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Sets a single style field value. |
341
|
|
|
* Style fields set by this method will overwrite those set by {@link setCustomStyle}. |
342
|
|
|
* @param string $name style field name |
343
|
|
|
* @param string $value style field value |
344
|
|
|
*/ |
345
|
|
|
public function setStyleField($name, $value) |
346
|
|
|
{ |
347
|
|
|
$this->_fields[$name] = $value; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Clears a single style field value; |
352
|
|
|
* @param string $name style field name |
353
|
|
|
*/ |
354
|
|
|
public function clearStyleField($name) |
355
|
|
|
{ |
356
|
|
|
unset($this->_fields[$name]); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param mixed $name |
361
|
|
|
* @return bool whether a style field has been defined by {@link setStyleField} |
362
|
|
|
*/ |
363
|
|
|
public function hasStyleField($name) |
364
|
|
|
{ |
365
|
|
|
return isset($this->_fields[$name]); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return string the width of the control |
370
|
|
|
*/ |
371
|
|
|
public function getWidth() |
372
|
|
|
{ |
373
|
|
|
return $this->_fields['width'] ?? ''; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param string $value the width of the control |
378
|
|
|
*/ |
379
|
|
|
public function setWidth($value) |
380
|
|
|
{ |
381
|
|
|
$this->_fields['width'] = $value; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Resets the style to the original empty state. |
386
|
|
|
*/ |
387
|
|
|
public function reset() |
388
|
|
|
{ |
389
|
|
|
$this->_fields = []; |
390
|
|
|
$this->_font = null; |
391
|
|
|
$this->_class = null; |
392
|
|
|
$this->_customStyle = null; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Copies the fields in a new style to this style. |
397
|
|
|
* If a style field is set in the new style, the corresponding field |
398
|
|
|
* in this style will be overwritten. |
399
|
|
|
* @param TStyle $style the new style |
400
|
|
|
*/ |
401
|
|
|
public function copyFrom($style) |
402
|
|
|
{ |
403
|
|
|
if ($style instanceof TStyle) { |
|
|
|
|
404
|
|
|
$this->_fields = array_merge($this->_fields, $style->_fields); |
405
|
|
|
if ($style->_class !== null) { |
406
|
|
|
$this->_class = $style->_class; |
407
|
|
|
} |
408
|
|
|
if ($style->_customStyle !== null) { |
409
|
|
|
$this->_customStyle = $style->_customStyle; |
410
|
|
|
} |
411
|
|
|
if ($style->_font !== null) { |
412
|
|
|
$this->getFont()->copyFrom($style->_font); |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Merges the style with a new one. |
419
|
|
|
* If a style field is not set in this style, it will be overwritten by |
420
|
|
|
* the new one. |
421
|
|
|
* @param TStyle $style the new style |
422
|
|
|
*/ |
423
|
|
|
public function mergeWith($style) |
424
|
|
|
{ |
425
|
|
|
if ($style instanceof TStyle) { |
|
|
|
|
426
|
|
|
$this->_fields = array_merge($style->_fields, $this->_fields); |
427
|
|
|
if ($this->_class === null) { |
428
|
|
|
$this->_class = $style->_class; |
429
|
|
|
} |
430
|
|
|
if ($this->_customStyle === null) { |
431
|
|
|
$this->_customStyle = $style->_customStyle; |
432
|
|
|
} |
433
|
|
|
if ($style->_font !== null) { |
434
|
|
|
$this->getFont()->mergeWith($style->_font); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Adds attributes related to CSS styles to renderer. |
441
|
|
|
* @param \Prado\Web\UI\THtmlWriter $writer the writer used for the rendering purpose |
442
|
|
|
*/ |
443
|
|
|
public function addAttributesToRender($writer) |
444
|
|
|
{ |
445
|
|
|
if ($this->_customStyle !== null) { |
446
|
|
|
foreach (explode(';', $this->_customStyle) as $style) { |
447
|
|
|
$arr = explode(':', $style, 2); |
448
|
|
|
if (isset($arr[1]) && trim($arr[0]) !== '') { |
449
|
|
|
$writer->addStyleAttribute(trim($arr[0]), trim($arr[1])); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
$writer->addStyleAttributes($this->_fields); |
454
|
|
|
if ($this->_font !== null) { |
455
|
|
|
$this->_font->addAttributesToRender($writer); |
456
|
|
|
} |
457
|
|
|
if ($this->_class !== null) { |
458
|
|
|
$writer->addAttribute('class', $this->_class); |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @return array list of style fields. |
464
|
|
|
*/ |
465
|
|
|
public function getStyleFields() |
466
|
|
|
{ |
467
|
|
|
return $this->_fields; |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|