Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.preview.js
1 /*!
2 * Live edit preview.
3 */
4 ( function () {
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 $copyElements.addClass( [ 'mw-preview-copyelements', 'mw-preview-copyelements-loading' ] );
75
76 api = new mw.Api();
77 postData = {
78 formatversion: 2,
79 action: 'parse',
80 title: mw.config.get( 'wgPageName' ),
81 summary: summary.getValue(),
82 prop: ''
83 };
84
85 if ( isDiff ) {
86 $wikiPreview.hide();
87
88 if ( postData.summary ) {
89 parseRequest = api.post( postData );
90 }
91
92 diffRequest = api.post( {
93 formatversion: 2,
94 action: 'query',
95 prop: 'revisions',
96 titles: mw.config.get( 'wgPageName' ),
97 rvdifftotext: $textbox.textSelection( 'getContents' ),
98 rvdifftotextpst: true,
99 rvprop: '',
100 rvsection: section === '' ? undefined : section,
101 uselang: mw.config.get( 'wgUserLanguage' )
102 } );
103
104 // Wait for the summary before showing the diff so the page doesn't jump twice
105 $.when( diffRequest, parseRequest ).done( function ( response ) {
106 var diffHtml;
107 try {
108 diffHtml = response[ 0 ].query.pages[ 0 ]
109 .revisions[ 0 ].diff.body;
110 $wikiDiff.find( 'table.diff tbody' ).html( diffHtml );
111 mw.hook( 'wikipage.diff' ).fire( $wikiDiff.find( 'table.diff' ) );
112 } catch ( err ) {
113 // "result.blah is undefined" error, ignore
114 mw.log.warn( err );
115 }
116 $wikiDiff.show();
117 } );
118 } else {
119 $wikiDiff.hide();
120
121 $.extend( postData, {
122 prop: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
123 text: $textbox.textSelection( 'getContents' ),
124 pst: true,
125 preview: true,
126 sectionpreview: section !== '',
127 disableeditsection: true,
128 useskin: mw.config.get( 'skin' ),
129 uselang: mw.config.get( 'wgUserLanguage' )
130 } );
131 if ( section === 'new' ) {
132 postData.section = 'new';
133 postData.sectiontitle = postData.summary;
134 }
135
136 parseRequest = api.post( postData );
137 parseRequest.done( function ( response ) {
138 var newList, $displaytitle, $content, $parent, $list;
139 if ( response.parse.jsconfigvars ) {
140 mw.config.set( response.parse.jsconfigvars );
141 }
142 if ( response.parse.modules ) {
143 mw.loader.load( response.parse.modules.concat(
144 response.parse.modulestyles
145 ) );
146 }
147
148 newList = [];
149 // eslint-disable-next-line no-jquery/no-each-util
150 $.each( response.parse.indicators, function ( name, indicator ) {
151 newList.push(
152 $( '<div>' )
153 .addClass( 'mw-indicator' )
154 .attr( 'id', mw.util.escapeIdForAttribute( 'mw-indicator-' + name ) )
155 .html( indicator )
156 .get( 0 ),
157 // Add a whitespace between the <div>s because
158 // they get displayed with display: inline-block
159 document.createTextNode( '\n' )
160 );
161 } );
162 $( '.mw-indicators' ).empty().append( newList );
163
164 if ( response.parse.displaytitle ) {
165 $displaytitle = $( $.parseHTML( response.parse.displaytitle ) );
166 $( '#firstHeading' ).msg(
167 mw.config.get( 'wgEditMessage', 'editing' ),
168 $displaytitle
169 );
170 document.title = mw.msg(
171 'pagetitle',
172 mw.msg(
173 mw.config.get( 'wgEditMessage', 'editing' ),
174 $displaytitle.text()
175 )
176 );
177 }
178 if ( response.parse.categorieshtml ) {
179 $content = $( $.parseHTML( response.parse.categorieshtml ) );
180 mw.hook( 'wikipage.categories' ).fire( $content );
181 $( '.catlinks[data-mw="interface"]' ).replaceWith( $content );
182 }
183 if ( response.parse.templates ) {
184 newList = response.parse.templates.map( function ( template ) {
185 return $( '<li>' )
186 .append( $( '<a>' )
187 .attr( {
188 href: mw.util.getUrl( template.title ),
189 class: ( template.exists ? '' : 'new' )
190 } )
191 .text( template.title )
192 );
193 } );
194
195 $editform.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList ).appendTo( '.templatesUsed' );
196 }
197 if ( response.parse.limitreporthtml ) {
198 $( '.limitreport' ).html( response.parse.limitreporthtml );
199 }
200 if ( response.parse.langlinks && mw.config.get( 'skin' ) === 'vector' ) {
201 newList = response.parse.langlinks.map( function ( langlink ) {
202 var bcp47 = mw.language.bcp47( langlink.lang );
203 return $( '<li>' )
204 .addClass( 'interlanguage-link interwiki-' + langlink.lang )
205 .append( $( '<a>' )
206 .attr( {
207 href: langlink.url,
208 title: langlink.title + ' - ' + langlink.langname,
209 lang: bcp47,
210 hreflang: bcp47
211 } )
212 .text( langlink.autonym )
213 );
214 } );
215 $list = $( '#p-lang ul' );
216 $parent = $list.parent();
217 $list.detach().empty().append( newList ).prependTo( $parent );
218 }
219
220 if ( response.parse.text ) {
221 $content = $wikiPreview.children( '.mw-content-ltr,.mw-content-rtl' );
222 $content
223 .detach()
224 .html( response.parse.text );
225
226 mw.hook( 'wikipage.content' ).fire( $content );
227
228 // Reattach
229 $wikiPreview.append( $content );
230
231 $wikiPreview.show();
232 }
233 } );
234 }
235 $.when( parseRequest, diffRequest ).done( function ( parseResp ) {
236 var parse = parseResp && parseResp[ 0 ].parse,
237 isSubject = ( section === 'new' ),
238 summaryMsg = isSubject ? 'subject-preview' : 'summary-preview',
239 $summaryPreview = $editform.find( '.mw-summary-preview' ).empty();
240 if ( parse && parse.parsedsummary ) {
241 $summaryPreview.append(
242 mw.message( summaryMsg ).parse(),
243 ' ',
244 $( '<span>' ).addClass( 'comment' ).html(
245 // There is no equivalent to rawParams
246 mw.message( 'parentheses' ).escaped()
247 // .replace() use $ as start of a pattern.
248 // $$ is the pattern for '$'.
249 // The inner .replace() duplicates any $ and
250 // the outer .replace() simplifies the $$.
251 .replace( '$1', parse.parsedsummary.replace( /\$/g, '$$$$' ) )
252 )
253 );
254 }
255 mw.hook( 'wikipage.editform' ).fire( $editform );
256 } ).always( function () {
257 $spinner.hide();
258 $copyElements.removeClass( 'mw-preview-copyelements-loading' );
259 } ).fail( function ( code, result ) {
260 // This just shows the error for whatever request failed first
261 var errorMsg = 'API error: ' + code;
262 if ( code === 'http' ) {
263 errorMsg = 'HTTP error: ';
264 if ( result.exception ) {
265 errorMsg += result.exception;
266 } else {
267 errorMsg += result.textStatus;
268 }
269 }
270 $errorBox = $( '<div>' )
271 .addClass( 'errorbox' )
272 .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
273 .append( document.createTextNode( errorMsg ) );
274 $wikiDiff.hide();
275 $wikiPreview.hide().before( $errorBox );
276 } );
277 }
278
279 $( function () {
280 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
281 // the scripts or styles without reloading the page.
282 if ( $( '#mw-userjsyoucanpreview' ).length || $( '#mw-usercssyoucanpreview' ).length ) {
283 return;
284 }
285
286 // The following elements can change in a preview but are not output
287 // by the server when they're empty until the preview response.
288 // TODO: Make the server output these always (in a hidden state), so we don't
289 // have to fish and (hopefully) put them in the right place (since skins
290 // can change where they are output).
291
292 if ( !document.getElementById( 'p-lang' ) && document.getElementById( 'p-tb' ) && mw.config.get( 'skin' ) === 'vector' ) {
293 $( '.portal' ).last().after(
294 $( '<div>' ).attr( {
295 class: 'portal',
296 id: 'p-lang',
297 role: 'navigation',
298 'aria-labelledby': 'p-lang-label'
299 } )
300 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw.msg( 'otherlanguages' ) ) )
301 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
302 );
303 }
304
305 if ( !$( '.mw-summary-preview' ).length ) {
306 $( '#wpSummaryWidget' ).after(
307 $( '<div>' ).addClass( 'mw-summary-preview' )
308 );
309 }
310
311 if ( !document.getElementById( 'wikiDiff' ) && document.getElementById( 'wikiPreview' ) ) {
312 $( '#wikiPreview' ).after(
313 $( '<div>' )
314 .hide()
315 .attr( 'id', 'wikiDiff' )
316 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
317 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
318 );
319 }
320
321 // This should be moved down to '#editform', but is kept on the body for now
322 // because the LiquidThreads extension is re-using this module with only half
323 // the EditPage (doesn't include #editform presumably, T57463).
324 $( document.body ).on( 'click', '#wpPreview, #wpDiff', doLivePreview );
325 } );
326
327 }() );