8 * @param {jQuery.Event} e
10 function doLivePreview( e
) {
11 var isDiff
, api
, parseRequest
, diffRequest
, postData
, copySelectors
, section
,
12 $wikiPreview
, $wikiDiff
, $editform
, $textbox
, $summary
, $copyElements
, $spinner
, $errorBox
;
14 isDiff
= ( e
.target
.name
=== 'wpDiff' );
15 $wikiPreview
= $( '#wikiPreview' );
16 $wikiDiff
= $( '#wikiDiff' );
17 $editform
= $( '#editform' );
18 $textbox
= $editform
.find( '#wpTextbox1' );
19 $summary
= $editform
.find( '#wpSummary' );
20 $spinner
= $( '.mw-spinner-preview' );
21 $errorBox
= $( '.errorbox' );
22 section
= $editform
.find( '[name="wpSection"]' ).val();
24 if ( $textbox
.length
=== 0 ) {
27 // Show changes for a new section is not yet supported
28 if ( isDiff
&& section
=== 'new' ) {
33 // Remove any previously displayed errors
35 // Show #wikiPreview if it's hidden to be able to scroll to it
36 // (if it is hidden, it's also empty, so nothing changes in the rendering)
39 // Jump to where the preview will appear
40 $wikiPreview
[ 0 ].scrollIntoView();
54 $copyElements
= $( copySelectors
.join( ',' ) );
56 // Not shown during normal preview, to be removed if present
57 $( '.mw-newarticletext' ).remove();
59 if ( $spinner
.length
=== 0 ) {
60 $spinner
= $.createSpinner( {
64 .addClass( 'mw-spinner-preview' )
65 .css( 'margin-top', '1em' );
66 $wikiPreview
.before( $spinner
);
71 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
72 // (e.g. empty #catlinks)
73 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
78 title
: mw
.config
.get( 'wgPageName' ),
79 summary
: $summary
.textSelection( 'getContents' ),
86 if ( postData
.summary
) {
87 parseRequest
= api
.post( postData
);
90 diffRequest
= api
.post( {
94 titles
: mw
.config
.get( 'wgPageName' ),
95 rvdifftotext
: $textbox
.textSelection( 'getContents' ),
96 rvdifftotextpst
: true,
98 rvsection
: section
=== '' ? undefined : section
101 // Wait for the summary before showing the diff so the page doesn't jump twice
102 $.when( diffRequest
, parseRequest
).done( function ( response
) {
104 query
= response
[ 0 ].query
;
106 diffHtml
= query
.pages
[ query
.pageids
[ 0 ] ]
107 .revisions
[ 0 ].diff
[ '*' ];
108 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
110 // "result.blah is undefined" error, ignore
118 $.extend( postData
, {
119 prop
: 'text|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
120 text
: $textbox
.textSelection( 'getContents' ),
123 sectionpreview
: section
!== '',
124 disableeditsection
: true,
125 uselang
: mw
.config
.get( 'wgUserLanguage' )
127 if ( section
=== 'new' ) {
128 postData
.section
= 'new';
129 postData
.sectiontitle
= postData
.summary
;
132 parseRequest
= api
.post( postData
);
133 parseRequest
.done( function ( response
) {
134 var li
, newList
, $displaytitle
, $content
, $parent
, $list
;
135 if ( response
.parse
.jsconfigvars
) {
136 mw
.config
.set( response
.parse
.jsconfigvars
);
138 if ( response
.parse
.modules
) {
139 mw
.loader
.load( response
.parse
.modules
.concat(
140 response
.parse
.modulescripts
,
141 response
.parse
.modulestyles
144 if ( response
.parse
.displaytitle
) {
145 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
146 $( '#firstHeading' ).msg(
147 mw
.config
.get( 'wgEditMessage', 'editing' ),
150 document
.title
= mw
.msg(
153 mw
.config
.get( 'wgEditMessage', 'editing' ),
158 if ( response
.parse
.categorieshtml
) {
159 $( '#catlinks' ).replaceWith( response
.parse
.categorieshtml
[ '*' ] );
161 if ( response
.parse
.templates
) {
163 $.each( response
.parse
.templates
, function ( i
, template
) {
167 href
: mw
.util
.getUrl( template
[ '*' ] ),
168 'class': ( template
.exists
!== undefined ? '' : 'new' )
170 .text( template
[ '*' ] )
175 $editform
.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
177 if ( response
.parse
.limitreporthtml
) {
178 $( '.limitreport' ).html( response
.parse
.limitreporthtml
[ '*' ] );
180 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
182 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
184 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
188 title
: langlink
[ '*' ] + ' - ' + langlink
.langname
,
190 hreflang
: langlink
.lang
192 .text( langlink
.autonym
)
196 $list
= $( '#p-lang ul' );
197 $parent
= $list
.parent();
198 $list
.detach().empty().append( newList
).prependTo( $parent
);
201 if ( response
.parse
.text
[ '*' ] ) {
202 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
205 .html( response
.parse
.text
[ '*' ] );
207 mw
.hook( 'wikipage.content' ).fire( $content
);
210 $wikiPreview
.append( $content
);
216 $.when( parseRequest
, diffRequest
).done( function ( parseResp
) {
217 var parse
= parseResp
&& parseResp
[ 0 ].parse
,
218 isSubject
= ( section
=== 'new' ),
219 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview',
220 $summaryPreview
= $editform
.find( '.mw-summary-preview' ).empty();
221 if ( parse
&& parse
.parsedsummary
&& parse
.parsedsummary
[ '*' ] !== '' ) {
222 $summaryPreview
.append(
223 mw
.message( summaryMsg
).parse(),
225 $( '<span>' ).addClass( 'comment' ).html(
226 // There is no equivalent to rawParams
227 mw
.message( 'parentheses' ).escaped()
228 .replace( '$1', parse
.parsedsummary
[ '*' ] )
232 mw
.hook( 'wikipage.editform' ).fire( $editform
);
233 } ).always( function () {
235 $copyElements
.animate( {
238 } ).fail( function ( code
, result
) {
239 // This just shows the error for whatever request failed first
240 var errorMsg
= 'API error: ' + code
;
241 if ( code
=== 'http' ) {
242 errorMsg
= 'HTTP error: ';
243 if ( result
.exception
) {
244 errorMsg
+= result
.exception
;
246 errorMsg
+= result
.textStatus
;
249 $errorBox
= $( '<div>' )
250 .addClass( 'errorbox' )
251 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
252 .append( document
.createTextNode( errorMsg
) );
254 $wikiPreview
.hide().before( $errorBox
);
259 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
260 // the scripts or styles without reloading the page.
261 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
265 // The following elements can change in a preview but are not output
266 // by the server when they're empty until the preview response.
267 // TODO: Make the server output these always (in a hidden state), so we don't
268 // have to fish and (hopefully) put them in the right place (since skins
269 // can change where they are output).
271 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
272 $( '.portal:last' ).after(
277 title
: mw
.msg( 'tooltip-p-lang' ),
278 'aria-labelledby': 'p-lang-label'
280 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
281 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
285 if ( !$( '.mw-summary-preview' ).length
) {
286 $( '#wpSummary' ).after(
287 $( '<div>' ).addClass( 'mw-summary-preview' )
291 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
292 $( '#wikiPreview' ).after(
295 .attr( 'id', 'wikiDiff' )
296 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
297 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
301 // This should be moved down to '#editform', but is kept on the body for now
302 // because the LiquidThreads extension is re-using this module with only half
303 // the EditPage (doesn't include #editform presumably, bug 55463).
304 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
307 }( mediaWiki
, jQuery
) );