X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/resources/src/jquery/jquery.badge.js diff --git a/resources/src/jquery/jquery.badge.js b/resources/src/jquery/jquery.badge.js new file mode 100644 index 00000000..40b3baf0 --- /dev/null +++ b/resources/src/jquery/jquery.badge.js @@ -0,0 +1,88 @@ +/*! + * jQuery Badge plugin + * + * @license MIT + * + * @author Ryan Kaldari , 2012 + * @author Andrew Garrett , 2012 + * @author Marius Hoch , 2012 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * This program is distributed WITHOUT ANY WARRANTY. + */ + +/** + * @class jQuery.plugin.badge + */ +( function ( $, mw ) { + /** + * Put a badge on an item on the page. The badge container will be appended to the selected element(s). + * + * $element.badge( text ); + * $element.badge( 5 ); + * $element.badge( '100+' ); + * $element.badge( text, inline ); + * $element.badge( 'New', true ); + * + * @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] True if the badge should be displayed inline, false + * if the badge should overlay the parent element. + * @param {boolean} [displayZero=false] True if the number zero should be displayed, + * false if the number zero should result in the badge being hidden + * @return {jQuery} + * @chainable + */ + $.fn.badge = function ( text, inline, displayZero ) { + var $badge = this.find( '.mw-badge' ), + badgeStyleClass = 'mw-badge-' + ( inline ? 'inline' : 'overlay' ), + isImportant = true, + displayBadge = true; + + // 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 + .toggleClass( 'mw-badge-important', isImportant ) + .find( '.mw-badge-content' ).text( text ); + } else { + // Otherwise, create a new badge with the specified text and style + $badge = $( '
' ) + .addClass( badgeStyleClass ) + .toggleClass( 'mw-badge-important', isImportant ) + .append( + $( '' ).text( text ) + ) + .appendTo( this ); + } + } else { + $badge.remove(); + } + return this; + }; + + /** + * @class jQuery + * @mixins jQuery.plugin.badge + */ +}( jQuery, mediaWiki ) );