Merge "Make MWHttpRequest::__construct() protected."
[lhc/web/wiklou.git] / resources / jquery / jquery.badge.js
1 // Badger v1.0 by Daniel Raftery
2 // http://thrivingkings.com/badger
3 // http://twitter.com/ThrivingKings
4 // Modified by Ryan Kaldari <rkaldari@wikimedia.org>
5
6 /**
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * This program is distributed WITHOUT ANY WARRANTY.
18 */
19
20 (function( $ ) {
21 $.fn.badge = function( badge, options ) {
22 var existingBadge = this.find( '.mw-badge' );
23 options = $.extend( {}, options );
24
25 badge = String(badge);
26 if ( badge.charAt(0) === '+' ) {
27 if ( existingBadge.length > 0 ) {
28 oldBadge = existingBadge.text();
29 badge = Math.round( Number( oldBadge ) + Number( badge.substr(1) ) );
30 } else {
31 badge = badge.substr(1);
32 }
33 } else if ( badge.charAt(0) === '-' ) {
34 if ( existingBadge.length > 0 ) {
35 oldBadge = existingBadge.text();
36 badge = Math.round( Number( oldBadge ) - Number( badge.substr(1) ) );
37 } else {
38 badge = 0;
39 }
40 }
41
42 if ( Number(badge) <= 0 ) {
43 // Clear any existing badge
44 existingBadge.remove();
45 } else {
46 // Don't add duplicates
47 var $badge = existingBadge;
48 if ( existingBadge.length > 0 ) {
49 this.find( '.mw-badge-content' ).text( badge );
50 } else {
51 $badge = $('<div/>')
52 .addClass('mw-badge')
53 .addClass('mw-badge-overlay')
54 .append(
55 $('<span/>')
56 .addClass('mw-badge-content')
57 .text(badge)
58 );
59 this.append($badge);
60 }
61
62 if ( options.type ) {
63 if ( options.type == 'inline' ) {
64 $badge.removeClass('mw-badge-overlay')
65 .addClass('mw-badge-inline');
66 } else if ( options.type == 'overlay' ) {
67 $badge.removeClass('mw-badge-inline')
68 .addClass('mw-badge-overlay');
69 }
70 }
71
72 // If a callback was specified, call it with the badge number
73 if ( options.callback ) {
74 options.callback( badge );
75 }
76 }
77 };
78 } ) ( jQuery );