[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/installation/includes/js/ -> xajax.js (source)

   1  function Xajax()
   2  {
   3      if (xajaxDebug) this.DebugMessage = function(text) { alert("Xajax Debug:\n " + text) };
   4  
   5      this.workId = 'xajaxWork'+ new Date().getTime();
   6      this.depth = 0;
   7  
   8      //Get the XMLHttpRequest Object
   9      this.getRequestObject = function()
  10      {
  11          if (xajaxDebug) this.DebugMessage("Initializing Request Object..");
  12          var req;
  13          try
  14          {
  15              req=new ActiveXObject("Msxml2.XMLHTTP");
  16          }
  17          catch (e)
  18          {
  19              try
  20              {
  21                  req=new ActiveXObject("Microsoft.XMLHTTP");
  22              }
  23              catch (e2)
  24              {
  25                  req=null;
  26              }
  27          }
  28          if(!req && typeof XMLHttpRequest != "undefined")
  29              req = new XMLHttpRequest();
  30  
  31              if (xajaxDebug) {
  32                  if (!req) this.DebugMessage("Request Object Instantiation failed.");
  33              }
  34  
  35          return req;
  36      }
  37  
  38      // xajax.$() is shorthand for document.getElementById()
  39      this.$ = function(sId)
  40      {
  41          if (!sId) {
  42              return null;
  43          }
  44          var returnObj = document.getElementById(sId);
  45          if (xajaxDebug && !returnObj && sId != this.workId) {
  46              this.DebugMessage("Element with the id \"" + sId + "\" not found.");
  47          }
  48          return returnObj;
  49      }
  50  
  51      // xajax.include(sFileName) dynamically includes an external javascript file
  52      this.include = function(sFileName)
  53      {
  54          var objHead = document.getElementsByTagName('head');
  55          var objScript = document.createElement('script');
  56          objScript.type = 'text/javascript';
  57          objScript.src = sFileName;
  58          objHead[0].appendChild(objScript);
  59      }
  60  
  61      // xajax.addHandler adds an event handler to an element
  62      this.addHandler = function(sElementId, sEvent, sFunctionName)
  63      {
  64          if (window.addEventListener)
  65          {
  66              eval("this.$('"+sElementId+"').addEventListener('"+sEvent+"',"+sFunctionName+",false);");
  67          }
  68          else
  69          {
  70              eval("this.$('"+sElementId+"').attachEvent('on"+sEvent+"',"+sFunctionName+",false);");
  71          }
  72      }
  73  
  74      // xajax.removeHandler removes an event handler from an element
  75      this.removeHandler = function(sElementId, sEvent, sFunctionName)
  76      {
  77          if (window.addEventListener)
  78          {
  79              eval("this.$('"+sElementId+"').removeEventListener('"+sEvent+"',"+sFunctionName+",false);");
  80          }
  81          else
  82          {
  83              eval("this.$('"+sElementId+"').detachEvent('on"+sEvent+"',"+sFunctionName+",false);");
  84          }
  85      }
  86  
  87      // xajax.create creates a new child node under a parent
  88      this.create = function(sParentId, sTag, sId)
  89      {
  90          var objParent = this.$(sParentId);
  91          objElement = document.createElement(sTag);
  92          objElement.setAttribute('id',sId);
  93          objParent.appendChild(objElement);
  94      }
  95  
  96      // xajax.insert inserts a new node before another node
  97      this.insert = function(sBeforeId, sTag, sId)
  98      {
  99          var objSibling = this.$(sBeforeId);
 100          objElement = document.createElement(sTag);
 101          objElement.setAttribute('id',sId);
 102          objSibling.parentNode.insertBefore(objElement, objSibling);
 103      }
 104  
 105      this.getInput = function(sType, sName, sId)
 106      {
 107          var Obj;
 108          if (sType == "radio" && !window.addEventListener)
 109          {
 110              Obj = document.createElement('<input type="radio" id="'+sId+'" name="'+sName+'">');
 111          }
 112          else
 113          {
 114              Obj = document.createElement('input');
 115              Obj.setAttribute('type',sType);
 116              Obj.setAttribute('name',sName);
 117              Obj.setAttribute('id',sId);
 118          }
 119          return Obj;
 120      }
 121  
 122      // xajax.createInput creates a new input node under a parent
 123      this.createInput = function(sParentId, sType, sName, sId)
 124      {
 125          var objParent = this.$(sParentId);
 126          var objElement = this.getInput(sType, sName, sId);
 127          objParent.appendChild(objElement);
 128      }
 129  
 130      // xajax.insertInput creates a new input node before another node
 131      this.insertInput = function(sBeforeId, sType, sName, sId)
 132      {
 133          var objSibling = this.$(sBeforeId);
 134          var objElement = this.getInput(sType, sName, sId);
 135          objSibling.parentNode.insertBefore(objElement, objSibling);
 136      }
 137  
 138      // xajax.remove deletes an element
 139      this.remove = function(sId)
 140      {
 141          objElement = this.$(sId);
 142          if (objElement.parentNode && objElement.parentNode.removeChild)
 143          {
 144              objElement.parentNode.removeChild(objElement);
 145          }
 146      }
 147  
 148      //xajax.replace searches for text in an attribute of an element and replaces it
 149      //with a different text
 150      this.replace = function(sId,sAttribute,sSearch,sReplace)
 151      {
 152          var bFunction = false;
 153  
 154          if (sAttribute == "innerHTML")
 155              sSearch = this.getBrowserHTML(sSearch);
 156  
 157          eval("var txt=document.getElementById('"+sId+"')."+sAttribute);
 158          if (typeof txt == "function")
 159          {
 160              txt = txt.toString();
 161              bFunction = true;
 162          }
 163          if (txt.indexOf(sSearch)>-1)
 164          {
 165              var newTxt = '';
 166              while (txt.indexOf(sSearch) > -1)
 167              {
 168                  x = txt.indexOf(sSearch)+sSearch.length+1;
 169                  newTxt += txt.substr(0,x).replace(sSearch,sReplace);
 170                  txt = txt.substr(x,txt.length-x);
 171              }
 172              newTxt += txt;
 173              if (bFunction)
 174              {
 175                  eval("newTxt =" + newTxt);
 176                  eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
 177              }
 178              else if (this.willChange(sId,sAttribute,newTxt))
 179              {
 180                  eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
 181              }
 182          }
 183      }
 184  
 185      // xajax.getFormValues() builds a query string XML message from the elements of a form object
 186      this.getFormValues = function(frm)
 187      {
 188          var objForm;
 189          var submitDisabledElements = false;
 190          if (arguments.length > 1 && arguments[1] == true)
 191              submitDisabledElements = true;
 192  
 193          if (typeof(frm) == "string")
 194              objForm = this.$(frm);
 195          else
 196              objForm = frm;
 197          var sXml = "<xjxquery><q>";
 198          if (objForm && objForm.tagName == 'FORM')
 199          {
 200              var formElements = objForm.elements;
 201              for( var i=0; i < formElements.length; i++)
 202              {
 203                  if (formElements[i].type && (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && formElements[i].checked == false)
 204                      continue;
 205                  if (formElements[i].disabled && formElements[i].disabled == true && submitDisabledElements == false) continue;
 206                  var name = formElements[i].name;
 207                  if (name)
 208                  {
 209                      if (sXml != '<xjxquery><q>')
 210                          sXml += '&';
 211                      if(formElements[i].type=='select-multiple')
 212                      {
 213                          for (var j = 0; j < formElements[i].length; j++)
 214                          {
 215                              if (formElements[i].options[j].selected == true)   sXml += name+"="+encodeURIComponent(formElements[i].options[j].value)+"&";
 216                          }
 217                      }
 218                      else
 219                      {
 220                          sXml += name+"="+encodeURIComponent(formElements[i].value);
 221                      }
 222                  }
 223              }
 224          }
 225  
 226          sXml +="</q></xjxquery>";
 227  
 228          return sXml;
 229      }
 230  
 231      // Generates an XML message that xajax can understand from a javascript object
 232      this.objectToXML = function(obj)
 233      {
 234          var sXml = "<xjxobj>";
 235          for (i in obj)
 236          {
 237              try
 238              {
 239                  if (i == 'constructor')
 240                      continue;
 241                  if (obj[i] && typeof(obj[i]) == 'function')
 242                      continue;
 243  
 244                  var key = i;
 245                  var value = obj[i];
 246                  if (value && typeof(value)=="object" &&
 247                      (value.constructor == Array
 248                       ) && this.depth <= 50)
 249                  {
 250                      this.depth++;
 251                      value = this.objectToXML(value);
 252                      this.depth--;
 253                  }
 254  
 255                  sXml += "<e><k>"+key+"</k><v>"+value+"</v></e>";
 256  
 257              }
 258              catch(e)
 259              {
 260                  if (xajaxDebug) this.DebugMessage(e);
 261              }
 262          }
 263          sXml += "</xjxobj>";
 264  
 265          return sXml;
 266      }
 267  
 268      // Sends a XMLHttpRequest to call the specified PHP function on the server
 269      // * sRequestType is optional -- defaults to POST
 270      this.call = function(sFunction, aArgs, sRequestType)
 271      {
 272          var i,r,postData;
 273          if (document.body && xajaxWaitCursor)
 274              document.body.style.cursor = 'wait';
 275          if (xajaxStatusMessages == true) window.status = 'Sending Request...';
 276          if (xajaxDebug) this.DebugMessage("Starting xajax...");
 277          if (sRequestType == null) {
 278             var xajaxRequestType = xajaxDefinedPost;
 279          }
 280          else {
 281              var xajaxRequestType = sRequestType;
 282          }
 283          var uri = xajaxRequestUri;
 284          var value;
 285          switch(xajaxRequestType)
 286          {
 287              case xajaxDefinedGet:{
 288                  var uriGet = uri.indexOf("?")==-1?"?xajax="+encodeURIComponent(sFunction):"&xajax="+encodeURIComponent(sFunction);
 289                  if (aArgs) {
 290                      for (i = 0; i<aArgs.length; i++)
 291                      {
 292                          value = aArgs[i];
 293                          if (typeof(value)=="object")
 294                              value = this.objectToXML(value);
 295                          uriGet += "&xajaxargs[]="+encodeURIComponent(value);
 296                      }
 297                  }
 298                  uriGet += "&xajaxr=" + new Date().getTime();
 299                  uri += uriGet;
 300                  postData = null;
 301                  } break;
 302              case xajaxDefinedPost:{
 303                  postData = "xajax="+encodeURIComponent(sFunction);
 304                  postData += "&xajaxr="+new Date().getTime();
 305                  if (aArgs) {
 306                      for (i = 0; i <aArgs.length; i++)
 307                      {
 308                          value = aArgs[i];
 309                          if (typeof(value)=="object")
 310                              value = this.objectToXML(value);
 311                          postData = postData+"&xajaxargs[]="+encodeURIComponent(value);
 312                      }
 313                  }
 314                  } break;
 315              default:
 316                  alert("Illegal request type: " + xajaxRequestType); return false; break;
 317          }
 318          r = this.getRequestObject();
 319          if (!r) return false;
 320          r.open(xajaxRequestType==xajaxDefinedGet?"GET":"POST", uri, true);
 321          if (xajaxRequestType == xajaxDefinedPost)
 322          {
 323              try
 324              {
 325                  r.setRequestHeader("Method", "POST " + uri + " HTTP/1.5");
 326                  r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 327              }
 328              catch(e)
 329              {
 330                  alert("Your browser does not appear to  support asynchronous requests using POST.");
 331                  return false;
 332              }
 333          }
 334          r.onreadystatechange = function()
 335          {
 336              if (r.readyState != 4)
 337                  return;
 338  
 339              if (r.status==200)
 340              {
 341                  if (xajaxDebug && r.responseText.length < 1000) xajax.DebugMessage("Received:\n" + r.responseText);
 342                  else if (xajaxDebug) xajax.DebugMessage("Received:\n" + r.responseText.substr(0,1000)+"...\n[long response]\n...</xajax>");
 343                  if (r.responseXML)
 344                      xajax.processResponse(r.responseXML);
 345                  else {
 346                      alert("Error: the XML response that was returned from the server is invalid.");
 347                      document.body.style.cursor = 'default';
 348                      if (xajaxStatusMessages == true) window.status = 'Invalid XML response error';
 349                  }
 350              }
 351  
 352              delete r;
 353          }
 354          if (xajaxDebug) this.DebugMessage("Calling "+sFunction +" uri="+uri+" (post:"+ postData +")");
 355          r.send(postData);
 356          if (xajaxStatusMessages == true) window.status = 'Waiting for data...';
 357          delete r;
 358          return true;
 359      }
 360  
 361      //Gets the text as it would be if it were being retrieved from
 362      //the innerHTML property in the current browser
 363      this.getBrowserHTML = function(html)
 364      {
 365          tmpXajax = this.$(this.workId);
 366          if (tmpXajax == null)
 367          {
 368              tmpXajax = document.createElement("div");
 369              tmpXajax.setAttribute('id',this.workId);
 370              tmpXajax.style.display = "none";
 371              tmpXajax.style.visibility = "hidden";
 372              document.body.appendChild(tmpXajax);
 373          }
 374          tmpXajax.innerHTML = html;
 375          var browserHTML = tmpXajax.innerHTML;
 376          tmpXajax.innerHTML = '';
 377  
 378          return browserHTML;
 379      }
 380  
 381      // Tests if the new Data is the same as the extant data
 382      this.willChange = function(element, attribute, newData)
 383      {
 384          if (!document.body)
 385          {
 386              return true;
 387          }
 388          var oldData;
 389          if (attribute == "innerHTML")
 390          {
 391              newData = this.getBrowserHTML(newData);
 392          }
 393          eval("oldData=document.getElementById('"+element+"')."+attribute);
 394          if (newData != oldData)
 395              return true;
 396  
 397          return false;
 398      }
 399  
 400      //Process XML xajaxResponses returned from the request
 401      this.processResponse = function(xml)
 402      {
 403          if (xajaxStatusMessages == true) window.status = 'Processing...';
 404          var tmpXajax = null;
 405          xml = xml.documentElement;
 406          if (xml == null) {
 407              alert("Error: the XML response that was returned from the server cannot be processed.");
 408              document.body.style.cursor = 'default';
 409              if (xajaxStatusMessages == true) window.status = 'XML response processing error';
 410              return;
 411          }
 412          for (i=0; i<xml.childNodes.length; i++)
 413          {
 414              if (xml.childNodes[i].nodeName == "cmd")
 415              {
 416                  var cmd;
 417                  var id;
 418                  var property;
 419                  var data;
 420                  var search;
 421                  var type;
 422                  var before;
 423  
 424                  for (j=0; j<xml.childNodes[i].attributes.length; j++)
 425                  {
 426                      if (xml.childNodes[i].attributes[j].name == "n")
 427                      {
 428                          cmd = xml.childNodes[i].attributes[j].value;
 429                      }
 430                      if (xml.childNodes[i].attributes[j].name == "t")
 431                      {
 432                          id = xml.childNodes[i].attributes[j].value;
 433                      }
 434                      if (xml.childNodes[i].attributes[j].name == "p")
 435                      {
 436                          property = xml.childNodes[i].attributes[j].value;
 437                      }
 438                      if (xml.childNodes[i].attributes[j].name == "c")
 439                      {
 440                          type = xml.childNodes[i].attributes[j].value;
 441                      }
 442                  }
 443                  if (xml.childNodes[i].childNodes.length > 1)
 444                  {
 445                      for (j=0; j<xml.childNodes[i].childNodes.length; j++)
 446                      {
 447                          if (xml.childNodes[i].childNodes[j].nodeName == "s")
 448                          {
 449                              if (xml.childNodes[i].childNodes[j].firstChild)
 450                                  search = xml.childNodes[i].childNodes[j].firstChild.nodeValue;
 451                          }
 452                          if (xml.childNodes[i].childNodes[j].nodeName == "r")
 453                          {
 454                              if (xml.childNodes[i].childNodes[j].firstChild)
 455                                  data = xml.childNodes[i].childNodes[j].firstChild.data;
 456                          }
 457                      }
 458                  }
 459                  else if (xml.childNodes[i].firstChild)
 460                      data = xml.childNodes[i].firstChild.nodeValue;
 461                  else
 462                      data = "";
 463  
 464                  var objElement = this.$(id);
 465                  try
 466                  {
 467                      if (cmd=="al")
 468                      {
 469                          alert(data);
 470                      }
 471                      if (cmd=="js")
 472                      {
 473                          eval(data);
 474                      }
 475                      if (cmd=="in")
 476                      {
 477                          this.include(data);
 478                      }
 479                      if (cmd=="as")
 480                      {
 481                          if (this.willChange(id,property,data))
 482                          {
 483                              eval("objElement."+property+"=data;");
 484                          }
 485                      }
 486                      if (cmd=="ap")
 487                      {
 488                          eval("objElement."+property+"+=data;");
 489                      }
 490                      if (cmd=="pp")
 491                      {
 492                          eval("objElement."+property+"=data+objElement."+property);
 493                      }
 494                      if (cmd=="rp")
 495                      {
 496                          this.replace(id,property,search,data)
 497                      }
 498                      if (cmd=="rm")
 499                      {
 500                          this.remove(id);
 501                      }
 502                      if (cmd=="ce")
 503                      {
 504                          this.create(id,data,property);
 505                      }
 506                      if (cmd=="ie")
 507                      {
 508                          this.insert(id,data,property);
 509                      }
 510                      if (cmd=="ci")
 511                      {
 512                          this.createInput(id,type,data,property);
 513                      }
 514                      if (cmd=="ii")
 515                      {
 516                          this.insertInput(id,type,data,property);
 517                      }
 518                      if (cmd=="ev")
 519                      {
 520                          eval("this.$('"+id+"')."+property+"= function(){"+data+";}");
 521                      }
 522                      if (cmd=="ah")
 523                      {
 524                          this.addHandler(id, property, data);
 525                      }
 526                      if (cmd=="rh")
 527                      {
 528                          this.removeHandler(id, property, data);
 529                      }
 530                  }
 531                  catch(e)
 532                  {
 533                      alert(e);
 534                  }
 535                  delete objElement;
 536                  delete cmd;
 537                  delete id;
 538                  delete property;
 539                  delete search;
 540                  delete data;
 541                  delete type;
 542                  delete before;
 543              }
 544          }
 545          delete xml;
 546          document.body.style.cursor = 'default';
 547          if (xajaxStatusMessages == true) window.status = 'Done';
 548      }
 549  }
 550  
 551  var xajax = new Xajax();


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1