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