Merge "Add countUnreadNotifications to WatchedItemStore"
[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 config = mw.config.get( [ 'wgAction', 'wgCurRevisionId' ] ),
26 // This should match EditPage::POST_EDIT_COOKIE_KEY_PREFIX:
27 cookieKey = 'PostEditRevision' + config.wgCurRevisionId,
28 cookieVal = mw.cookie.get( cookieKey ),
29 $div, id;
30
31 function removeConfirmation() {
32 $div.remove();
33 mw.hook( 'postEdit.afterRemoval' ).fire();
34 }
35
36 function fadeOutConfirmation() {
37 clearTimeout( id );
38 $div.find( '.postedit' ).addClass( 'postedit postedit-faded' );
39 setTimeout( removeConfirmation, 500 );
40
41 return false;
42 }
43
44 function showConfirmation( data ) {
45 data = data || {};
46 if ( data.message === undefined ) {
47 data.message = $.parseHTML( mw.message( 'postedit-confirmation-saved', data.user || mw.user ).escaped() );
48 }
49
50 $div = mw.template.get( 'mediawiki.action.view.postEdit', 'postEdit.html' ).render();
51
52 if ( typeof data.message === 'string' ) {
53 $div.find( '.postedit-content' ).text( data.message );
54 } else if ( typeof data.message === 'object' ) {
55 $div.find( '.postedit-content' ).append( data.message );
56 }
57
58 $div
59 .click( fadeOutConfirmation )
60 .prependTo( 'body' );
61
62 id = setTimeout( fadeOutConfirmation, 3000 );
63 }
64
65 mw.hook( 'postEdit' ).add( showConfirmation );
66
67 if ( config.wgAction === 'view' && cookieVal ) {
68 mw.config.set( 'wgPostEdit', true );
69
70 mw.hook( 'postEdit' ).fire( {
71 // The following messages can be used here:
72 // postedit-confirmation-saved
73 // postedit-confirmation-created
74 // postedit-confirmation-restored
75 message: mw.msg(
76 'postedit-confirmation-' + cookieVal,
77 mw.user
78 )
79 } );
80 mw.cookie.set( cookieKey, null );
81 }
82
83 }( mediaWiki, jQuery ) );