X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fjquery%2Fjquery.badge.js;h=9404e8183389d9ef4cf4010f23a2b44439e0948f;hb=10d2be2b3ad4a4a21e991dac68a5cb7007dfd7ba;hp=982974761d1906bce3690cf99381dc1a4d1c8784;hpb=79daaea1a8cd204107379ca3114dae004b08f204;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/jquery/jquery.badge.js b/resources/jquery/jquery.badge.js index 982974761d..9404e81833 100644 --- a/resources/jquery/jquery.badge.js +++ b/resources/jquery/jquery.badge.js @@ -21,34 +21,56 @@ * * 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 {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 ) { - var div, $badge = this.find( '.mw-badge' ); + $.fn.badge = function ( text, inline, displayZero ) { + var $badge = this.find( '.mw-badge' ), + badgeStyleClass = 'mw-badge-' + ( inline ? 'inline' : 'overlay' ), + isImportant = true, displayBadge = true; - if ( text ) { + // 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 ( 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 - div = document.createElement( 'div' ); - div.className = 'mw-badge mw-badge-' + ( inline ? 'inline' : 'overlay' ); - div.innerHTML = '' + text + ''; - $( div ).appendTo( this ); + $badge = $( '
' ) + .addClass( badgeStyleClass ) + .toggleClass( 'mw-badge-important', isImportant ) + .append( + $( '' ).text( text ) + ) + .appendTo( this ); } } else { $badge.remove(); } return this; }; -}( jQuery ) ); +}( jQuery, mediaWiki ) );