User:F.trott/ajaxFormPreview.js

function ajaxFormPreviewInit{

if ((wgNamespaceNumber % 2 == 0) && /\.(js|css)$/.test(wgTitle)) return;

if(!document.getElementById('wikiPreview')) return;

var btnOld = document.getElementById('wpPreview') if (!btnOld || !document.getElementById('wikiPreview')) return // need preview-button and preview-placeholder

var btn = document.createElement('input') btn.type = 'button' btn.onclick = ajaxFormPreviewClick btn.id = btnOld.id btn.name = btnOld.name;

btn.value = btnOld.value; btn.title = btnOld.title;

btn.value2 = btn.value

btn.accessKey = btnOld.accessKey;

btnOld.parentNode.replaceChild(btn, btnOld); }

function ajaxFormPreviewClick{ajaxFormPreviewRun(this)}

function ajaxFormPreviewRun(btn){

var wkPreview = document.getElementById('wikiPreview') var form = document.createbox var aj = sajax_init_object var aj2 = sajax_init_object

if (!wkPreview || !form || !aj || !aj2 || !validate_all ) return

var el = document.getElementById("form_error_header") var frag=document.createElement("div");

// remove old error messages var elts = document.body.getElementsByTagName("span"); for (var i = 0; i < elts.length; ++i) if (elts[i].className == 'errorMessage') elts[i].innerHTML = '';

if (el) el.parentNode.removeChild(el);

var htm

// gray out old preview wkPreview.style.opacity = '0.3' wkPreview.style.color = 'gray'

document.body.style.cursor = 'wait'

//if (el=document.getElementById('newarticletext')) el.style.display = 'none'

btn.style.width = Math.max(btn.scrollWidth, btn.offsetWidth) + 'px'; btn.value = '...'; btn.disabled='1';

//prepare var action = document.URL; if (wgAction=='formedit') action += '&live';

var boundary = '123xyz' var data = ''

//FCKeditor visible? update free text first // if (!oFCKeditor.ready) return false;   //sajax_do_call in action - what do we do? if ( typeof FCKeditorAPI != "undefined" ) if ( showFCKEditor & RTE_VISIBLE ) {

var SRCtextarea = document.getElementById( 'free_text' );

if ( SRCtextarea ) {

var oEditorIns = FCKeditorAPI.GetInstance( 'free_text' ); SRCtextarea.value = oEditorIns.GetData( oEditorIns.Config.FormatSource );

}

}

elts = form.elements;

for (i=0; i < elts.length; ++i) { if (elts[i].name && elts[i].name != '' &&     ((elts[i].type!='submit' && elts[i].type!='radio' && elts[i].type!='checkbox') || elts[i].checked)) { addData (elts[i].name, elts[i].value); } }  //send

aj.open('POST', action, true) aj.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+boundary) aj.send(data + '--' + boundary) aj.onreadystatechange = function{

if (aj.readyState != 4) return

// Got Wikitext. Now fetch HTML...

frag.innerHTML = aj.responseText;

action = frag.getElementsByTagName("form")["editform"].action + '&live';

data = ''

elts = frag.getElementsByTagName("form")["editform"].elements;

for (i=0; i < elts.length; ++i) { if (elts[i].name && elts[i].name != '') { //alert (elts[i].tagName + " " + elts[i].type + " " + elts[i].name + ": " + elts[i].value); addData (elts[i].name, elts[i].value); }   }

// alert("Action: " + action + "\n\nWikitext was:\n" + frag.innerHTML + '\n\nFetching HTML...') aj2.open('POST', action, true) aj2.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+boundary) aj2.send(data + '--' + boundary) aj2.onreadystatechange = function{

if (aj2.readyState != 4) return

htm = aj2.responseText.replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&quot;/g,'"').replace(/&amp;/g,'&').replace(/&apos;/g,"'")

htm = htm.replace(/(.*[\n\r])*.* /,).replace(/<.preview><.livepreview>(.*[\n\r])*.*/,)

// alert(htm)

frag.innerHTML=htm; // remove calls to SMW's #info function elts = frag.getElementsByTagName("span"); for (var i = 0; i < elts.length; ++i) if (elts[i].className == 'smwttpersist') elts[i].parentNode.removeChild(elts[i]);

// remove forms elts = frag.getElementsByTagName("form"); for (var i = 0; i < elts.length; ++i) elts[i].parentNode.removeChild(elts[i]);

wkPreview.innerHTML = ''; wkPreview.appendChild(frag);

btn.value = btn.value2; btn.disabled=''; btn.blur;

wkPreview.style.opacity = ''; wkPreview.style.color = ''; wkPreview.style.display='block';

ajaxFormPreviewFinish(wkPreview);

window.scrollTo(wkPreview.offsetLeft, wkPreview.offsetTop); document.body.style.cursor = '';

} // aj2.onreadystatechange = function{

} // aj.onreadystatechange = function{

function addData(name, value){ if (!value) value = form[name] ? form[name].value : '' data += '--' + boundary + '\nContent-Disposition: form-data; name="'+name+'"\n\n' + value + '\n' } }

function ajaxFormPreviewFinish(el){ sortables_init; if (window.ajaxPreviewExec) ajaxPreviewExec(el); }

if (wgAction=='formedit' || wgPageName == 'Special:AddData' || wgPageName == 'Special:EditData') addOnloadHook(ajaxFormPreviewInit);