Add RL template module with HTML markup language
[mediawiki.git] / resources / src / mediawiki.action / mediawiki.action.view.postEdit.js
blobee2246dde364e82aa0fd4df3bd67f92a6ffa7c52
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 showConfirmation( data ) {
28                 data = data || {};
29                 if ( data.message === undefined ) {
30                         data.message = $.parseHTML( mw.message( 'postedit-confirmation-saved', data.user || mw.user ).escaped() );
31                 }
33                 $div = $( mw.template.get( 'mediawiki.action.view.postEdit', 'postEdit.html' ).render() );
35                 if ( typeof data.message === 'string' ) {
36                         $div.find( '.postedit-content' ).text( data.message );
37                 } else if ( typeof data.message === 'object' ) {
38                         $div.find( '.postedit-content' ).append( data.message );
39                 }
41                 $div
42                         .click( fadeOutConfirmation )
43                         .prependTo( 'body' );
45                 id = setTimeout( fadeOutConfirmation, 3000 );
46         }
48         function fadeOutConfirmation() {
49                 clearTimeout( id );
50                 $div.find( '.postedit' ).addClass( 'postedit postedit-faded' );
51                 setTimeout( removeConfirmation, 500 );
53                 return false;
54         }
56         function removeConfirmation() {
57                 $div.remove();
58                 mw.hook( 'postEdit.afterRemoval' ).fire();
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 ) );