From 6a7e6b10af2055ed40310d87d5a1e40dba66687c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Tue, 4 Aug 2015 18:29:51 +0200 Subject: [PATCH] mw.widgets.DateInputWidget: Don't get stuck on today's date if none given 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 --- .../mw.widgets.CalendarWidget.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js b/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js index 9016e895b9..7eaa62e6ce 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js @@ -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 @@ -156,6 +157,7 @@ if ( this.displayLayer === this.previousDisplayLayer && + this.date === this.previousDate && this.previousMoment && this.previousMoment.isSame( this.moment, this.precision === 'month' ? 'month' : 'day' ) ) { @@ -299,6 +301,7 @@ this.previousMoment = moment( this.moment ); this.previousDisplayLayer = this.displayLayer; + this.previousDate = this.date; this.$body.on( 'click', this.onBodyClick.bind( this ) ); }; @@ -396,13 +399,19 @@ * 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(); } @@ -436,7 +445,7 @@ * 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; -- 2.20.1