Bruger:Rodejong/ajaxpreview.js

I denne artikel skal vi dykke ned i emnet Bruger:Rodejong/ajaxpreview.js, som har fanget opmærksomhed fra akademikere, eksperter og den brede offentlighed på grund af dets relevans i dag. Fra dets oprindelse til dets implikationer på forskellige områder, har Bruger:Rodejong/ajaxpreview.js været genstand for debat og undersøgelse, hvilket har genereret forskellige typer meninger og perspektiver, der beriger det aktuelle panorama. Gennem en detaljeret analyse sigter vi mod at give læseren en bred og komplet vision af Bruger:Rodejong/ajaxpreview.js, der adresserer dets mest relevante aspekter for at kaste lys over dette emne af stor interesse.
//
// Preview gebaseerd op Ajax.
//
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 tOld = document.getElementById('toolbar') || document.getElementById('wpTextbox1')
   tOld.style.clear = 'none'
   var d = document.createElement('div'); d.style.cssText = 'width:100%; clear:both'
   tOld.parentNode.insertBefore(d, tOld)
   var tNew = document.createElement('div'); tNew.style.cssText = 'float:'+ ajaxPreviewPos
   tOld.parentNode.insertBefore(tNew, tOld)
 }
 addBtn(window.ajaxPreviewButton, 'wpPreview', ajaxPreviewKey)
 addBtn(window.ajaxDiffButton, 'wpDiff', ajaxDiffKey)
 function addBtn(name, 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 (!name){ //extract last word from standard buttons
    name = btnOld.value.split(' '); name = name
    name = name.substring(0,1).toUpperCase() + name.substring(1) 
  }
  btn.value = name;  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.setAttribute('accesskey', '')
      btnOld.title = btnOld.title.replace(tooltipAccessKeyRegexp, '')
    }
    //btn.setAttribute('accesskey', akey)
	btn.accessKey = akey
    btn.title += ' '
  }
  btn.value2 = btn.value
 }
}
//
// Preview Click
//
function ajaxPreviewClick(){ajaxPreviewRun(this)}

//
// Preview Run
//
function ajaxPreviewRun(btn){
 var wkPreview = document.getElementById('wikiPreview'), form = document.editform
 var aj = sajax_init_object()
 if (!wkPreview || !form || !aj) return
 var oldHeight = wkPreview.offsetHeight
 var el, htm, 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 = form.wpTextbox1.value, action = form.action
 var boundary = '--------123xyz', data = ''
 if (isDiff){
   addData('wpDiff', ''); addData('wpStarttime'); addData('wpEdittime')
   if (!window.ajaxPreview_CSS) ajaxPreview_CSS = importStylesheetURI('/skins-1.5/common/diff.css') 
 }else{
   action += '&live'
   if (form.wpSection && form.wpSection.value) txt += '\n<br /><references />'
 } 
 addData('wpTextbox1', txt); addData('wpSection'); addData('wpSummary')
 //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
  wkPreview.style.display = 'block'
  if (isDiff){
    var htm = aj.responseText
    var p1 = htm.indexOf("<table class='diff'>" )
    var p2 = htm.indexOf('</table>', p1)
    htm = (p1!=-1 && p2!=-1) ? htm.substring(p1, p2+8) : 'Error'
  }else{
    htm = aj.responseText.replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&quot;/g,'"').replace(/&amp;/g,'&').replace(/&apos;/g,"'")
  }
  wkPreview.innerHTML = htm
  btn.value = btn.value2
  btn.blur()
 
  if (el = wkPreview.getElementsByTagName('h2')){
    if (el.style.textAlign != 'right') el.innerHML +=  ' (Ajax)'
    el.style.textAlign = 'right'
  }
 
  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)
 }
 
 function addData(name, value){
   if (!value) value = form ? form.value : ''
   data += '--' + boundary + '\nContent-Disposition: form-data; name="'+name+'"\n\n' + value + '\n'
 }
}
 
//
// Preview Finish
//
function ajaxPreviewFinish(el){
 sortables_init()
 if (window.createCollapseButtons){//en.wp
   createCollapseButtons()
   createNavigationBarToggleButton()
 } 
 if (window.ajaxPreviewExec) ajaxPreviewExec(el)
}
 
if (wgAction=='edit' || wgAction=='submit') addOnloadHook(ajaxPreviewInit)