summaryrefslogtreecommitdiff
path: root/resources/jquery/jquery.badge.js
blob: 04495b71672c877c739d754b48f666af314ced69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * jQuery Badge plugin
 *
 * Based on Badger plugin by Daniel Raftery (http://thrivingkings.com/badger).
 *
 * @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
 * 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.
 */
( function ( $ ) {

	/**
	 * Allows you to put a numeric "badge" on an item on the page.
	 * See mediawiki.org/wiki/ResourceLoader/Default_modules#jQuery.badge
	 *
	 * @param {string|number} badgeCount An explicit number, or "+n"/ "-n"
	 *  to modify the existing value. If the new value is equal or lower than 0,
	 *  any existing badge will be removed. The badge container will be appended
	 *  to the selected element(s).
	 * @param {Object} options Optional parameters specified below
	 *   type: 'inline' or 'overlay' (default)
	 *   callback: will be called with the number now shown on the badge as a parameter
	 */
	$.fn.badge = function ( badgeCount, options ) {
		var $badge,
			oldBadgeCount,
			newBadgeCount,
			$existingBadge = this.find( '.mw-badge' );

		options = $.extend( { type : 'overlay' }, options );

		// If there is no existing badge, this will give an empty string
		oldBadgeCount = Number( $existingBadge.text() );
		if ( isNaN( oldBadgeCount ) ) {
			oldBadgeCount = 0;
		}

		// If badgeCount is a number, use that as the new badge
		if ( typeof badgeCount === 'number' ) {
			newBadgeCount = badgeCount;
		} else if ( typeof badgeCount === 'string' ) {
			// If badgeCount is "+x", add x to the old badge
			if ( badgeCount.charAt(0) === '+' ) {
				newBadgeCount = oldBadgeCount + Number( badgeCount.substr(1) );
			// If badgeCount is "-x", subtract x from the old badge
			} else if ( badgeCount.charAt(0) === '-' ) {
				newBadgeCount = oldBadgeCount - Number( badgeCount.substr(1) );
			// If badgeCount can be converted into a number, convert it
			} else if ( !isNaN( Number( badgeCount ) ) ) {
				newBadgeCount = Number( badgeCount );
			} else {
				newBadgeCount = 0;
			}
		// Other types are not supported, fall back to 0.
		} else {
			newBadgeCount = 0;
		}

		// Badge count must be a whole number
		newBadgeCount = Math.round( newBadgeCount );

		if ( newBadgeCount <= 0 ) {
			// Badges should only exist for values > 0.
			$existingBadge.remove();
		} else {
			// Don't add duplicates
			if ( $existingBadge.length ) {
				$badge = $existingBadge;
				// Insert the new count into the badge
				this.find( '.mw-badge-content' ).text( newBadgeCount );
			} else {
				// Contruct a new badge with the count
				$badge = $( '<div>' )
					.addClass( 'mw-badge' )
					.append(
						$( '<span>' )
							.addClass( 'mw-badge-content' )
							.text( newBadgeCount )
					);
				this.append( $badge );
			}

			if ( options.type === 'inline' ) {
				$badge
					.removeClass( 'mw-badge-overlay' )
					.addClass( 'mw-badge-inline' );
			// Default: overlay
			} else {
				$badge
					.removeClass( 'mw-badge-inline' )
					.addClass( 'mw-badge-overlay' );

			}

			// If a callback was specified, call it with the badge count
			if ( $.isFunction( options.callback ) ) {
				options.callback( newBadgeCount );
			}
		}
	};
}( jQuery ) );