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|jsconfigvars|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
.jsconfigvars
) {
131 mw
.config
.set( response
.parse
.jsconfigvars
);
133 if ( response
.parse
.modules
) {
134 mw
.loader
.load( response
.parse
.modules
.concat(
135 response
.parse
.modulescripts
,
136 response
.parse
.modulestyles
,
137 response
.parse
.modulemessages
) );
139 if ( response
.parse
.displaytitle
) {
140 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
141 $( '#firstHeading' ).msg(
142 mw
.config
.get( 'wgEditMessage', 'editing' ),
145 document
.title
= mw
.msg(
148 mw
.config
.get( 'wgEditMessage', 'editing' ),
153 if ( response
.parse
.categorieshtml
) {
154 $( '#catlinks' ).replaceWith( response
.parse
.categorieshtml
['*'] );
156 if ( response
.parse
.templates
) {
158 $.each( response
.parse
.templates
, function ( i
, template
) {
162 'href': mw
.util
.getUrl( template
['*'] ),
163 'class': ( template
.exists
!== undefined ? '' : 'new' )
165 .text( template
['*'] )
170 $editform
.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
172 if ( response
.parse
.limitreporthtml
) {
173 $( '.limitreport' ).html( response
.parse
.limitreporthtml
['*'] );
175 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
177 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
179 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
182 'href': langlink
.url
,
183 'title': langlink
['*'] + ' - ' + langlink
.langname
,
184 'lang': langlink
.lang
,
185 'hreflang': langlink
.lang
187 .text( langlink
.autonym
)
191 $list
= $( '#p-lang ul' );
192 $parent
= $list
.parent();
193 $list
.detach().empty().append( newList
).prependTo( $parent
);
196 if ( response
.parse
.text
['*'] ) {
197 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
200 .html( response
.parse
.text
['*'] );
202 mw
.hook( 'wikipage.content' ).fire( $content
);
205 $wikiPreview
.append( $content
);
212 request
.done( function ( response
) {
213 var isSubject
= ( section
=== 'new' ),
214 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview';
215 if ( response
.parse
.parsedsummary
) {
216 $editform
.find( '.mw-summary-preview' )
219 mw
.message( summaryMsg
).parse(),
221 $( '<span>' ).addClass( 'comment' ).html(
222 // There is no equivalent to rawParams
223 mw
.message( 'parentheses' ).escaped()
224 .replace( '$1', response
.parse
.parsedsummary
['*'] )
229 request
.always( function () {
231 $copyElements
.animate( {
235 request
.fail( function ( code
, result
) {
236 var errorMsg
= 'API error: ' + code
;
237 if ( code
=== 'http' ) {
238 errorMsg
= 'HTTP error: ';
239 if ( result
.exception
) {
240 errorMsg
+= result
.exception
;
242 errorMsg
+= result
.textStatus
;
245 $errorBox
= $( '<div>' )
246 .addClass( 'errorbox' )
247 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
248 .append( document
.createTextNode( errorMsg
) );
250 $wikiPreview
.hide().before( $errorBox
);
255 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
256 // the scripts or styles without reloading the page.
257 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
261 // The following elements can change in a preview but are not output
262 // by the server when they're empty until the preview response.
263 // TODO: Make the server output these always (in a hidden state), so we don't
264 // have to fish and (hopefully) put them in the right place (since skins
265 // can change where they are output).
267 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
268 $( '.portal:last' ).after(
272 'role': 'navigation',
273 'title': mw
.msg( 'tooltip-p-lang' ),
274 'aria-labelledby': 'p-lang-label'
276 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
277 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
281 if ( !$( '.mw-summary-preview' ).length
) {
282 $( '#wpSummary' ).after(
283 $( '<div>' ).addClass( 'mw-summary-preview' )
287 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
288 $( '#wikiPreview' ).after(
291 .attr( 'id', 'wikiDiff' )
292 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
293 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
297 // This should be moved down to '#editform', but is kept on the body for now
298 // because the LiquidThreads extension is re-using this module with only half
299 // the EditPage (doesn't include #editform presumably, bug 55463).
300 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
303 }( mediaWiki
, jQuery
) );