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