Merge "Add tests for WikiMap and WikiReference"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.view.postEdit.js
1 ( function ( mw, $ ) {
2 'use strict';
3
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 */
13
14 /**
15 * After the listener for #postEdit removes the notification.
16 *
17 * @event postEdit_afterRemoval
18 * @member mw.hook
19 */
20
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;
26
27 function removeConfirmation() {
28 $div.remove();
29 mw.hook( 'postEdit.afterRemoval' ).fire();
30 }
31
32 function fadeOutConfirmation() {
33 clearTimeout( id );
34 $div.find( '.postedit' ).addClass( 'postedit postedit-faded' );
35 setTimeout( removeConfirmation, 500 );
36
37 return false;
38 }
39
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 }
45
46 $div = mw.template.get( 'mediawiki.action.view.postEdit', 'postEdit.html' ).render();
47
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 }
53
54 $div
55 .click( fadeOutConfirmation )
56 .prependTo( 'body' );
57
58 id = setTimeout( fadeOutConfirmation, 3000 );
59 }
60
61 mw.hook( 'postEdit' ).add( showConfirmation );
62
63 if ( config.wgAction === 'view' && cookieVal ) {
64 mw.config.set( 'wgPostEdit', true );
65
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 }
78
79 }( mediaWiki, jQuery ) );