3 * MediaWiki Widgets – DateInputWidget class.
5 * @copyright 2016 MediaWiki Widgets Team and others; see AUTHORS.txt
6 * @license The MIT License (MIT); see LICENSE.txt
9 namespace MediaWiki\Widget
;
18 class DateInputWidget
extends \OOUI\TextInputWidget
{
20 protected $inputFormat = null;
21 protected $displayFormat = null;
22 protected $longDisplayFormat = null;
23 protected $placeholderLabel = null;
24 protected $placeholderDateFormat = null;
25 protected $precision = null;
26 protected $mustBeAfter = null;
27 protected $mustBeBefore = null;
30 * @param array $config Configuration options
31 * - string $config['inputFormat'] Date format string to use for the textual input field.
32 * Displayed while the widget is active, and the user can type in a date in this format.
33 * Should be short and easy to type. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
35 * - string $config['displayFormat'] Date format string to use for the clickable label.
36 * while the widget is inactive. Should be as unambiguous as possible (for example, prefer
37 * to spell out the month, rather than rely on the order), even if that makes it longer.
38 * Applicable only if the widget is infused. (default: language-specific)
39 * - string $config['longDisplayFormat'] If a custom displayFormat is not specified, use
40 * unabbreviated day of the week and month names in the default language-specific
41 * displayFormat. (default: false)
42 * - string $config['placeholderLabel'] Placeholder text shown when the widget is not
43 * selected. Applicable only if the widget is infused. (default: taken from message
44 * `mw-widgets-dateinput-no-date`)
45 * - string $config['placeholderDateFormat'] User-visible date format string displayed
46 * in the textual input field when it's empty. Should be the same as `inputFormat`, but
47 * translated to the user's language. (default: 'YYYY-MM-DD' or 'YYYY-MM', depending on
49 * - string $config['precision'] Date precision to use, 'day' or 'month' (default: 'day')
50 * - string $config['mustBeAfter'] Validates the date to be after this.
51 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
52 * - string $config['mustBeBefore'] Validates the date to be before this.
53 * In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
55 public function __construct( array $config = [] ) {
56 $config = array_merge( [
57 // Default config values
59 'longDisplayFormat' => false,
63 if ( isset( $config['inputFormat'] ) ) {
64 $this->inputFormat
= $config['inputFormat'];
66 if ( isset( $config['placeholderDateFormat'] ) ) {
67 $this->placeholderDateFormat
= $config['placeholderDateFormat'];
69 $this->precision
= $config['precision'];
70 if ( isset( $config['mustBeAfter'] ) ) {
71 $this->mustBeAfter
= $config['mustBeAfter'];
73 if ( isset( $config['mustBeBefore'] ) ) {
74 $this->mustBeBefore
= $config['mustBeBefore'];
77 // Properties stored for the infused JS widget
78 if ( isset( $config['displayFormat'] ) ) {
79 $this->displayFormat
= $config['displayFormat'];
81 if ( isset( $config['longDisplayFormat'] ) ) {
82 $this->longDisplayFormat
= $config['longDisplayFormat'];
84 if ( isset( $config['placeholderLabel'] ) ) {
85 $this->placeholderLabel
= $config['placeholderLabel'];
88 // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
89 if ( $this->placeholderDateFormat
!== null ) {
90 $placeholder = $this->placeholderDateFormat
;
91 } elseif ( $this->inputFormat
!== null ) {
92 // We have no way to display a translated placeholder for custom formats
95 $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text();
98 $config = array_merge( [
99 // Processed config values
100 'placeholder' => $placeholder,
103 parent
::__construct( $config );
105 // Calculate min/max attributes (which are skipped by TextInputWidget) and add to <input>
106 // min/max attributes are inclusive, but mustBeAfter/Before are exclusive
107 if ( $this->mustBeAfter
!== null ) {
108 $min = new DateTime( $this->mustBeAfter
);
109 $min = $min->modify( '+1 day' );
110 $min = $min->format( 'Y-m-d' );
111 $this->input
->setAttributes( [ 'min' => $min ] );
113 if ( $this->mustBeBefore
!== null ) {
114 $max = new DateTime( $this->mustBeBefore
);
115 $max = $max->modify( '-1 day' );
116 $max = $max->format( 'Y-m-d' );
117 $this->input
->setAttributes( [ 'max' => $max ] );
121 $this->addClasses( [ 'mw-widget-dateInputWidget' ] );
124 protected function getJavaScriptClassName() {
125 return 'mw.widgets.DateInputWidget';
128 public function getConfig( &$config ) {
129 if ( $this->inputFormat
!== null ) {
130 $config['inputFormat'] = $this->inputFormat
;
132 if ( $this->displayFormat
!== null ) {
133 $config['displayFormat'] = $this->displayFormat
;
135 if ( $this->longDisplayFormat
!== null ) {
136 $config['longDisplayFormat'] = $this->longDisplayFormat
;
138 if ( $this->placeholderLabel
!== null ) {
139 $config['placeholderLabel'] = $this->placeholderLabel
;
141 if ( $this->placeholderDateFormat
!== null ) {
142 $config['placeholderDateFormat'] = $this->placeholderDateFormat
;
144 if ( $this->precision
!== null ) {
145 $config['precision'] = $this->precision
;
147 if ( $this->mustBeAfter
!== null ) {
148 $config['mustBeAfter'] = $this->mustBeAfter
;
150 if ( $this->mustBeBefore
!== null ) {
151 $config['mustBeBefore'] = $this->mustBeBefore
;
153 $config['$overlay'] = true;
154 return parent
::getConfig( $config );
157 public function getInputElement( $config ) {
158 // Inserts date/month type attribute
159 return parent
::getInputElement( $config )
161 'type' => ( $config['precision'] === 'month' ) ?
'month' : 'date'