summaryrefslogtreecommitdiff
path: root/skins/common/preview.js
blob: db5633d0dd219680aa4665145dd61a86f2dc2f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * Live preview script for MediaWiki
 */

function doLivePreview( e ) {
	e.preventDefault();
	var previewText = $j('#wpTextbox1').val();

	var editToken = $j( '[name="wpEditToken"]' ).attr( 'value' );
	var editTime = $j( '[name="wpEdittime"]' ).attr( 'value' );
	var startTime = $j( '[name="wpStarttime"]' ).attr( 'value' );

	var postData = { 'action' : 'submit', 'wpTextbox1' : previewText, 'wpPreview' : true,
		'wpEditToken' : editToken, 'wpEdittime': editTime, 'wpStarttime': startTime, 'title' : wgPageName };
	
	// Hide active diff, used templates, old preview if shown
	var copyElements = ['#wikiPreview', '.templatesUsed', '.hiddencats',
						'#catlinks'];
	var copySelector = copyElements.join(',');

	$j.each( copyElements, function(k,v) { $j(v).fadeOut('fast'); } );
	
	// Display a loading graphic
	var loadSpinner = $j('<div class="mw-ajax-loader"/>');
	$j('#wikiPreview').before( loadSpinner );
	
	var page = $j('<div/>');
	page.load( wgScript+'?action=submit '+copySelector,
				postData,
		function() {
			
			for( var i=0; i<copyElements.length; ++i) {
				// For all the specified elements, find the elements in the loaded page
				//  and the real page, empty the element in the real page, and fill it
				//  with the content of the loaded page
				var copyContent = page.find( copyElements[i] ).contents();
				$j(copyElements[i]).empty().append( copyContent );
				var newClasses = page.find( copyElements[i] ).attr('class');
				$j(copyElements[i]).attr( 'class', newClasses );
			}
			
			$j.each( copyElements, function(k,v) {
				// Don't belligerently show elements that are supposed to be hidden
				$j(v).fadeIn( 'fast', function() { $j(this).css('display', ''); } );
			} );
			
			loadSpinner.remove();
		} );
}

$j(document).ready( function() {
	$j('#wpPreview').click( doLivePreview );
} );