Merge "Document rationale or primary use case for many indexes"
[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 cookieVal,
26 config = mw.config.get( [ 'wgAction', 'wgCurRevisionId' ] ),
27 // This should match EditPage::POST_EDIT_COOKIE_KEY_PREFIX:
28 cookieKey = 'PostEditRevision' + config.wgCurRevisionId;
29
30 function showConfirmation( data ) {
31 var $container, $popup, $content, timeoutId;
32
33 function fadeOutConfirmation() {
34 $popup.addClass( 'postedit-faded' );
35 setTimeout( function () {
36 $container.remove();
37 mw.hook( 'postEdit.afterRemoval' ).fire();
38 }, 250 );
39 }
40
41 data = data || {};
42
43 if ( data.message === undefined ) {
44 data.message = $.parseHTML( mw.message( 'postedit-confirmation-saved', data.user || mw.user ).escaped() );
45 }
46
47 $content = $( '<div>' ).addClass( 'postedit-icon postedit-icon-checkmark postedit-content' );
48 if ( typeof data.message === 'string' ) {
49 $content.text( data.message );
50 } else if ( typeof data.message === 'object' ) {
51 $content.append( data.message );
52 }
53
54 $popup = $( '<div>' ).addClass( 'postedit mw-notification' ).append(
55 $content,
56 $( '<a>' ).addClass( 'postedit-close' ).attr( 'href', '#' ).text( '×' )
57 ).on( 'click', function ( e ) {
58 e.preventDefault();
59 clearTimeout( timeoutId );
60 fadeOutConfirmation();
61 } );
62
63 $container = $( '<div>' ).addClass( 'postedit-container' ).append( $popup );
64 timeoutId = setTimeout( fadeOutConfirmation, 3000 );
65
66 $( 'body' ).prepend( $container );
67 }
68
69 mw.hook( 'postEdit' ).add( showConfirmation );
70
71 // Only when viewing wiki pages, that exist
72 // (E.g. not on special pages or non-view actions)
73 if ( config.wgCurRevisionId && config.wgAction === 'view' ) {
74 cookieVal = mw.cookie.get( cookieKey );
75 if ( cookieVal ) {
76 mw.config.set( 'wgPostEdit', true );
77
78 mw.hook( 'postEdit' ).fire( {
79 // The following messages can be used here:
80 // postedit-confirmation-saved
81 // postedit-confirmation-created
82 // postedit-confirmation-restored
83 message: mw.msg(
84 'postedit-confirmation-' + cookieVal,
85 mw.user
86 )
87 } );
88
89 mw.cookie.set( cookieKey, null );
90 }
91 }
92
93 }( mediaWiki, jQuery ) );