6 * @author Ryan Kaldari <rkaldari@wikimedia.org>, 2012
7 * @author Andrew Garrett <agarrett@wikimedia.org>, 2012
8 * @author Marius Hoch <hoo@online.de>, 2012
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * This program is distributed WITHOUT ANY WARRANTY.
24 * @class jQuery.plugin.badge
26 ( function ( $, mw ) {
28 * Put a badge on an item on the page. The badge container will be appended to the selected element(s).
30 * $element.badge( text );
31 * $element.badge( 5 );
32 * $element.badge( '100+' );
33 * $element.badge( text, inline );
34 * $element.badge( 'New', true );
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
45 $.fn.badge = function ( text, inline, displayZero ) {
46 var $badge = this.find( '.mw-badge' ),
47 badgeStyleClass = 'mw-badge-' + ( inline ? 'inline' : 'overlay' ),
51 // If we're displaying zero, ensure style to be non-important
52 if ( mw.language.convertNumber( text, true ) === 0 ) {
57 // If text is falsey (besides 0), hide the badge
63 // If a badge already exists, reuse it
64 if ( $badge.length ) {
66 .toggleClass( 'mw-badge-important', isImportant )
67 .find( '.mw-badge-content' )
70 // Otherwise, create a new badge with the specified text and style
71 $badge = $( '<div class="mw-badge"></div>' )
72 .addClass( badgeStyleClass )
73 .toggleClass( 'mw-badge-important', isImportant )
75 $( '<span class="mw-badge-content"></span>' ).text( text )
87 * @mixins jQuery.plugin.badge
89 }( jQuery, mediaWiki ) );