summaryrefslogtreecommitdiff
path: root/vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php
blob: 210729fdc5954da8eb90dad5bf3ea8589390b673 (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
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
<?php

namespace OOUI;

/**
 * Input widget with a text field.
 */
class TextInputWidget extends InputWidget {

	/* Properties */

	/**
	 * Input field type.
	 *
	 * @var string
	 */
	protected $type = null;

	/**
	 * Prevent changes.
	 *
	 * @var boolean
	 */
	protected $readOnly = false;

	/**
	 * Allow multiple lines of text.
	 *
	 * @var boolean
	 */
	protected $multiline = false;

	/**
	 * @param array $config Configuration options
	 * @param string $config['type'] HTML tag `type` attribute: 'text', 'password', 'search', 'email'
	 *   or 'url'. Ignored if `multiline` is true. (default: 'text')
	 *
	 *   Some values of `type` result in additional behaviors:
	 *   - `search`: implies `icon: 'search'` and `indicator: 'clear'`; when clicked, the indicator
	 *     empties the text field
	 * @param string $config['placeholder'] Placeholder text
	 * @param boolean $config['autofocus'] Ask the browser to focus this widget, using the 'autofocus'
	 *   HTML attribute (default: false)
	 * @param boolean $config['readOnly'] Prevent changes (default: false)
	 * @param number $config['maxLength'] Maximum allowed number of characters to input
	 * @param boolean $config['multiline'] Allow multiple lines of text (default: false)
	 * @param int $config['rows'] If multiline, number of visible lines in textarea
	 * @param boolean $config['required'] Mark the field as required.
	 *   Implies `indicator: 'required'`. (default: false)
	 * @param boolean $config['autocomplete'] If the field should support autocomplete
	 *   or not (default: true)
	 */
	public function __construct( array $config = array() ) {
		// Config initialization
		$config = array_merge( array(
			'type' => 'text',
			'readOnly' => false,
			'autofocus' => false,
			'required' => false,
			'autocomplete' => true,
		), $config );
		if ( $config['type'] === 'search' ) {
			if ( !array_key_exists( 'icon', $config ) ) {
				$config['icon'] = 'search';
			}
		}
		if ( $config['required'] ) {
			if ( !array_key_exists( 'indicator', $config ) ) {
				$config['indicator'] = 'required';
			}
		}

		// Parent constructor
		parent::__construct( $config );

		// Properties
		$this->type = $this->getSaneType( $config );
		$this->multiline = isset( $config['multiline'] ) ? (bool)$config['multiline'] : false;

		// Mixins
		$this->mixin( new IconElement( $this, $config ) );
		$this->mixin( new IndicatorElement( $this, $config ) );

		// Initialization
		$this
			->addClasses( array( 'oo-ui-textInputWidget', 'oo-ui-textInputWidget-type-' . $this->type ) )
			->appendContent( $this->icon, $this->indicator );
		$this->setReadOnly( $config['readOnly'] );
		if ( isset( $config['placeholder'] ) ) {
			$this->input->setAttributes( array( 'placeholder' => $config['placeholder'] ) );
		}
		if ( isset( $config['maxLength'] ) ) {
			$this->input->setAttributes( array( 'maxlength' => $config['maxLength'] ) );
		}
		if ( $config['autofocus'] ) {
			$this->input->setAttributes( array( 'autofocus' => 'autofocus' ) );
		}
		if ( $config['required'] ) {
			$this->input->setAttributes( array( 'required' => 'required', 'aria-required' => 'true' ) );
		}
		if ( !$config['autocomplete'] ) {
			$this->input->setAttributes( array( 'autocomplete' => 'off' ) );
		}
		if ( $this->multiline && isset( $config['rows'] ) ) {
			$this->input->setAttributes( array( 'rows' => $config['rows'] ) );
		}
	}

	/**
	 * Check if the widget is read-only.
	 *
	 * @return boolean
	 */
	public function isReadOnly() {
		return $this->readOnly;
	}

	/**
	 * Set the read-only state of the widget. This should probably change the widget's appearance and
	 * prevent it from being used.
	 *
	 * @param boolean $state Make input read-only
	 * @chainable
	 */
	public function setReadOnly( $state ) {
		$this->readOnly = (bool)$state;
		if ( $this->readOnly ) {
			$this->input->setAttributes( array( 'readonly' => 'readonly' ) );
		} else {
			$this->input->removeAttributes( array( 'readonly' ) );
		}
		return $this;
	}

	protected function getInputElement( $config ) {
		if ( isset( $config['multiline'] ) && $config['multiline'] ) {
			return new Tag( 'textarea' );
		} else {
			$input = new Tag( 'input' );
			$input->setAttributes( array( 'type' => $this->getSaneType( $config ) ) );
			return $input;
		}
	}

	private function getSaneType( $config ) {
		if ( isset( $config['multiline'] ) && $config['multiline'] ) {
			return 'multiline';
		} else {
			$type = in_array( $config['type'], array( 'text', 'password', 'search', 'email', 'url' ) ) ?
				$config['type'] :
				'text';
			return $type;
		}
	}

	/**
	 * Check if input supports multiple lines.
	 *
	 * @return boolean
	 */
	public function isMultiline() {
		return (bool)$this->multiline;
	}

	public function getConfig( &$config ) {
		if ( $this->isMultiline() ) {
			$config['multiline'] = true;
			$rows = $this->input->getAttribute( 'rows' );
			if ( $rows !== null ) {
				$config['rows'] = $rows;
			}
		} else {
			$type = $this->input->getAttribute( 'type' );
			if ( $type !== 'text' ) {
				$config['type'] = $type;
			}
		}
		if ( $this->isReadOnly() ) {
			$config['readOnly'] = true;
		}
		$placeholder = $this->input->getAttribute( 'placeholder' );
		if ( $placeholder !== null ) {
			$config['placeholder'] = $placeholder;
		}
		$maxlength = $this->input->getAttribute( 'maxlength' );
		if ( $maxlength !== null ) {
			$config['maxLength'] = $maxlength;
		}
		$autofocus = $this->input->getAttribute( 'autofocus' );
		if ( $autofocus !== null ) {
			$config['autofocus'] = true;
		}
		$required = $this->input->getAttribute( 'required' );
		$ariarequired = $this->input->getAttribute( 'aria-required' );
		if ( ( $required !== null ) || ( $ariarequired !== null ) ) {
			$config['required'] = true;
		}
		$autocomplete = $this->input->getAttribute( 'autocomplete' );
		if ( $autocomplete !== null ) {
			$config['autocomplete'] = false;
		}
		return parent::getConfig( $config );
	}
}