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