home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / preload.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  5.3 KB  |  145 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2. //
  3. // preload.js
  4. //
  5. // Service for adding and deleting preload calls to
  6. // the BODY/onLoad MM_preloadImages handler.
  7. //
  8. // Call from applyBehavior() and deleteBehavior(). There may be prior calls where some can be
  9. // removed and new ones added.
  10. //
  11. //--------------------------------------------------------------
  12. //   
  13. // preloadUpdate(newImagesToPreload, oldImagesPreloaded, preDeleteFlag);
  14. // -------------
  15. //    newImagesToPreload    - an array of images that may need adding to the preload call
  16. //    oldImagesPreloaded    - an array of images that may exist in preload call that should be
  17. ///                           deleted if not in newImagesToPreload array
  18. //    preDeleteFlag         - true if pre-delete (I expect there to be 1 user of this image)
  19. //                            false if post-delete (I expect there to be no users of this image)
  20. // Usage
  21. // -----
  22. //   preloadUpdate(imgArray)                  //adds images to preloader
  23. //   preloadUpdate(newImgArray,oldImgArray,1) //for applyBehavior, removes old, adds new, delete if 1 user
  24. //   preloadUpdate("",oldImgArray,0)          //for deleteBehavior, removes old, delete if 0 users
  25. //
  26.  
  27.  
  28. function preloadUpdate(newArray,oldArray,preDelete) {
  29.   if (!newArray) newArray = new Array();
  30.   if (!oldArray) oldArray = new Array();
  31.   var imgsToDel = new Array();
  32.   var i, j;
  33.  
  34.   for (i=0; i<oldArray.length; i++) { //build up images to delete
  35.     for (j=0; j<newArray.length && newArray[j]!=oldArray[i]; j++);
  36.     if (j==newArray.length) imgsToDel.push(oldArray[i]);
  37.   }
  38.   if (imgsToDel.length) preloadDel(imgsToDel,preDelete);
  39.   if (newArray.length) preloadAdd(newArray);
  40. }
  41.  
  42.  
  43.  
  44. function preloadAdd(imgArr){
  45.   var i, bodyNode = dw.getDocumentDOM().body;
  46.  
  47.   for (i=0;i<imgArr.length;i++) {
  48.     if ( !preloadImagePreloaded(bodyNode.onLoad,imgArr[i]) ){
  49.       bodyNode.onload = preloadAddImgToHandler(bodyNode.onLoad,imgArr[i]);
  50.   } }
  51. }
  52.  
  53. function preloadDel(imgArr,preDelete){
  54.    var bodyNode = dw.getDocumentDOM().body;
  55.    var entireDoc = dw.getDocumentDOM().documentElement.innerHTML;
  56.    var i, currImg, users;
  57.    
  58.    preDelete = (preDelete)? 1 : 0;
  59.    for (i=0;i<imgArr.length;i++){
  60.      currImg = imgArr[i];
  61.  
  62.      //find MM_????(???? 'imagename' ???1);
  63.      pattPreloadUsers = new RegExp("MM_\\w+\\([^)]*'" +currImg.replace(/\|/g,"\\\|")+ "'[^)]*,1\\)","g");
  64.  
  65.      if ( preloadImagePreloaded(bodyNode.onload,currImg) ){ //if already there
  66.        users = entireDoc.match(pattPreloadUsers);
  67.        if (!users || users.length <= preDelete) {    //if zero or one user of this preload (me)
  68.          bodyNode.onload = preloadDelImgFromHandler(bodyNode.onload,currImg);
  69.        }
  70.      }
  71.    }
  72. }
  73.  
  74.  
  75.  
  76. // description: determine if image is already preloaded in the onload event
  77. // of the body tag
  78.  
  79. function preloadImagePreloaded(onLoadVal,imgName){
  80.    var fnPatt = new RegExp("MM_preloadImages\\([^\\)]*\\'" + imgName + "\\'[^\\)]*" + "\\)");
  81.    return ( fnPatt.exec(onLoadVal) != null );
  82. }
  83.  
  84.  
  85. function preloadAddImgToHandler(onLoadVal,imgName){
  86.    var fnCalls, endPoint, i, currCall, bFoundPreloadCall, imgNameQuoted = "'"+imgName+"'";
  87.  
  88.    if ( onLoadVal ) fnCalls = dw.getTokens(onLoadVal,";");
  89.    if ( !fnCalls ) fnCalls = new Array();
  90.    
  91.    // go through function calls in body onload handler, and add
  92.    // the image to the first MM_preloadImages call that doesn't
  93.    // have a preload ID in it. Not adding the image
  94.    // to a function with a preload ID ensures backward compatibility
  95.    
  96.    for (i=0;i<fnCalls.length;i++){
  97.      bFoundPreloadCall = false;
  98.      currCall = fnCalls[i]; 
  99.      if ( currCall.indexOf("MM_preload") != -1 && currCall.indexOf("#") == -1  ){ // if call exists & no preload ID
  100.        if (currCall.indexOf(imgNameQuoted)==-1) { //if img not already there
  101.          //add img to this function call
  102.          endPoint = currCall.lastIndexOf(")"); // index of last )
  103.          if (endPoint != -1) {
  104.            currCall = currCall.substring(0,endPoint) + "," + imgNameQuoted + currCall.substring(endPoint);
  105.            fnCalls[i] = currCall;
  106.        } }
  107.        bFoundPreloadCall = true;
  108.        break;
  109.    } }
  110.    if ( !bFoundPreloadCall ){
  111.      fnCalls[fnCalls.length] = "MM_preloadImages(" + imgNameQuoted + ")";
  112.    }
  113.    return fnCalls.join(";");
  114. }
  115.  
  116.  
  117.  
  118. function preloadDelImgFromHandler(onLoadVal,imgName){
  119.    var fnCalls, retVal;
  120.    var fnArgs, patt, i, currCall, imgNameQuoted = "'"+imgName+"'";
  121.  
  122.    if ( onLoadVal ) fnCalls = dw.getTokens(onLoadVal,";");
  123.    if ( !fnCalls ) fnCalls = new Array();
  124.    
  125.    for (i=0;i<fnCalls.length;i++){
  126.      bFoundPreloadCall = false;
  127.      currCall = fnCalls[i]; 
  128.      if ( currCall.indexOf("MM_preloadImages") != -1 && currCall.indexOf("#") == -1  ){ // if call exists & no preload ID
  129.        if (currCall.indexOf(imgNameQuoted)!=-1) { //if img already there
  130.          patt = new RegExp(""+imgNameQuoted.replace(/\|/g,"\\\|")+"");
  131.          currCall = currCall.replace(patt,"");           //remove call
  132.          currCall = currCall.replace(/\,{2,}/, "\,");    //remove extra commas
  133.          currCall = currCall.replace(/\(\s*\,/, "\(");   //remove (,
  134.          currCall = currCall.replace(/\,\s*\)/, "\)");   //remove ,)
  135.          currCall = currCall.replace(/\s*MM_preloadImages\s*\(\s*\)\s*/, ""); //if last arg, remove function
  136.          fnCalls[i] = currCall;
  137.          break;
  138.        }
  139.      }
  140.    }
  141.    retVal = fnCalls.join(";");
  142.    retVal = retVal.replace(/;{2,}/, ";"); //remove extra ;
  143.    return retVal
  144. }
  145.