home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / Rollover.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  9.7 KB  |  309 lines

  1. //
  2. // Copyright 1998, 1999, 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved. 
  3. // ----------------------------------------------------
  4. //
  5. // Rollover.js
  6. //
  7. // Implementation of Rollover Image object
  8. //
  9.  
  10. //********************* GLOBALS **************************
  11.  
  12. var helpDoc = MM.HELP_objRolloverImage;
  13.  
  14. //********************* API **************************
  15.     
  16. function commandButtons()
  17. {
  18.    return new Array( MM.BTN_OK,     "setRolloverTag()" 
  19.                    , MM.BTN_Cancel, "cancelRolloverTag()"
  20.                    , MM.BTN_Help,   "displayHelp()");
  21. }
  22.  
  23.  
  24. //**************** LOCAL FUNCTIONS *******************
  25.  
  26. // initializeUI()
  27. //
  28. // Called from rollover body onload
  29. //
  30. function initializeUI()
  31. {
  32.   document.rolloverForm.tbImageName.value = getUniqueImgName();
  33.   document.rolloverForm.tbImageName.focus();
  34.   document.rolloverForm.tbImageName.select();
  35. }
  36.  
  37.  
  38. function setRolloverTag() {
  39.    with( document.rolloverForm ) {
  40.       // Validate that we at least have an original 
  41.       // and a rollover image
  42.       //
  43.       if ( tbRolloverImage.value == "" ||
  44.            tbOriginalImage.value == "" )
  45.       {           
  46.          alert( MSG_NoImagesToInsert );
  47.          return;
  48.       }
  49.  
  50.       // We require a unique image name; the form was initialized 
  51.       // with one; if the user has deleted it or has chosen a non
  52.       // unique name, warn and regenerate one
  53.       //      
  54.       if ( tbImageName.value == "" )
  55.       {
  56.          alert( MSG_NoImageName );
  57.          tbImageName.value = getUniqueImgName();
  58.          return;      
  59.       }
  60.       else
  61.       if ( !validateUniqueImgName( tbImageName.value ) )
  62.       {
  63.          alert( MM_format( MSG_NameNotUnique, tbImageName.value ) );
  64.          tbImageName.value = getUniqueImgName();
  65.          return;      
  66.       }
  67.  
  68.       // Build an image tag with a unique name for the original image
  69.       //
  70.       var imgTagFmt     = "<img name=\"%s\" border=\"0\" src=\"%s\"%s>";
  71.       var altText       = (tbAltText.value == "")?"":" alt=\"" + tbAltText.value + "\""
  72.       var imgName       = tbImageName.value;
  73.       var imgTag        = MM_format( imgTagFmt
  74.                                    , imgName
  75.                                    , dw.doURLEncoding( tbOriginalImage.value )
  76.                                    , altText );
  77.                                    
  78.       // Quote note: the final behavior string will be applied
  79.       // by swap image applyBehavior(), which will escape URLs
  80.       // and escape-quote references.  The string we generate 
  81.       // here is passed directly to inspectBehavior(), and our
  82.       // image references must match the image references as it
  83.       // loads them in it's initializeUI.  We don't need to escape
  84.       // all quotes here to make this match, but we do on imgName
  85.       // as *unbalanced* single quotes will throw off getTokens()
  86.       // which is used by inspectBehavior() to parse this argument
  87.       // string.
  88.       //                                   
  89.  
  90.       //For DW3, we simplified calls to MM_swapImage(), just pass simple name
  91.       //var nsImageRef    = getFullImgRef( "NS 4.0", imgName );
  92.       //var ieImageRef    = getFullImgRef( "IE 4.0", imgName );
  93.  
  94.       var nsImageRef    = imgName;
  95.       var ieImageRef    = "";
  96.       
  97.       // Now build the wrapper anchor tag with the swap image
  98.       // event handlers around this image to return
  99.       //
  100.       var anchorTagFmt    = "<a href=\"%s\" onMouseOut=\"%s\" onMouseOver=\"%s\">%s</a>";
  101.       var gotoURL         = (tbURL.value == "")? "#" : dw.doURLEncoding( tbURL.value );
  102.       var swapImageFnCall = null;
  103.  
  104.       // Note that a preload flag of 1
  105.       // tells inspectBehavior() to install the proper
  106.       // preload call in the documents onload handler
  107.       //
  108.       var swapImageFmt = "MM_swapImage('%s','%s','%s',%s)";
  109.       var preloadFlag = ( cbPreloadImages.checked )? "1" : "0";
  110.       swapImageFnCall  = MM_format( swapImageFmt
  111.                                   , nsImageRef
  112.                                   , ieImageRef
  113.                                   , dw.doURLEncoding( tbRolloverImage.value )
  114.                                   , preloadFlag );
  115.       
  116.       MM.commandReturnValue = MM_format( anchorTagFmt
  117.                              , gotoURL
  118.                              , "MM_swapImgRestore()"
  119.                              , swapImageFnCall
  120.                              , imgTag );
  121.       window.close();                             
  122.    }
  123. }
  124.  
  125. function cancelRolloverTag()
  126. {
  127.    MM.commandReturnValue = "";
  128.    window.close()
  129. }
  130.  
  131. //
  132. // --------- Local Rollover functions ---------
  133. //
  134.  
  135. // getFullImgRef()
  136. //
  137. // Return a full object name reference for the given 
  138. // image name in the given browser.  The object name
  139. // returned must match the object names for images
  140. // expected by the swap image behavior
  141. //
  142. function getFullImgRef( browser, imgName )
  143. {
  144.    var framePrefix = "";
  145.    var objectName  = "document." + escQuotes( imgName ); // see quote note
  146.  
  147.    // Always use a frame prefix even when image is in current
  148.    // frame to match swap image behavior naming expectations   
  149.    //
  150.    var frameset = dw.getDocumentDOM( "parent" );
  151.    if ( frameset )
  152.    {
  153.       var i;
  154.       var current = dw.getDocumentDOM( "document" );                
  155.       var frames  = frameset.getElementsByTagName( "frame" );
  156.       for( i = 0; i < frames.length; i++ )
  157.       {
  158.          if ( current == dw.getDocumentDOM( "parent.frames[" + i + "]" ) )
  159.          {
  160.             var frameName = frames[i].getAttribute( "name" );
  161.             if ( !frameName )
  162.                framePrefix = "parent.frames[" + i + "].";
  163.             else
  164.                framePrefix = "parent.frames['" + frameName + "'].";
  165.                
  166.             break;                  
  167.          }
  168.       }
  169.    }
  170.    
  171.    // Netscape considers layers in its hierarchical
  172.    // containment/naming -- build an encompassing
  173.    // layer refs string if any around our insertion 
  174.    // point for the image
  175.    //   
  176.    if ( browser.indexOf( "NS" ) != -1 )
  177.    {
  178.       var dom        = dw.getDocumentDOM();
  179.       var selArr     = dom.getSelection();
  180.       var layerRefs  = new Array();
  181.       
  182.       buildNSLayerRefs( dom.layers, selArr[1], layerRefs );
  183.       
  184.       if ( layerRefs.length > 0 )
  185.          return( framePrefix + layerRefs.join(".") + "." + objectName );
  186.    }
  187.    
  188.    // else IE reference, or no NS layers
  189.    return( framePrefix + objectName );
  190. }
  191.  
  192. // buildNSLayerRefs()
  193. //
  194. // Given a dreamweaver document.layers array
  195. // and an insertion point offset, recurse 
  196. // encompassing layers, pushing layer references
  197. // along the way into the given array
  198. //
  199. function buildNSLayerRefs( layers, ip, layerRefs )
  200. {
  201.    var layerOffsets;
  202.    
  203.    for( var i = 0; i < layers.length; i++ )
  204.    {
  205.       layerOffsets = dw.nodeToOffsets( layers[i] );
  206.       
  207.       // ips on boundaries are not within scope of tag
  208.       if ( (ip > layerOffsets[0]) && (ip < layerOffsets[1]) )
  209.       {                                                      
  210.          // Push this layer reference; prefer ID to NAME to index
  211.          var layerRef;
  212.          var layerName = layers[i].getAttribute( "id" );
  213.          
  214.          if ( !layerName )
  215.             layerName = layers[i].getAttribute( "name" );
  216.             
  217.          if ( layerName )
  218.             layerRef = "document.layers['" + layerName + "']";
  219.          else
  220.             layerRef = "document.layers[" + i + "]";
  221.          
  222.          layerRefs.push( layerRef );
  223.  
  224.          // Dive into this layer's layers if there are any         
  225.          if ( layers[i].document.layers )
  226.             buildNSLayerRefs( layers[i].document.layers, ip, layerRefs );
  227.             
  228.          break;               
  229.       }
  230.    }
  231. }
  232.  
  233. // getUniqueImgName()
  234. //
  235. // This guy returns a unique image name that
  236. // doesn't exist as yet in the user's document.
  237. // 
  238. function getUniqueImgName()
  239. {
  240.    var frameset      = dw.getDocumentDOM( "parent" );
  241.    var existingImgs  = new Array();
  242.    var i;
  243.    
  244.    if ( frameset )
  245.    {
  246.       var nFrames = frameset.getElementsByTagName( "frame" ).length;
  247.       for( i = 0; i < nFrames; i++ )
  248.          existingImgs = existingImgs.concat( dw.getDocumentDOM( "parent.frames[" + i + "]" ).getElementsByTagName( "IMG" ) );
  249.    }
  250.    else
  251.    {
  252.       existingImgs = dw.getDocumentDOM( "document" ).getElementsByTagName( "IMG" );
  253.    }
  254.    
  255.    var   namePrefix = "Image";
  256.    var   nameIdx    = existingImgs.length + 1;
  257.    var   imgName    = namePrefix + nameIdx++;
  258.    var   bExists    = false;
  259.    
  260.    while( true )
  261.    {
  262.       bExists = false;
  263.       for( i = 0; i < existingImgs.length; i++ )
  264.       {
  265.          if ( existingImgs[i].getAttribute( "name" ) == imgName )
  266.          {
  267.             bExists = true;
  268.             break;
  269.          }
  270.       }
  271.       
  272.       if ( !bExists )
  273.          break;
  274.       
  275.       imgName = namePrefix + nameIdx++;
  276.    }
  277.  
  278.    return imgName;
  279. }
  280.  
  281. // validateUniqueImgName()
  282. //
  283. // Given an image name, return true if it is unique, 
  284. // false otherwise
  285. //
  286. function validateUniqueImgName( imgName )
  287. {
  288.    var frameset      = dw.getDocumentDOM( "parent" );
  289.    var existingImgs  = new Array();
  290.    var i;
  291.    
  292.    if ( frameset )
  293.    {
  294.       var nFrames = frameset.getElementsByTagName( "frame" ).length;
  295.       for( i = 0; i < nFrames; i++ )
  296.          existingImgs = existingImgs.concat( dw.getDocumentDOM( "parent.frames[" + i + "]" ).getElementsByTagName( "IMG" ) );
  297.    }
  298.    else
  299.    {
  300.       existingImgs = dw.getDocumentDOM( "document" ).getElementsByTagName( "IMG" );
  301.    }
  302.    
  303.    for( i = 0; i < existingImgs.length; i++ )
  304.       if ( existingImgs[i].getAttribute( "name" ) == imgName )
  305.          return( false ); // name is not unique
  306.       
  307.    return( true );
  308. }
  309.