Don't attempt to stash new section edits
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.stash.js
1 /*!
2 * Scripts for pre-emptive edit preparing on action=edit
3 */
4 ( function ( mw, $ ) {
5 $( function () {
6 var api = new mw.Api(), pending = null, $form = $( '#editform' );
7
8 function stashEdit( token ) {
9 var data = $form.serializeObject();
10
11 pending = api.post( {
12 action: 'stashedit',
13 token: token,
14 title: mw.config.get( 'wgPageName' ),
15 section: data.wpSection,
16 sectiontitle: '',
17 text: data.wpTextbox1,
18 contentmodel: data.model,
19 contentformat: data.format,
20 baserevid: data.parentRevId
21 } );
22 }
23
24 function onEditChanged() {
25 // If a stash request is already in flight, abort it, since its
26 // payload has just been invalidated by this change.
27 if ( pending ) {
28 pending.abort();
29 }
30 api.getToken( 'edit' ).then( stashEdit );
31 }
32
33 // We don't attempt to stash new section edits because in such cases
34 // the parser output varies on the edit summary (since it determines
35 // the new section's name).
36 if ( $form.find( 'input[name=wpSection]' ).val() === 'new' ) {
37 return;
38 }
39
40 $form.find( '#wpTextbox1' ).on( 'change', onEditChanged );
41 } );
42 }( mediaWiki, jQuery ) );