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