/[webpac2]/trunk/web/iwf/iwfajax.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/web/iwf/iwfajax.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 48 by dpavlin, Mon Nov 14 16:15:08 2005 UTC revision 66 by dpavlin, Wed Nov 16 15:33:08 2005 UTC
# Line 1  Line 1 
1  // -----------------------------------------------------------------------------  // =========================================================================
2  // IWF - Interactive Website Framework.  Javascript library for creating  /// IWF - Interactive Website Framework.  Javascript library for creating
3  // responsive thin client interfaces.  /// responsive thin client interfaces.
4  //  ///
5  // Copyright (C) 2005 Brock Weaver brockweaver@gmail.com  /// Copyright (C) 2005 Brock Weaver brockweaver@users.sourceforge.net
6  //  ///
7  //     This library is free software; you can redistribute it and/or modify  ///     This library is free software; you can redistribute it and/or modify
8  // it under the terms of the GNU Lesser General Public License as published  /// it under the terms of the GNU Lesser General Public License as published
9  // by the Free Software Foundation; either version 2.1 of the License, or  /// by the Free Software Foundation; either version 2.1 of the License, or
10  // (at your option) any later version.  /// (at your option) any later version.
11  //  ///
12  //     This library is distributed in the hope that it will be useful, but  ///     This library is distributed in the hope that it will be useful, but
13  // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  /// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public  /// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  // License for more details.  /// License for more details.
16    ///
17    ///    You should have received a copy of the GNU Lesser General Public License
18    /// along with this library; if not, write to the Free Software Foundation,
19    /// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20    ///
21    /// Brock Weaver
22    /// brockweaver@users.sourceforge.net
23    /// 1605 NW Maple Pl
24    /// Ankeny, IA 50021
25    ///
26    //! http://iwf.sourceforge.net/
27    // --------------------------------------------------------------------------
28    //! NOTE: To minimize file size, strip all fluffy comments (except the LGPL, of course!)
29    //! using the following regex (global flag and multiline on):
30    //!            ^\t*//([^/!].*|$)
31  //  //
32  //    You should have received a copy of the GNU Lesser General Public License  // This reduces file size by about 30%, give or take.
 // along with this library; if not, write to the Free Software Foundation,  
 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
33  //  //
34  // Brock Weaver  //!  To rip out only logging statements (commented or uncommented):
35  // brockweaver@gmail.com  //!            ^/{0,2}iwfLog.*
36  // 1605 NW Maple Pl  // --------------------------------------------------------------------------
37  // Ankeny, IA 50021  
 // -----------------------------------------------------------------------------  
38    
39  // --------------------------------------------------------------------------  // --------------------------------------------------------------------------
40  // iwfajax.js  //
41    //! iwfajax.js
42  //  //
43  // Thread-safe background xml request via XmlHttpRequest object  // Thread-safe background xml request via XmlHttpRequest object
44  //  //
45  // Dependencies:  //! Dependencies:
46  // iwfxml.js  //! iwfxml.js
47  // iwfcore.js  //! iwfcore.js
48  // iwfgui.js (optional -- pretty progress bar)  //! iwfgui.js (optional -- pretty progress bar)
49  //  //
50  // Brock Weaver - brockweaver@sourceforge.net - iwf.sourceforge.net  //! Brock Weaver - brockweaver@users.sourceforge.net
51  // v 0.1 - 2005-06-05  //! v 0.2 - 2005-11-14
52  // Initial release.  //! core bug patch
 // --------------------------------------------------------------------------  
 // Known Issues:  
 //  AddToHistory does not work  
 //  Iframe not implemented  
