mw.widgets.DateInputWidget: Don't construct moment() objects when not needed
authorBartosz Dziewoński <matma.rex@gmail.com>
Fri, 19 Feb 2016 20:51:44 +0000 (21:51 +0100)
committerJames D. Forrester <jforrester@wikimedia.org>
Fri, 19 Feb 2016 22:56:39 +0000 (14:56 -0800)
Fixed a typo too.

Bug: T126788
Change-Id: I3b2dac752a43945e5525a8ea9349a4b46a7549b2

resources/src/mediawiki.widgets/mw.widgets.DateInputWidget.js

index cba580b..03baeb3 100644 (file)
                this.placeholderLabel = config.placeholderLabel;
 
                // Validate and set min and max dates as properties
-               mustBeAfter = moment( config.mustBeAfter, 'YYYY-MM-DD' );
-               mustBeBefore = moment( config.mustBeBefore, 'YYYY-MM-DD' );
-               if (
-                       config.mustBeAfter !== undefined &&
-                       mustBeAfter.isValid()
-               ) {
-                       this.mustBeAfter = mustBeAfter;
+               if ( config.mustBeAfter !== undefined ) {
+                       mustBeAfter = moment( config.mustBeAfter, 'YYYY-MM-DD' );
+                       if ( mustBeAfter.isValid() ) {
+                               this.mustBeAfter = mustBeAfter;
+                       }
                }
-
-               if (
-                       config.mustBeBefore !== undefined &&
-                       mustBeBefore.isValid()
-               ) {
-                       this.mustBeBefore = mustBeBefore;
+               if ( config.mustBeBefore !== undefined ) {
+                       mustBeBefore = moment( config.mustBeBefore, 'YYYY-MM-DD' );
+                       if ( mustBeBefore.isValid() ) {
+                               this.mustBeBefore = mustBeBefore;
+                       }
                }
 
                // Parent constructor
         * @private
         */
        mw.widgets.DateInputWidget.prototype.updateUI = function () {
+               var moment;
                if ( this.getValue() === '' ) {
                        this.textInput.setValue( '' );
                        this.calendar.setDate( null );
                        this.label.setLabel( this.placeholderLabel );
                        this.$element.addClass( 'mw-widget-dateInputWidget-empty' );
                } else {
+                       moment = this.getMoment();
                        if ( !this.inTextInput ) {
-                               this.textInput.setValue( this.getMoment().format( this.getInputFormat() ) );
+                               this.textInput.setValue( moment.format( this.getInputFormat() ) );
                        }
                        if ( !this.inCalendar ) {
                                this.calendar.setDate( this.getValue() );
                        }
-                       this.label.setLabel( this.getMoment().format( this.getDisplayFormat() ) );
+                       this.label.setLabel( moment.format( this.getDisplayFormat() ) );
                        this.$element.removeClass( 'mw-widget-dateInputWidget-empty' );
                }
        };
        mw.widgets.DateInputWidget.prototype.isValidDate = function ( date ) {
                // "Half-strict mode": for example, for the format 'YYYY-MM-DD', 2015-1-3 instead of 2015-01-03
                // is okay, but 2015-01 isn't, and neither is 2015-01-foo. Use Moment's "fuzzy" mode and check
-               // parsing flags for the details (stoled from implementation of moment#isValid).
+               // parsing flags for the details (stolen from implementation of moment#isValid).
                var
                        mom = moment( date, this.getInputFormat() ),
                        flags = mom.parsingFlags();
         * @return {boolean}
         */
        mw.widgets.DateInputWidget.prototype.isInRange = function ( date ) {
-               var momentDate = moment( date, 'YYYY-MM-DD' ),
-                       isAfter = ( this.mustBeAfter === undefined || momentDate.isAfter( this.mustBeAfter ) ),
-                       isBefore = ( this.mustBeBefore === undefined || momentDate.isBefore( this.mustBeBefore ) );
-
+               var momentDate, isAfter, isBefore;
+               if ( this.mustBeAfter === undefined && this.mustBeBefore === undefined ) {
+                       return true;
+               }
+               momentDate = moment( date, 'YYYY-MM-DD' );
+               isAfter = ( this.mustBeAfter === undefined || momentDate.isAfter( this.mustBeAfter ) );
+               isBefore = ( this.mustBeBefore === undefined || momentDate.isBefore( this.mustBeBefore ) );
                return isAfter && isBefore;
        };