Debian Codegraph @

resources/src/mediawiki.action/mediawiki.action.view.postEdit.js

raw symbols SCIP
1 ( function () {
2 'use strict';
3
4 /**
5 * Fired after an edit was successfully saved.
6 *
7 * Does not fire for null edits.
8 *
9 * Code that fires the postEdit hook should first set `wgRevisionId` and `wgCurRevisionId`
10 * to the revision associated with the edit that triggered the postEdit hook, then fire
11 * the postEdit hook.
12 *
13 * @example
14 * mw.config.set( {
15 * wgCurRevisionId: data.newrevid,
16 * wgRevisionId: data.newrevid
17 * } );
18 * // Now fire the hook.
19 * mw.hook( 'postEdit' ).fire();
20 *
21 * @event ~'postEdit'
22 * @memberof Hooks
23 * @param {Object} [data] Optional data
24 * @param {string|jQuery|Array} [data.message] Message that listeners
25 * should use when displaying notifications. String for plain text,
26 * use array or jQuery object to pass actual nodes.
27 * @param {string|mw.user} [data.user=mw.user] User that made the edit.
28 * @param {boolean} [data.tempUserCreated] Whether a temporary user account
29 * was created.
30 */
31
32 /**
33 * After the listener for #postEdit removes the notification.
34 *
35 * @deprecated
36 * @event ~'postEdit.afterRemoval'
37 * @memberof Hooks
38 */
39
40 const config = require( './config.json' );
41 const storageKey = 'mw-PostEdit' + mw.config.get( 'wgPageName' );
42
43 function showConfirmation( data ) {
44 data = data || {};
45
46 const label = data.message || mw.message(
47 config.EditSubmitButtonLabelPublish ?
48 'postedit-confirmation-published' :
49 'postedit-confirmation-saved',
50 data.user || mw.user,
51 mw.config.get( 'wgRevisionId' )
52 ).parseDom();
53
54 data.message = new OO.ui.MessageWidget( {
55 type: 'success',
56 inline: true,
57 label: label
58 } ).$element[ 0 ];
59
60 mw.notify( data.message, {
61 classes: [ 'postedit' ]
62 } );
63
64 // Deprecated - use the 'postEdit' hook, and an additional pause if required
65 mw.hook( 'postEdit.afterRemoval' ).fire();
66
67 if ( data.tempUserCreated ) {
68 mw.tempUserCreated.showPopup();
69 }
70 }
71
72 function init() {
73 // JS-only flag that allows another module providing a hook handler to suppress the default one.
74 if ( !mw.config.get( 'wgPostEditConfirmationDisabled' ) ) {
75 mw.hook( 'postEdit' ).add( showConfirmation );
76 }
77
78 // Check storage and cookie (set server-side)
79 let action = mw.storage.session.get( storageKey ) || mw.config.get( 'wgPostEdit' );
80 if ( action ) {
81 let tempUserCreated = false;
82 const plusPos = action.indexOf( '+' );
83 if ( plusPos > -1 ) {
84 action = action.slice( 0, plusPos );
85 tempUserCreated = true;
86 }
87
88 // Set 'wgPostEdit' when displaying a message requested via storage, to allow CampaignEvents
89 // to override post-edit behavior for some page creations performed using VisualEditor, which
90 // shows a message via storage when creating new pages (T240041#8148006):
91 // https://gerrit.wikimedia.org/g/mediawiki/extensions/CampaignEvents/+/e380af0c69b17ecb05fc3258f92c9df625a35449/resources/ext.campaignEvents.eventpage/index.js#187
92 // https://gerrit.wikimedia.org/g/mediawiki/extensions/VisualEditor/+/192c1051120c8dd331f00b9024b5beadab1cb89a/modules/ve-mw/init/targets/ve.init.mw.ArticleTarget.js#656
93 // TODO: We should provide a better API for this that doesn't require extensions to parse the
94 // 'action' value themselves, and doesn't require accessing 'wgPostEdit' from mw.config.
95 mw.config.set( 'wgPostEdit', action );
96
97 module.exports.fireHook( action, tempUserCreated );
98 }
99
100 // Clear storage (cookie is cleared server-side)
101 mw.storage.session.remove( storageKey );
102 }
103
104 /**
105 * Show post-edit messages.
106 *
107 * @example
108 * var postEdit = require( 'mediawiki.action.view.postEdit' );
109 * postEdit.fireHook( 'saved' );
110 *
111 * @class mw.plugin.action.view.postEdit
112 * @singleton
113 * @ignore
114 */
115 module.exports = {
116
117 /**
118 * Show a post-edit message now.
119 *
120 * This is just a shortcut for firing mw.hook#postEdit.
121 *
122 * @ignore
123 * @param {string} [action] One of 'saved', 'created', 'restored'
124 * @param {boolean} [tempUserCreated] Whether a temporary account was created during this edit
125 */
126 fireHook: ( action, tempUserCreated ) => {
127 if ( !action ) {
128 action = 'saved';
129 }
130 if ( action === 'saved' && config.EditSubmitButtonLabelPublish ) {
131 action = 'published';
132 }
133 mw.hook( 'postEdit' ).fire( {
134 // The following messages can be used here:
135 // * postedit-confirmation-published
136 // * postedit-confirmation-saved
137 // * postedit-confirmation-created
138 // * postedit-confirmation-restored
139 message: mw.message(
140 'postedit-confirmation-' + action,
141 mw.user,
142 mw.config.get( 'wgRevisionId' )
143 ).parseDom(),
144 tempUserCreated: tempUserCreated
145 } );
146 },
147
148 /**
149 * Show a post-edit message on the next page load.
150 *
151 * The necessary data is stored in session storage for up to 20 minutes, and cleared when the
152 * page is loaded again.
153 *
154 * @ignore
155 * @param {string} [action] One of 'saved', 'created', 'restored'
156 * @param {boolean} [tempUserCreated] Whether a temporary account was created during this edit
157 */
158 fireHookOnPageReload: ( action, tempUserCreated ) => {
159 if ( !action ) {
160 action = 'saved';
161 }
162 if ( tempUserCreated ) {
163 action += '+tempuser';
164 }
165 mw.storage.session.set(
166 storageKey,
167 action,
168 1200 // same duration as EditPage::POST_EDIT_COOKIE_DURATION
169 );
170 }
171 };
172
173 init();
174
175 }() );