Add a "loading" spinner for live preview
[lhc/web/wiklou.git] / skins / common / preview.js
1 /**
2 * Live preview script for MediaWiki
3 */
4
5 function setupLivePreview() {
6 var livePreviewButton = $j('#wpLivePreview');
7
8 livePreviewButton.click( doLivePreview );
9 }
10
11 function doLivePreview( e ) {
12 e.preventDefault();
13 var previewText = $j('#wpTextbox1').val();
14 var postData = { 'action' : 'submit', 'wpTextbox1' : previewText, 'wpPreview' : true,
15 'title' : wgPageName };
16
17 // Hide active diff, used templates, old preview if shown
18 $j('#wikiDiff').slideUp();
19 $j('#wikiPreview').slideUp();
20 $j('.templatesUsed').slideUp();
21 $j('.hiddencats').slideUp();
22
23 // Display a loading graphic
24 var loadSpinner = $j('<div class="mw-ajax-loader"/>');
25 $j('#wikiPreview').before( loadSpinner );
26
27 var page = $j('<html/>');
28 page.load( wgScript+'?action=submit',
29 postData,
30 function() {
31 var copyElements = ['#wikiPreview', '.templatesUsed', '.hiddencats',
32 '#catlinks'];
33
34 for( var i=0; i<copyElements.length; ++i) {
35 // For all the specified elements, find the elements in the loaded page
36 // and the real page, empty the element in the real page, and fill it
37 // with the content of the loaded page
38 var copyContent = page.find( copyElements[i] ).contents();
39 $j(copyElements[i]).empty().append( copyContent );
40 var newClasses = page.find( copyElements[i] ).attr('class');
41 $j(copyElements[i]).attr( 'class', newClasses );
42 }
43
44 loadSpinner.remove();
45
46 $j('#wikiPreview').slideDown();
47 } );
48 }
49
50 js2AddOnloadHook( setupLivePreview );