4 * Based on Badger plugin by Daniel Raftery (http://thrivingkings.com/badger).
10 * @author Ryan Kaldari <rkaldari@wikimedia.org>, 2012
11 * @author Andrew Garrett <agarrett@wikimedia.org>, 2012
12 * @author Marius Hoch <hoo@online.de>, 2012
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * This program is distributed WITHOUT ANY WARRANTY.
29 * Allows you to put a numeric "badge" on an item on the page.
30 * See mediawiki.org/wiki/ResourceLoader/Default_modules#jQuery.badge
32 * @param {string|number} badgeCount An explicit number, or "+n"/ "-n"
33 * to modify the existing value. If the new value is equal or lower than 0,
34 * any existing badge will be removed. The badge container will be appended
35 * to the selected element(s).
36 * @param {Object} options Optional parameters specified below
37 * type: 'inline' or 'overlay' (default)
38 * callback: will be called with the number now shown on the badge as a parameter
40 $.fn
.badge = function ( badgeCount
, options
) {
44 $existingBadge
= this.find( '.mw-badge' );
46 options
= $.extend( { type
: 'overlay' }, options
);
48 // If there is no existing badge, this will give an empty string
49 oldBadgeCount
= Number( $existingBadge
.text() );
50 if ( isNaN( oldBadgeCount
) ) {
54 // If badgeCount is a number, use that as the new badge
55 if ( typeof badgeCount
=== 'number' ) {
56 newBadgeCount
= badgeCount
;
57 } else if ( typeof badgeCount
=== 'string' ) {
58 // If badgeCount is "+x", add x to the old badge
59 if ( badgeCount
.charAt(0) === '+' ) {
60 newBadgeCount
= oldBadgeCount
+ Number( badgeCount
.substr(1) );
61 // If badgeCount is "-x", subtract x from the old badge
62 } else if ( badgeCount
.charAt(0) === '-' ) {
63 newBadgeCount
= oldBadgeCount
- Number( badgeCount
.substr(1) );
64 // If badgeCount can be converted into a number, convert it
65 } else if ( !isNaN( Number( badgeCount
) ) ) {
66 newBadgeCount
= Number( badgeCount
);
70 // Other types are not supported, fall back to 0.
75 // Badge count must be a whole number
76 newBadgeCount
= Math
.round( newBadgeCount
);
78 if ( newBadgeCount
<= 0 ) {
79 // Badges should only exist for values > 0.
80 $existingBadge
.remove();
82 // Don't add duplicates
83 if ( $existingBadge
.length
) {
84 $badge
= $existingBadge
;
85 // Insert the new count into the badge
86 this.find( '.mw-badge-content' ).text( newBadgeCount
);
88 // Contruct a new badge with the count
90 .addClass( 'mw-badge' )
93 .addClass( 'mw-badge-content' )
94 .text( newBadgeCount
)
96 this.append( $badge
);
99 if ( options
.type
=== 'inline' ) {
101 .removeClass( 'mw-badge-overlay' )
102 .addClass( 'mw-badge-inline' );
106 .removeClass( 'mw-badge-inline' )
107 .addClass( 'mw-badge-overlay' );
111 // If a callback was specified, call it with the badge count
112 if ( $.isFunction( options
.callback
) ) {
113 options
.callback( newBadgeCount
);