2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
class HTMLTextField extends HTMLFormField {
|
2016-04-01 12:06:49 +00:00
|
|
|
protected $mPlaceholder = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
* - type: HTML textfield type
|
|
|
|
|
* - size: field size in characters (defaults to 45)
|
|
|
|
|
* - placeholder/placeholder-message: set HTML placeholder attribute
|
|
|
|
|
* - spellcheck: set HTML spellcheck attribute
|
|
|
|
|
* - persistent: upon unsuccessful requests, retain the value (defaults to true, except
|
|
|
|
|
* for password fields)
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $params ) {
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
|
|
|
|
|
if ( isset( $params['placeholder-message'] ) ) {
|
|
|
|
|
$this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->parse();
|
|
|
|
|
} elseif ( isset( $params['placeholder'] ) ) {
|
|
|
|
|
$this->mPlaceholder = $params['placeholder'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
function getSize() {
|
2013-11-19 13:08:16 +00:00
|
|
|
return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-18 22:35:15 +00:00
|
|
|
function getSpellCheck() {
|
|
|
|
|
$val = isset( $this->mParams['spellcheck'] ) ? $this->mParams['spellcheck'] : null;
|
2015-06-26 05:32:28 +00:00
|
|
|
if ( is_bool( $val ) ) {
|
2015-05-18 22:35:15 +00:00
|
|
|
// "spellcheck" attribute literally requires "true" or "false" to work.
|
|
|
|
|
return $val === true ? 'true' : 'false';
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 11:54:15 +00:00
|
|
|
public function isPersistent() {
|
|
|
|
|
if ( isset( $this->mParams['persistent'] ) ) {
|
|
|
|
|
return $this->mParams['persistent'];
|
|
|
|
|
}
|
|
|
|
|
// don't put passwords into the HTML body, they could get cached or otherwise leaked
|
|
|
|
|
return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
function getInputHTML( $value ) {
|
2016-04-01 11:54:15 +00:00
|
|
|
if ( !$this->isPersistent() ) {
|
|
|
|
|
$value = '';
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$attribs = [
|
2013-11-19 12:55:50 +00:00
|
|
|
'id' => $this->mID,
|
|
|
|
|
'name' => $this->mName,
|
|
|
|
|
'size' => $this->getSize(),
|
|
|
|
|
'value' => $value,
|
2015-04-19 19:10:45 +00:00
|
|
|
'dir' => $this->mDir,
|
2015-05-18 22:35:15 +00:00
|
|
|
'spellcheck' => $this->getSpellCheck(),
|
2015-10-29 17:51:02 +00:00
|
|
|
] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$attribs['class'] = $this->mClass;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2016-04-01 12:06:49 +00:00
|
|
|
if ( $this->mPlaceholder !== '' ) {
|
|
|
|
|
$attribs['placeholder'] = $this->mPlaceholder;
|
|
|
|
|
}
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
# @todo Enforce pattern, step, required, readonly on the server side as
|
2013-11-19 12:55:50 +00:00
|
|
|
# well
|
2016-02-17 09:09:32 +00:00
|
|
|
$allowedParams = [
|
2014-09-30 16:06:56 +00:00
|
|
|
'type',
|
2013-11-19 12:55:50 +00:00
|
|
|
'min',
|
|
|
|
|
'max',
|
|
|
|
|
'pattern',
|
|
|
|
|
'title',
|
|
|
|
|
'step',
|
|
|
|
|
'list',
|
2013-05-02 18:27:44 +00:00
|
|
|
'maxlength',
|
|
|
|
|
'tabindex',
|
|
|
|
|
'disabled',
|
|
|
|
|
'required',
|
|
|
|
|
'autofocus',
|
|
|
|
|
'multiple',
|
|
|
|
|
'readonly'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2013-05-02 18:27:44 +00:00
|
|
|
$attribs += $this->getAttributes( $allowedParams );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2014-09-30 16:06:56 +00:00
|
|
|
# Extract 'type'
|
2015-04-21 21:03:49 +00:00
|
|
|
$type = $this->getType( $attribs );
|
|
|
|
|
return Html::input( $this->mName, $value, $type, $attribs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getType( &$attribs ) {
|
2014-09-30 16:06:56 +00:00
|
|
|
$type = isset( $attribs['type'] ) ? $attribs['type'] : 'text';
|
|
|
|
|
unset( $attribs['type'] );
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
# Implement tiny differences between some field variants
|
|
|
|
|
# here, rather than creating a new class for each one which
|
|
|
|
|
# is essentially just a clone of this one.
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $this->mParams['type'] ) ) {
|
|
|
|
|
switch ( $this->mParams['type'] ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
case 'int':
|
2014-07-30 17:56:25 +00:00
|
|
|
$type = 'number';
|
2013-11-19 12:55:50 +00:00
|
|
|
break;
|
|
|
|
|
case 'float':
|
2014-07-30 17:56:25 +00:00
|
|
|
$type = 'number';
|
2013-11-19 13:08:16 +00:00
|
|
|
$attribs['step'] = 'any';
|
2013-11-19 12:55:50 +00:00
|
|
|
break;
|
|
|
|
|
# Pass through
|
2014-02-28 21:42:27 +00:00
|
|
|
case 'email':
|
2013-11-19 12:55:50 +00:00
|
|
|
case 'password':
|
|
|
|
|
case 'file':
|
2014-02-28 21:42:27 +00:00
|
|
|
case 'url':
|
2014-07-30 17:56:25 +00:00
|
|
|
$type = $this->mParams['type'];
|
2013-11-19 12:55:50 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-30 16:06:56 +00:00
|
|
|
|
2015-04-21 21:03:49 +00:00
|
|
|
return $type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getInputOOUI( $value ) {
|
2016-04-01 11:54:15 +00:00
|
|
|
if ( !$this->isPersistent() ) {
|
|
|
|
|
$value = '';
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-21 21:03:49 +00:00
|
|
|
$attribs = $this->getTooltipAndAccessKey();
|
|
|
|
|
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$attribs['classes'] = [ $this->mClass ];
|
2015-04-21 21:03:49 +00:00
|
|
|
}
|
2016-04-01 12:06:49 +00:00
|
|
|
if ( $this->mPlaceholder !== '' ) {
|
|
|
|
|
$attribs['placeholder'] = $this->mPlaceholder;
|
|
|
|
|
}
|
2015-04-21 21:03:49 +00:00
|
|
|
|
|
|
|
|
# @todo Enforce pattern, step, required, readonly on the server side as
|
|
|
|
|
# well
|
2016-02-17 09:09:32 +00:00
|
|
|
$allowedParams = [
|
2015-04-21 21:03:49 +00:00
|
|
|
'autofocus',
|
|
|
|
|
'autosize',
|
|
|
|
|
'disabled',
|
|
|
|
|
'flags',
|
|
|
|
|
'indicator',
|
|
|
|
|
'maxlength',
|
|
|
|
|
'readonly',
|
|
|
|
|
'required',
|
|
|
|
|
'tabindex',
|
|
|
|
|
'type',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-04-21 21:03:49 +00:00
|
|
|
|
2016-03-02 19:09:53 +00:00
|
|
|
$attribs += OOUI\Element::configFromHtmlAttributes(
|
|
|
|
|
$this->getAttributes( $allowedParams )
|
|
|
|
|
);
|
2015-04-21 21:03:49 +00:00
|
|
|
|
|
|
|
|
$type = $this->getType( $attribs );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return $this->getInputWidget( [
|
2015-04-21 21:03:49 +00:00
|
|
|
'id' => $this->mID,
|
|
|
|
|
'name' => $this->mName,
|
|
|
|
|
'value' => $value,
|
|
|
|
|
'type' => $type,
|
2016-02-17 09:09:32 +00:00
|
|
|
] + $attribs );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2015-07-01 05:27:23 +00:00
|
|
|
|
|
|
|
|
protected function getInputWidget( $params ) {
|
|
|
|
|
return new OOUI\TextInputWidget( $params );
|
|
|
|
|
}
|
2015-10-29 17:51:02 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of data-* attributes to add to the field.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function getDataAttribs() {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|