jquery.accessKeyLabel: make modifier info public
[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,
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 title: mw.config.get( 'wgPageName' ),
79 summary: $summary.textSelection( 'getContents' ),
80 prop: ''
81 };
82
83 if ( isDiff ) {
84 $wikiPreview.hide();
85
86 if ( postData.summary ) {
87 parseRequest = api.post( postData );
88 }
89
90 diffRequest = api.post( {
91 action: 'query',
92 indexpageids: true,
93 prop: 'revisions',
94 titles: mw.config.get( 'wgPageName' ),
95 rvdifftotext: $textbox.textSelection( 'getContents' ),
96 rvdifftotextpst: true,
97 rvprop: '',
98 rvsection: section === '' ? undefined : section
99 } );
100
101 // Wait for the summary before showing the diff so the page doesn't jump twice
102 $.when( diffRequest, parseRequest ).done( function ( response ) {
103 var diffHtml,
104 query = response[ 0 ].query;
105 try {
106 diffHtml = query.pages[ query.pageids[ 0 ] ]
107 .revisions[ 0 ].diff[ '*' ];
108 $wikiDiff.find( 'table.diff tbody' ).html( diffHtml );
109 mw.hook( 'wikipage.diff' ).fire( $wikiDiff.find( 'table.diff' ) );
110 } catch ( e ) {
111 // "result.blah is undefined" error, ignore
112 mw.log.warn( e );
113 }
114 $wikiDiff.show();
115 } );
116 } else {
117 $wikiDiff.hide();
118
119 $.extend( postData, {
120 prop: 'text|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
121 text: $textbox.textSelection( 'getContents' ),
122 pst: true,
123 preview: true,
124 sectionpreview: section !== '',
125 disableeditsection: true,
126 uselang: mw.config.get( 'wgUserLanguage' )
127 } );
128 if ( section === 'new' ) {
129 postData.section = 'new';
130 postData.sectiontitle = postData.summary;
131 }
132
133 parseRequest = api.post( postData );
134 parseRequest.done( function ( response ) {
135 var li, newList, $displaytitle, $content, $parent, $list;
136 if ( response.parse.jsconfigvars ) {
137 mw.config.set( response.parse.jsconfigvars );
138 }
139 if ( response.parse.modules ) {
140 mw.loader.load( response.parse.modules.concat(
141 response.parse.modulescripts,
142 response.parse.modulestyles
143 ) );
144 }
145 if ( response.parse.displaytitle ) {
146 $displaytitle = $( $.parseHTML( response.parse.displaytitle ) );
147 $( '#firstHeading' ).msg(
148 mw.config.get( 'wgEditMessage', 'editing' ),
149 $displaytitle
150 );
151 document.title = mw.msg(
152 'pagetitle',
153 mw.msg(
154 mw.config.get( 'wgEditMessage', 'editing' ),
155 $displaytitle.text()
156 )
157 );
158 }
159 if ( response.parse.categorieshtml ) {
160 $( '#catlinks' ).replaceWith( response.parse.categorieshtml[ '*' ] );
161 }
162 if ( response.parse.templates ) {
163 newList = [];
164 $.each( response.parse.templates, function ( i, template ) {
165 li = $( '<li>' )
166 .append( $( '<a>' )
167 .attr( {
168 href: mw.util.getUrl( template[ '*' ] ),
169 'class': ( template.exists !== undefined ? '' : 'new' )
170 } )
171 .text( template[ '*' ] )
172 );
173 newList.push( li );
174 } );
175
176 $editform.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList ).appendTo( '.templatesUsed' );
177 }
178 if ( response.parse.limitreporthtml ) {
179 $( '.limitreport' ).html( response.parse.limitreporthtml[ '*' ] );
180 }
181 if ( response.parse.langlinks && mw.config.get( 'skin' ) === 'vector' ) {
182 newList = [];
183 $.each( response.parse.langlinks, function ( i, langlink ) {
184 li = $( '<li>' )
185 .addClass( 'interlanguage-link interwiki-' + langlink.lang )
186 .append( $( '<a>' )
187 .attr( {
188 href: langlink.url,
189 title: langlink[ '*' ] + ' - ' + langlink.langname,
190 lang: langlink.lang,
191 hreflang: langlink.lang
192 } )
193 .text( langlink.autonym )
194 );
195 newList.push( li );
196 } );
197 $list = $( '#p-lang ul' );
198 $parent = $list.parent();
199 $list.detach().empty().append( newList ).prependTo( $parent );
200 }
201
202 if ( response.parse.text[ '*' ] ) {
203 $content = $wikiPreview.children( '.mw-content-ltr,.mw-content-rtl' );
204 $content
205 .detach()
206 .html( response.parse.text[ '*' ] );
207
208 mw.hook( 'wikipage.content' ).fire( $content );
209
210 // Reattach
211 $wikiPreview.append( $content );
212
213 $wikiPreview.show();
214 }
215 } );
216 }
217 $.when( parseRequest, diffRequest ).done( function ( parseResp ) {
218 var parse = parseResp && parseResp[ 0 ].parse,
219 isSubject = ( section === 'new' ),
220 summaryMsg = isSubject ? 'subject-preview' : 'summary-preview',
221 $summaryPreview = $editform.find( '.mw-summary-preview' ).empty();
222 if ( parse && parse.parsedsummary && parse.parsedsummary[ '*' ] !== '' ) {
223 $summaryPreview.append(
224 mw.message( summaryMsg ).parse(),
225 ' ',
226 $( '<span>' ).addClass( 'comment' ).html(
227 // There is no equivalent to rawParams
228 mw.message( 'parentheses' ).escaped()
229 .replace( '$1', parse.parsedsummary[ '*' ] )
230 )
231 );
232 }
233 mw.hook( 'wikipage.editform' ).fire( $editform );
234 } ).always( function () {
235 $spinner.hide();
236 $copyElements.animate( {
237 opacity: 1
238 }, 'fast' );
239 } ).fail( function ( code, result ) {
240 // This just shows the error for whatever request failed first
241 var errorMsg = 'API error: ' + code;
242 if ( code === 'http' ) {
243 errorMsg = 'HTTP error: ';
244 if ( result.exception ) {
245 errorMsg += result.exception;
246 } else {
247 errorMsg += result.textStatus;
248 }
249 }
250 $errorBox = $( '<div>' )
251 .addClass( 'errorbox' )
252 .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
253 .append( document.createTextNode( errorMsg ) );
254 $wikiDiff.hide();
255 $wikiPreview.hide().before( $errorBox );
256 } );
257 }
258
259 $( function () {
260 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
261 // the scripts or styles without reloading the page.
262 if ( $( '#mw-userjsyoucanpreview' ).length || $( '#mw-usercssyoucanpreview' ).length ) {
263 return;
264 }
265
266 // The following elements can change in a preview but are not output
267 // by the server when they're empty until the preview response.
268 // TODO: Make the server output these always (in a hidden state), so we don't
269 // have to fish and (hopefully) put them in the right place (since skins
270 // can change where they are output).
271
272 if ( !document.getElementById( 'p-lang' ) && document.getElementById( 'p-tb' ) && mw.config.get( 'skin' ) === 'vector' ) {
273 $( '.portal:last' ).after(
274 $( '<div>' ).attr( {
275 'class': 'portal',
276 id: 'p-lang',
277 role: 'navigation',
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 ) );