Merge "Fix "UTPage" creation in tests"
[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', 'wgCookiePrefix', 'wgCurRevisionId' ] ),
22 // This should match EditPage::POST_EDIT_COOKIE_KEY_PREFIX:
23 cookieKey = config.wgCookiePrefix + 'PostEditRevision' + config.wgCurRevisionId,
24 $div, id;
25
26 function showConfirmation( data ) {
27 data = data || {};
28 if ( data.message === undefined ) {
29 data.message = $.parseHTML( mw.message( 'postedit-confirmation', data.user || mw.user ).escaped() );
30 }
31
32 $div = $(
33 '<div class="postedit-container">' +
34 '<div class="postedit">' +
35 '<div class="postedit-icon postedit-icon-checkmark postedit-content"></div>' +
36 '<a href="#" class="postedit-close">&times;</a>' +
37 '</div>' +
38 '</div>'
39 );
40
41 if ( typeof data.message === 'string' ) {
42 $div.find( '.postedit-content' ).text( data.message );
43 } else if ( typeof data.message === 'object' ) {
44 $div.find( '.postedit-content' ).append( data.message );
45 }
46
47 $div
48 .click( fadeOutConfirmation )
49 .prependTo( 'body' );
50
51 id = setTimeout( fadeOutConfirmation, 3000 );
52 }
53
54 function fadeOutConfirmation() {
55 clearTimeout( id );
56 $div.find( '.postedit' ).addClass( 'postedit postedit-faded' );
57 setTimeout( removeConfirmation, 500 );
58
59 return false;
60 }
61
62 function removeConfirmation() {
63 $div.remove();
64 mw.hook( 'postEdit.afterRemoval' ).fire();
65 }
66
67 mw.hook( 'postEdit' ).add( showConfirmation );
68
69 if ( config.wgAction === 'view' && $.cookie( cookieKey ) === '1' ) {
70 $.cookie( cookieKey, null, { path: '/' } );
71 mw.config.set( 'wgPostEdit', true );
72
73 mw.hook( 'postEdit' ).fire();
74 }
75
76 } ( mediaWiki, jQuery ) );