diff options
Diffstat (limited to 'resources/jquery/jquery.badge.js')
-rw-r--r-- | resources/jquery/jquery.badge.js | 121 |
1 files changed, 40 insertions, 81 deletions
diff --git a/resources/jquery/jquery.badge.js b/resources/jquery/jquery.badge.js index 04495b71..9404e818 100644 --- a/resources/jquery/jquery.badge.js +++ b/resources/jquery/jquery.badge.js @@ -1,8 +1,6 @@ /** * jQuery Badge plugin * - * Based on Badger plugin by Daniel Raftery (http://thrivingkings.com/badger). - * * @license MIT */ @@ -23,95 +21,56 @@ * * This program is distributed WITHOUT ANY WARRANTY. */ -( function ( $ ) { - +( function ( $, mw ) { /** - * Allows you to put a numeric "badge" on an item on the page. + * 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 {string|number} badgeCount An explicit number, or "+n"/ "-n" - * to modify the existing value. If the new value is equal or lower than 0, - * any existing badge will be removed. The badge container will be appended - * to the selected element(s). - * @param {Object} options Optional parameters specified below - * type: 'inline' or 'overlay' (default) - * callback: will be called with the number now shown on the badge as a parameter + * @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 ( badgeCount, options ) { - var $badge, - oldBadgeCount, - newBadgeCount, - $existingBadge = this.find( '.mw-badge' ); - - options = $.extend( { type : 'overlay' }, options ); - - // If there is no existing badge, this will give an empty string - oldBadgeCount = Number( $existingBadge.text() ); - if ( isNaN( oldBadgeCount ) ) { - oldBadgeCount = 0; - } + $.fn.badge = function ( text, inline, displayZero ) { + var $badge = this.find( '.mw-badge' ), + badgeStyleClass = 'mw-badge-' + ( inline ? 'inline' : 'overlay' ), + isImportant = true, displayBadge = true; - // If badgeCount is a number, use that as the new badge - if ( typeof badgeCount === 'number' ) { - newBadgeCount = badgeCount; - } else if ( typeof badgeCount === 'string' ) { - // If badgeCount is "+x", add x to the old badge - if ( badgeCount.charAt(0) === '+' ) { - newBadgeCount = oldBadgeCount + Number( badgeCount.substr(1) ); - // If badgeCount is "-x", subtract x from the old badge - } else if ( badgeCount.charAt(0) === '-' ) { - newBadgeCount = oldBadgeCount - Number( badgeCount.substr(1) ); - // If badgeCount can be converted into a number, convert it - } else if ( !isNaN( Number( badgeCount ) ) ) { - newBadgeCount = Number( badgeCount ); - } else { - newBadgeCount = 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; } - // Other types are not supported, fall back to 0. - } else { - newBadgeCount = 0; + // If text is falsey (besides 0), hide the badge + } else if ( !text ) { + displayBadge = false; } - // Badge count must be a whole number - newBadgeCount = Math.round( newBadgeCount ); - - if ( newBadgeCount <= 0 ) { - // Badges should only exist for values > 0. - $existingBadge.remove(); - } else { - // Don't add duplicates - if ( $existingBadge.length ) { - $badge = $existingBadge; - // Insert the new count into the badge - this.find( '.mw-badge-content' ).text( newBadgeCount ); - } else { - // Contruct a new badge with the count - $badge = $( '<div>' ) - .addClass( 'mw-badge' ) - .append( - $( '<span>' ) - .addClass( 'mw-badge-content' ) - .text( newBadgeCount ) - ); - this.append( $badge ); - } - - if ( options.type === 'inline' ) { + if ( displayBadge ) { + // If a badge already exists, reuse it + if ( $badge.length ) { $badge - .removeClass( 'mw-badge-overlay' ) - .addClass( 'mw-badge-inline' ); - // Default: overlay + .toggleClass( 'mw-badge-important', isImportant ) + .find( '.mw-badge-content' ) + .text( text ); } else { - $badge - .removeClass( 'mw-badge-inline' ) - .addClass( 'mw-badge-overlay' ); - - } - - // If a callback was specified, call it with the badge count - if ( $.isFunction( options.callback ) ) { - options.callback( newBadgeCount ); + // Otherwise, create a new badge with the specified text and style + $badge = $( '<div class="mw-badge"></div>' ) + .addClass( badgeStyleClass ) + .toggleClass( 'mw-badge-important', isImportant ) + .append( + $( '<span class="mw-badge-content"></span>' ).text( text ) + ) + .appendTo( this ); } + } else { + $badge.remove(); } + return this; }; -}( jQuery ) ); +}( jQuery, mediaWiki ) ); |