From fbc80326a60cf6b8cd9081339924761f050766a1 Mon Sep 17 00:00:00 2001 From: Geoffrey Mon Date: Tue, 18 Oct 2016 11:58:06 -0400 Subject: [PATCH] Add PHP DateInputWidget Bug: T120733 Change-Id: I8f80e27304a916bba16ed255c8e78073f9da3813 --- autoload.php | 1 + includes/widget/AUTHORS.txt | 1 + includes/widget/DateInputWidget.php | 165 ++++++++++++++++++ .../mw.widgets.DateInputWidget.js | 11 ++ 4 files changed, 178 insertions(+) create mode 100644 includes/widget/DateInputWidget.php diff --git a/autoload.php b/autoload.php index 1beb00c5f2..ac6743f679 100644 --- a/autoload.php +++ b/autoload.php @@ -919,6 +919,7 @@ $wgAutoloadLocalClasses = [ 'MediaWiki\\Tidy\\TidyDriverBase' => __DIR__ . '/includes/tidy/TidyDriverBase.php', 'MediaWiki\\Widget\\ComplexNamespaceInputWidget' => __DIR__ . '/includes/widget/ComplexNamespaceInputWidget.php', 'MediaWiki\\Widget\\ComplexTitleInputWidget' => __DIR__ . '/includes/widget/ComplexTitleInputWidget.php', + 'MediaWiki\\Widget\\DateInputWidget' => __DIR__ . '/includes/widget/DateInputWidget.php', 'MediaWiki\\Widget\\DateTimeInputWidget' => __DIR__ . '/includes/widget/DateTimeInputWidget.php', 'MediaWiki\\Widget\\NamespaceInputWidget' => __DIR__ . '/includes/widget/NamespaceInputWidget.php', 'MediaWiki\\Widget\\SearchInputWidget' => __DIR__ . '/includes/widget/SearchInputWidget.php', diff --git a/includes/widget/AUTHORS.txt b/includes/widget/AUTHORS.txt index ca188ba1fe..2490b9d8fa 100644 --- a/includes/widget/AUTHORS.txt +++ b/includes/widget/AUTHORS.txt @@ -5,6 +5,7 @@ Bartosz Dziewoński Brad Jorsch Ed Sanders Florian Schmidt +Geoffrey Mon James D. Forrester Roan Kattouw Sucheta Ghoshal diff --git a/includes/widget/DateInputWidget.php b/includes/widget/DateInputWidget.php new file mode 100644 index 0000000000..f011f0b8af --- /dev/null +++ b/includes/widget/DateInputWidget.php @@ -0,0 +1,165 @@ + 'day', + ], $config ); + + // Properties + if ( isset( $config['inputFormat'] ) ) { + $this->inputFormat = $config['inputFormat']; + } + if ( isset( $config['placeholderDateFormat'] ) ) { + $this->placeholderDateFormat = $config['placeholderDateFormat']; + } + $this->precision = $config['precision']; + if ( isset( $config['mustBeAfter'] ) ) { + $this->mustBeAfter = $config['mustBeAfter']; + } + if ( isset( $config['mustBeBefore'] ) ) { + $this->mustBeBefore = $config['mustBeBefore']; + } + + // Properties stored for the infused JS widget + if ( isset( $config['displayFormat'] ) ) { + $this->displayFormat = $config['displayFormat']; + } + if ( isset( $config['placeholderLabel'] ) ) { + $this->placeholderLabel = $config['placeholderLabel']; + } + if ( isset( $config['overlay'] ) ) { + $this->overlay = $config['overlay']; + } + + // Set up placeholder text visible if the browser doesn't override it (logic taken from JS) + if ( $this->placeholderDateFormat !== null ) { + $placeholder = $this->placeholderDateFormat; + } elseif ( $this->inputFormat !== null ) { + // We have no way to display a translated placeholder for custom formats + $placeholder = ''; + } else { + $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text(); + } + + $config = array_merge( [ + // Processed config values + 'placeholder' => $placeholder, + ], $config ); + + // Parent constructor + parent::__construct( $config ); + + // Calculate min/max attributes (which are skipped by TextInputWidget) and add to + // min/max attributes are inclusive, but mustBeAfter/Before are exclusive + if ( $this->mustBeAfter !== null ) { + $min = new DateTime( $this->mustBeAfter ); + $min = $min->modify( '+1 day' ); + $min = $min->format( 'Y-m-d' ); + $this->input->setAttributes( [ 'min' => $min ] ); + } + if ( $this->mustBeBefore !== null ) { + $max = new DateTime( $this->mustBeBefore ); + $max = $max->modify( '-1 day' ); + $max = $max->format( 'Y-m-d' ); + $this->input->setAttributes( [ 'max' => $max ] ); + } + + // Initialization + $this->addClasses( [ 'mw-widget-dateInputWidget' ] ); + } + + protected function getJavaScriptClassName() { + return 'mw.widgets.DateInputWidget'; + } + + public function getConfig( &$config ) { + if ( $this->inputFormat !== null ) { + $config['inputFormat'] = $this->inputFormat; + } + if ( $this->displayFormat !== null ) { + $config['displayFormat'] = $this->displayFormat; + } + if ( $this->placeholderLabel !== null ) { + $config['placeholderLabel'] = $this->placeholderLabel; + } + if ( $this->placeholderDateFormat !== null ) { + $config['placeholderDateFormat'] = $this->placeholderDateFormat; + } + if ( $this->precision !== null ) { + $config['precision'] = $this->precision; + } + if ( $this->mustBeAfter !== null ) { + $config['mustBeAfter'] = $this->mustBeAfter; + } + if ( $this->mustBeBefore !== null ) { + $config['mustBeBefore'] = $this->mustBeBefore; + } + if ( $this->overlay !== null ) { + $config['overlay'] = $this->overlay; + } + return parent::getConfig( $config ); + } + + public function getInputElement( $config ) { + // Inserts date/month type attribute + return parent::getInputElement( $config ) + ->setAttributes( [ + 'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date' + ] ); + } +} diff --git a/resources/src/mediawiki.widgets/mw.widgets.DateInputWidget.js b/resources/src/mediawiki.widgets/mw.widgets.DateInputWidget.js index a9b5bc30b5..ed251f24ee 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.DateInputWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.DateInputWidget.js @@ -183,6 +183,11 @@ .addClass( 'mw-widget-dateInputWidget' ) .append( this.$handle, this.textInput.$element, this.calendar.$element ); + // config.overlay is the selector to be used for config.$overlay, specified from PHP + if ( config.overlay ) { + config.$overlay = $( config.overlay ); + } + if ( config.$overlay ) { this.calendar.setFloatableContainer( this.$element ); config.$overlay.append( this.calendar.$element ); @@ -219,6 +224,12 @@ this.updateUI(); this.textInput.toggle( false ); this.calendar.toggle( false ); + + // Hide unused from PHP after infusion is done + // See InputWidget#reusePreInfuseDOM about config.$input + if ( config.$input ) { + config.$input.addClass( 'oo-ui-element-hidden' ); + } }; /* Inheritance */ -- 2.20.1