Merge "Revert "ApiSandbox: Display params as JSON on request page""
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.stash.js
index 704287a..7e966ea 100644 (file)
@@ -1,6 +1,7 @@
 /*!
  * Scripts for pre-emptive edit preparing on action=edit
  */
+/* eslint-disable no-use-before-define */
 ( function ( mw, $ ) {
        if ( !mw.config.get( 'wgAjaxEditStash' ) ) {
                return;
@@ -9,7 +10,11 @@
        $( function () {
                var idleTimeout = 3000,
                        api = new mw.Api(),
-                       pending = null,
+                       timer,
+                       stashReq,
+                       lastText,
+                       lastSummary,
+                       lastTextHash,
                        $form = $( '#editform' ),
                        $text = $form.find( '#wpTextbox1' ),
                        $summary = $form.find( '#wpSummary' ),
                        model = $form.find( '[name=model]' ).val(),
                        format = $form.find( '[name=format]' ).val(),
                        revId = $form.find( '[name=parentRevId]' ).val(),
-                       lastText = $text.textSelection( 'getContents' ),
-                       lastSummary = $summary.textSelection( 'getContents' ),
-                       lastTextHash = null,
                        lastPriority = 0,
-                       origSummary = lastSummary,
-                       timer = null,
                        PRIORITY_LOW = 1,
                        PRIORITY_HIGH = 2;
 
+               // We don't attempt to stash new section edits because in such cases the parser output
+               // varies on the edit summary (since it determines the new section's name).
+               if ( !$form.length || section === 'new' ) {
+                       return;
+               }
+
                // Send a request to stash the edit to the API.
                // If a request is in progress, abort it since its payload is stale and the API
                // may limit concurrent stash parses.
-               function stashEdit( priority, hashForReuse ) {
-                       if ( pending ) {
-                               pending.abort();
-                               pending = null;
-                       }
+               function stashEdit() {
+                       var req, params,
+                               textChanged = isTextChanged(),
+                               priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
 
-                       api.getToken( 'csrf' ).then( function ( token ) {
-                               // If applicable, just send the hash key to reuse the last text server-side
-                               var req, params;
+                       if ( stashReq ) {
+                               if ( lastPriority > priority ) {
+                                       // Stash request for summary change should wait on pending text change stash
+                                       stashReq.then( checkStash );
+                                       return;
+                               }
+                               stashReq.abort();
+                       }
 
-                               // Update tracking of the last text/summary sent out
+                       // Update the "last" tracking variables
+                       lastSummary = $summary.textSelection( 'getContents' );
+                       lastPriority = priority;
+                       if ( textChanged ) {
                                lastText = $text.textSelection( 'getContents' );
-                               lastSummary = $summary.textSelection( 'getContents' );
-                               lastPriority = priority;
-                               lastTextHash = null; // "failed" until proven successful
-
-                               params = {
-                                       action: 'stashedit',
-                                       token: token,
-                                       title: mw.config.get( 'wgPageName' ),
-                                       section: section,
-                                       sectiontitle: '',
-                                       summary: lastSummary,
-                                       contentmodel: model,
-                                       contentformat: format,
-                                       baserevid: revId
-                               };
-                               if ( hashForReuse ) {
-                                       params.stashedtexthash = hashForReuse;
+                               // Reset hash
+                               lastTextHash = null;
+                       }
+
+                       params = {
+                               action: 'stashedit',
+                               title: mw.config.get( 'wgPageName' ),
+                               section: section,
+                               sectiontitle: '',
+                               summary: lastSummary,
+                               contentmodel: model,
+                               contentformat: format,
+                               baserevid: revId
+                       };
+                       if ( lastTextHash ) {
+                               params.stashedtexthash = lastTextHash;
+                       } else {
+                               params.text = lastText;
+                       }
+
+                       req = api.postWithToken( 'csrf', params );
+                       stashReq = req;
+                       req.then( function ( data ) {
+                               if ( req === stashReq ) {
+                                       stashReq = null;
+                               }
+                               if ( data.stashedit && data.stashedit.texthash ) {
+                                       lastTextHash = data.stashedit.texthash;
                                } else {
-                                       params.text = lastText;
+                                       // Request failed or text hash expired;
+                                       // include the text in a future stash request.
+                                       lastTextHash = null;
                                }
-
-                               req = api.post( params );
-                               pending = req;
-                               req.then( function ( data ) {
-                                       if ( req === pending ) {
-                                               pending = null;
-                                       }
-                                       if ( data.stashedit && data.stashedit.texthash ) {
-                                               lastTextHash = data.stashedit.texthash;
-                                       }
-                               } );
                        } );
                }
 
-               // Check if edit body text changed since the last stashEdit() call or if no edit
-               // stash calls have yet been made
+               // Whether the body text content changed since the last stashEdit()
                function isTextChanged() {
-                       var newText = $text.textSelection( 'getContents' );
-                       return newText !== lastText;
+                       return lastText !== $text.textSelection( 'getContents' );
                }
 
-               // Check if summary changed since the last stashEdit() call or if no edit
-               // stash calls have yet been made
+               // Whether the edit summary has changed since the last stashEdit()
                function isSummaryChanged() {
-                       var newSummary = $summary.textSelection( 'getContents' );
-                       return newSummary !== lastSummary;
+                       return lastSummary !== $summary.textSelection( 'getContents' );
                }
 
-               function onEditorIdle() {
-                       var textChanged = isTextChanged(),
-                               summaryChanged = isSummaryChanged(),
-                               priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
-
-                       if ( !textChanged && !summaryChanged ) {
-                               return; // nothing to do
-                       }
-
-                       if ( pending && lastPriority > priority ) {
-                               // Stash requests for summary changes should wait on pending text change stashes
-                               pending.then( onEditorIdle );
+               // Check whether text or summary have changed and call stashEdit()
+               function checkStash() {
+                       if ( !isTextChanged() && !isSummaryChanged() ) {
                                return;
                        }
 
-                       stashEdit( priority, textChanged ? null : lastTextHash );
+                       stashEdit();
                }
 
                function onKeyUp( e ) {
                        }
 
                        clearTimeout( timer );
-                       timer = setTimeout( onEditorIdle, idleTimeout );
+                       timer = setTimeout( checkStash, idleTimeout );
                }
 
                function onSummaryFocus() {
                        // Summary typing is usually near the end of the workflow and involves less pausing.
-                       // Re-stash frequently in hopes of capturing the final summary before submission.
+                       // Re-stash more frequently in hopes of capturing the final summary before submission.
                        idleTimeout = 1000;
                        // Stash now since the text is likely the final version. The re-stashes based on the
                        // summary are targeted at caching edit checks that need the final summary.
-                       onEditorIdle();
+                       checkStash();
                }
 
                function onTextFocus() {
-                       // User returned to the text field...
-                       if ( $summary.textSelection( 'getContents' ) === origSummary ) {
-                               idleTimeout = 3000; // no summary yet; reset stash rate to default
-                       }
-               }
-
-               function onFormLoaded() {
-                       if (
-                               // Reverts may involve use (undo) links; stash as they review the diff.
-                               // Since the form has a pre-filled summary, stash the edit immediately.
-                               mw.util.getParamValue( 'undo' ) !== null
-                               // Pressing "show changes" and "preview" also signify that the user will
-                               // probably save the page soon
-                               || $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
-                       ) {
-                               stashEdit( PRIORITY_HIGH, null );
-                       }
-               }
-
-               // We don't attempt to stash new section edits because in such cases the parser output
-               // varies on the edit summary (since it determines the new section's name).
-               if ( $form.find( 'input[name=wpSection]' ).val() === 'new' ) {
-                       return;
+                       // User returned to the text field... reset stash rate to default
+                       idleTimeout = 3000;
                }
 
                $text.on( {
-                       change: onEditorIdle,
                        keyup: onKeyUp,
-                       focus: onTextFocus
+                       focus: onTextFocus,
+                       change: checkStash
                } );
                $summary.on( {
+                       keyup: onKeyUp,
                        focus: onSummaryFocus,
-                       focusout: onEditorIdle,
-                       keyup: onKeyUp
+                       focusout: checkStash
                } );
-               onFormLoaded();
+
+               if (
+                       // Reverts may involve use (undo) links; stash as they review the diff.
+                       // Since the form has a pre-filled summary, stash the edit immediately.
+                       mw.util.getParamValue( 'undo' ) !== null ||
+                       // Pressing "show changes" and "preview" also signify that the user will
+                       // probably save the page soon
+                       $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
+               ) {
+                       checkStash();
+               }
        } );
 }( mediaWiki, jQuery ) );