Merge "Add url parameter to trigger autogenerated gallery type."
[lhc/web/wiklou.git] / resources / jquery / jquery.badge.js
index 8a1f298..9404e81 100644 (file)
  *
  * This program is distributed WITHOUT ANY WARRANTY.
  */
-( function ( $ ) {
+( function ( $, mw ) {
        /**
         * Allows you to put a "badge" on an item on the page. The badge container
         * will be appended to the selected element(s).
         * See mediawiki.org/wiki/ResourceLoader/Default_modules#jQuery.badge
         *
-        * @param text The value to display in the badge. If the value is falsey (0,
-        *   null, false, '', etc.), any existing badge will be removed.
-        * @param boolean inline True if the badge should be displayed inline, false
-        *   if the badge should overlay the parent element (default is inline)
-        * @param boolean displayZero True if the number zero should be displayed,
-        *   false if the number zero should result in the badge being hidden
-        *   (default is zero will result in the badge being hidden)
+        * @param {number|string} text The value to display in the badge. If the value is falsey (0,
+        *  null, false, '', etc.), any existing badge will be removed.
+        * @param {boolean} inline True if the badge should be displayed inline, false
+        *  if the badge should overlay the parent element (default is inline)
+        * @param {boolean} displayZero True if the number zero should be displayed,
+        *  false if the number zero should result in the badge being hidden
+        *  (default is zero will result in the badge being hidden)
         */
        $.fn.badge = function ( text, inline, displayZero ) {
                var $badge = this.find( '.mw-badge' ),
                        badgeStyleClass = 'mw-badge-' + ( inline ? 'inline' : 'overlay' ),
-                       badgeColorClass = 'mw-badge-red'; // default color is red
+                       isImportant = true, displayBadge = true;
 
-               // If we're displaying zero, change the color to grey
-               if ( displayZero && text === 0 ) {
-                       badgeColorClass = 'mw-badge-grey';
-                       // Change zero to string so that it will be displayed
-                       text = '0';
+               // If we're displaying zero, ensure style to be non-important
+               if ( mw.language.convertNumber( text, true ) === 0 ) {
+                       isImportant = false;
+                       if ( !displayZero ) {
+                               displayBadge = false;
+                       }
+               // If text is falsey (besides 0), hide the badge
+               } else if ( !text ) {
+                       displayBadge = false;
                }
-               if ( text ) {
+
+               if ( displayBadge ) {
                        // If a badge already exists, reuse it
                        if ( $badge.length ) {
-                               $badge.find( '.mw-badge-content' ).text( text );
+                               $badge
+                                       .toggleClass( 'mw-badge-important', isImportant )
+                                       .find( '.mw-badge-content' )
+                                               .text( text );
                        } else {
                                // Otherwise, create a new badge with the specified text and style
                                $badge = $( '<div class="mw-badge"></div>' )
                                        .addClass( badgeStyleClass )
-                                       .addClass( badgeColorClass )
-                                       .append( $( '<span class="mw-badge-content"></span>' ).text ( text ) )
+                                       .toggleClass( 'mw-badge-important', isImportant )
+                                       .append(
+                                               $( '<span class="mw-badge-content"></span>' ).text( text )
+                                       )
                                        .appendTo( this );
                        }
                } else {
@@ -63,4 +73,4 @@
                }
                return this;
        };
-}( jQuery ) );
+}( jQuery, mediaWiki ) );