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