Merge "Better path traversal prevention in TemplateParser."
[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, $div, id;
29
30 function removeConfirmation() {
31 $div.remove();
32 mw.hook( 'postEdit.afterRemoval' ).fire();
33 }
34
35 function fadeOutConfirmation() {
36 clearTimeout( id );
37 $div.find( '.postedit' ).addClass( 'postedit postedit-faded' );
38 setTimeout( removeConfirmation, 500 );
39
40 return false;
41 }
42
43 function showConfirmation( data ) {
44 data = data || {};
45 if ( data.message === undefined ) {
46 data.message = $.parseHTML( mw.message( 'postedit-confirmation-saved', data.user || mw.user ).escaped() );
47 }
48
49 $div = mw.template.get( 'mediawiki.action.view.postEdit', 'postEdit.html' ).render();
50
51 if ( typeof data.message === 'string' ) {
52 $div.find( '.postedit-content' ).text( data.message );
53 } else if ( typeof data.message === 'object' ) {
54 $div.find( '.postedit-content' ).append( data.message );
55 }
56
57 $div
58 .click( fadeOutConfirmation )
59 .prependTo( 'body' );
60
61 id = setTimeout( fadeOutConfirmation, 3000 );
62 }
63
64 mw.hook( 'postEdit' ).add( showConfirmation );
65
66 // Only when viewing wiki pages, that exist
67 // (E.g. not on special pages or non-view actions)
68 if ( config.wgCurRevisionId && config.wgAction === 'view' ) {
69 cookieVal = mw.cookie.get( cookieKey );
70 if ( cookieVal ) {
71 mw.config.set( 'wgPostEdit', true );
72
73 mw.hook( 'postEdit' ).fire( {
74 // The following messages can be used here:
75 // postedit-confirmation-saved
76 // postedit-confirmation-created
77 // postedit-confirmation-restored
78 message: mw.msg(
79 'postedit-confirmation-' + cookieVal,
80 mw.user
81 )
82 } );
83
84 mw.cookie.set( cookieKey, null );
85 }
86 }
87
88 }( mediaWiki, jQuery ) );