Merge "Remove old WebResponse::setCookie() calling method"
[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 if ( !mw.config.get( 'wgAjaxEditStash' ) ) {
6 return;
7 }
8
9 $( function () {
10 var idleTimeout = 3000,
11 api = new mw.Api(),
12 pending = null,
13 $form = $( '#editform' ),
14 $text = $form.find( '#wpTextbox1' ),
15 $summary = $form.find( '#wpSummary' ),
16 section = $form.find( '[name=wpSection]' ).val(),
17 model = $form.find( '[name=model]' ).val(),
18 format = $form.find( '[name=format]' ).val(),
19 revId = $form.find( '[name=parentRevId]' ).val(),
20 lastText = $text.textSelection( 'getContents' ),
21 timer = null;
22
23 // Send a request to stash the edit to the API.
24 // If a request is in progress, abort it since its payload is stale and the API
25 // may limit concurrent stash parses.
26 function stashEdit() {
27 if ( pending ) {
28 pending.abort();
29 }
30
31 api.getToken( 'csrf' ).then( function ( token ) {
32 lastText = $text.textSelection( 'getContents' );
33
34 pending = api.post( {
35 action: 'stashedit',
36 token: token,
37 title: mw.config.get( 'wgPageName' ),
38 section: section,
39 sectiontitle: '',
40 text: lastText,
41 summary: $summary.textSelection( 'getContents' ),
42 contentmodel: model,
43 contentformat: format,
44 baserevid: revId
45 } );
46 } );
47 }
48
49 // Check if edit body text changed since the last stashEdit() call or if no edit
50 // stash calls have yet been made
51 function isChanged() {
52 var newText = $text.textSelection( 'getContents' );
53 return newText !== lastText;
54 }
55
56 function onEditorIdle() {
57 if ( !isChanged() ) {
58 return;
59 }
60
61 stashEdit();
62 }
63
64 function onTextKeyUp( e ) {
65 // Ignore keystrokes that don't modify text, like cursor movements.
66 // See <http://www.javascripter.net/faq/keycodes.htm> and
67 // <http://www.quirksmode.org/js/keys.html>. We don't have to be exhaustive,
68 // because the cost of misfiring is low.
69 // * Key code 33-40: Page Up/Down, End, Home, arrow keys.
70 // * Key code 16-18: Shift, Ctrl, Alt.
71 if ( ( e.which >= 33 && e.which <= 40 ) || ( e.which >= 16 && e.which <= 18 ) ) {
72 return;
73 }
74
75 clearTimeout( timer );
76 timer = setTimeout( onEditorIdle, idleTimeout );
77 }
78
79 function onFormLoaded() {
80 if (
81 // Reverts may involve use (undo) links; stash as they review the diff.
82 // Since the form has a pre-filled summary, stash the edit immediately.
83 mw.util.getParamValue( 'undo' ) !== null
84 // Pressing "show changes" and "preview" also signify that the user will
85 // probably save the page soon
86 || $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
87 ) {
88 stashEdit();
89 }
90 }
91
92 // We don't attempt to stash new section edits because in such cases
93 // the parser output varies on the edit summary (since it determines
94 // the new section's name).
95 if ( $form.find( 'input[name=wpSection]' ).val() === 'new' ) {
96 return;
97 }
98
99 $text.on( { change: onEditorIdle, keyup: onTextKeyUp } );
100 $summary.on( { focus: onEditorIdle } );
101 onFormLoaded();
102
103 } );
104 }( mediaWiki, jQuery ) );