Difference between revisions of "MediaWiki:Common.js"

From Dragalia Lost Wiki
Jump to navigation Jump to search
>Maintenance script-gpuser
(Automated Import)
>SandovalCurse
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
+
/* Any JavaScript here will be loaded for users using the 2 column main page layout */
 
/*****************************************
 
/*****************************************
/* Front Page 3-column height equalization *
+
/* Front Page column height equalization *
 
/*****************************************/
 
/*****************************************/
 
// Author:  Shawn Bruckner
 
// Author:  Shawn Bruckner
// Date:    2013-Sept-21
+
// Date:    2015-Feb-12
 
// License: CC-BY 3.0
 
// License: CC-BY 3.0
 
// Version: beta
 
// Version: beta
Line 10: Line 10:
 
var fp = fp || {
 
var fp = fp || {
 
   equalizeColumns : function() {
 
   equalizeColumns : function() {
     $( '.fpmain' ).each( function () {
+
     $( '.fpcontent' ).each( function () {
       fp.resetSectionBoxHeights( $( this ).find( '.fpsection1, .fpsection2, .fpsection3, .fpsection4' ) );
+
       fp.resetSectionBoxHeights( $( this ).find( '#fptopsection, #fpflexsection, #fpbottomsection' ) );
 
     } );
 
     } );
     if ( $( window ).width() > 789 && $( window ).width() < 1390 ) {
+
    var excludeSel = '';
       $( '.fpmain' ).each( function (index) {
+
     if ( $( window ).width() > 1539 ) {
        var leftHeight = $( this ).find( '.fpsection1' ).height() + $( this ).find( '.fpsection4' ).height();
+
       excludeSel = '.fpmaybercol'; // at this width, it's necessary to hit those boxes in a separate pass after .fpcontent
        var rightHeight = $( this ).find( '.fpsection2' ).height() + $( this ).find( '.fpsection3' ).height();
+
    }
        var difference = Math.abs( rightHeight - leftHeight );
+
    if ( $( window ).width() > 889 ) {
       
+
      fp.equalizeColumnsOfBlock( '.fpcontent',
        if ( leftHeight < rightHeight ) {
+
                                '#fptopsection, #fpbottomsection',
          fp.adjustSectionBoxHeights( difference, $( this ).find( '.fpsection1, .fpsection4' ) );
+
                                '#fpbottomsection',
        } else if ( rightHeight < leftHeight ) {
+
                                '#fpflexsection',
          fp.adjustSectionBoxHeights( difference, $( this ).find( '.fpsection2, .fpsection3' ) );
+
                                '#fpflexsection',
        }
+
                                excludeSel
      } );
+
                              );
     } else if ( $( window ).width() > 1389 ) {
+
     }
       $( '.fpmain' ).each( function (index) {
+
    if ( $( window ).width() > 1539 ) {
        var leftHeight = $( this ).find( '.fpsection1' ).height() + $( this ).find( '.fpsection4' ).height();
+
       fp.equalizeColumnsOfBlock( '.fpmaybecols',
        var middleHeight = $( this ).find( '.fpsection2' ).height();
+
                                '.fpmaybelcol',
        var rightHeight = $( this ).find( '.fpsection3' ).height();
+
                                '.fpmaybelcol',
        var maxHeight = Math.max( leftHeight, middleHeight, rightHeight );
+
                                '.fpmaybercol',
       
+
                                '.fpmaybercol',
        if ( leftHeight < maxHeight ) {
+
                                ''
          fp.adjustSectionBoxHeights( maxHeight - leftHeight, $( this ).find( '.fpsection1, .fpsection4' ) );
+
                              );
        }
 
        if ( middleHeight < maxHeight ) {
 
          fp.adjustSectionBoxHeights( maxHeight - middleHeight, $( this ).find( '.fpsection2' ) );
 
        }
 
        if ( rightHeight < maxHeight ) {
 
          fp.adjustSectionBoxHeights( maxHeight - rightHeight, $( this ).find( '.fpsection3' ) );
 
        }
 
      } );
 
 
     }
 
     }
 
   },
 
   },
  
   findAdjustableSectionBoxes : function ( sections ) {
+
   equalizeColumnsOfBlock : function( blockSel, leftSel, leftBottomSel, rightSel, rightBottomSel, excludeSel ) {
     var boxes = sections.find( '.fpbox.fpgreedy' );
+
     $( blockSel ).each( function ( index ) {
 +
      var tryCount = 0;
 +
      do {
 +
        var leftBottom = $( this ).find( leftBottomSel ).offset().top + $( this ).find( leftBottomSel ).height();
 +
        var rightBottom = $( this ).find( rightBottomSel ).offset().top + $( this ).find( rightBottomSel ).height();
  
    if ( boxes.length === 0 ) {
+
        var difference = Math.round( Math.abs( rightBottom - leftBottom ) );
      return sections.find( '.fpbox' ).not( '.fpnoresize' );
+
       
    } else {
+
        if ( leftBottom < rightBottom ) {
       return boxes;
+
          fp.adjustSectionBoxHeights( difference, $( this ).find( leftSel ).not( excludeSel ) );
     }
+
        } else if ( rightBottom < leftBottom ) {
 +
          fp.adjustSectionBoxHeights( difference, $( this ).find( rightSel ).not( excludeSel ) );
 +
        }
 +
        ++tryCount;
 +
       } while ( Math.round( leftBottom ) != Math.round( rightBottom ) && tryCount < 4 );
 +
     } );
 
   },
 
   },
  
 
   resetSectionBoxHeights : function ( sections ) {
 
   resetSectionBoxHeights : function ( sections ) {
     fp.findAdjustableSectionBoxes( sections ).each( function () {
+
     sections.each( function () {
      $( this ).height( 'auto' );
+
      $( this ).find( '.fpbox' ).each( function () {
 +
        $( this ).height( 'auto' );
 +
      } );
 
     } );
 
     } );
 
   },
 
   },
  
 
   adjustSectionBoxHeights : function ( heightToAdd, sections ) {
 
   adjustSectionBoxHeights : function ( heightToAdd, sections ) {
     var boxes, lastBox, remainingHeightToAdd, boxHeightToAdd;
+
     var boxCount = 0;
     boxes = fp.findAdjustableSectionBoxes( sections );
+
     sections.each( function() {
    lastBox = boxes.last();
+
      boxCount += $( this ).find( '.fpbox' ).length;
     remainingHeightToAdd = heightToAdd;
+
     } );
    boxHeightToAdd = Math.floor( heightToAdd / boxes.length );
 
  
     boxes.each( function() {
+
     var avgHeightToAdd = heightToAdd / boxCount;
       if ( this === lastBox.get( 0 ) ) {
+
    var decimalPortion = 0.0;
        $( this ).height( $( this ).height() + remainingHeightToAdd );
+
    var boxes, heightToAdd;
      } else {
+
    sections.each( function() {
         $( this ).height( $( this ).height() + boxHeightToAdd );
+
       boxes = $( this ).find( '.fpbox' );
        remainingHeightToAdd -= boxHeightToAdd;
+
 
       }
+
      boxes.each( function() {
 +
        heightToAdd = Math.round( decimalPortion + avgHeightToAdd ); /* should iron out rounding error */
 +
        decimalPortion += avgHeightToAdd - heightToAdd;
 +
         $( this ).height( $( this ).height() + heightToAdd );
 +
       } );
 
     } );
 
     } );
 
   }
 
   }
Line 84: Line 90:
 
/* End Front Page column height equalization *
 
/* End Front Page column height equalization *
 
/*********************************************/
 
/*********************************************/
 +
 +
 +
/*************************
 +
* JQuery Random  Plugin *
 +
*************************/
 +
 +
/**
 +
* Adds a 'random' filter to jQuery, which selects 1 or more elements at random for the current jQuery set.
 +
* Defaults to 1 element if a amount isn't given.
 +
*/
 +
 +
jQuery.fn.random = function(count) {
 +
    var count = (typeof count !== 'undefined') ?  count : 1;
 +
    // Return the current set if an invalid count is asked for.
 +
    if ( count < 1 || count >= this.length || ! Number.isInteger(Number(count)) ) {
 +
        return jQuery(this);
 +
    }
 +
 +
    var indexes = [];
 +
    var resultset = [];
 +
    while ( indexes.length < count ) {
 +
        // Generate a random index
 +
        var index = Math.floor(Math.random() * this.length);
 +
       
 +
        // reroll the random index if it's already present
 +
        var reroll = false;
 +
        for (var i = 0; i < indexes.length; i++) {
 +
            if ( indexes[i] == index ) {
 +
                reroll = true;
 +
            }
 +
        }
 +
        if (reroll) {
 +
            continue;
 +
        }
 +
 +
        // Add the index/element to the result set
 +
        indexes.push(index);
 +
        resultset.push(this[index]);
 +
    }
 +
    return jQuery(resultset);
 +
}
 +
 +
/*****************
 +
* Random subset *
 +
*****************/
 +
 +
/**
 +
* A random subset of list elements within elements with the 'random-subset' class are show, while the rest are hidden.
 +
* The 'data-random-subset-count' attribute can be used to specify the number of elements to be displayed.
 +
*/
 +
 +
$('.random-subset').each(function() {
 +
    var count = 1;
 +
    // If the data-random-subset-count attribute is present use that count
 +
    if ($(this).attr('data-random-subset-count')) {
 +
        count = $(this).attr('data-random-subset-count');
 +
    }
 +
    var entries = $(this).find('li');
 +
    $(entries).random(entries.length - count).remove();
 +
    // show the root element in case it was hidden while waiting for JS.
 +
    $(this).show();
 +
});

Revision as of 11:09, 4 May 2018

/* Any JavaScript here will be loaded for users using the 2 column main page layout */
/*****************************************
/* Front Page column height equalization *
/*****************************************/
// Author:  Shawn Bruckner
// Date:    2015-Feb-12
// License: CC-BY 3.0
// Version: beta

var fp = fp || {
  equalizeColumns : function() {
    $( '.fpcontent' ).each( function () {
      fp.resetSectionBoxHeights( $( this ).find( '#fptopsection, #fpflexsection, #fpbottomsection' ) );
    } );
    var excludeSel = '';
    if ( $( window ).width() > 1539 ) {
      excludeSel = '.fpmaybercol'; // at this width, it's necessary to hit those boxes in a separate pass after .fpcontent
    }
    if ( $( window ).width() > 889 ) {
      fp.equalizeColumnsOfBlock( '.fpcontent',
                                 '#fptopsection, #fpbottomsection',
                                 '#fpbottomsection',
                                 '#fpflexsection',
                                 '#fpflexsection',
                                 excludeSel
                               );
    }
    if ( $( window ).width() > 1539 ) {
      fp.equalizeColumnsOfBlock( '.fpmaybecols',
                                 '.fpmaybelcol',
                                 '.fpmaybelcol',
                                 '.fpmaybercol',
                                 '.fpmaybercol',
                                 ''
                               );
    }
  },

  equalizeColumnsOfBlock : function( blockSel, leftSel, leftBottomSel, rightSel, rightBottomSel, excludeSel ) {
    $( blockSel ).each( function ( index ) {
      var tryCount = 0;
      do {
        var leftBottom = $( this ).find( leftBottomSel ).offset().top + $( this ).find( leftBottomSel ).height();
        var rightBottom = $( this ).find( rightBottomSel ).offset().top + $( this ).find( rightBottomSel ).height();

        var difference = Math.round( Math.abs( rightBottom - leftBottom ) );
        
        if ( leftBottom < rightBottom ) {
          fp.adjustSectionBoxHeights( difference, $( this ).find( leftSel ).not( excludeSel ) );
        } else if ( rightBottom < leftBottom ) {
          fp.adjustSectionBoxHeights( difference, $( this ).find( rightSel ).not( excludeSel ) );
        }
        ++tryCount;
      } while ( Math.round( leftBottom ) != Math.round( rightBottom ) && tryCount < 4 );
    } );
  },

  resetSectionBoxHeights : function ( sections ) {
    sections.each( function () {
      $( this ).find( '.fpbox' ).each( function () {
        $( this ).height( 'auto' );
      } );
    } );
  },

  adjustSectionBoxHeights : function ( heightToAdd, sections ) {
    var boxCount = 0;
    sections.each( function() {
      boxCount += $( this ).find( '.fpbox' ).length;
    } );

    var avgHeightToAdd = heightToAdd / boxCount;
    var decimalPortion = 0.0;
    var boxes, heightToAdd;
    sections.each( function() {
      boxes = $( this ).find( '.fpbox' );

      boxes.each( function() {
        heightToAdd = Math.round( decimalPortion + avgHeightToAdd ); /* should iron out rounding error */
        decimalPortion += avgHeightToAdd - heightToAdd;
        $( this ).height( $( this ).height() + heightToAdd );
      } );
    } );
  }
};

$( document ).ready( fp.equalizeColumns );
$( window ).resize( fp.equalizeColumns );
/*********************************************
/* End Front Page column height equalization *
/*********************************************/


/*************************
 * JQuery Random  Plugin *
 *************************/

/**
 * Adds a 'random' filter to jQuery, which selects 1 or more elements at random for the current jQuery set.
 * Defaults to 1 element if a amount isn't given.
 */

jQuery.fn.random = function(count) {
    var count = (typeof count !== 'undefined') ?  count : 1;
    // Return the current set if an invalid count is asked for.
    if ( count < 1 || count >= this.length || ! Number.isInteger(Number(count)) ) {
        return jQuery(this);
    }

    var indexes = [];
    var resultset = [];
    while ( indexes.length < count ) {
        // Generate a random index
        var index = Math.floor(Math.random() * this.length);
        
        // reroll the random index if it's already present
        var reroll = false;
        for (var i = 0; i < indexes.length; i++) {
            if ( indexes[i] == index ) {
                reroll = true;
            }
        }
        if (reroll) {
            continue;
        }

        // Add the index/element to the result set
        indexes.push(index);
        resultset.push(this[index]);
    }
    return jQuery(resultset);
}

/*****************
 * Random subset *
 *****************/

/**
 * A random subset of list elements within elements with the 'random-subset' class are show, while the rest are hidden.
 * The 'data-random-subset-count' attribute can be used to specify the number of elements to be displayed.
 */

$('.random-subset').each(function() {
    var count = 1;
    // If the data-random-subset-count attribute is present use that count
    if ($(this).attr('data-random-subset-count')) {
        count = $(this).attr('data-random-subset-count');
    }
    var entries = $(this).find('li');
    $(entries).random(entries.length - count).remove();
    // show the root element in case it was hidden while waiting for JS.
    $(this).show();
});