SyncCode I3 main.js

From MWWiki

Jump to: navigation, search

Back to WordPress Synchronizer

/**
 * Ties everything together.  This method is invoked from the beginning, and
 * multiple times by callees to indicate that their tasks have completed and the
 * next stage of processing can begin.
 */
function ajaxMain(funcName) {
  var xmlhttp = createXMLHttp();
 
  // error handling
  if (xmlhttp == null) {
    return;
  }
 
  // disable the button so it can't be submitted twice
  var btn = document.getElementById("startbtn");
  btn.href = "#";
  btn.innerHTML = "(disabled)";
 
  // set the event handler
  xmlhttp.onreadystatechange = function() {
    // only if the request is complete
    if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete") {
      var response = xmlhttp.responseText;
      eval(funcName + "Ajax" + '(response)');
    } 
  }
 
  // is there any particular request?
  var request = eval(funcName + "Init" + '()');
  if (request) {
    xmlhttp.open("GET", request, true);
    xmlhttp.send(null);
  }
}
 
/**
 * This is the intermediate step, which occurs only after ajaxMain() and all its
 * subsidiaries have finished with their work.  In essence, this is the final step.
 * Once the user clicks the "submit" button that is generated by all the other
 * method calls, this method is invoked to actually trasmit via RPC the necessary
 * blog posts to/from the local and remote blogs.
 */
function POSTrequest(elements) {
  var xmlhttp = createXMLHttp();
 
  // error handling
  if (xmlhttp == null) {
    return;
  }
 
  // disable the button so it can't be submitted twice
  var btn = document.getElementById("startbtn");
  btn.href = "#";
  btn.innerHTML = "(disabled)";
 
  // extract all the variables to send
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete") {
      document.getElementById("datacontent").innerHTML += xmlhttp.responseText + '<br />';
      var done = document.getElementById("startbtn");
      done.href = "javascript:ajaxMain('grabRemote');";
      done.innerHTML = "<b>Begin</b>";
    } 
  }
 
  // build the argument list
  var str = "elements=" + elements;
  for (i = 0; i < elements; i++) {
    var arg = "id_" + i;
    str += "&" + arg + "=" + document.getElementById(arg).value;
  }
 
  // clear out the document
  document.getElementById("datacontent").innerHTML = "Sending " + elements + " new entries (this could take a couple minutes)...<br />";
 
  // submit the request
  xmlhttp.open('POST', 'rpc.php?function=sync', true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.setRequestHeader("Content-length", str.length);
  xmlhttp.send(str); 
}
Personal tools