From 308907d60360ff598cf3f3b356c31635584a3006 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 16 Sep 2015 18:44:10 +0200 Subject: [PATCH] mw.widgets.CalendarWidget: Simplify the logic for deciding animations It's all in one place now. Change-Id: Ie8831775c1110bbcbb0ee4b84211c3df1c6b3e72 --- .../mw.widgets.CalendarWidget.js | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js b/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js index 19dfc9a8b8..49acfefe4e 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.CalendarWidget.js @@ -149,8 +149,9 @@ * Update the calendar. * * @private - * @param {string|null} [fade=null] Direction in which to fade out current calendar contents, 'previous', - * 'next' or 'up' + * @param {string|null} [fade=null] Direction in which to fade out current calendar contents, + * 'previous', 'next', 'up' or 'down'; or 'auto', which has the same result as 'previous' or + * 'next' depending on whether the current date is later or earlier than the previous. * @returns {string} Format */ mw.widgets.CalendarWidget.prototype.updateUI = function ( fade ) { @@ -167,6 +168,18 @@ return; } + if ( fade === 'auto' ) { + if ( !this.previousMoment ) { + fade = null; + } else if ( this.previousMoment.isBefore( this.moment, this.precision === 'month' ? 'month' : 'day' ) ) { + fade = 'next'; + } else if ( this.previousMoment.isAfter( this.moment, this.precision === 'month' ? 'month' : 'day' ) ) { + fade = 'previous'; + } else { + fade = null; + } + } + items = []; if ( this.$oldBody ) { this.$oldBody.remove(); @@ -375,7 +388,6 @@ */ mw.widgets.CalendarWidget.prototype.onBodyClick = function ( e ) { var - previousMoment = moment( this.moment ), $target = $( e.target ), layers = this.getDisplayLayers(), currentLayer = layers.indexOf( this.displayLayer ); @@ -390,10 +402,7 @@ } if ( currentLayer === 0 ) { this.setDateFromMoment(); - this.updateUI( - this.precision === 'day' && this.moment.isBefore( previousMoment, 'month' ) ? 'previous' : - this.precision === 'day' && this.moment.isAfter( previousMoment, 'month' ) ? 'next' : null - ); + this.updateUI( 'auto' ); } else { // One layer down this.displayLayer = layers[ currentLayer - 1 ]; @@ -493,40 +502,37 @@ /*jshint +W024*/ nextDirectionKey = dir === 'ltr' ? OO.ui.Keys.RIGHT : OO.ui.Keys.LEFT, prevDirectionKey = dir === 'ltr' ? OO.ui.Keys.LEFT : OO.ui.Keys.RIGHT, - updateInDirection = null; + changed = true; if ( !this.isDisabled() ) { switch ( e.which ) { case prevDirectionKey: this.moment.subtract( 1, this.precision === 'month' ? 'month' : 'day' ); - updateInDirection = 'previous'; break; case nextDirectionKey: this.moment.add( 1, this.precision === 'month' ? 'month' : 'day' ); - updateInDirection = 'next'; break; case OO.ui.Keys.UP: this.moment.subtract( 1, this.precision === 'month' ? 'month' : 'week' ); - updateInDirection = 'previous'; break; case OO.ui.Keys.DOWN: this.moment.add( 1, this.precision === 'month' ? 'month' : 'week' ); - updateInDirection = 'next'; break; case OO.ui.Keys.PAGEUP: this.moment.subtract( 1, this.precision === 'month' ? 'year' : 'month' ); - updateInDirection = 'previous'; break; case OO.ui.Keys.PAGEDOWN: this.moment.add( 1, this.precision === 'month' ? 'year' : 'month' ); - updateInDirection = 'next'; + break; + default: + changed = false; break; } - if ( updateInDirection ) { + if ( changed ) { this.displayLayer = this.getDisplayLayers()[ 0 ]; this.setDateFromMoment(); - this.updateUI( updateInDirection ); + this.updateUI( 'auto' ); return false; } } -- 2.20.1