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