8 * @param {jQuery.Event} e
10 function doLivePreview( e
) {
11 var isDiff
, api
, request
, postData
, copySelectors
, section
,
12 $wikiPreview
, $wikiDiff
, $editform
, $copyElements
, $spinner
, $errorBox
;
14 isDiff
= ( e
.target
.name
=== 'wpDiff' );
15 $wikiPreview
= $( '#wikiPreview' );
16 $wikiDiff
= $( '#wikiDiff' );
17 $editform
= $( '#editform' );
18 $errorBox
= $( '.errorbox' );
19 section
= $editform
.find( '[name="wpSection"]' ).val();
21 // Show changes for a new section is not yet supported
22 if ( isDiff
&& section
=== 'new' ) {
27 // Remove any previously displayed errors
29 // Show #wikiPreview if it's hidden to be able to scroll to it
30 // (if it is hidden, it's also empty, so nothing changes in the rendering)
33 // Jump to where the preview will appear
34 $wikiPreview
[0].scrollIntoView();
48 $copyElements
= $( copySelectors
.join( ',' ) );
50 // Not shown during normal preview, to be removed if present
51 $( '.mw-newarticletext' ).remove();
53 $spinner
= $.createSpinner( {
57 $wikiPreview
.before( $spinner
);
59 marginTop
: $spinner
.height()
62 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
63 // (e.g. empty #catlinks)
64 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
69 uselang
: mw
.config
.get( 'wgUserLanguage' ),
70 title
: mw
.config
.get( 'wgPageName' ),
71 text
: $editform
.find( '#wpTextbox1' ).textSelection( 'getContents' ),
72 summary
: $editform
.find( '#wpSummary' ).textSelection( 'getContents' )
75 if ( section
!== '' ) {
76 postData
.sectionpreview
= '';
77 if ( section
=== 'new' ) {
78 postData
.section
= section
;
79 postData
.sectiontitle
= postData
.summary
;
86 // First PST the input, then diff it
87 postData
.onlypst
= '';
88 request
= api
.post( postData
);
89 request
.done( function ( response
) {
95 titles
: mw
.config
.get( 'wgPageName' ),
96 rvdifftotext
: response
.parse
.text
['*'],
99 if ( section
!== '' ) {
100 postData
.rvsection
= section
;
102 return api
.post( postData
).done( function ( result2
) {
104 var diffHtml
= result2
.query
.pages
[result2
.query
.pageids
[0]]
105 .revisions
[0].diff
['*'];
106 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
108 // "result.blah is undefined" error, ignore
116 $.extend( postData
, {
119 prop
: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml',
120 disableeditsection
: true
122 request
= api
.post( postData
);
123 request
.done( function ( response
) {
124 var li
, newList
, $displaytitle
, $content
, $parent
, $list
;
125 if ( response
.parse
.modules
) {
126 mw
.loader
.load( response
.parse
.modules
.concat(
127 response
.parse
.modulescripts
,
128 response
.parse
.modulestyles
,
129 response
.parse
.modulemessages
) );
131 if ( response
.parse
.displaytitle
) {
132 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
133 $( '#firstHeading' ).msg(
134 mw
.config
.get( 'wgEditMessage', 'editing' ),
137 document
.title
= mw
.msg(
140 mw
.config
.get( 'wgEditMessage', 'editing' ),
145 if ( response
.parse
.categorieshtml
) {
146 $( '#catlinks' ).replaceWith( response
.parse
.categorieshtml
['*'] );
148 if ( response
.parse
.templates
) {
150 $.each( response
.parse
.templates
, function ( i
, template
) {
154 'href': mw
.util
.getUrl( template
['*'] ),
155 'class': ( template
.exists
!== undefined ? '' : 'new' )
157 .text( template
['*'] )
162 $editform
.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
164 if ( response
.parse
.limitreporthtml
) {
165 $( '.limitreport' ).html( response
.parse
.limitreporthtml
['*'] );
167 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
169 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
171 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
174 'href': langlink
.url
,
175 'title': langlink
['*'] + ' - ' + langlink
.langname
,
176 'lang': langlink
.lang
,
177 'hreflang': langlink
.lang
179 .text( langlink
.autonym
)
183 $list
= $( '#p-lang ul' );
184 $parent
= $list
.parent();
185 $list
.detach().empty().append( newList
).prependTo( $parent
);
188 if ( response
.parse
.text
['*'] ) {
189 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
192 .html( response
.parse
.text
['*'] );
194 mw
.hook( 'wikipage.content' ).fire( $content
);
197 $wikiPreview
.append( $content
);
204 request
.done( function ( response
) {
205 var isSubject
= ( section
=== 'new' ),
206 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview';
207 if ( response
.parse
.parsedsummary
) {
208 $editform
.find( '.mw-summary-preview' )
211 mw
.message( summaryMsg
).parse(),
213 $( '<span>' ).addClass( 'comment' ).html(
214 // There is no equivalent to rawParams
215 mw
.message( 'parentheses' ).escaped()
216 .replace( '$1', response
.parse
.parsedsummary
['*'] )
221 request
.always( function () {
223 $copyElements
.animate( {
227 request
.fail( function ( code
, result
) {
228 var errorMsg
= 'API error: ' + code
;
229 if ( code
=== 'http' ) {
230 errorMsg
= 'HTTP error: ';
231 if ( result
.exception
) {
232 errorMsg
+= result
.exception
;
234 errorMsg
+= result
.textStatus
;
237 $errorBox
= $( '<div>' )
238 .addClass( 'errorbox' )
239 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
240 .append( document
.createTextNode( errorMsg
) );
242 $wikiPreview
.hide().before( $errorBox
);
247 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
248 // the scripts or styles without reloading the page.
249 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
253 // The following elements can change in a preview but are not output
254 // by the server when they're empty until the preview response.
255 // TODO: Make the server output these always (in a hidden state), so we don't
256 // have to fish and (hopefully) put them in the right place (since skins
257 // can change where they are output).
259 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
260 $( '.portal:last' ).after(
264 'role': 'navigation',
265 'title': mw
.msg( 'tooltip-p-lang' ),
266 'aria-labelledby': 'p-lang-label'
268 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
269 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
273 if ( !$( '.mw-summary-preview' ).length
) {
274 $( '#wpSummary' ).after(
275 $( '<div>' ).addClass( 'mw-summary-preview' )
279 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
280 $( '#wikiPreview' ).after(
283 .attr( 'id', 'wikiDiff' )
284 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
285 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
289 // This should be moved down to '#editform', but is kept on the body for now
290 // because the LiquidThreads extension is re-using this module with only half
291 // the EditPage (doesn't include #editform presumably, bug 55463).
292 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
295 }( mediaWiki
, jQuery
) );