53  // --------------------------------------------------------------------------  // --------------------------------------------------------------------------
54    //! v 0.1 - 2005-06-05
55    //! Initial release.
56    //
57    //! Known Issues:
58    //!  AddToHistory does not work
59    //!  Iframe not implemented
60    //
61    // =========================================================================
62    
63    
64    
65    
66    // show progress bar if they included iwfgui.js, window.status otherwise.
67    var _iwfShowGuiProgress = iwfExists(window.iwfShow);
68    
69    
70    
71    
72    
73    
74    // -----------------------------------
75    // Begin: Dependency Check
76    // -----------------------------------
77    
78  if (!window.iwfGetById || !window.iwfXmlDoc){  if (!window.iwfGetById || !window.iwfXmlDoc){
79          iwfLog("IWF Dependency Error: iwfajax.js is dependent upon both iwfcore.js and iwfxml.js, so you *must* reference those files first.\n\nExample:\n\n<script type='text/javascript' src='iwfcore.js'></script>\n<script type='text/javascript' src='iwfxml.js'></script>\n<script type='text/javascript' src='iwfajax.js'></script>", true);          iwfLog("IWF Dependency Error: iwfajax.js is dependent upon both iwfcore.js and iwfxml.js, so you *must* reference those files first.\n\nExample:\n\n<script type='text/javascript' src='iwfcore.js'></script>\n<script type='text/javascript' src='iwfxml.js'></script>\n<script type='text/javascript' src='iwfajax.js'></script>", true);
80  }  }
81    
82  // -----------------------------------  // -----------------------------------
83    // End: Dependency Check
84    // -----------------------------------
85    
86    // -----------------------------------
87  // Begin: AJAX Request and Response  // Begin: AJAX Request and Response
88  // -----------------------------------  // -----------------------------------
89    
# Line 62  function iwfRequest(urlOrForm, targetEle Line 98  function iwfRequest(urlOrForm, targetEle
98                  if (req.readyState == 4) {                  if (req.readyState == 4) {
99                          _iwfOnRequestEnd();                          _iwfOnRequestEnd();
100                          if (req.status == 200 || req.status == 0) {                          if (req.status == 200 || req.status == 0) {
101  iwfLog('exact response from server:\n\n' + req.responseText);  //iwfLog('exact response from server:\n\n' + req.responseText);
102                                  _iwfResponseHandler(req.responseText, localCallback, localTarget);                                  _iwfResponseHandler(req.responseText, localCallback, localTarget);
103                          } else {                          } else {
104                                  _iwfOnRequestError(req.status, req.statusText, req.responseText);                                  _iwfOnRequestError(req.status, req.statusText, req.responseText);
# Line 121  iwfLog('exact response from server:\n\n' Line 157  iwfLog('exact response from server:\n\n'
157                  }                  }
158    
159                  if (localTarget){                  if (localTarget){
160                          var elTgt = iwfGetOrCreateWithinForm(urlOrForm, 'iwfTarget', 'input', 'hidden');                          var elTgt = iwfGetOrCreateByNameWithinForm(urlOrForm, 'iwfTarget', 'input', 'hidden');
161                          if (elTgt){                          if (elTgt){
162                                  iwfAttribute(elTgt, 'value', localTarget);                                  iwfAttribute(elTgt, 'value', localTarget);
163                                  iwfRemoveAttribute(elTgt, 'disabled');                                  iwfRemoveAttribute(elTgt, 'disabled');
# Line 158  iwfLog('exact response from server:\n\n' Line 194  iwfLog('exact response from server:\n\n'
194          // prevent any browser caching of our url by requesting a unique url everytime...          // prevent any browser caching of our url by requesting a unique url everytime...
195          url += ((url.indexOf('?') > -1) ? '&' : '?') + 'iwfRequestId=' + new Date().valueOf();          url += ((url.indexOf('?') > -1) ? '&' : '?') + 'iwfRequestId=' + new Date().valueOf();
196    
197  iwfLog("url = " + url);  //iwfLog("url = " + url);
198  iwfLog("method = " + method);  //iwfLog("method = " + method);
199  iwfLog("postdata = " + postdata);  //iwfLog("postdata = " + postdata);
200  iwfLog("contenttype = " + contentType);  //iwfLog("contenttype = " + contentType);
201    
202    
203          var req = null;          var req = null;
204          if (!addToHistory){          if (!addToHistory){
205  iwfLog("using XHR to perform request...");  //iwfLog("using XHR to perform request...");
206                  // use XHR to perform the request, as this will                  // use XHR to perform the request, as this will
207                  // prevent the browser from adding it to history.                  // prevent the browser from adding it to history.
208                  req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");                  req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
# Line 229  function _iwfGetFormData(form, url, ctl) Line 265  function _iwfGetFormData(form, url, ctl)
265          // if there is a target specified in the <form> tag,          // if there is a target specified in the <form> tag,
266          // copy its contents to a hidden <input type='hidden'> tag.          // copy its contents to a hidden <input type='hidden'> tag.
267    
268  iwfLog("total elements in form named '" + form.name + "': " + form.elements.length);  //iwfLog("total elements in form named '" + form.name + "': " + form.elements.length);
269          for(var i=0;i<form.elements.length;i++){          for(var i=0;i<form.elements.length;i++){
270                  var el = form.elements[i];                  var el = form.elements[i];
271                  var nm = iwfAttribute(el, 'name');                  var nm = iwfAttribute(el, 'name');
# Line 240  iwfLog("total elements in form named '" Line 276  iwfLog("total elements in form named '"
276                                          switch(iwfAttribute(el, 'type')){                                          switch(iwfAttribute(el, 'type')){
277                                                  case 'checkbox':                                                  case 'checkbox':
278                                                  case 'radio':                                                  case 'radio':
279                                                          if (iwfAttribute(el, 'checked')){                                                          if (iwfAttribute(el, 'checked') || el.checked){
280                                                                  val = iwfAttribute(el, 'value');                                                                  val = iwfAttribute(el, 'value') || el.value;
281                                                          }                                                          }
282                                                          break;                                                          break;
283                                                  case 'button':                                                  case 'button':
# Line 275  iwfLog("total elements in form named '" Line 311  iwfLog("total elements in form named '"
311                                          }                                          }
312                                          break;                                          break;
313                                  case 'textarea':                                  case 'textarea':
314                                          val = iwfAttribute(el, 'innerText');                                          val = iwfAttribute(el, 'innerText') || el.value;
315                                          break;                                          break;
316                                  case 'button':                                  case 'button':
317                                          if (el == ctl){                                          if (el == ctl){
318                                                  val = iwfAttribute(el, 'innerText');                                                  val = iwfAttribute(el, 'innerText') || el.value;
319                                          }                                          }
320                                          break;                                          break;
321                                  case 'select':                                  case 'select':
# Line 310  iwfLog("total elements in form named '" Line 346  iwfLog("total elements in form named '"
346  }  }
347    
348  function _iwfResponseReceived(doc){  function _iwfResponseReceived(doc){
349          iwfLog('iframeloaded');  iwfLog('iframeloaded');
350          var xmlDoc = new iwfXmlDoc(doc.innerHTML);          var xmlDoc = new iwfXmlDoc(doc.innerHTML);
351  }  }
352    
# Line 333  function _iwfResponseHandler(origText, c Line 369  function _iwfResponseHandler(origText, c
369                  for(var i=0; i< doc.response.childNodes.length; i++){                  for(var i=0; i< doc.response.childNodes.length; i++){
370                          var node = doc.response.childNodes[i];                          var node = doc.response.childNodes[i];
371                          if (node.nodeName.indexOf("#") != 0){                          if (node.nodeName.indexOf("#") != 0){
372  iwfLog('node.target=' + node.target + '\ntgt=' + tgt);  //iwfLog('node.target=' + node.target + '\ntgt=' + tgt);
373                                  if (!tgt) {                                  if (!tgt) {
374                                          // server target is ignored if a client target exists.                                          // server target is ignored if a client target exists.
375                                          tgt = node.target;                                          tgt = node.target;
# Line 422  function _iwfInsertHtml(html, parentNode Line 458  function _iwfInsertHtml(html, parentNode
458                          // our html to inject contains a form node.                          // our html to inject contains a form node.
459                          // bubble up the chain until we find a <form> node, or we have no parents                          // bubble up the chain until we find a <form> node, or we have no parents
460                          var elParent = el;                          var elParent = el;
461                          while (elParent && elParent.tagName.toLowerCase() != 'form'){                          // I have doubts about adding elParent.tagName here
462                            // but I don't have better idea. -dpavlin
463                            while (elParent && elParent.tagName && elParent.tagName.toLowerCase() != 'form'){
464                                  elParent = iwfGetParent(elParent);                                  elParent = iwfGetParent(elParent);
465                          }                          }
466    
467                          if (elParent && elParent.tagName.toLowerCase() == 'form'){                          if (elParent && elParent.tagName && elParent.tagName.toLowerCase() == 'form'){
468                                  iwfLog('IWF Ajax Error: Attempting to inject html which contains a <form> node into a target element which is itself a <form> node, or is already contained by a <form> node.\nThis is bad html, and will not work appropriately on some major browsers.', true);                                  iwfLog('IWF Ajax Error: Attempting to inject html which contains a <form> node into a target element which is itself a <form> node, or is already contained by a <form> node.\nThis is bad html, and will not work appropriately on some major browsers.', true);
469                          }                          }
470                  }                  }
# Line 461  function _iwfInsertHtml(html, parentNode Line 499  function _iwfInsertHtml(html, parentNode
499    
500                          // this code is the worst hack in this entire framework.                          // this code is the worst hack in this entire framework.
501                          // the code in the try portion should work anywhere -- but                          // the code in the try portion should work anywhere -- but
502                          // ie barfs when you try to set the innerHTML on a script element.                          // IE barfs when you try to set the innerHTML on a script element.
503                          // not only that, but ie won't parse script unless a visible element                          // not only that, but IE won't parse script unless a visible element
504                          // is contained in the innerHTML when it is set, so we need the <code>ie hack here</code>                          // is contained in the innerHTML when it is set, so we need the <code>IE hack here</code>
505                          // and it cannot be removed.                          // and it cannot be removed.
506                          // I don't understand why creating a new node and setting its innerHTML causes the browsers                          // I don't understand why creating a new node and setting its innerHTML causes the browsers
507                          // to parse and execute the script, but it does.                          // to parse and execute the script, but it does.
# Line 475  function _iwfInsertHtml(html, parentNode Line 513  function _iwfInsertHtml(html, parentNode
513                          // Plus I'm getting lazy. :)                          // Plus I'm getting lazy. :)
514                          //                          //
515                          try {                          try {
516                                  // moz (DOM)                                  //! moz (DOM)
517                                  var elScript = iwfGetOrCreateById('iwfScript' + i, 'script', 'body');                                  var elScript = iwfGetOrCreateById('iwfScript' + i, 'script');
518                                  elScript.type = 'text/javascript';                                  elScript.type = 'text/javascript';
519                                  elScript.defer = 'true';                                  elScript.defer = 'true';
520                                  elScript.innerHTML = scriptHtml;                                  elScript.innerHTML = scriptHtml;
521    
522                                    iwfAppendChild('body', elScript);
523    
524                          } catch(e){                          } catch(e){
525                                  // ie hack -- need a visible tag within a non-script element to have scripting apply... Don't ask me why, ask the IE team why...  //iwfLog("IE Hack for injecting script tag...", true);
526                                    //! IE hack
527                                    // IE needs a visible tag within a non-script element to have scripting apply... Don't ask me why, ask the IE team why.
528                                    // My guess is the visible element causes the page to at least partially re-render, which in turn kicks off any script parsing
529                                    // code in IE.
530                                  var elDiv = iwfGetOrCreateById('iwfScript' + i, '<div style="display:none"></div>', 'body');                                  var elDiv = iwfGetOrCreateById('iwfScript' + i, '<div style="display:none"></div>', 'body');
531                                  elDiv.innerHTML = '<code>ie hack here</code><script id="iwfScript' + i + '" defer="true">' + scriptHtml + '</script' + '>';                                  elDiv.innerHTML = '<code>IE hack here</code><script id="iwfScript' + i + '" defer="true">' + scriptHtml + '</script' + '>';
532                          }                          }
533    
534                          i++;                          i++;
# Line 496  function _iwfInsertHtml(html, parentNode Line 540  function _iwfInsertHtml(html, parentNode
540    
541  function iwfCleanScripts(){  function iwfCleanScripts(){
542          var i = 0;          var i = 0;
543          while((el = iwfGetById('iwfScript' + i))){          while(iwfRemoveNode('iwfScript' + (i++)));
544                  iwfRemoveNode(el);  
                 i++;  
         }  
545  }  }
546    
547    
# Line 509  var _iwfPendingRequests = 0; Line 551  var _iwfPendingRequests = 0;
551  var _iwfRequestTicker = null;  var _iwfRequestTicker = null;
552  var _iwfRequestTickCount = 0;  var _iwfRequestTickCount = 0;
553  var _iwfRequestTickDuration = 100;  var _iwfRequestTickDuration = 100;
554    
555    
556    // TODO: make the styles applied be configurable variables at the top of this file.
557    function _iwfGetBusyBar(inner){
558            var b = iwfGetById('iwfBusy');
559            if (!b){
560                    b = iwfGetOrCreateById('iwfBusy', 'div', 'body');
561                    if(b.style){
562                            b.style.position = 'absolute';
563                            b.style.border = '1px solid black';
564                            b.style.backgroundColor = '#efefef';
565                            b.style.textAlign = 'center';
566                    }
567                    iwfWidth(b, 100);
568                    iwfHeight(b, 20);
569                    iwfZIndex(b, 9999);
570    
571                    iwfX(b, 0);
572                    iwfY(b, 0);
573    
574    //              iwfX(b, iwfClientWidth() - iwfWidth(b)-5);
575    //              iwfY(b, iwfClientHeight() - iwfHeight(b)-5);
576    
577                    iwfHide(b);
578            }
579    
580    
581    
582            var bb = iwfGetById('iwfBusyBar');
583            if(!bb){
584                    bb = iwfGetOrCreateById('iwfBusyBar', 'div', b);
585                    bb.style.backgroundColor = 'navy';
586                    bb.style.color = 'white';
587                    bb.style.textAlign = 'center';
588                    iwfWidth(bb, 1);
589                    iwfHeight(bb, 20);
590                    iwfX(bb, 0);
591                    iwfY(bb, 0);
592            }
593    
594            if(inner){
595                    return bb;
596            } else {
597                    return b;
598            }
599    
600    }
601    
602  function _iwfOnRequestStart(){  function _iwfOnRequestStart(){
603          _iwfPendingRequests++;          _iwfPendingRequests++;
604          _iwfTotalRequests++;          _iwfTotalRequests++;
# Line 519  function _iwfOnRequestStart(){ Line 609  function _iwfOnRequestStart(){
609                  _iwfRequestTickCount = 0;                  _iwfRequestTickCount = 0;
610                  if (window.iwfOnRequestStart){                  if (window.iwfOnRequestStart){
611                          _iwfRequestTickDuration = iwfOnRequestStart();                          _iwfRequestTickDuration = iwfOnRequestStart();
612                    } else if (_iwfShowGuiProgress) {
613                            // use gui busy implementation
614                            var bb = _iwfGetBusyBar(true);
615                            iwfWidth(bb, 1);
616                            bb.innerHTML = '0%';
617                            iwfShow(_iwfGetBusyBar(false));
618                  } else {                  } else {
619                          // use default busy implementation...                          // use default busy implementation...
                         // TODO: make this better!  
620                          window.status = 'busy.';                          window.status = 'busy.';
621                  }                  }
622                  if (!_iwfRequestTickDuration){                  if (!_iwfRequestTickDuration){
# Line 536  function _iwfOnRequestTick(){ Line 631  function _iwfOnRequestTick(){
631          if (window.iwfOnRequestTick){          if (window.iwfOnRequestTick){
632                  iwfOnRequestTick(_iwfRequestTickCount, _iwfRequestTickDuration, _iwfPendingRequests);                  iwfOnRequestTick(_iwfRequestTickCount, _iwfRequestTickDuration, _iwfPendingRequests);
633          } else if (!window.iwfOnRequestStart) {          } else if (!window.iwfOnRequestStart) {
634                  // use default busy implementation...                  if (_iwfShowGuiProgress) {
635                  // TODO: make this better!                          // use gui busy implementation
636                  window.status = 'busy...............................................'.substr(0, (_iwfRequestTickCount % 45) + 5);                          var bar = _iwfGetBusyBar(true);
637                            if(bar){
638                                    var w = iwfWidth(bar) + 1;
639                                    if (w > 95){
640                                            w = 95;
641                                    }
642                                    iwfWidth(bar, w);
643                                    bar.innerHTML = "loading " + iwfIntFormat(w) + "%";
644                            }
645                    } else {
646                            // use default busy implementation...
647                            window.status = 'busy...............................................'.substr(0, (_iwfRequestTickCount % 45) + 5);
648                    }
649          } else {          } else {
650                  // they didn't define a tick function,                  // they didn't define a tick function,
651                  // but they did define a start one, so do nothing.                  // but they did define a start one, so do nothing.
# Line 555  function _iwfOnRequestEnd(){ Line 662  function _iwfOnRequestEnd(){
662                  if (window.iwfOnRequestEnd){                  if (window.iwfOnRequestEnd){
663                          iwfOnRequestEnd();                          iwfOnRequestEnd();
664                  } else if (!window.iwfOnRequestStart) {                  } else if (!window.iwfOnRequestStart) {
665                          // use default busy implementation...                          if (_iwfShowGuiProgress) {
666                          // TODO: make this better!                          // use gui busy implementation
667                          window.status = 'done.';                                  var bar = _iwfGetBusyBar(true);
668                                    if(bar){
669                                            iwfWidth(bar, 100);
670                                            bar.innerHTML = "Done";
671                                            iwfHideGentlyDelay(_iwfGetBusyBar(false), 15, 500);
672                                    }
673                            } else {
674                                    // use default busy implementation...
675                                    window.status = 'done.';
676                            }
677                  } else {                  } else {
678                          // they didn't define an end function,                          // they didn't define an end function,
679                          // but they did define a start one, so do nothing.                          // but they did define a start one, so do nothing.
# Line 567  function _iwfOnRequestEnd(){ Line 683  function _iwfOnRequestEnd(){
683                  if (window.iwfOnRequestProgress){                  if (window.iwfOnRequestProgress){
684                          iwfOnRequestProgress(_iwfPendingRequests, _iwfTotalRequests);                          iwfOnRequestProgress(_iwfPendingRequests, _iwfTotalRequests);
685                  } else if (!window.iwfOnRequestStart) {                  } else if (!window.iwfOnRequestStart) {
686                          // use default busy implementation...                          if (_iwfShowGuiProgress) {
687                          // TODO: make this better!                          // use gui busy implementation
688                          window.status = 'Remaining: ' + _iwfPendingRequests;                                  var pct = (1 - (_iwfPendingRequests/_iwfTotalRequests)) * 100;
689                                    if (pct > 100){
690                                            pct = 100;
691                                    }
692                                    var bar = _iwfGetBusyBar(true);
693                                    if(bar){
694                                            iwfWidth(bar, pct);
695                                            bar.innerHTML = "loading " + iwfIntFormat(pct) + "%";
696                                    }
697                            } else {
698                                    // use default busy implementation...
699                                    window.status = 'Remaining: ' + _iwfPendingRequests;
700                            }
701                  } else {                  } else {
702                          // they didn't define an end function,                          // they didn't define an end function,
703                          // but they did define a start one, so do nothing.                          // but they did define a start one, so do nothing.

Legend:
Removed from v.48  
changed lines
  Added in v.66

  ViewVC Help
Powered by ViewVC 1.1.26