3 namespace MediaWiki\Widget
;
11 * @copyright 2016 MediaWiki Widgets Team and others; see AUTHORS.txt
14 class DateInputWidget
extends \OOUI\TextInputWidget
{
16 protected $inputFormat = null;
17 protected $displayFormat = null;
18 protected $longDisplayFormat = null;
19 protected $placeholderLabel = null;
20 protected $placeholderDateFormat = null;
21 protected $precision = null;
22 protected $mustBeAfter = null;
23 protected $mustBeBefore = null;
26 * @param array $config Configuration options
27 * - string $config['inputFormat'] Date format string to use for the textual input field.
28 * Displayed while the widget is active, and the user can type in a date in this format.
29 * Should be short and easy to type. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
31 * - string $config['displayFormat'] Date format string to use for the clickable label.
32 * while the widget is inactive. Should be as unambiguous as possible (for example, prefer
33 * to spell out the month, rather than rely on the order), even if that makes it longer.
34 * Applicable only if the widget is infused. (default: language-specific)
35 * - string $config['longDisplayFormat'] If a custom displayFormat is not specified, use
36 * unabbreviated day of the week and month names in the default language-specific
37 * displayFormat. (default: false)
38 * - string $config['placeholderLabel'] Placeholder text shown when the widget is not
39 * selected. Applicable only if the widget is infused. (default: taken from message
40 * `mw-widgets-dateinput-no-date`)
41 * - string $config['placeholderDateFormat'] User-visible date format string displayed
42 * in the textual input field when it's empty. Should be the same as `inputFormat`, but
43 * translated to the user's language. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
45 * - string $config['precision'] Date precision to use, 'day' or 'month' (default: 'day')
46 * - string $config['mustBeAfter'] Validates the date to be after this.
47 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
48 * - string $config['mustBeBefore'] Validates the date to be before this.
49 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
51 public function __construct( array $config = [] ) {
52 $config = array_merge( [
53 // Default config values
55 'longDisplayFormat' => false,
59 if ( isset( $config['inputFormat'] ) ) {
60 $this->inputFormat
= $config['inputFormat'];
62 if ( isset( $config['placeholderDateFormat'] ) ) {
63 $this->placeholderDateFormat
= $config['placeholderDateFormat'];
65 $this->precision
= $config['precision'];
66 if ( isset( $config['mustBeAfter'] ) ) {
67 $this->mustBeAfter
= $config['mustBeAfter'];
69 if ( isset( $config['mustBeBefore'] ) ) {
70 $this->mustBeBefore
= $config['mustBeBefore'];
73 // Properties stored for the infused JS widget
74 if ( isset( $config['displayFormat'] ) ) {
75 $this->displayFormat
= $config['displayFormat'];
77 if ( isset( $config['longDisplayFormat'] ) ) {
78 $this->longDisplayFormat
= $config['longDisplayFormat'];
80 if ( isset( $config['placeholderLabel'] ) ) {
81 $this->placeholderLabel
= $config['placeholderLabel'];
84 // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
85 if ( $this->placeholderDateFormat
!== null ) {
86 $placeholder = $this->placeholderDateFormat
;
87 } elseif ( $this->inputFormat
!== null ) {
88 // We have no way to display a translated placeholder for custom formats
91 $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text();
94 $config = array_merge( [
95 // Processed config values
96 'placeholder' => $placeholder,
99 parent
::__construct( $config );
101 // Calculate min/max attributes (which are skipped by TextInputWidget) and add to <input>
102 // min/max attributes are inclusive, but mustBeAfter/Before are exclusive
103 if ( $this->mustBeAfter
!== null ) {
104 $min = new DateTime( $this->mustBeAfter
);
105 $min = $min->modify( '+1 day' );
106 $min = $min->format( 'Y-m-d' );
107 $this->input
->setAttributes( [ 'min' => $min ] );
109 if ( $this->mustBeBefore
!== null ) {
110 $max = new DateTime( $this->mustBeBefore
);
111 $max = $max->modify( '-1 day' );
112 $max = $max->format( 'Y-m-d' );
113 $this->input
->setAttributes( [ 'max' => $max ] );
117 $this->addClasses( [ 'mw-widget-dateInputWidget' ] );
120 protected function getJavaScriptClassName() {
121 return 'mw.widgets.DateInputWidget';
124 public function getConfig( &$config ) {
125 if ( $this->inputFormat
!== null ) {
126 $config['inputFormat'] = $this->inputFormat
;
128 if ( $this->displayFormat
!== null ) {
129 $config['displayFormat'] = $this->displayFormat
;
131 if ( $this->longDisplayFormat
!== null ) {
132 $config['longDisplayFormat'] = $this->longDisplayFormat
;
134 if ( $this->placeholderLabel
!== null ) {
135 $config['placeholderLabel'] = $this->placeholderLabel
;
137 if ( $this->placeholderDateFormat
!== null ) {
138 $config['placeholderDateFormat'] = $this->placeholderDateFormat
;
140 if ( $this->precision
!== null ) {
141 $config['precision'] = $this->precision
;
143 if ( $this->mustBeAfter
!== null ) {
144 $config['mustBeAfter'] = $this->mustBeAfter
;
146 if ( $this->mustBeBefore
!== null ) {
147 $config['mustBeBefore'] = $this->mustBeBefore
;
149 $config['$overlay'] = true;
150 return parent
::getConfig( $config );
153 public function getInputElement( $config ) {
154 // Inserts date/month type attribute
155 return parent
::getInputElement( $config )
157 'type' => ( $config['precision'] === 'month' ) ?
'month' : 'date'