9bf1352d7ce0dc60e0594d75792a6d33a0d14c99
8 * @param {jQuery.Event} e
10 function doLivePreview( e
) {
11 var isDiff
, api
, request
, postData
, copySelectors
, section
,
12 $wikiPreview
, $wikiDiff
, $editform
, $copyElements
, $spinner
;
16 // Deprecated: Use mw.hook instead
17 $( mw
).trigger( 'LivePreviewPrepare' );
19 isDiff
= ( e
.target
.name
=== 'wpDiff' );
20 $wikiPreview
= $( '#wikiPreview' );
21 $wikiDiff
= $( '#wikiDiff' );
22 $editform
= $( '#editform' );
23 section
= $editform
.find( '[name="wpSection"]' ).val();
25 // Show #wikiPreview if it's hidden to be able to scroll to it
26 // (if it is hidden, it's also empty, so nothing changes in the rendering)
29 // Jump to where the preview will appear
30 $wikiPreview
[0].scrollIntoView();
44 $copyElements
= $( copySelectors
.join( ',' ) );
46 // Not shown during normal preview, to be removed if present
47 $( '.mw-newarticletext' ).remove();
49 $spinner
= $.createSpinner( {
53 $wikiPreview
.before( $spinner
);
55 marginTop
: $spinner
.height()
58 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
59 // (e.g. empty #catlinks)
60 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
65 uselang
: mw
.config
.get( 'wgUserLanguage' ),
66 title
: mw
.config
.get( 'wgPageName' ),
67 text
: $editform
.find( '#wpTextbox1' ).val(),
68 summary
: $editform
.find( '#wpSummary' ).val()
74 // First PST the input, then diff it
75 postData
.onlypst
= '';
76 request
= api
.post( postData
);
77 request
.done( function ( response
) {
83 titles
: mw
.config
.get( 'wgPageName' ),
84 rvdifftotext
: response
.parse
.text
['*'],
87 if ( section
!== '' ) {
88 postData
.rvsection
= section
;
90 return api
.post( postData
).done( function ( result2
) {
92 var diffHtml
= result2
.query
.pages
[result2
.query
.pageids
[0]]
93 .revisions
[0].diff
['*'];
94 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
96 // "result.blah is undefined" error, ignore
104 $.extend( postData
, {
107 prop
: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml'
109 if ( section
!== '' ) {
110 postData
.sectionpreview
= '';
112 request
= api
.post( postData
);
113 request
.done( function ( response
) {
114 var li
, newList
, $next
, $parent
, $list
;
115 if ( response
.parse
.modules
) {
116 mw
.loader
.load( response
.parse
.modules
.concat(
117 response
.parse
.modulescripts
,
118 response
.parse
.modulestyles
,
119 response
.parse
.modulemessages
) );
121 if ( response
.parse
.displaytitle
) {
122 $( '#firstHeading' ).html( '<span dir="auto">' + response
.parse
.displaytitle
+ '</span>' );
124 if ( response
.parse
.categorieshtml
) {
125 $( '#catlinks' ).replaceWith( response
.parse
.categorieshtml
['*'] );
127 if ( response
.parse
.templates
) {
129 $.each( response
.parse
.templates
, function ( i
, template
) {
133 'href': mw
.util
.getUrl( template
['*'] ),
134 'class': ( template
.exists
!== undefined ? '' : 'new' )
136 .text( template
['*'] )
141 $editform
.find( '.mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
143 if ( response
.parse
.limitreporthtml
) {
144 $( '.limitreport' ).html( response
.parse
.limitreporthtml
['*'] );
146 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
148 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
150 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
153 'href': langlink
.url
,
154 'title': langlink
['*'] + ' - ' + langlink
.langname
,
155 'lang': langlink
.lang
,
156 'hreflang': langlink
.lang
158 .text( langlink
.autonym
)
162 $list
= $( '#p-lang ul' );
163 $parent
= $list
.parent();
164 $list
.detach().empty().append( newList
).prependTo( $parent
);
167 if ( response
.parse
.text
['*'] ) {
168 $next
= $wikiPreview
.next();
169 // If there is no next node, use parent instead.
170 // Only query parent if needed, false otherwise.
171 $parent
= !$next
.length
&& $wikiPreview
.parent();
175 .html( response
.parse
.text
['*'] );
177 mw
.hook( 'wikipage.content' ).fire( $wikiPreview
);
181 $parent
.append( $wikiPreview
);
183 $next
.before( $wikiPreview
);
190 request
.done( function ( response
) {
191 if ( response
.parse
.parsedsummary
) {
192 // TODO implement special behavior for section === 'new'
193 $editform
.find( '.mw-summary-preview' )
196 mw
.message( 'summary-preview' ).parse(),
198 $( '<span>' ).addClass( 'comment' ).html(
199 // There is no equivalent to rawParams
200 mw
.message( 'parentheses' ).escaped()
201 .replace( '$1', response
.parse
.parsedsummary
['*'] )
206 request
.always( function () {
208 $copyElements
.animate( {
215 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
216 // the scripts or styles without reloading the page.
217 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
221 // The following elements can change in a preview but are not output
222 // by the server when they're empty until the preview response.
223 // TODO: Make the server output these always (in a hidden state), so we don't
224 // have to fish and (hopefully) put them in the right place (since skins
225 // can change where they are output).
227 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
228 $( '.portal:last' ).after(
232 'role': 'navigation',
233 'title': mw
.msg( 'tooltip-p-lang' ),
234 'aria-labelledby': 'p-lang-label'
236 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
237 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
241 if ( !$( '.mw-summary-preview' ).length
) {
242 $( '.editCheckboxes' ).before(
243 $( '<div>' ).addClass( 'mw-summary-preview' )
247 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
248 $( '#wikiPreview' ).after(
250 .attr( 'id', 'wikiDiff' )
251 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
252 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
256 // This should be moved down to '#editform', but is kept on the body for now
257 // because the LiquidThreads extension is re-using this module with only half
258 // the EditPage (doesn't include #editform presumably, bug 55463).
259 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
262 }( mediaWiki
, jQuery
) );