UiElement.php
3.36 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Controls;
use YahnisElsts\AdminMenuEditor\Customizable\HtmlHelper;
abstract class UiElement {
/**
* @var string
*/
protected $id = '';
/**
* @var string|callable
*/
protected $description = '';
/**
* @var array List of CSS classes to apply to the outermost DOM node of the element.
* This property might not be meaningful for elements that output multiple nodes without
* a common parent or that don't have a visible representation.
*/
protected $classes = array();
/**
* @var array List of CSS styles to apply to the outermost DOM node of the element.
*/
protected $styles = array();
protected $renderCondition = true;
/**
* Lets the renderer know that the element doesn't want new line breaks added
* before and after its content.
*
* - Block elements (e.g. <fieldset>) and elements that surround their
* content with <p> or <br> tags should set this to true.
* - Elements that output partial or unclosed tags should also set this to
* true to avoid producing invalid HTML.
*
* @var bool
*/
protected $declinesExternalLineBreaks = false;
public function __construct($params = array()) {
if ( !empty($params['id']) ) {
$this->id = $params['id'];
}
if ( !empty($params['description']) ) {
$this->description = $params['description'];
}
if ( !empty($params['classes']) ) {
$this->classes = (array)$params['classes'];
}
if ( !empty($params['styles']) ) {
$this->styles = (array)$params['styles'];
}
if ( isset($params['renderCondition']) ) {
$this->renderCondition = $params['renderCondition'];
}
}
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @return string
*/
public function getDescription() {
if ( is_string($this->description) ) {
return $this->description;
} elseif ( is_callable($this->description) ) {
return call_user_func($this->description);
} else {
return strval($this->description);
}
}
/**
* @return array
*/
public function getClasses() {
return $this->classes;
}
protected function buildTag($tagName, $attributes = array(), $content = null) {
return HtmlHelper::tag($tagName, $attributes, $content);
}
/**
* @return bool
*/
public function declinesExternalLineBreaks() {
return $this->declinesExternalLineBreaks;
}
public function shouldRender() {
if ( is_callable($this->renderCondition) ) {
return call_user_func($this->renderCondition);
}
return (bool)$this->renderCondition;
}
public function serializeForJs() {
$description = $this->getDescription();
$result = ['t' => $this->getJsUiElementType()];
if ( !empty($this->classes) ) {
$result['classes'] = $this->classes;
}
if ( !empty($this->styles) ) {
$result['styles'] = $this->styles;
}
if ( !empty($description) ) {
$result['description'] = $description;
}
if ( !empty($this->id) ) {
$result['id'] = $this->id;
}
$params = $this->getKoComponentParams();
if ( !empty($params) ) {
$result['params'] = $params;
}
return $result;
}
abstract protected function getJsUiElementType();
/**
* Get additional parameters for the Knockout component that renders this element.
*
* @return array
*/
protected function getKoComponentParams() {
return [];
}
public function enqueueKoComponentDependencies() {
//Do nothing by default.
}
}