Jump to content

User:Js/ajaxPreview.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by TheDJ (talk | contribs) at 23:16, 22 April 2010 (switched the strings around). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function ajaxPreviewInit(){
 if ((wgNamespaceNumber % 2 == 0) && /\.(js|css)$/.test(wgTitle)) return
 if (typeof ajaxPreviewKey != 'string') ajaxPreviewKey = 'p'
 if (typeof ajaxDiffKey != 'string') ajaxDiffKey  = 'v'
 ajaxPreviewPos = window.ajaxPreviewPos || 'right'

 if (ajaxPreviewPos != 'bottom'){
   var d = document.createElement('div'); d.style.cssText = 'width:100%; clear:both'
   var tNew = document.createElement('div'); tNew.style.cssText = 'float:'+ ajaxPreviewPos
   if( typeof wgWikiEditorPreferences != 'undefined' && wgWikiEditorPreferences['toolbar'] ) {
     var tOld = document.getElementById('wikiPreview');
     tOld.parentNode.insertBefore(d, tOld.nextSibling);
     tOld.parentNode.insertBefore(tNew, d.nextSibling);
   } else {
     var tOld = document.getElementById('toolbar') || document.getElementById( 'editform' );
     tOld.style.clear = 'none'
     tOld.parentNode.insertBefore(d, tOld)
     tOld.parentNode.insertBefore(tNew, tOld)
   }
 }
 addBtn(window.ajaxPreviewButton, 'wpPreview', ajaxPreviewKey)
 addBtn(window.ajaxDiffButton, 'wpDiff', ajaxDiffKey)

 function addBtn(btnName, id, akey){ 
  var btnOld = document.getElementById(id);
  if (!btnOld) return;
  var btn = document.createElement('input');
  btn.type = 'button';
  btn.onclick = ajaxPreviewClick;
  btn.id = id + 'Live';
  if (!btnName){ //extract last word from standard buttons
    var btnName = btnOld.value.split(' '); btnName = btnName[btnName.length-1];
    btnName = btnName.substring(0,1).toUpperCase() + btnName.substring(1);
  }
  btn.value = btnName;
  btn.title = btnOld.value + ' (Ajax)'
  if (ajaxPreviewPos == 'bottom'){
    btnOld.parentNode.insertBefore(btn, btnOld)
    btn.value = btnOld.value
    btnOld.value = '>'
  }else{
    btn.style.cssText = 'height:22px; padding:0 1px'
    tNew.appendChild(btn)
  }
  if (akey){ //reassign acces key
    if (btnOld.accessKey == akey){ 
      btnOld.accessKey = ''
      btnOld.title = btnOld.title.replace(tooltipAccessKeyRegexp, '')
    }
	btn.accessKey = akey
    btn.title += ' ['+tooltipAccessKeyPrefix+akey+']'
  }
  btn.value2 = btn.value
 }
}



function ajaxPreviewClick(){ajaxPreviewRun(this)}

function ajaxPreviewRun(btn){
 var wkPreview = document.getElementById('wikiPreview')
 var previewform = document.editform;
 var aj = sajax_init_object()
 if (!wkPreview || !previewform || !aj) return
 var oldHeight = wkPreview.offsetHeight
 var el;
 var htm;
 var isDiff = (btn.id=='wpDiffLive');

 wkPreview.style.opacity = '0.3';
 wkPreview.style.color = 'gray';
 document.body.style.cursor = 'wait'
 if (el=document.getElementById('wikiDiff')) el.style.display = 'none'
 if (el=document.getElementById('newarticletext')) el.style.display = 'none'
 btn.style.width = Math.max(btn.scrollWidth, btn.offsetWidth) + 'px';
 btn.value = '...'

 //prepare
 var txt = previewform.wpTextbox1.value;
 var action = wgScriptPath + "/api.php";
 var params = '';
 
 if (isDiff){
   params = "action=query&format=json&prop=revisions&rvprop=&indexpageids=&titles="+ encodeURIComponent( wgPageName );
   params += "&rvdifftotext=" + encodeURIComponent( txt );
   if (!window.ajaxPreview_CSS) ajaxPreview_CSS = importStylesheetURI('/skins-1.5/common/diff.css') 
 }else{
   params = "action=parse&format=json&pst&prop=text&title=" + encodeURIComponent( wgPageName );
   if (previewform.wpSection && previewform.wpSection.value) txt += '\n<br /><references />'
   params += "&text=" + encodeURIComponent( txt );
 }

 //send
 aj.open('POST', action, true);
 aj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 aj.onreadystatechange = function(){
  if (aj.readyState != 4) return
  wkPreview.style.display = 'block'
  var jsonObject = eval('(' + aj.responseText + ')');

  if (isDiff){
   try {
    var htm = '<h2>Changes (Live)</h2>';
    htm += '<table class="diff"><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content">';
    var index = jsonObject['query']['pageids'][0];
    htm += jsonObject['query']['pages'][index]['revisions'][0]['diff']['*'];
    htm += '</table>';
   } catch( err ) {
    var htm = 'error: ' + err;
   }
  }else{
   try {
    var htm = '<h2>Preview (Live)</h2>';
    htm += jsonObject['parse']['text']['*'];
   } catch( err ) {
    var htm = "error: "+err;
   }
  }
  wkPreview.innerHTML = htm
  btn.value = btn.value2
  btn.blur()

  if (window.ajaxPreviewScrollTop && wkPreview.scrollIntoView) wkPreview.scrollIntoView()
  else document.documentElement.scrollTop +=  wkPreview.offsetHeight - oldHeight 

  wkPreview.style.opacity = ''; wkPreview.style.color = ''; document.body.style.cursor = ''
  if (!isDiff) ajaxPreviewFinish(wkPreview)
 }
 aj.send(params);
}


function ajaxPreviewFinish(el){
 sortables_init()
 if (window.createCollapseButtons){//en.wp
   createCollapseButtons()
   createNavigationBarToggleButton()
 } 
 if (window.ajaxPreviewExec) ajaxPreviewExec(el)
}


if (wgCanonicalNamespace!='Special' && (wgAction=='edit' || wgAction=='submit'))
 addOnloadHook(ajaxPreviewInit)