Merge "Add "rel=discussion" attribute to talk page tabs in skin template"
[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 idleTimeout = 4000,
7 api = new mw.Api(),
8 pending = null,
9 $form = $( '#editform' ),
10 $text = $form.find( '#wpTextbox1' ),
11 data = {},
12 timer = null;
13
14 /* Has the edit body text changed since the last stashEdit() call? */
15 function isChanged() {
16 // Normalize line endings to CRLF, like $.fn.serializeObject does.
17 var newText = $text.val().replace( /\r?\n/g, '\r\n' );
18 return newText !== data.wpTextbox1;
19 }
20
21 function onEditChanged() {
22 if ( !isChanged() ) {
23 return;
24 }
25
26 // If a request is in progress, abort it; its payload is stale.
27 if ( pending ) {
28 pending.abort();
29 }
30
31 data = $form.serializeObject();
32 pending = api.postWithToken( 'edit', {
33 action: 'stashedit',
34 title: mw.config.get( 'wgPageName' ),
35 section: data.wpSection,
36 sectiontitle: '',
37 text: data.wpTextbox1,
38 contentmodel: data.model,
39 contentformat: data.format,
40 baserevid: data.parentRevId
41 } );
42 }
43
44 function onKeyPress( e ) {
45 // Ignore keystrokes that don't modify text, like cursor movements.
46 // See <http://stackoverflow.com/q/2284844>.
47 if ( e.which === 0 ) {
48 return;
49 }
50
51 clearTimeout( timer );
52
53 if ( pending ) {
54 pending.abort();
55 }
56
57 timer = setTimeout( onEditChanged, idleTimeout );
58 }
59
60 // We don't attempt to stash new section edits because in such cases
61 // the parser output varies on the edit summary (since it determines
62 // the new section's name).
63 if ( $form.find( 'input[name=wpSection]' ).val() === 'new' ) {
64 return;
65 }
66
67 $text.on( { change: onEditChanged, keypress: onKeyPress } );
68
69 } );
70 }( mediaWiki, jQuery ) );