build: Replace jscs+jshint with eslint
[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 /* eslint-disable no-use-before-define */
5 ( function ( mw, $ ) {
6 if ( !mw.config.get( 'wgAjaxEditStash' ) ) {
7 return;
8 }
9
10 $( function () {
11 var idleTimeout = 3000,
12 api = new mw.Api(),
13 timer,
14 pending,
15 lastText,
16 lastSummary,
17 lastTextHash,
18 $form = $( '#editform' ),
19 $text = $form.find( '#wpTextbox1' ),
20 $summary = $form.find( '#wpSummary' ),
21 section = $form.find( '[name=wpSection]' ).val(),
22 model = $form.find( '[name=model]' ).val(),
23 format = $form.find( '[name=format]' ).val(),
24 revId = $form.find( '[name=parentRevId]' ).val(),
25 lastPriority = 0,
26 PRIORITY_LOW = 1,
27 PRIORITY_HIGH = 2;
28
29 // Send a request to stash the edit to the API.
30 // If a request is in progress, abort it since its payload is stale and the API
31 // may limit concurrent stash parses.
32 function stashEdit() {
33 api.getToken( 'csrf' ).then( function ( token ) {
34 var req, params,
35 textChanged = isTextChanged(),
36 priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
37
38 if ( pending ) {
39 if ( lastPriority > priority ) {
40 // Stash request for summary change should wait on pending text change stash
41 pending.then( checkStash );
42 return;
43 }
44 pending.abort();
45 }
46
47 // Update the "last" tracking variables
48 lastSummary = $summary.textSelection( 'getContents' );
49 lastPriority = priority;
50 if ( textChanged ) {
51 lastText = $text.textSelection( 'getContents' );
52 // Reset hash
53 lastTextHash = null;
54 }
55
56 params = {
57 action: 'stashedit',
58 token: token,
59 title: mw.config.get( 'wgPageName' ),
60 section: section,
61 sectiontitle: '',
62 summary: lastSummary,
63 contentmodel: model,
64 contentformat: format,
65 baserevid: revId
66 };
67 if ( lastTextHash ) {
68 params.stashedtexthash = lastTextHash;
69 } else {
70 params.text = lastText;
71 }
72
73 req = api.post( params );
74 pending = req;
75 req.then( function ( data ) {
76 if ( req === pending ) {
77 pending = null;
78 }
79 if ( data.stashedit && data.stashedit.texthash ) {
80 lastTextHash = data.stashedit.texthash;
81 } else {
82 // Request failed or text hash expired;
83 // include the text in a future stash request.
84 lastTextHash = null;
85 }
86 } );
87 } );
88 }
89
90 // Whether the body text content changed since the last stashEdit()
91 function isTextChanged() {
92 return lastText !== $text.textSelection( 'getContents' );
93 }
94
95 // Whether the edit summary has changed since the last stashEdit()
96 function isSummaryChanged() {
97 return lastSummary !== $summary.textSelection( 'getContents' );
98 }
99
100 // Check whether text or summary have changed and call stashEdit()
101 function checkStash() {
102 if ( !isTextChanged() && !isSummaryChanged() ) {
103 return;
104 }
105
106 stashEdit();
107 }
108
109 function onKeyUp( e ) {
110 // Ignore keystrokes that don't modify text, like cursor movements.
111 // See <http://www.javascripter.net/faq/keycodes.htm> and
112 // <http://www.quirksmode.org/js/keys.html>. We don't have to be exhaustive,
113 // because the cost of misfiring is low.
114 // * Key code 33-40: Page Up/Down, End, Home, arrow keys.
115 // * Key code 16-18: Shift, Ctrl, Alt.
116 if ( ( e.which >= 33 && e.which <= 40 ) || ( e.which >= 16 && e.which <= 18 ) ) {
117 return;
118 }
119
120 clearTimeout( timer );
121 timer = setTimeout( checkStash, idleTimeout );
122 }
123
124 function onSummaryFocus() {
125 // Summary typing is usually near the end of the workflow and involves less pausing.
126 // Re-stash more frequently in hopes of capturing the final summary before submission.
127 idleTimeout = 1000;
128 // Stash now since the text is likely the final version. The re-stashes based on the
129 // summary are targeted at caching edit checks that need the final summary.
130 checkStash();
131 }
132
133 function onTextFocus() {
134 // User returned to the text field... reset stash rate to default
135 idleTimeout = 3000;
136 }
137
138 function onFormLoaded() {
139 if (
140 // Reverts may involve use (undo) links; stash as they review the diff.
141 // Since the form has a pre-filled summary, stash the edit immediately.
142 mw.util.getParamValue( 'undo' ) !== null ||
143 // Pressing "show changes" and "preview" also signify that the user will
144 // probably save the page soon
145 $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
146 ) {
147 checkStash();
148 }
149 }
150
151 // We don't attempt to stash new section edits because in such cases the parser output
152 // varies on the edit summary (since it determines the new section's name).
153 if ( $form.find( 'input[name=wpSection]' ).val() === 'new' ) {
154 return;
155 }
156
157 $text.on( {
158 change: checkStash,
159 keyup: onKeyUp,
160 focus: onTextFocus
161 } );
162 $summary.on( {
163 focus: onSummaryFocus,
164 focusout: checkStash,
165 keyup: onKeyUp
166 } );
167 onFormLoaded();
168 } );
169 }( mediaWiki, jQuery ) );