| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* DOM element abstraction. |
| 7 |
* |
| 8 |
* @abstract |
| 9 |
*/ |
| 10 |
class Element extends Tag { |
| 11 |
|
| 12 |
/* Static Properties */ |
| 13 |
|
| 14 |
/** |
| 15 |
* HTML tag name. |
| 16 |
* |
| 17 |
* This may be ignored if getTagName() is overridden. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
public static $tagName = 'div'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Default text direction, used for some layout calculations. Use setDefaultDir() to change. |
| 25 |
* |
| 26 |
* Currently only per-document directionality is supported. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public static $defaultDir = 'ltr'; |
| 31 |
|
| 32 |
/* Properties */ |
| 33 |
|
| 34 |
/** |
| 35 |
* Element data. |
| 36 |
* |
| 37 |
* @var mixed |
| 38 |
*/ |
| 39 |
protected $data = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var bool |
| 43 |
*/ |
| 44 |
protected $visible = true; |
| 45 |
|
| 46 |
/** |
| 47 |
* Strings of the CSS classes explicitly configured for this element (as opposed to #$classes, |
| 48 |
* which contains all classes for this element). |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $ownClasses = []; |
| 53 |
|
| 54 |
/** |
| 55 |
* @var callable[] |
| 56 |
*/ |
| 57 |
protected $configCallbacks = []; |
| 58 |
|
| 59 |
/* Static methods */ |
| 60 |
|
| 61 |
/** |
| 62 |
* Emits a deprecation warning with provided message. |
| 63 |
* |
| 64 |
* @param string $message Message about the deprecation |
| 65 |
*/ |
| 66 |
public static function warnDeprecation( $message = '' ) { |
| 67 |
trigger_error( $message, E_USER_DEPRECATED ); |
| 68 |
} |
| 69 |
|
| 70 |
/* Methods */ |
| 71 |
|
| 72 |
/** |
| 73 |
* @param array $config Configuration options |
| 74 |
* - string[] $config['classes'] CSS class names to add |
| 75 |
* - string $config['id'] HTML id attribute |
| 76 |
* - string $config['text'] Text to insert |
| 77 |
* - string[]|HtmlSnippet[]|Element[] $config['content'] Content to append (after text). |
| 78 |
* Strings will be HTML-escaped for output, use an HtmlSnippet instance to prevent that. |
| 79 |
* - mixed $config['data'] Element data |
| 80 |
*/ |
| 81 |
public function __construct( array $config = [] ) { |
| 82 |
// Parent constructor |
| 83 |
parent::__construct( $this->getTagName() ); |
| 84 |
|
| 85 |
// Initialization |
| 86 |
if ( isset( $config['infusable'] ) && is_bool( $config['infusable'] ) ) { |
| 87 |
$this->setInfusable( $config['infusable'] ); |
| 88 |
} |
| 89 |
if ( isset( $config['data'] ) ) { |
| 90 |
$this->setData( $config['data'] ); |
| 91 |
} |
| 92 |
if ( isset( $config['classes'] ) && is_array( $config['classes'] ) ) { |
| 93 |
$this->ownClasses = $config['classes']; |
| 94 |
$this->addClasses( $this->ownClasses ); |
| 95 |
} |
| 96 |
if ( isset( $config['id'] ) ) { |
| 97 |
$this->setAttributes( [ 'id' => $config['id'] ] ); |
| 98 |
} |
| 99 |
if ( isset( $config['text'] ) ) { |
| 100 |
// JS compatibility |
| 101 |
$this->appendContent( $config['text'] ); |
| 102 |
} |
| 103 |
if ( isset( $config['content'] ) ) { |
| 104 |
$this->appendContent( $config['content'] ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get the HTML tag name. |
| 110 |
* |
| 111 |
* Override this method to base the result on instance information. |
| 112 |
* |
| 113 |
* @return string HTML tag name |
| 114 |
*/ |
| 115 |
public function getTagName() { |
| 116 |
return $this::$tagName; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Toggle visibility of an element. |
| 121 |
* |
| 122 |
* @param bool|null $show Make element visible, omit to toggle visibility |
| 123 |
* @return $this |
| 124 |
*/ |
| 125 |
public function toggle( $show = null ) { |
| 126 |
$show = $show === null ? !$this->visible : $show; |
| 127 |
|
| 128 |
$this->visible = $show; |
| 129 |
$this->toggleClasses( [ 'oo-ui-element-hidden' ], !$this->visible ); |
| 130 |
|
| 131 |
return $this; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get element data. |
| 136 |
* |
| 137 |
* @return mixed Element data |
| 138 |
*/ |
| 139 |
public function getData() { |
| 140 |
return $this->data; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Set element data. |
| 145 |
* |
| 146 |
* @param mixed $data Element data |
| 147 |
* @return $this |
| 148 |
*/ |
| 149 |
public function setData( $data ) { |
| 150 |
$this->data = $data; |
| 151 |
return $this; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Check if element supports one or more methods. |
| 156 |
* |
| 157 |
* @param string|string[] $methods Method or list of methods to check |
| 158 |
* @return bool All methods are supported |
| 159 |
*/ |
| 160 |
public function supports( $methods ) { |
| 161 |
foreach ( (array)$methods as $method ) { |
| 162 |
if ( !method_exists( $this, $method ) ) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Register an additional function to call when building the config. See ::getConfig(). |
| 172 |
* |
| 173 |
* @param callable $func The function. Parameters and return value are the same as ::getConfig(). |
| 174 |
*/ |
| 175 |
public function registerConfigCallback( callable $func ) { |
| 176 |
$this->configCallbacks[] = $func; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Add the necessary properties to the given `$config` array to allow |
| 181 |
* reconstruction of this widget via its constructor. |
| 182 |
* @param array &$config An array which will be mutated to add the necessary configuration |
| 183 |
* properties. Unless you are implementing a subclass, you should |
| 184 |
* always pass a new empty array `[]`. |
| 185 |
* @return array A configuration array which can be passed to this object's |
| 186 |
* constructor to recreate it. This is a return value to allow |
| 187 |
* the safe use of copy-by-value functions like `array_merge` in |
| 188 |
* the implementation. |
| 189 |
*/ |
| 190 |
public function getConfig( &$config ) { |
| 191 |
// If there are traits, add their config |
| 192 |
foreach ( $this->configCallbacks as $func ) { |
| 193 |
$func( $config ); |
| 194 |
} |
| 195 |
|
| 196 |
if ( $this->data !== null ) { |
| 197 |
$config['data'] = $this->data; |
| 198 |
} |
| 199 |
if ( $this->ownClasses !== [] ) { |
| 200 |
$config['classes'] = $this->ownClasses; |
| 201 |
} |
| 202 |
return $config; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Create a modified version of the configuration array suitable for |
| 207 |
* JSON serialization by replacing `Tag` references and |
| 208 |
* `HtmlSnippet`s. |
| 209 |
* |
| 210 |
* @return array A serialized configuration array. |
| 211 |
*/ |
| 212 |
private function getSerializedConfig() { |
| 213 |
// Ensure that '_' comes first in the output. |
| 214 |
$config = [ '_' => true ]; |
| 215 |
$config = $this->getConfig( $config ); |
| 216 |
// Post-process config array to turn Tag references into ID references |
| 217 |
// and HtmlSnippet references into a { html: 'string' } JSON form. |
| 218 |
$replaceElements = static function ( &$item ) { |
| 219 |
if ( $item instanceof Tag ) { |
| 220 |
$item->ensureInfusableId(); |
| 221 |
$item = [ 'tag' => $item->getAttribute( 'id' ) ]; |
| 222 |
} elseif ( $item instanceof HtmlSnippet ) { |
| 223 |
$item = [ 'html' => (string)$item ]; |
| 224 |
} |
| 225 |
}; |
| 226 |
array_walk_recursive( $config, $replaceElements ); |
| 227 |
// Set '_' last to ensure that subclasses can't accidentally step on it. |
| 228 |
$config['_'] = $this->getJavaScriptClassName(); |
| 229 |
return $config; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* The class name of the JavaScript version of this widget |
| 234 |
* @return string |
| 235 |
*/ |
| 236 |
protected function getJavaScriptClassName() { |
| 237 |
return str_replace( 'OOUI\\', 'OO.ui.', get_class( $this ) ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* @return string[] |
| 242 |
*/ |
| 243 |
protected function getGeneratedAttributes() { |
| 244 |
$attributesArray = parent::getGeneratedAttributes(); |
| 245 |
// Add `data-ooui` attribute from serialized config array. |
| 246 |
if ( $this->infusable ) { |
| 247 |
$serialized = $this->getSerializedConfig(); |
| 248 |
$attributesArray['data-ooui'] = json_encode( $serialized ); |
| 249 |
} |
| 250 |
return $attributesArray; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Render element into HTML. |
| 255 |
* |
| 256 |
* @return string HTML serialization |
| 257 |
*/ |
| 258 |
public function toString() { |
| 259 |
Theme::singleton()->updateElementClasses( $this ); |
| 260 |
if ( $this->isInfusable() ) { |
| 261 |
$this->ensureInfusableId(); |
| 262 |
} |
| 263 |
return parent::toString(); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get the direction of the user interface for a given element. |
| 268 |
* |
| 269 |
* Currently only per-document directionality is supported. |
| 270 |
* |
| 271 |
* @param Tag $element Element to check |
| 272 |
* @return string Text direction, either 'ltr' or 'rtl' |
| 273 |
*/ |
| 274 |
public static function getDir( Tag $element ) { |
| 275 |
return self::$defaultDir; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Set the default direction of the user interface. |
| 280 |
* |
| 281 |
* @param string $dir Text direction, either 'ltr' or 'rtl' |
| 282 |
*/ |
| 283 |
public static function setDefaultDir( $dir ) { |
| 284 |
self::$defaultDir = $dir === 'rtl' ? 'rtl' : 'ltr'; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* A helper method to massage an array of HTML attributes into a format that is more likely to |
| 289 |
* work with an OOUI PHP element, camel-casing attribute names and setting values of boolean |
| 290 |
* ones to true. Intended as a convenience to be used when refactoring legacy systems using HTML |
| 291 |
* to use OOUI. |
| 292 |
* |
| 293 |
* @param array $attrs HTML attributes, e.g. `[ 'disabled' => '', 'accesskey' => 'k' ]` |
| 294 |
* @return array OOUI PHP element config, e.g. `[ 'disabled' => true, 'accessKey' => 'k' ]` |
| 295 |
*/ |
| 296 |
public static function configFromHtmlAttributes( array $attrs ) { |
| 297 |
$booleanAttrs = [ |
| 298 |
'disabled' => true, |
| 299 |
'required' => true, |
| 300 |
'autofocus' => true, |
| 301 |
'multiple' => true, |
| 302 |
'readonly' => true, |
| 303 |
]; |
| 304 |
$attributeToConfig = [ |
| 305 |
'maxlength' => 'maxLength', |
| 306 |
'minlength' => 'minLength', |
| 307 |
'readonly' => 'readOnly', |
| 308 |
'tabindex' => 'tabIndex', |
| 309 |
'accesskey' => 'accessKey', |
| 310 |
]; |
| 311 |
$config = []; |
| 312 |
foreach ( $attrs as $key => $value ) { |
| 313 |
if ( isset( $booleanAttrs[$key] ) && $value !== false && $value !== null ) { |
| 314 |
$value = true; |
| 315 |
} |
| 316 |
if ( isset( $attributeToConfig[$key] ) ) { |
| 317 |
$key = $attributeToConfig[$key]; |
| 318 |
} |
| 319 |
$config[$key] = $value; |
| 320 |
} |
| 321 |
return $config; |
| 322 |
} |
| 323 |
} |