summaryrefslogtreecommitdiff
path: root/resources/jquery/jquery.badge.js
blob: 9404e8183389d9ef4cf4010f23a2b44439e0948f (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
/**
 * 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
 * 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 ( $, mw ) {
	/**
	 * 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 {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 if the badge should be displayed inline, false
	 *  if the badge should overlay the parent element (default is inline)
	 * @param {boolean} displayZero True if the number zero should be displayed,
	 *  false if the number zero should result in the badge being hidden
	 *  (default is zero will result in the badge being hidden)
	 */
	$.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 = $( '<div class="mw-badge"></div>' )
					.addClass( badgeStyleClass )
					.toggleClass( 'mw-badge-important', isImportant )
					.append(
						$( '<span class="mw-badge-content"></span>' ).text( text )
					)
					.appendTo( this );
			}
		} else {
			$badge.remove();
		}
		return this;
	};
}( jQuery, mediaWiki ) );