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