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