-// Badger v1.0 by Daniel Raftery
-// http://thrivingkings.com/badger
-// http://twitter.com/ThrivingKings
-// Modified by Ryan Kaldari <rkaldari@wikimedia.org>
+/**
+ * jQuery Badge plugin
+ *
+ * @license MIT
+ */
/**
+ * @author Ryan Kaldari <rkaldari@wikimedia.org>, 2012
+ * @author Andrew Garrett <agarrett@wikimedia.org>, 2012
+ * @author Marius Hoch <hoo@online.de>, 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
*
* This program is distributed WITHOUT ANY WARRANTY.
*/
+( function ( $ ) {
+ /**
+ * 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)
+ */
+ $.fn.badge = function ( text, inline ) {
+ var div, $badge = this.find( '.mw-badge' );
-(function( $ ) {
- $.fn.badge = function( badge, options ) {
- var existingBadge = this.find( '.mw-badge' );
- options = $.extend( {}, options );
-
- badge = String(badge);
- if ( badge.charAt(0) === '+' ) {
- if ( existingBadge.length > 0 ) {
- oldBadge = existingBadge.text();
- badge = Math.round( Number( oldBadge ) + Number( badge.substr(1) ) );
- } else {
- badge = badge.substr(1);
- }
- } else if ( badge.charAt(0) === '-' ) {
- if ( existingBadge.length > 0 ) {
- oldBadge = existingBadge.text();
- badge = Math.round( Number( oldBadge ) - Number( badge.substr(1) ) );
+ if ( text ) {
+ // If a badge already exists, reuse it
+ if ( $badge.length ) {
+ $badge.find( '.mw-badge-content' ).text( text );
} else {
- badge = 0;
+ // Otherwise, create a new badge with the specified text and style
+ $badge = $( '<div class="mw-badge mw-badge-' + ( inline ? 'inline' : 'overlay' ) + '"></div>' )
+ .append( $( '<span class="mw-badge-content"></span>' ).text ( text ) )
+ .appendTo( this );
}
- }
-
- if ( Number(badge) <= 0 ) {
- // Clear any existing badge
- existingBadge.remove();
} else {
- // Don't add duplicates
- var $badge = existingBadge;
- if ( existingBadge.length > 0 ) {
- this.find( '.mw-badge-content' ).text( badge );
- } else {
- $badge = $('<div/>')
- .addClass('mw-badge')
- .addClass('mw-badge-overlay')
- .append(
- $('<span/>')
- .addClass('mw-badge-content')
- .text(badge)
- );
- this.append($badge);
- }
-
- if ( options.type ) {
- if ( options.type == 'inline' ) {
- $badge.removeClass('mw-badge-overlay')
- .addClass('mw-badge-inline');
- } else if ( options.type == 'overlay' ) {
- $badge.removeClass('mw-badge-inline')
- .addClass('mw-badge-overlay');
- }
- }
-
- // If a callback was specified, call it with the badge number
- if ( options.callback ) {
- options.callback( badge );
- }
+ $badge.remove();
}
+ return this;
};
-} ) ( jQuery );
+}( jQuery ) );