LivePreview: Let's not potentially mangle #wikiPreview
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.preview.js
1 /*!
2 * Live edit preview.
3 */
4 ( function ( mw, $ ) {
5
6 /**
7 * @ignore
8 * @param {jQuery.Event} e
9 */
10 function doLivePreview( e ) {
11 var isDiff, api, request, postData, copySelectors, section,
12 $wikiPreview, $wikiDiff, $editform, $copyElements, $spinner, $errorBox;
13
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();
20
21 // Show changes for a new section is not yet supported
22 if ( isDiff && section === 'new' ) {
23 return;
24 }
25 e.preventDefault();
26
27 // Remove any previously displayed errors
28 $errorBox.remove();
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)
31 $wikiPreview.show();
32
33 // Jump to where the preview will appear
34 $wikiPreview[0].scrollIntoView();
35
36 copySelectors = [
37 // Main
38 '#firstHeading',
39 '#wikiPreview',
40 '#wikiDiff',
41 '#catlinks',
42 '#p-lang',
43 // Editing-related
44 '.templatesUsed',
45 '.limitreport',
46 '.mw-summary-preview'
47 ];
48 $copyElements = $( copySelectors.join( ',' ) );
49
50 // Not shown during normal preview, to be removed if present
51 $( '.mw-newarticletext' ).remove();
52
53 $spinner = $.createSpinner( {
54 size: 'large',
55 type: 'block'
56 } );
57 $wikiPreview.before( $spinner );
58 $spinner.css( {
59 marginTop: $spinner.height()
60 } );
61
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' );
65
66 api = new mw.Api();
67 postData = {
68 action: 'parse',
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' )
73 };
74
75 if ( section !== '' ) {
76 postData.sectionpreview = '';
77 if ( section === 'new' ) {
78 postData.section = section;
79 postData.sectiontitle = postData.summary;
80 }
81 }
82
83 if ( isDiff ) {
84 $wikiPreview.hide();
85
86 // First PST the input, then diff it
87 postData.onlypst = '';
88 request = api.post( postData );
89 request.done( function ( response ) {
90 var postData;
91 postData = {
92 action: 'query',
93 indexpageids: '',
94 prop: 'revisions',
95 titles: mw.config.get( 'wgPageName' ),
96 rvdifftotext: response.parse.text['*'],
97 rvprop: ''
98 };
99 if ( section !== '' ) {
100 postData.rvsection = section;
101 }
102 return api.post( postData ).done( function ( result2 ) {
103 try {
104 var diffHtml = result2.query.pages[result2.query.pageids[0]]
105 .revisions[0].diff['*'];
106 $wikiDiff.find( 'table.diff tbody' ).html( diffHtml );
107 } catch ( e ) {
108 // "result.blah is undefined" error, ignore
109 mw.log.warn( e );
110 }
111 $wikiDiff.show();
112 } );
113 } );
114 } else {
115 $wikiDiff.hide();
116 $.extend( postData, {
117 pst: '',
118 preview: '',
119 prop: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml',
120 disableeditsection: true
121 } );
122 request = api.post( postData );
123 request.done( function ( response ) {
124 var li, newList, $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 ) );
130 }
131 if ( response.parse.displaytitle ) {
132 $( '#firstHeading' ).html( response.parse.displaytitle );
133 document.title = mw.msg(
134 'pagetitle',
135 mw.msg(
136 mw.config.get( 'wgEditMessage', 'editing' ),
137 $( '#firstHeading' ).text()
138 )
139 );
140 }
141 if ( response.parse.categorieshtml ) {
142 $( '#catlinks' ).replaceWith( response.parse.categorieshtml['*'] );
143 }
144 if ( response.parse.templates ) {
145 newList = [];
146 $.each( response.parse.templates, function ( i, template ) {
147 li = $( '<li>' )
148 .append( $( '<a>' )
149 .attr( {
150 'href': mw.util.getUrl( template['*'] ),
151 'class': ( template.exists !== undefined ? '' : 'new' )
152 } )
153 .text( template['*'] )
154 );
155 newList.push( li );
156 } );
157
158 $editform.find( '.mw-editfooter-list' ).detach().empty().append( newList ).appendTo( '.templatesUsed' );
159 }
160 if ( response.parse.limitreporthtml ) {
161 $( '.limitreport' ).html( response.parse.limitreporthtml['*'] );
162 }
163 if ( response.parse.langlinks && mw.config.get( 'skin' ) === 'vector' ) {
164 newList = [];
165 $.each( response.parse.langlinks, function ( i, langlink ) {
166 li = $( '<li>' )
167 .addClass( 'interlanguage-link interwiki-' + langlink.lang )
168 .append( $( '<a>' )
169 .attr( {
170 'href': langlink.url,
171 'title': langlink['*'] + ' - ' + langlink.langname,
172 'lang': langlink.lang,
173 'hreflang': langlink.lang
174 } )
175 .text( langlink.autonym )
176 );
177 newList.push( li );
178 } );
179 $list = $( '#p-lang ul' );
180 $parent = $list.parent();
181 $list.detach().empty().append( newList ).prependTo( $parent );
182 }
183
184 if ( response.parse.text['*'] ) {
185 $content = $wikiPreview.children( '.mw-content-ltr,.mw-content-rtl' );
186 $content
187 .detach()
188 .html( response.parse.text['*'] );
189
190 mw.hook( 'wikipage.content' ).fire( $content );
191
192 // Reattach
193 $wikiPreview.append( $content );
194
195 $wikiPreview.show();
196
197 }
198 } );
199 }
200 request.done( function ( response ) {
201 var isSubject = ( section === 'new' ),
202 summaryMsg = isSubject ? 'subject-preview' : 'summary-preview';
203 if ( response.parse.parsedsummary ) {
204 $editform.find( '.mw-summary-preview' )
205 .empty()
206 .append(
207 mw.message( summaryMsg ).parse(),
208 ' ',
209 $( '<span>' ).addClass( 'comment' ).html(
210 // There is no equivalent to rawParams
211 mw.message( 'parentheses' ).escaped()
212 .replace( '$1', response.parse.parsedsummary['*'] )
213 )
214 );
215 }
216 } );
217 request.always( function () {
218 $spinner.remove();
219 $copyElements.animate( {
220 opacity: 1
221 }, 'fast' );
222 } );
223 request.fail( function ( code, result ) {
224 var errorMsg = 'API error: ' + code;
225 if ( code === 'http' ) {
226 errorMsg = 'HTTP error: ';
227 if ( result.exception ) {
228 errorMsg += result.exception;
229 } else {
230 errorMsg += result.textStatus;
231 }
232 }
233 $errorBox = $( '<div>' )
234 .addClass( 'errorbox' )
235 .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
236 .append( document.createTextNode( errorMsg ) );
237 $wikiDiff.hide();
238 $wikiPreview.hide().before( $errorBox );
239 } );
240 }
241
242 $( function () {
243 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
244 // the scripts or styles without reloading the page.
245 // Do not enable for ProofreadPage Index page editors, which have no textbox
246 if ( $( '#mw-userjsyoucanpreview' ).length || $( '#mw-usercssyoucanpreview' ).length || $( '#wpTextbox1' ).length === 0 ) {
247 return;
248 }
249
250 // The following elements can change in a preview but are not output
251 // by the server when they're empty until the preview response.
252 // TODO: Make the server output these always (in a hidden state), so we don't
253 // have to fish and (hopefully) put them in the right place (since skins
254 // can change where they are output).
255
256 if ( !document.getElementById( 'p-lang' ) && document.getElementById( 'p-tb' ) && mw.config.get( 'skin' ) === 'vector' ) {
257 $( '.portal:last' ).after(
258 $( '<div>' ).attr( {
259 'class': 'portal',
260 'id': 'p-lang',
261 'role': 'navigation',
262 'title': mw.msg( 'tooltip-p-lang' ),
263 'aria-labelledby': 'p-lang-label'
264 } )
265 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw.msg( 'otherlanguages' ) ) )
266 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
267 );
268 }
269
270 if ( !$( '.mw-summary-preview' ).length ) {
271 $( '#wpSummary' ).after(
272 $( '<div>' ).addClass( 'mw-summary-preview' )
273 );
274 }
275
276 if ( !document.getElementById( 'wikiDiff' ) && document.getElementById( 'wikiPreview' ) ) {
277 $( '#wikiPreview' ).after(
278 $( '<div>' )
279 .hide()
280 .attr( 'id', 'wikiDiff' )
281 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
282 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
283 );
284 }
285
286 // This should be moved down to '#editform', but is kept on the body for now
287 // because the LiquidThreads extension is re-using this module with only half
288 // the EditPage (doesn't include #editform presumably, bug 55463).
289 $( document.body ).on( 'click', '#wpPreview, #wpDiff', doLivePreview );
290 } );
291
292 }( mediaWiki, jQuery ) );