8 * @param {jQuery.Event} e
10 function doLivePreview( e
) {
11 var isDiff
, api
, request
, 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 $errorBox
= $( '.errorbox' );
21 section
= $editform
.find( '[name="wpSection"]' ).val();
23 if ( $textbox
.length
=== 0 ) {
26 // Show changes for a new section is not yet supported
27 if ( isDiff
&& section
=== 'new' ) {
32 // Remove any previously displayed errors
34 // Show #wikiPreview if it's hidden to be able to scroll to it
35 // (if it is hidden, it's also empty, so nothing changes in the rendering)
38 // Jump to where the preview will appear
39 $wikiPreview
[0].scrollIntoView();
53 $copyElements
= $( copySelectors
.join( ',' ) );
55 // Not shown during normal preview, to be removed if present
56 $( '.mw-newarticletext' ).remove();
58 $spinner
= $.createSpinner( {
62 $wikiPreview
.before( $spinner
);
64 marginTop
: $spinner
.height()
67 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
68 // (e.g. empty #catlinks)
69 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
74 uselang
: mw
.config
.get( 'wgUserLanguage' ),
75 title
: mw
.config
.get( 'wgPageName' ),
76 text
: $textbox
.textSelection( 'getContents' ),
77 summary
: $summary
.textSelection( 'getContents' )
80 if ( section
!== '' ) {
81 postData
.sectionpreview
= '';
82 if ( section
=== 'new' ) {
83 postData
.section
= section
;
84 postData
.sectiontitle
= postData
.summary
;
91 // First PST the input, then diff it
92 postData
.onlypst
= '';
93 request
= api
.post( postData
);
94 request
.done( function ( response
) {
100 titles
: mw
.config
.get( 'wgPageName' ),
101 rvdifftotext
: response
.parse
.text
['*'],
104 if ( section
!== '' ) {
105 postData
.rvsection
= section
;
107 return api
.post( postData
).done( function ( result2
) {
109 var diffHtml
= result2
.query
.pages
[result2
.query
.pageids
[0]]
110 .revisions
[0].diff
['*'];
111 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
113 // "result.blah is undefined" error, ignore
121 $.extend( postData
, {
124 prop
: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml',
125 disableeditsection
: true
127 request
= api
.post( postData
);
128 request
.done( function ( response
) {
129 var li
, newList
, $displaytitle
, $content
, $parent
, $list
;
130 if ( response
.parse
.modules
) {
131 mw
.loader
.load( response
.parse
.modules
.concat(
132 response
.parse
.modulescripts
,
133 response
.parse
.modulestyles
,
134 response
.parse
.modulemessages
) );
136 if ( response
.parse
.displaytitle
) {
137 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
138 $( '#firstHeading' ).msg(
139 mw
.config
.get( 'wgEditMessage', 'editing' ),
142 document
.title
= mw
.msg(
145 mw
.config
.get( 'wgEditMessage', 'editing' ),
150 if ( response
.parse
.categorieshtml
) {
151 $( '#catlinks' ).replaceWith( response
.parse
.categorieshtml
['*'] );
153 if ( response
.parse
.templates
) {
155 $.each( response
.parse
.templates
, function ( i
, template
) {
159 'href': mw
.util
.getUrl( template
['*'] ),
160 'class': ( template
.exists
!== undefined ? '' : 'new' )
162 .text( template
['*'] )
167 $editform
.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
169 if ( response
.parse
.limitreporthtml
) {
170 $( '.limitreport' ).html( response
.parse
.limitreporthtml
['*'] );
172 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
174 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
176 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
179 'href': langlink
.url
,
180 'title': langlink
['*'] + ' - ' + langlink
.langname
,
181 'lang': langlink
.lang
,
182 'hreflang': langlink
.lang
184 .text( langlink
.autonym
)
188 $list
= $( '#p-lang ul' );
189 $parent
= $list
.parent();
190 $list
.detach().empty().append( newList
).prependTo( $parent
);
193 if ( response
.parse
.text
['*'] ) {
194 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
197 .html( response
.parse
.text
['*'] );
199 mw
.hook( 'wikipage.content' ).fire( $content
);
202 $wikiPreview
.append( $content
);
209 request
.done( function ( response
) {
210 var isSubject
= ( section
=== 'new' ),
211 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview';
212 if ( response
.parse
.parsedsummary
) {
213 $editform
.find( '.mw-summary-preview' )
216 mw
.message( summaryMsg
).parse(),
218 $( '<span>' ).addClass( 'comment' ).html(
219 // There is no equivalent to rawParams
220 mw
.message( 'parentheses' ).escaped()
221 .replace( '$1', response
.parse
.parsedsummary
['*'] )
226 request
.always( function () {
228 $copyElements
.animate( {
232 request
.fail( function ( code
, result
) {
233 var errorMsg
= 'API error: ' + code
;
234 if ( code
=== 'http' ) {
235 errorMsg
= 'HTTP error: ';
236 if ( result
.exception
) {
237 errorMsg
+= result
.exception
;
239 errorMsg
+= result
.textStatus
;
242 $errorBox
= $( '<div>' )
243 .addClass( 'errorbox' )
244 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
245 .append( document
.createTextNode( errorMsg
) );
247 $wikiPreview
.hide().before( $errorBox
);
252 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
253 // the scripts or styles without reloading the page.
254 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
258 // The following elements can change in a preview but are not output
259 // by the server when they're empty until the preview response.
260 // TODO: Make the server output these always (in a hidden state), so we don't
261 // have to fish and (hopefully) put them in the right place (since skins
262 // can change where they are output).
264 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
265 $( '.portal:last' ).after(
269 'role': 'navigation',
270 'title': mw
.msg( 'tooltip-p-lang' ),
271 'aria-labelledby': 'p-lang-label'
273 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
274 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
278 if ( !$( '.mw-summary-preview' ).length
) {
279 $( '#wpSummary' ).after(
280 $( '<div>' ).addClass( 'mw-summary-preview' )
284 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
285 $( '#wikiPreview' ).after(
288 .attr( 'id', 'wikiDiff' )
289 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
290 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
294 // This should be moved down to '#editform', but is kept on the body for now
295 // because the LiquidThreads extension is re-using this module with only half
296 // the EditPage (doesn't include #editform presumably, bug 55463).
297 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
300 }( mediaWiki
, jQuery
) );