summaryrefslogtreecommitdiff
path: root/skins/common/preview.js
blob: b117e85b7ba98844142a295c04ed7e1871412edc (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

function openXMLHttpRequest() {
	if( window.XMLHttpRequest ) {
		return new XMLHttpRequest();
	} else if( window.ActiveXObject && navigator.platform != 'MacPPC' ) {
		// IE/Mac has an ActiveXObject but it doesn't work.
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return null;
	}
}

/**
 * Returns true if could open the request,
 * false otherwise (eg no browser support).
 */
function livePreview(target, text, postUrl) {
	prevTarget = target;
	if( !target ) {
		window.alert('Live preview failed!\nTry normal preview.');
		var fallback = document.getElementById('wpPreview');
		if ( fallback ) { fallback.style.display = 'inline'; }
	}
	prevReq = openXMLHttpRequest();
	if( !prevReq ) return false;
	
	prevReq.onreadystatechange = updatePreviewText;
	prevReq.open("POST", postUrl, true);
	
	var postData = 'wpTextbox1=' + encodeURIComponent(text);
	prevReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	prevReq.send(postData);
	return true;
}

function updatePreviewText() {
	if( prevReq.readyState != 4 ) {
		return;
	}
	if( prevReq.status != 200 ) {
		window.alert('Failed to connect: ' + prevReq.status +
			' "' + prevReq.statusText + '"');
		var fallback = document.getElementById('wpPreview');
		if ( fallback ) { fallback.style.display = 'inline'; }
		return;
	}
	prevTarget.innerHTML = prevReq.responseText;

	/* Hide the active diff if it exists */
	var diff = document.getElementById('wikiDiff');
	if ( diff ) { diff.style.display = 'none'; }
}