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