[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/resources/src/jquery/ -> jquery.badge.js (source)

   1  /*!
   2   * jQuery Badge plugin
   3   *
   4   * @license MIT
   5   *
   6   * @author Ryan Kaldari <[email protected]>, 2012
   7   * @author Andrew Garrett <[email protected]>, 2012
   8   * @author Marius Hoch <[email protected]>, 2012
   9   *
  10   * Permission is hereby granted, free of charge, to any person obtaining a copy
  11   * of this software and associated documentation files (the "Software"), to deal
  12   * in the Software without restriction, including without limitation the rights
  13   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14   * copies of the Software, and to permit persons to whom the Software is
  15   * furnished to do so, subject to the following conditions:
  16   *
  17   * The above copyright notice and this permission notice shall be included in
  18   * all copies or substantial portions of the Software.
  19   *
  20   * This program is distributed WITHOUT ANY WARRANTY.
  21   */
  22  
  23  /**
  24   * @class jQuery.plugin.badge
  25   */
  26  ( function ( $, mw ) {
  27      /**
  28       * Put a badge on an item on the page. The badge container will be appended to the selected element(s).
  29       *
  30       *     $element.badge( text );
  31       *     $element.badge( 5 );
  32       *     $element.badge( '100+' );
  33       *     $element.badge( text, inline );
  34       *     $element.badge( 'New', true );
  35       *
  36       * @param {number|string} text The value to display in the badge. If the value is falsey (0,
  37       *  null, false, '', etc.), any existing badge will be removed.
  38       * @param {boolean} [inline=true] True if the badge should be displayed inline, false
  39       *  if the badge should overlay the parent element.
  40       * @param {boolean} [displayZero=false] True if the number zero should be displayed,
  41       *  false if the number zero should result in the badge being hidden
  42       * @return {jQuery}
  43       * @chainable
  44       */
  45      $.fn.badge = function ( text, inline, displayZero ) {
  46          var $badge = this.find( '.mw-badge' ),
  47              badgeStyleClass = 'mw-badge-' + ( inline ? 'inline' : 'overlay' ),
  48              isImportant = true, displayBadge = true;
  49  
  50          // If we're displaying zero, ensure style to be non-important
  51          if ( mw.language.convertNumber( text, true ) === 0 ) {
  52              isImportant = false;
  53              if ( !displayZero ) {
  54                  displayBadge = false;
  55              }
  56          // If text is falsey (besides 0), hide the badge
  57          } else if ( !text ) {
  58              displayBadge = false;
  59          }
  60  
  61          if ( displayBadge ) {
  62              // If a badge already exists, reuse it
  63              if ( $badge.length ) {
  64                  $badge
  65                      .toggleClass( 'mw-badge-important', isImportant )
  66                      .find( '.mw-badge-content' )
  67                          .text( text );
  68              } else {
  69                  // Otherwise, create a new badge with the specified text and style
  70                  $badge = $( '<div class="mw-badge"></div>' )
  71                      .addClass( badgeStyleClass )
  72                      .toggleClass( 'mw-badge-important', isImportant )
  73                      .append(
  74                          $( '<span class="mw-badge-content"></span>' ).text( text )
  75                      )
  76                      .appendTo( this );
  77              }
  78          } else {
  79              $badge.remove();
  80          }
  81          return this;
  82      };
  83  
  84      /**
  85       * @class jQuery
  86       * @mixins jQuery.plugin.badge
  87       */
  88  }( jQuery, mediaWiki ) );


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1