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