Merge "Improve "selfmove" message's wording"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.view.postEdit.js
1 ( function ( mw, $ ) {
2 'use strict';
3
4 /**
5 * Fired after an edit was successfully saved.
6 *
7 * Does not fire for null edits.
8 *
9 * @event postEdit
10 * @member mw.hook
11 * @param {Object} [data] Optional data
12 * @param {string|jQuery|Array} [data.message] Message that listeners
13 * should use when displaying notifications. String for plain text,
14 * use array or jQuery object to pass actual nodes.
15 * @param {string|mw.user} [data.user=mw.user] User that made the edit.
16 */
17
18 /**
19 * After the listener for #postEdit removes the notification.
20 *
21 * @event postEdit_afterRemoval
22 * @member mw.hook
23 */
24
25 var postEdit = mw.config.get( 'wgPostEdit' );
26
27 function showConfirmation( data ) {
28 var $container, $popup, $content, timeoutId;
29
30 function fadeOutConfirmation() {
31 $popup.addClass( 'postedit-faded' );
32 setTimeout( function () {
33 $container.remove();
34 mw.hook( 'postEdit.afterRemoval' ).fire();
35 }, 250 );
36 }
37
38 data = data || {};
39
40 if ( data.message === undefined ) {
41 data.message = $.parseHTML( mw.message( 'postedit-confirmation-saved', data.user || mw.user ).escaped() );
42 }
43
44 $content = $( '<div>' ).addClass( 'postedit-icon postedit-icon-checkmark postedit-content' );
45 if ( typeof data.message === 'string' ) {
46 $content.text( data.message );
47 } else if ( typeof data.message === 'object' ) {
48 $content.append( data.message );
49 }
50
51 $popup = $( '<div>' ).addClass( 'postedit mw-notification' ).append( $content )
52 .click( function () {
53 clearTimeout( timeoutId );
54 fadeOutConfirmation();
55 } );
56
57 $container = $( '<div>' ).addClass( 'postedit-container' ).append( $popup );
58 timeoutId = setTimeout( fadeOutConfirmation, 3000 );
59
60 $( 'body' ).prepend( $container );
61 }
62
63 mw.hook( 'postEdit' ).add( showConfirmation );
64
65 if ( postEdit ) {
66 mw.hook( 'postEdit' ).fire( {
67 // The following messages can be used here:
68 // postedit-confirmation-saved
69 // postedit-confirmation-created
70 // postedit-confirmation-restored
71 message: mw.msg(
72 'postedit-confirmation-' + postEdit,
73 mw.user
74 )
75 } );
76 }
77
78 }( mediaWiki, jQuery ) );