Merge "ForeignStructuredUpload: Depend on mediawiki.widgets.DateInputWidget"
[mediawiki.git] / resources / src / mediawiki.action / mediawiki.action.view.postEdit.js
blob168a1c18d5702b2b5ecc7a2ee3ccde9fceffb7dd
1 ( function ( mw, $ ) {
2         'use strict';
4         /**
5          * @event postEdit
6          * @member mw.hook
7          * @param {Object} [data] Optional data
8          * @param {string|jQuery|Array} [data.message] Message that listeners
9          *  should use when displaying notifications. String for plain text,
10          *  use array or jQuery object to pass actual nodes.
11          * @param {string|mw.user} [data.user=mw.user] User that made the edit.
12          */
14         /**
15          * After the listener for #postEdit removes the notification.
16          *
17          * @event postEdit_afterRemoval
18          * @member mw.hook
19          */
21         var config = mw.config.get( [ 'wgAction', 'wgCurRevisionId' ] ),
22                 // This should match EditPage::POST_EDIT_COOKIE_KEY_PREFIX:
23                 cookieKey = 'PostEditRevision' + config.wgCurRevisionId,
24                 cookieVal = mw.cookie.get( cookieKey ),
25                 $div, id;
27         function removeConfirmation() {
28                 $div.remove();
29                 mw.hook( 'postEdit.afterRemoval' ).fire();
30         }
32         function fadeOutConfirmation() {
33                 clearTimeout( id );
34                 $div.find( '.postedit' ).addClass( 'postedit postedit-faded' );
35                 setTimeout( removeConfirmation, 500 );
37                 return false;
38         }
40         function showConfirmation( data ) {
41                 data = data || {};
42                 if ( data.message === undefined ) {
43                         data.message = $.parseHTML( mw.message( 'postedit-confirmation-saved', data.user || mw.user ).escaped() );
44                 }
46                 $div = mw.template.get( 'mediawiki.action.view.postEdit', 'postEdit.html' ).render();
48                 if ( typeof data.message === 'string' ) {
49                         $div.find( '.postedit-content' ).text( data.message );
50                 } else if ( typeof data.message === 'object' ) {
51                         $div.find( '.postedit-content' ).append( data.message );
52                 }
54                 $div
55                         .click( fadeOutConfirmation )
56                         .prependTo( 'body' );
58                 id = setTimeout( fadeOutConfirmation, 3000 );
59         }
61         mw.hook( 'postEdit' ).add( showConfirmation );
63         if ( config.wgAction === 'view' && cookieVal ) {
64                 mw.config.set( 'wgPostEdit', true );
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-' + cookieVal,
73                                 mw.user
74                         )
75                 } );
76                 mw.cookie.set( cookieKey, null );
77         }
79 }( mediaWiki, jQuery ) );