mw.widgets.CalendarWidget: Simplify the logic for deciding animations
authorBartosz Dziewoński <matma.rex@gmail.com>
Wed, 16 Sep 2015 16:44:10 +0000 (18:44 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 16 Sep 2015 17:49:01 +0000 (19:49 +0200)
It's all in one place now.

Change-Id: Ie8831775c1110bbcbb0ee4b84211c3df1c6b3e72

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

index 19dfc9a..49acfef 100644 (file)
         * 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 ) {
                        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();
         */
        mw.widgets.CalendarWidget.prototype.onBodyClick = function ( e ) {
                var
-                       previousMoment = moment( this.moment ),
                        $target = $( e.target ),
                        layers = this.getDisplayLayers(),
                        currentLayer = layers.indexOf( this.displayLayer );
                }
                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 ];
                        /*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;
                        }
                }