1878c89d9f08524de2c15113a0844b2d902ed3c6
8 * @param {jQuery.Event} e
10 function doLivePreview( e
) {
11 var isDiff
, api
, request
, postData
, copySelectors
, section
,
12 $wikiPreview
, $wikiDiff
, $editform
, $copyElements
, $spinner
;
14 isDiff
= ( e
.target
.name
=== 'wpDiff' );
15 $wikiPreview
= $( '#wikiPreview' );
16 $wikiDiff
= $( '#wikiDiff' );
17 $editform
= $( '#editform' );
18 section
= $editform
.find( '[name="wpSection"]' ).val();
20 // Show changes for a new section is not yet supported
21 if ( isDiff
&& section
=== 'new' ) {
26 // Show #wikiPreview if it's hidden to be able to scroll to it
27 // (if it is hidden, it's also empty, so nothing changes in the rendering)
30 // Jump to where the preview will appear
31 $wikiPreview
[0].scrollIntoView();
45 $copyElements
= $( copySelectors
.join( ',' ) );
47 // Not shown during normal preview, to be removed if present
48 $( '.mw-newarticletext' ).remove();
50 $spinner
= $.createSpinner( {
54 $wikiPreview
.before( $spinner
);
56 marginTop
: $spinner
.height()
59 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
60 // (e.g. empty #catlinks)
61 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
66 uselang
: mw
.config
.get( 'wgUserLanguage' ),
67 title
: mw
.config
.get( 'wgPageName' ),
68 text
: $editform
.find( '#wpTextbox1' ).textSelection( 'getContents' ),
69 summary
: $editform
.find( '#wpSummary' ).textSelection( 'getContents' )
72 if ( section
!== '' ) {
73 postData
.sectionpreview
= '';
74 if ( section
=== 'new' ) {
75 postData
.section
= section
;
76 postData
.sectiontitle
= postData
.summary
;
83 // First PST the input, then diff it
84 postData
.onlypst
= '';
85 request
= api
.post( postData
);
86 request
.done( function ( response
) {
92 titles
: mw
.config
.get( 'wgPageName' ),
93 rvdifftotext
: response
.parse
.text
['*'],
96 if ( section
!== '' ) {
97 postData
.rvsection
= section
;
99 return api
.post( postData
).done( function ( result2
) {
101 var diffHtml
= result2
.query
.pages
[result2
.query
.pageids
[0]]
102 .revisions
[0].diff
['*'];
103 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
105 // "result.blah is undefined" error, ignore
113 $.extend( postData
, {
116 prop
: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml',
117 disableeditsection
: true
119 request
= api
.post( postData
);
120 request
.done( function ( response
) {
121 var li
, newList
, $content
, $parent
, $list
;
122 if ( response
.parse
.modules
) {
123 mw
.loader
.load( response
.parse
.modules
.concat(
124 response
.parse
.modulescripts
,
125 response
.parse
.modulestyles
,
126 response
.parse
.modulemessages
) );
128 if ( response
.parse
.displaytitle
) {
129 $( '#firstHeading' ).html( response
.parse
.displaytitle
);
130 document
.title
= mw
.msg(
133 mw
.config
.get( 'wgEditMessage', 'editing' ),
134 $( '#firstHeading' ).text()
138 if ( response
.parse
.categorieshtml
) {
139 $( '#catlinks' ).replaceWith( response
.parse
.categorieshtml
['*'] );
141 if ( response
.parse
.templates
) {
143 $.each( response
.parse
.templates
, function ( i
, template
) {
147 'href': mw
.util
.getUrl( template
['*'] ),
148 'class': ( template
.exists
!== undefined ? '' : 'new' )
150 .text( template
['*'] )
155 $editform
.find( '.mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
157 if ( response
.parse
.limitreporthtml
) {
158 $( '.limitreport' ).html( response
.parse
.limitreporthtml
['*'] );
160 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
162 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
164 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
167 'href': langlink
.url
,
168 'title': langlink
['*'] + ' - ' + langlink
.langname
,
169 'lang': langlink
.lang
,
170 'hreflang': langlink
.lang
172 .text( langlink
.autonym
)
176 $list
= $( '#p-lang ul' );
177 $parent
= $list
.parent();
178 $list
.detach().empty().append( newList
).prependTo( $parent
);
181 if ( response
.parse
.text
['*'] ) {
182 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
185 .html( response
.parse
.text
['*'] );
187 mw
.hook( 'wikipage.content' ).fire( $content
);
190 $wikiPreview
.append( $content
);
197 request
.done( function ( response
) {
198 var isSubject
= ( section
=== 'new' ),
199 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview';
200 if ( response
.parse
.parsedsummary
) {
201 $editform
.find( '.mw-summary-preview' )
204 mw
.message( summaryMsg
).parse(),
206 $( '<span>' ).addClass( 'comment' ).html(
207 // There is no equivalent to rawParams
208 mw
.message( 'parentheses' ).escaped()
209 .replace( '$1', response
.parse
.parsedsummary
['*'] )
214 request
.always( function () {
216 $copyElements
.animate( {
220 request
.fail( function ( code
, result
) {
221 var errorMsg
= 'API error: ' + code
;
222 if ( code
=== 'http' ) {
223 errorMsg
= 'HTTP error: ';
224 if ( result
.exception
) {
225 errorMsg
+= result
.exception
;
227 errorMsg
+= result
.textStatus
;
230 $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' )
232 .append( $( '<div>' )
233 .addClass( 'errorbox' )
234 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
235 .append( document
.createTextNode( errorMsg
) ) );
241 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
242 // the scripts or styles without reloading the page.
243 // Do not enable for ProofreadPage Index page editors, which have no textbox
244 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
|| $( '#wpTextbox1' ).length
=== 0 ) {
248 // The following elements can change in a preview but are not output
249 // by the server when they're empty until the preview response.
250 // TODO: Make the server output these always (in a hidden state), so we don't
251 // have to fish and (hopefully) put them in the right place (since skins
252 // can change where they are output).
254 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
255 $( '.portal:last' ).after(
259 'role': 'navigation',
260 'title': mw
.msg( 'tooltip-p-lang' ),
261 'aria-labelledby': 'p-lang-label'
263 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
264 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
268 if ( !$( '.mw-summary-preview' ).length
) {
269 $( '#wpSummary' ).after(
270 $( '<div>' ).addClass( 'mw-summary-preview' )
274 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
275 $( '#wikiPreview' ).after(
278 .attr( 'id', 'wikiDiff' )
279 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
280 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
284 // This should be moved down to '#editform', but is kept on the body for now
285 // because the LiquidThreads extension is re-using this module with only half
286 // the EditPage (doesn't include #editform presumably, bug 55463).
287 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
290 }( mediaWiki
, jQuery
) );