Merge "(Bug 45775) Adjusted the margin of "userloginForm""
[lhc/web/wiklou.git] / resources / jquery / jquery.badge.js
1 /**
2 * jQuery Badge plugin
3 *
4 * @license MIT
5 */
6
7 /**
8 * @author Ryan Kaldari <rkaldari@wikimedia.org>, 2012
9 * @author Andrew Garrett <agarrett@wikimedia.org>, 2012
10 * @author Marius Hoch <hoo@online.de>, 2012
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * This program is distributed WITHOUT ANY WARRANTY.
23 */
24 ( function ( $, mw ) {
25 /**
26 * Allows you to put a "badge" on an item on the page. The badge container
27 * will be appended to the selected element(s).
28 * See mediawiki.org/wiki/ResourceLoader/Default_modules#jQuery.badge
29 *
30 * @param {number|string} text The value to display in the badge. If the value is falsey (0,
31 * null, false, '', etc.), any existing badge will be removed.
32 * @param {boolean} inline True if the badge should be displayed inline, false
33 * if the badge should overlay the parent element (default is inline)
34 * @param {boolean} displayZero True if the number zero should be displayed,
35 * false if the number zero should result in the badge being hidden
36 * (default is zero will result in the badge being hidden)
37 */
38 $.fn.badge = function ( text, inline, displayZero ) {
39 var $badge = this.find( '.mw-badge' ),
40 badgeStyleClass = 'mw-badge-' + ( inline ? 'inline' : 'overlay' ),
41 isImportant = true, displayBadge = true;
42
43 // If we're displaying zero, ensure style to be non-important
44 if ( mw.language.convertNumber( text, true ) === 0 ) {
45 isImportant = false;
46 if ( !displayZero ) {
47 displayBadge = false;
48 }
49 // If text is falsey (besides 0), hide the badge
50 } else if ( !text ) {
51 displayBadge = false;
52 }
53
54 if ( displayBadge ) {
55 // If a badge already exists, reuse it
56 if ( $badge.length ) {
57 $badge
58 .toggleClass( 'mw-badge-important', isImportant )
59 .find( '.mw-badge-content' )
60 .text( text );
61 } else {
62 // Otherwise, create a new badge with the specified text and style
63 $badge = $( '<div class="mw-badge"></div>' )
64 .addClass( badgeStyleClass )
65 .toggleClass( 'mw-badge-important', isImportant )
66 .append(
67 $( '<span class="mw-badge-content"></span>' ).text( text )
68 )
69 .appendTo( this );
70 }
71 } else {
72 $badge.remove();
73 }
74 return this;
75 };
76 }( jQuery, mediaWiki ) );