Merge "Cache VCS commit id/date text on Special:Version"
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.edit.preview.js
index c5cd61e..4c2fc3a 100644 (file)
@@ -29,6 +29,7 @@
                // update from from the ajax-loaded preview page.
                copySelectors = [
                        // Main
+                       '#firstHeading',
                        '#wikiPreview',
                        '#wikiDiff',
                        '#catlinks',
@@ -47,7 +48,7 @@
                $spinner = $.createSpinner( {
                        size: 'large',
                        type: 'block'
-               });
+               } );
                $wikiPreview.before( $spinner );
                $spinner.css( {
                        marginTop: $spinner.height()
 
                $previewDataHolder = $( '<div>' );
                targetUrl = $editform.attr( 'action' );
+               targetUrl += targetUrl.indexOf( '?' ) !== -1 ? '&' : '?';
+               targetUrl += $.param( {
+                       debug: mw.config.get( 'debug' ),
+                       uselang: mw.config.get( 'wgUserLanguage' ),
+                       useskin: mw.config.get( 'skin' )
+               } );
 
                // Gather all the data from the form
                postData = $editform.formToArray();
                // TODO: This should use the action=parse API instead of loading the entire page,
                // although that requires figuring out how to convert that raw data into proper HTML.
                $previewDataHolder.load( targetUrl + ' ' + copySelectors.join( ',' ), postData, function () {
-                       var i, $from;
+                       var i, $from, $next, $parent;
 
                        // Copy the contents of the specified elements from the loaded page to the real page.
                        // Also copy their class attributes.
                        for ( i = 0; i < copySelectors.length; i++ ) {
                                $from = $previewDataHolder.find( copySelectors[i] );
 
-                               $( copySelectors[i] )
-                                       .empty()
-                                       .append( $from.contents() )
-                                       .attr( 'class', $from.attr( 'class' ) );
+                               if ( copySelectors[i] === '#wikiPreview' ) {
+                                       $next = $wikiPreview.next();
+                                       // If there is no next node, use parent instead.
+                                       // Only query parent if needed, false otherwise.
+                                       $parent = !$next.length && $wikiPreview.parent();
+
+                                       $wikiPreview
+                                               .detach()
+                                               .empty()
+                                               .append( $from.contents() )
+                                               .attr( 'class', $from.attr( 'class' ) );
+
+                                       mw.hook( 'wikipage.content' ).fire( $wikiPreview );
+
+                                       // Reattach
+                                       if ( $parent ) {
+                                               $parent.append( $wikiPreview );
+                                       } else {
+                                               $next.before( $wikiPreview );
+                                       }
+
+                               } else {
+                                       $( copySelectors[i] )
+                                               .empty()
+                                               .append( $from.contents() )
+                                               .attr( 'class', $from.attr( 'class' ) );
+                               }
                        }
 
                        // Deprecated: Use mw.hook instead
                        $( mw ).trigger( 'LivePreviewDone', [copySelectors] );
 
-                       mw.hook( 'wikipage.content' ).fire( $wikiPreview );
-
                        $spinner.remove();
                        $copyElements.animate( {
                                opacity: 1