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
|
<?php
namespace OOUI;
/**
* DOM element abstraction.
*
* @abstract
*/
class Element extends Tag {
/* Static Properties */
/**
* HTML tag name.
*
* This may be ignored if getTagName() is overridden.
*
* @var string
*/
public static $tagName = 'div';
/**
* Default text direction, used for some layout calculations. Use setDefaultDir() to change.
*
* Currently only per-document directionality is supported.
*
* @var string
*/
public static $defaultDir = 'ltr';
/* Properties */
/**
* Element data.
*
* @var mixed
*/
protected $data = null;
/**
* @var bool
*/
protected $visible = true;
/**
* Strings of the CSS classes explicitly configured for this element (as opposed to #$classes,
* which contains all classes for this element).
*
* @var array
*/
protected $ownClasses = [];
/**
* @var callable[]
*/
protected $configCallbacks = [];
/* Static methods */
/**
* Emits a deprecation warning with provided message.
*
* @param string $message Message about the deprecation
*/
public static function warnDeprecation( $message = '' ) {
trigger_error( $message, E_USER_DEPRECATED );
}
/* Methods */
/**
* @param array $config Configuration options
* - string[] $config['classes'] CSS class names to add
* - string $config['id'] HTML id attribute
* - string $config['text'] Text to insert
* - string[]|HtmlSnippet[]|Element[] $config['content'] Content to append (after text).
* Strings will be HTML-escaped for output, use an HtmlSnippet instance to prevent that.
* - mixed $config['data'] Element data
*/
public function __construct( array $config = [] ) {
// Parent constructor
parent::__construct( $this->getTagName() );
// Initialization
if ( isset( $config['infusable'] ) && is_bool( $config['infusable'] ) ) {
$this->setInfusable( $config['infusable'] );
}
if ( isset( $config['data'] ) ) {
$this->setData( $config['data'] );
}
if ( isset( $config['classes'] ) && is_array( $config['classes'] ) ) {
$this->ownClasses = $config['classes'];
$this->addClasses( $this->ownClasses );
}
if ( isset( $config['id'] ) ) {
$this->setAttributes( [ 'id' => $config['id'] ] );
}
if ( isset( $config['text'] ) ) {
// JS compatibility
$this->appendContent( $config['text'] );
}
if ( isset( $config['content'] ) ) {
$this->appendContent( $config['content'] );
}
}
/**
* Get the HTML tag name.
*
* Override this method to base the result on instance information.
*
* @return string HTML tag name
*/
public function getTagName() {
return $this::$tagName;
}
/**
* Toggle visibility of an element.
*
* @param bool|null $show Make element visible, omit to toggle visibility
* @return $this
*/
public function toggle( $show = null ) {
$show = $show === null ? !$this->visible : $show;
$this->visible = $show;
$this->toggleClasses( [ 'oo-ui-element-hidden' ], !$this->visible );
return $this;
}
/**
* Get element data.
*
* @return mixed Element data
*/
public function getData() {
return $this->data;
}
/**
* Set element data.
*
* @param mixed $data Element data
* @return $this
*/
public function setData( $data ) {
$this->data = $data;
return $this;
}
/**
* Check if element supports one or more methods.
*
* @param string|string[] $methods Method or list of methods to check
* @return bool All methods are supported
*/
public function supports( $methods ) {
foreach ( (array)$methods as $method ) {
if ( !method_exists( $this, $method ) ) {
return false;
}
}
return true;
}
/**
* Register an additional function to call when building the config. See ::getConfig().
*
* @param callable $func The function. Parameters and return value are the same as ::getConfig().
*/
public function registerConfigCallback( callable $func ) {
$this->configCallbacks[] = $func;
}
/**
* Add the necessary properties to the given `$config` array to allow
* reconstruction of this widget via its constructor.
* @param array &$config An array which will be mutated to add the necessary configuration
* properties. Unless you are implementing a subclass, you should
* always pass a new empty array `[]`.
* @return array A configuration array which can be passed to this object's
* constructor to recreate it. This is a return value to allow
* the safe use of copy-by-value functions like `array_merge` in
* the implementation.
*/
public function getConfig( &$config ) {
// If there are traits, add their config
foreach ( $this->configCallbacks as $func ) {
$func( $config );
}
if ( $this->data !== null ) {
$config['data'] = $this->data;
}
if ( $this->ownClasses !== [] ) {
$config['classes'] = $this->ownClasses;
}
return $config;
}
/**
* Create a modified version of the configuration array suitable for
* JSON serialization by replacing `Tag` references and
* `HtmlSnippet`s.
*
* @return array A serialized configuration array.
*/
private function getSerializedConfig() {
// Ensure that '_' comes first in the output.
$config = [ '_' => true ];
$config = $this->getConfig( $config );
// Post-process config array to turn Tag references into ID references
// and HtmlSnippet references into a { html: 'string' } JSON form.
$replaceElements = static function ( &$item ) {
if ( $item instanceof Tag ) {
$item->ensureInfusableId();
$item = [ 'tag' => $item->getAttribute( 'id' ) ];
} elseif ( $item instanceof HtmlSnippet ) {
$item = [ 'html' => (string)$item ];
}
};
array_walk_recursive( $config, $replaceElements );
// Set '_' last to ensure that subclasses can't accidentally step on it.
$config['_'] = $this->getJavaScriptClassName();
return $config;
}
/**
* The class name of the JavaScript version of this widget
* @return string
*/
protected function getJavaScriptClassName() {
return str_replace( 'OOUI\\', 'OO.ui.', get_class( $this ) );
}
/**
* @return string[]
*/
protected function getGeneratedAttributes() {
$attributesArray = parent::getGeneratedAttributes();
// Add `data-ooui` attribute from serialized config array.
if ( $this->infusable ) {
$serialized = $this->getSerializedConfig();
$attributesArray['data-ooui'] = json_encode( $serialized );
}
return $attributesArray;
}
/**
* Render element into HTML.
*
* @return string HTML serialization
*/
public function toString() {
Theme::singleton()->updateElementClasses( $this );
if ( $this->isInfusable() ) {
$this->ensureInfusableId();
}
return parent::toString();
}
/**
* Get the direction of the user interface for a given element.
*
* Currently only per-document directionality is supported.
*
* @param Tag $element Element to check
* @return string Text direction, either 'ltr' or 'rtl'
*/
public static function getDir( Tag $element ) {
return self::$defaultDir;
}
/**
* Set the default direction of the user interface.
*
* @param string $dir Text direction, either 'ltr' or 'rtl'
*/
public static function setDefaultDir( $dir ) {
self::$defaultDir = $dir === 'rtl' ? 'rtl' : 'ltr';
}
/**
* A helper method to massage an array of HTML attributes into a format that is more likely to
* work with an OOUI PHP element, camel-casing attribute names and setting values of boolean
* ones to true. Intended as a convenience to be used when refactoring legacy systems using HTML
* to use OOUI.
*
* @param array $attrs HTML attributes, e.g. `[ 'disabled' => '', 'accesskey' => 'k' ]`
* @return array OOUI PHP element config, e.g. `[ 'disabled' => true, 'accessKey' => 'k' ]`
*/
public static function configFromHtmlAttributes( array $attrs ) {
$booleanAttrs = [
'disabled' => true,
'required' => true,
'autofocus' => true,
'multiple' => true,
'readonly' => true,
];
$attributeToConfig = [
'maxlength' => 'maxLength',
'readonly' => 'readOnly',
'tabindex' => 'tabIndex',
'accesskey' => 'accessKey',
];
$config = [];
foreach ( $attrs as $key => $value ) {
if ( isset( $booleanAttrs[$key] ) && $value !== false && $value !== null ) {
$value = true;
}
if ( isset( $attributeToConfig[$key] ) ) {
$key = $attributeToConfig[$key];
}
$config[$key] = $value;
}
return $config;
}
}
|