| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 7 |
use MediaWiki\HTMLForm\VFormHTMLForm; |
| 8 |
use MediaWiki\Message\Message; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class HTMLButtonField extends HTMLFormField { |
| 31 |
|
| 32 |
protected $buttonType = 'button'; |
| 33 |
|
| 34 |
protected $buttonLabel = null; |
| 35 |
|
| 36 |
|
| 37 |
protected $mFlags = []; |
| 38 |
|
| 39 |
|
| 40 |
protected $mFormnovalidate = false; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( $info ) { |
| 47 |
$info['nodata'] = true; |
| 48 |
|
| 49 |
$this->setShowEmptyLabel( false ); |
| 50 |
|
| 51 |
parent::__construct( $info ); |
| 52 |
|
| 53 |
if ( isset( $info['flags'] ) ) { |
| 54 |
$this->mFlags = $info['flags']; |
| 55 |
} |
| 56 |
|
| 57 |
if ( isset( $info['formnovalidate'] ) ) { |
| 58 |
$this->mFormnovalidate = $info['formnovalidate']; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
if ( isset( $info['buttonlabel-message'] ) ) { |
| 63 |
$this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse(); |
| 64 |
} elseif ( isset( $info['buttonlabel'] ) ) { |
| 65 |
if ( $info['buttonlabel'] === ' ' || $info['buttonlabel'] === "\u{00A0}" ) { |
| 66 |
|
| 67 |
$this->buttonLabel = "\u{00A0}"; |
| 68 |
} else { |
| 69 |
$this->buttonLabel = htmlspecialchars( $info['buttonlabel'] ); |
| 70 |
} |
| 71 |
} elseif ( isset( $info['buttonlabel-raw'] ) ) { |
| 72 |
$this->buttonLabel = $info['buttonlabel-raw']; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public function getInputHTML( $value ) { |
| 77 |
$flags = ''; |
| 78 |
$prefix = 'mw-htmlform-'; |
| 79 |
if ( $this->mParent instanceof VFormHTMLForm ) { |
| 80 |
$prefix = 'mw-ui-'; |
| 81 |
|
| 82 |
$flags .= ' ' . $prefix . 'button'; |
| 83 |
} |
| 84 |
foreach ( $this->mFlags as $flag ) { |
| 85 |
$flags .= ' ' . $prefix . $flag; |
| 86 |
} |
| 87 |
$attr = [ |
| 88 |
'class' => 'mw-htmlform-submit ' . $this->mClass . $flags, |
| 89 |
'id' => $this->mID, |
| 90 |
'type' => $this->buttonType, |
| 91 |
'name' => $this->mName, |
| 92 |
'value' => $this->getDefault(), |
| 93 |
'formnovalidate' => $this->mFormnovalidate, |
| 94 |
] + $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
| 95 |
|
| 96 |
return Html::rawElement( 'button', $attr, |
| 97 |
$this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) ); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function getInputOOUI( $value ) { |
| 107 |
return new \OOUI\ButtonInputWidget( [ |
| 108 |
'name' => $this->mName, |
| 109 |
'value' => $this->getDefault(), |
| 110 |
'label' => $this->buttonLabel |
| 111 |
? new \OOUI\HtmlSnippet( $this->buttonLabel ) |
| 112 |
: $this->getDefault(), |
| 113 |
'type' => $this->buttonType, |
| 114 |
'classes' => [ 'mw-htmlform-submit', $this->mClass ], |
| 115 |
'id' => $this->mID, |
| 116 |
'flags' => $this->mFlags, |
| 117 |
] + \OOUI\Element::configFromHtmlAttributes( |
| 118 |
$this->getAttributes( [ 'disabled', 'tabindex' ] ) |
| 119 |
) ); |
| 120 |
} |
| 121 |
|
| 122 |
public function getInputCodex( $value, $hasErrors ) { |
| 123 |
$flags = $this->mFlags; |
| 124 |
$buttonLabel = $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ); |
| 125 |
$buttonClasses = [ 'mw-htmlform-submit', 'cdx-button', $this->mClass ]; |
| 126 |
$buttonAttribs = [ |
| 127 |
'class' => $buttonClasses, |
| 128 |
'id' => $this->mID, |
| 129 |
'type' => $this->buttonType, |
| 130 |
'name' => $this->mName, |
| 131 |
'value' => $this->getDefault(), |
| 132 |
'formnovalidate' => $this->mFormnovalidate, |
| 133 |
] + $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
| 134 |
|
| 135 |
return static::buildCodexComponent( |
| 136 |
$flags, |
| 137 |
$buttonLabel, |
| 138 |
$buttonAttribs |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static function buildCodexComponent( |
| 152 |
$flags, |
| 153 |
$buttonLabel, |
| 154 |
$attribs |
| 155 |
) { |
| 156 |
$flagClasses = []; |
| 157 |
$flagClassMap = [ |
| 158 |
'progressive' => 'cdx-button--action-progressive', |
| 159 |
'destructive' => 'cdx-button--action-destructive', |
| 160 |
'primary' => 'cdx-button--weight-primary', |
| 161 |
'quiet' => 'cdx-button--weight-quiet', |
| 162 |
]; |
| 163 |
|
| 164 |
foreach ( $flags as $flag ) { |
| 165 |
if ( isset( $flagClassMap[$flag] ) ) { |
| 166 |
$flagClasses[] = $flagClassMap[$flag]; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
$buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses ); |
| 171 |
$attribs['class'] = $buttonClassesAndFlags; |
| 172 |
|
| 173 |
$buttonHtml = Html::rawElement( |
| 174 |
'button', $attribs, $buttonLabel |
| 175 |
); |
| 176 |
|
| 177 |
return $buttonHtml; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
protected function needsLabel() { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function validate( $value, $alldata ) { |
| 198 |
return true; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
class_alias( HTMLButtonField::class, 'HTMLButtonField' ); |