Merge "Add and update some missing release notes for rl modules."
[lhc/web/wiklou.git] / resources / jquery / jquery.badge.js
1 /**
2 * jQuery Badge plugin
3 *
4 * Based on Badger plugin by Daniel Raftery (http://thrivingkings.com/badger).
5 *
6 * @license MIT
7 */
8
9 /**
10 * @author Ryan Kaldari <rkaldari@wikimedia.org>, 2012
11 * @author Andrew Garrett <agarrett@wikimedia.org>, 2012
12 * @author Marius Hoch <hoo@online.de>, 2012
13 *
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:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * This program is distributed WITHOUT ANY WARRANTY.
25 */
26 ( function ( $ ) {
27
28 /**
29 * Allows you to put a numeric "badge" on an item on the page.
30 * See mediawiki.org/wiki/ResourceLoader/Default_modules#jQuery.badge
31 *
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
39 */
40 $.fn.badge = function ( badgeCount, options ) {
41 var $badge,
42 oldBadgeCount,
43 newBadgeCount,
44 $existingBadge = this.find( '.mw-badge' );
45
46 options = $.extend( { type : 'overlay' }, options );
47
48 // If there is no existing badge, this will give an empty string
49 oldBadgeCount = Number( $existingBadge.text() );
50 if ( isNaN( oldBadgeCount ) ) {
51 oldBadgeCount = 0;
52 }
53
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 );
67 } else {
68 newBadgeCount = 0;
69 }
70 // Other types are not supported, fall back to 0.
71 } else {
72 newBadgeCount = 0;
73 }
74
75 // Badge count must be a whole number
76 newBadgeCount = Math.round( newBadgeCount );
77
78 if ( newBadgeCount <= 0 ) {
79 // Badges should only exist for values > 0.
80 $existingBadge.remove();
81 } else {
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 );
87 } else {
88 // Contruct a new badge with the count
89 $badge = $( '<div>' )
90 .addClass( 'mw-badge' )
91 .append(
92 $( '<span>' )
93 .addClass( 'mw-badge-content' )
94 .text( newBadgeCount )
95 );
96 this.append( $badge );
97 }
98
99 if ( options.type === 'inline' ) {
100 $badge
101 .removeClass( 'mw-badge-overlay' )
102 .addClass( 'mw-badge-inline' );
103 // Default: overlay
104 } else {
105 $badge
106 .removeClass( 'mw-badge-inline' )
107 .addClass( 'mw-badge-overlay' );
108
109 }
110
111 // If a callback was specified, call it with the badge count
112 if ( $.isFunction( options.callback ) ) {
113 options.callback( newBadgeCount );
114 }
115 }
116 };
117 }( jQuery ) );