LivePreview: Process jsconfigvars
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.preview.js
index 3bf8e32..5074d94 100644 (file)
@@ -9,16 +9,28 @@
         */
        function doLivePreview( e ) {
                var isDiff, api, request, postData, copySelectors, section,
-                       $wikiPreview, $wikiDiff, $editform, $copyElements, $spinner;
-
-               e.preventDefault();
+                       $wikiPreview, $wikiDiff, $editform, $textbox, $summary, $copyElements, $spinner, $errorBox;
 
                isDiff = ( e.target.name === 'wpDiff' );
                $wikiPreview = $( '#wikiPreview' );
                $wikiDiff = $( '#wikiDiff' );
                $editform = $( '#editform' );
+               $textbox = $editform.find( '#wpTextbox1' );
+               $summary = $editform.find( '#wpSummary' );
+               $errorBox = $( '.errorbox' );
                section = $editform.find( '[name="wpSection"]' ).val();
 
+               if ( $textbox.length === 0 ) {
+                       return;
+               }
+               // Show changes for a new section is not yet supported
+               if ( isDiff && section === 'new' ) {
+                       return;
+               }
+               e.preventDefault();
+
+               // Remove any previously displayed errors
+               $errorBox.remove();
                // Show #wikiPreview if it's hidden to be able to scroll to it
                // (if it is hidden, it's also empty, so nothing changes in the rendering)
                $wikiPreview.show();
                        action: 'parse',
                        uselang: mw.config.get( 'wgUserLanguage' ),
                        title: mw.config.get( 'wgPageName' ),
-                       text: $editform.find( '#wpTextbox1' ).textSelection( 'getContents' ),
-                       summary: $editform.find( '#wpSummary' ).textSelection( 'getContents' )
+                       text: $textbox.textSelection( 'getContents' ),
+                       summary: $summary.textSelection( 'getContents' )
                };
 
+               if ( section !== '' ) {
+                       postData.sectionpreview = '';
+                       if ( section === 'new' ) {
+                               postData.section = section;
+                               postData.sectiontitle = postData.summary;
+                       }
+               }
+
                if ( isDiff ) {
                        $wikiPreview.hide();
 
                        $.extend( postData, {
                                pst: '',
                                preview: '',
-                               prop: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml',
+                               prop: 'text|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
                                disableeditsection: true
                        } );
-                       if ( section !== '' ) {
-                               postData.sectionpreview = '';
-                       }
                        request = api.post( postData );
                        request.done( function ( response ) {
-                               var li, newList, $content, $parent, $list;
+                               var li, newList, $displaytitle, $content, $parent, $list;
+                               if ( response.parse.jsconfigvars ) {
+                                       mw.config.set( response.parse.jsconfigvars );
+                               }
                                if ( response.parse.modules ) {
                                        mw.loader.load( response.parse.modules.concat(
                                                response.parse.modulescripts,
                                                response.parse.modulemessages ) );
                                }
                                if ( response.parse.displaytitle ) {
-                                       $( '#firstHeading' ).html( response.parse.displaytitle );
+                                       $displaytitle = $( $.parseHTML( response.parse.displaytitle ) );
+                                       $( '#firstHeading' ).msg(
+                                               mw.config.get( 'wgEditMessage', 'editing' ),
+                                               $displaytitle
+                                       );
+                                       document.title = mw.msg(
+                                               'pagetitle',
+                                               mw.msg(
+                                                       mw.config.get( 'wgEditMessage', 'editing' ),
+                                                       $displaytitle.text()
+                                               )
+                                       );
                                }
                                if ( response.parse.categorieshtml ) {
                                        $( '#catlinks' ).replaceWith( response.parse.categorieshtml['*'] );
                                                newList.push( li );
                                        } );
 
-                                       $editform.find( '.mw-editfooter-list' ).detach().empty().append( newList ).appendTo( '.templatesUsed' );
+                                       $editform.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList ).appendTo( '.templatesUsed' );
                                }
                                if ( response.parse.limitreporthtml ) {
                                        $( '.limitreport' ).html( response.parse.limitreporthtml['*'] );
                        } );
                }
                request.done( function ( response ) {
+                       var isSubject = ( section === 'new' ),
+                               summaryMsg = isSubject ? 'subject-preview' : 'summary-preview';
                        if ( response.parse.parsedsummary ) {
-                               // TODO implement special behavior for section === 'new'
                                $editform.find( '.mw-summary-preview' )
                                        .empty()
                                        .append(
-                                               mw.message( 'summary-preview' ).parse(),
+                                               mw.message( summaryMsg ).parse(),
                                                ' ',
                                                $( '<span>' ).addClass( 'comment' ).html(
                                                        // There is no equivalent to rawParams
                                opacity: 1
                        }, 'fast' );
                } );
+               request.fail( function ( code, result ) {
+                       var errorMsg = 'API error: ' +  code;
+                       if ( code === 'http' ) {
+                               errorMsg = 'HTTP error: ';
+                               if ( result.exception ) {
+                                       errorMsg += result.exception;
+                               } else {
+                                       errorMsg += result.textStatus;
+                               }
+                       }
+                       $errorBox = $( '<div>' )
+                               .addClass( 'errorbox' )
+                               .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
+                               .append( document.createTextNode( errorMsg ) );
+                       $wikiDiff.hide();
+                       $wikiPreview.hide().before( $errorBox );
+               } );
        }
 
        $( function () {
                }
 
                if ( !$( '.mw-summary-preview' ).length ) {
-                       $( '.editCheckboxes' ).before(
+                       $( '#wpSummary' ).after(
                                $( '<div>' ).addClass( 'mw-summary-preview' )
                        );
                }