summaryrefslogtreecommitdiff
path: root/resources/jquery/jquery.messageBox.js
blob: a69fca593ce7016946580bf8c915e37174e17183 (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
/**
 * jQuery messageBox
 *
 * Function to inform the user of something. Use sparingly (since there's mw.log for
 * messages aimed at developers / debuggers). Based on the function in MediaWiki's
 * legacy javascript (wikibits.js) by Aryeh Gregor called jsMsg() added in r23233.
 *
 * @author Krinkle <krinklemail@gmail.com>
 *
 * Dual license:
 * @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0>
 * @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 */
( function( $, mw ) {
// @return jQuery object of the message box
$.messageBoxNew = function( options ) {
	options = $.extend( {
		'id': 'js-messagebox', // unique identifier for this message box
		'parent': 'body', // jQuery/CSS selector
		'insert': 'prepend' // 'prepend' or 'append'
	}, options );
	var $curBox = $( '#'+ options.id );
	// Only create a new box if it doesn't exist already
	if ( $curBox.size() > 0 ) {
		if ( $curBox.hasClass( 'js-messagebox' ) ) {
			return $curBox;
		} else {
			return $curBox.addClass( 'js-messagebox' );
		}
	} else {
		var $newBox = $( '<div/>', {
			'id': options.id,
			'class': 'js-messagebox',
			'css': {
				'display': 'none'
			}
		});
		if ( $( options.parent ).length < 1 ) {
			options.parent = 'body';
		}
		if ( options.insert === 'append' ) {
			$newBox.appendTo( options.parent );
			return $newBox;
		} else {
			$newBox.prependTo( options.parent );
			return $newBox;
		}
	}
};
// Calling with no message or message set to empty string or null will hide the group,
// setting 'replace' to true as well will reset and hide the group entirely.
// If there are no visible groups the main message box is hidden automatically,
// and shown again once there are messages
// @return jQuery object of message group
$.messageBox = function( options ) {
	options = $.extend( {
		'message': '',
		'group': 'default',
		'replace': false, // if true replaces any previous message in this group
		'target': 'js-messagebox'
	}, options );	  
	var $target = $.messageBoxNew( { id: options.target } );
	var groupID = options.target + '-' + options.group;
	var $group = $( '#' + groupID );
	// Create group container if not existant
	if ( $group.size() < 1 ) {
		$group = $( '<div/>', {
			'id': groupID,
			'class': 'js-messagebox-group'
		});
		$target.prepend( $group );
	}
	// Replace ?
	if ( options.replace === true ) {
		$group.empty();
	}
	// Hide it ?
	if ( options.message === '' || options.message === null ) {
		$group.hide();
	} else {
		// Actual message addition
		$group.prepend( $( '<p/>' ).append( options.message ) ).show();
		$target.slideDown();
	}
	// If the last visible group was just hidden, slide the entire box up
	// Othere wise slideDown (if already visible nothing will happen)
	if ( $target.find( '> *:visible' ).size() === 0 ) {
		// to avoid a sudden dissapearance of the last group followed by
		// a slide up of only the outline, show it for a second
		$group.show();
		$target.slideUp();
		$group.hide();
	} else {
		$target.slideDown();
	}
	return $group;
};
} )( jQuery, mediaWiki );