8 * @param {jQuery.Event} e
10 function doLivePreview( e
) {
11 var isDiff
, api
, request
, postData
, copySelectors
, section
,
12 $wikiPreview
, $wikiDiff
, $editform
, $copyElements
, $spinner
;
16 isDiff
= ( e
.target
.name
=== 'wpDiff' );
17 $wikiPreview
= $( '#wikiPreview' );
18 $wikiDiff
= $( '#wikiDiff' );
19 $editform
= $( '#editform' );
20 section
= $editform
.find( '[name="wpSection"]' ).val();
22 // Show #wikiPreview if it's hidden to be able to scroll to it
23 // (if it is hidden, it's also empty, so nothing changes in the rendering)
26 // Jump to where the preview will appear
27 $wikiPreview
[0].scrollIntoView();
41 $copyElements
= $( copySelectors
.join( ',' ) );
43 // Not shown during normal preview, to be removed if present
44 $( '.mw-newarticletext' ).remove();
46 $spinner
= $.createSpinner( {
50 $wikiPreview
.before( $spinner
);
52 marginTop
: $spinner
.height()
55 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
56 // (e.g. empty #catlinks)
57 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
62 uselang
: mw
.config
.get( 'wgUserLanguage' ),
63 title
: mw
.config
.get( 'wgPageName' ),
64 text
: $editform
.find( '#wpTextbox1' ).textSelection( 'getContents' ),
65 summary
: $editform
.find( '#wpSummary' ).textSelection( 'getContents' )
71 // First PST the input, then diff it
72 postData
.onlypst
= '';
73 request
= api
.post( postData
);
74 request
.done( function ( response
) {
80 titles
: mw
.config
.get( 'wgPageName' ),
81 rvdifftotext
: response
.parse
.text
['*'],
84 if ( section
!== '' ) {
85 postData
.rvsection
= section
;
87 return api
.post( postData
).done( function ( result2
) {
89 var diffHtml
= result2
.query
.pages
[result2
.query
.pageids
[0]]
90 .revisions
[0].diff
['*'];
91 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
93 // "result.blah is undefined" error, ignore
101 $.extend( postData
, {
104 prop
: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml',
105 disableeditsection
: true
107 if ( section
!== '' ) {
108 postData
.sectionpreview
= '';
110 request
= api
.post( postData
);
111 request
.done( function ( response
) {
112 var li
, newList
, $next
, $parent
, $list
;
113 if ( response
.parse
.modules
) {
114 mw
.loader
.load( response
.parse
.modules
.concat(
115 response
.parse
.modulescripts
,
116 response
.parse
.modulestyles
,
117 response
.parse
.modulemessages
) );
119 if ( response
.parse
.displaytitle
) {
120 $( '#firstHeading' ).html( response
.parse
.displaytitle
);
122 if ( response
.parse
.categorieshtml
) {
123 $( '#catlinks' ).replaceWith( response
.parse
.categorieshtml
['*'] );
125 if ( response
.parse
.templates
) {
127 $.each( response
.parse
.templates
, function ( i
, template
) {
131 'href': mw
.util
.getUrl( template
['*'] ),
132 'class': ( template
.exists
!== undefined ? '' : 'new' )
134 .text( template
['*'] )
139 $editform
.find( '.mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
141 if ( response
.parse
.limitreporthtml
) {
142 $( '.limitreport' ).html( response
.parse
.limitreporthtml
['*'] );
144 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
146 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
148 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
151 'href': langlink
.url
,
152 'title': langlink
['*'] + ' - ' + langlink
.langname
,
153 'lang': langlink
.lang
,
154 'hreflang': langlink
.lang
156 .text( langlink
.autonym
)
160 $list
= $( '#p-lang ul' );
161 $parent
= $list
.parent();
162 $list
.detach().empty().append( newList
).prependTo( $parent
);
165 if ( response
.parse
.text
['*'] ) {
166 $next
= $wikiPreview
.next();
167 // If there is no next node, use parent instead.
168 // Only query parent if needed, false otherwise.
169 $parent
= !$next
.length
&& $wikiPreview
.parent();
173 .html( response
.parse
.text
['*'] );
175 mw
.hook( 'wikipage.content' ).fire( $wikiPreview
);
179 $parent
.append( $wikiPreview
);
181 $next
.before( $wikiPreview
);
188 request
.done( function ( response
) {
189 if ( response
.parse
.parsedsummary
) {
190 // TODO implement special behavior for section === 'new'
191 $editform
.find( '.mw-summary-preview' )
194 mw
.message( 'summary-preview' ).parse(),
196 $( '<span>' ).addClass( 'comment' ).html(
197 // There is no equivalent to rawParams
198 mw
.message( 'parentheses' ).escaped()
199 .replace( '$1', response
.parse
.parsedsummary
['*'] )
204 request
.always( function () {
206 $copyElements
.animate( {
213 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
214 // the scripts or styles without reloading the page.
215 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
219 // The following elements can change in a preview but are not output
220 // by the server when they're empty until the preview response.
221 // TODO: Make the server output these always (in a hidden state), so we don't
222 // have to fish and (hopefully) put them in the right place (since skins
223 // can change where they are output).
225 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
226 $( '.portal:last' ).after(
230 'role': 'navigation',
231 'title': mw
.msg( 'tooltip-p-lang' ),
232 'aria-labelledby': 'p-lang-label'
234 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
235 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
239 if ( !$( '.mw-summary-preview' ).length
) {
240 $( '.editCheckboxes' ).before(
241 $( '<div>' ).addClass( 'mw-summary-preview' )
245 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
246 $( '#wikiPreview' ).after(
248 .attr( 'id', 'wikiDiff' )
249 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
250 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
254 // This should be moved down to '#editform', but is kept on the body for now
255 // because the LiquidThreads extension is re-using this module with only half
256 // the EditPage (doesn't include #editform presumably, bug 55463).
257 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
260 }( mediaWiki
, jQuery
) );