mw.widgets.DateInputWidget: Don't get stuck on today's date if none given
authorBartosz Dziewoński <matma.rex@gmail.com>
Tue, 4 Aug 2015 16:29:51 +0000 (18:29 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Tue, 4 Aug 2015 16:33:58 +0000 (18:33 +0200)
If no date was given, the calendar would select today, but the
DateInputWidget itself would have no value.

However, this made the date picker ignore attempts to actually choose
today: the calendar already had that date chosen, so no 'change' even
was fired, and date input stayed empty.

Change mw.widgets.CalendarWidget not to actually select today's date,
just focus the calendar on it.

Follow-up to 66686f8c5e4df1c2cc94273918b9328269d2ec09.

Change-Id: I3ba064982b045a577209d56a009ad0098c357754

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

index 9016e89..7eaa62e 100644 (file)
@@ -17,8 +17,9 @@
         * @constructor
         * @param {Object} [config] Configuration options
         * @cfg {string} [precision='day'] Date precision to use, 'day' or 'month'
-        * @cfg {string|null} [date=null] Day or month date (depending on `precision`), in the
-        *     format 'YYYY-MM-DD' or 'YYYY-MM'. When null, defaults to current date.
+        * @cfg {string|null} [date=null] Day or month date (depending on `precision`), in the format
+        *     'YYYY-MM-DD' or 'YYYY-MM'. When null, the calendar will show today's date, but not select
+        *     it.
         */
        mw.widgets.CalendarWidget = function MWWCalendarWidget( config ) {
                // Config initialization
 
                if (
                        this.displayLayer === this.previousDisplayLayer &&
+                       this.date === this.previousDate &&
                        this.previousMoment &&
                        this.previousMoment.isSame( this.moment, this.precision === 'month' ? 'month' : 'day' )
                ) {
 
                this.previousMoment = moment( this.moment );
                this.previousDisplayLayer = this.displayLayer;
+               this.previousDate = this.date;
 
                this.$body.on( 'click', this.onBodyClick.bind( this ) );
        };
         * Set the date.
         *
         * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
-        *     When null, defaults to current date. When invalid, the date is not changed.
+        *     When null, the calendar will show today's date, but not select it. When invalid, the date
+        *     is not changed.
         */
        mw.widgets.CalendarWidget.prototype.setDate = function ( date ) {
                var mom = date !== null ? moment( date, this.getDateFormat() ) : moment();
                if ( mom.isValid() ) {
                        this.moment = mom;
-                       this.setDateFromMoment();
+                       if ( date !== null ) {
+                               this.setDateFromMoment();
+                       } else if ( this.date !== null ) {
+                               this.date = null;
+                               this.emit( 'change', this.date );
+                       }
                        this.displayLayer = this.getDisplayLayers()[ 0 ];
                        this.updateUI();
                }
         * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
         * not be localised.
         *
-        * @returns {string} Date string
+        * @returns {string|null} Date string
         */
        mw.widgets.CalendarWidget.prototype.getDate = function () {
                return this.date;