summaryrefslogtreecommitdiff
path: root/vendor/oojs/oojs-ui/php/elements/LabelElement.php
blob: d5cf7bee28a520336700a42b5fdd0ad859431011 (plain)
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
<?php

namespace OOUI;

/**
 * Element containing a label.
 *
 * @abstract
 */
class LabelElement extends ElementMixin {
	/**
	 * Label value.
	 *
	 * @var string|HtmlSnippet|null
	 */
	protected $label = null;

	public static $targetPropertyName = 'label';

	/**
	 * @param Element $element Element being mixed into
	 * @param array $config Configuration options
	 * @param string|HtmlSnippet $config['label'] Label text
	 */
	public function __construct( Element $element, array $config = array() ) {
		// Parent constructor
		// FIXME 'labelElement' is a very stupid way to call '$label'
		$target = isset( $config['labelElement'] ) ? $config['labelElement'] : new Tag( 'span' );
		parent::__construct( $element, $target, $config );

		// Initialization
		$this->target->addClasses( array( 'oo-ui-labelElement-label' ) );
		$this->setLabel( isset( $config['label'] ) ? $config['label'] : null );
	}

	/**
	 * Set the label.
	 *
	 * An empty string will result in the label being hidden. A string containing only whitespace will
	 * be converted to a single `&nbsp;`.
	 *
	 * @param string|HtmlSnippet|null $label Label text
	 * @chainable
	 */
	public function setLabel( $label ) {
		$this->label = $label;

		$this->target->clearContent();
		if ( $this->label !== null ) {
			if ( is_string( $this->label ) && $this->label !== '' && trim( $this->label ) === '' ) {
				$this->target->appendContent( new HtmlSnippet( '&nbsp;' ) );
			} else {
				$this->target->appendContent( $label );
			}
		}

		$this->element->toggleClasses( array( 'oo-ui-labelElement' ), !!$this->label );

		return $this;
	}

	/**
	 * Get the label.
	 *
	 * @return string|HtmlSnippet|null Label text
	 */
	public function getLabel() {
		return $this->label;
	}

	public function getConfig( &$config ) {
		if ( $this->label !== null ) {
			$config['label'] = $this->label;
		}
		return parent::getConfig( $config );
	}
}