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