postEdit: Show a different success messsage on publish vs. save wikis
[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(
42 mw.config.get( 'wgEditSubmitButtonLabelPublish' ) ?
43 'postedit-confirmation-published' :
44 'postedit-confirmation-saved',
45 data.user || mw.user
46 ).escaped() );
47 }
48
49 $content = $( '<div>' ).addClass( 'postedit-icon postedit-icon-checkmark postedit-content' );
50 if ( typeof data.message === 'string' ) {
51 $content.text( data.message );
52 } else if ( typeof data.message === 'object' ) {
53 $content.append( data.message );
54 }
55
56 $popup = $( '<div>' ).addClass( 'postedit mw-notification' ).append( $content )
57 .click( function () {
58 clearTimeout( timeoutId );
59 fadeOutConfirmation();
60 } );
61
62 $container = $( '<div>' ).addClass( 'postedit-container' ).append( $popup );
63 timeoutId = setTimeout( fadeOutConfirmation, 3000 );
64
65 $( 'body' ).prepend( $container );
66 }
67
68 mw.hook( 'postEdit' ).add( showConfirmation );
69
70 if ( postEdit ) {
71 mw.hook( 'postEdit' ).fire( {
72 // The following messages can be used here:
73 // postedit-confirmation-saved
74 // postedit-confirmation-created
75 // postedit-confirmation-restored
76 message: mw.msg(
77 'postedit-confirmation-' + postEdit,
78 mw.user
79 )
80 } );
81 }
82
83 }( mediaWiki, jQuery ) );