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

  1. //
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3. //
  4. // docInfo.js
  5. //
  6. // Provides information about the user's document.
  7. //
  8. //--------------------------------------------------------------
  9. //   
  10. //
  11. //getAllObjectRefs(browserType,tagName){
  12. //getAllObjectTags(tagName){
  13. //getSelectedObj(){
  14. //createUniqueName(tagName,tagString,arrToSearch){ 
  15. //makeUniqueName(tag, baseName) {
  16.  
  17.  
  18. var REF_UNNAMED     = "unnamed <";     //this is what getObjectRefs() returns for unnamed objects;
  19. var REF_CANNOT      = "cannot reference <"; //objects that cannot be referenced
  20.  
  21.  
  22. //Passed a browser type and one or more tags, returns an array of object
  23. //references for the tag(s) and browser type. If the document is in a
  24. //frameset, returns references in all of the frames of the parent frameset.
  25.  
  26. function getAllObjectRefs(browserType,tagName){
  27.   var refsArray = new Array(),theFrame,frameName,Tag,i,j;
  28.   var frameList = dw.getObjectRefs("NS 4.0","parent","frame"); //get list of frames
  29.   var numArgs = arguments.length;
  30.   for (i=1;i<numArgs;i++){
  31.     Tag = arguments[i]
  32.     if (frameList && frameList.length>0) { //if frames
  33.       for (j=0; j<frameList.length; j++) {
  34.         if (frameList[j].indexOf(REF_UNNAMED) != -1) theFrame = "parent.frames[" + j + "]";
  35.         else {
  36.           //check for duplicately named frames by checking numer of ['s in name
  37.           //for instance,parent.frames['name'][0] would have 2
  38.           if (frameList[j].indexOf("[")!=frameList[j].lastIndexOf("["))
  39.             frameList[j] = frameList[j].substring(0,frameList[j].indexOf("]") + 1)  
  40.           theFrame = frameList[j];
  41.         }  
  42.         //if the frame is the current document, use simple ref
  43.         if (dw.getDocumentDOM(theFrame) == dw.getDocumentDOM("document")) theFrame = "document";
  44.         refsArray = refsArray.concat(dw.getObjectRefs(browserType,theFrame,Tag));
  45.       }
  46.     } else 
  47.       refsArray = refsArray.concat(dw.getObjectRefs(browserType,"document",Tag)); 
  48.   }
  49.   return refsArray;
  50. }
  51.  
  52.  
  53. //Passed a tagName, returns an array of all tags of tagName. If the document is in a frameset,
  54. //the array includes all of the tags in all of the frames of the document's parent.
  55.  
  56. function getAllObjectTags(tagName){
  57.   var tagsArray = new Array(),theFrame,frameName,i;
  58.   var frameList = dw.getObjectRefs("NS 4.0","parent","frame"); //get list of frames
  59.   if (frameList && frameList.length>0) { //if frames
  60.     for (i=0;i<frameList.length; i++){
  61.       if (frameList[i].indexOf(REF_UNNAMED)!=-1) theFrame = "parent.frames["+i+"]";
  62.       else {
  63.         //check for duplicately named frames by checking numer of ['s in name
  64.         //for instance,parent.frames['name'][0] would have 2
  65.         if (frameList[i].indexOf("[")!=frameList[i].lastIndexOf("["))
  66.           frameList[i] = frameList[i].substring(0,frameList[i].indexOf("]") + 1)  
  67.         theFrame = frameList[i];
  68.       }  
  69.       tagsArray = tagsArray.concat(dw.getObjectTags(theFrame,tagName));  
  70.     }  
  71.   } else tagsArray = tagsArray.concat(dw.getObjectTags("document",tagName));
  72.   return tagsArray;
  73. }
  74.   
  75.  
  76. //function: getSelectedObj
  77. //description: returns the currently selected object
  78.  
  79. function getSelectedObj(){
  80.    var currSel = dreamweaver.getSelection();
  81.    return dreamweaver.offsetsToNode(currSel[0],currSel[1]);
  82. }
  83.  
  84.  
  85. //Creates a unique name for objs of tagName, using tagString
  86. //for instance: if tagString = Image, returns a name like Image1
  87. function createUniqueName(tagName,tagString,arrToSearch){ 
  88.   var frameListSize,dupe=true,counter=1;
  89.   var objsArray=(createUniqueName.arguments.length == 3)?arrToSearch:
  90.           dreamweaver.getDocumentDOM('document').getElementsByTagName(tagName);
  91.   var objsArrayLen = objsArray.length;
  92.   var objName = tagString + counter;
  93.   
  94.   while (dupe==true){ //check new name against name of all other tagName objs
  95.     dupe=false;
  96.     objName = tagString + counter++; 
  97.     //iterates through possible names: tagName1, then tagName2, etc.
  98.     for (i=0;dupe==false && i<objsArrayLen;i++){
  99.       //if another object of this type has the same name
  100.       if (objsArray[i].getAttribute("name") == objName) 
  101.         dupe=true; //then repeat the loop, trying a new name
  102.     }
  103.   }
  104.   return objName; //return new name 
  105.     
  106. }
  107.  
  108. //Given a tag and a base name (or id), generates a unique name.
  109. //For ex: makeUniqueName("IMG","myImage") returns myImage1, myImage2 etc.
  110.  
  111. function makeUniqueName(tag, baseName) {
  112.   var objArray,tagCounter=1,i,possName,name,DOM,nameList="";
  113.  
  114.   possName = baseName + tagCounter++;
  115.   DOM = dreamweaver.getDocumentDOM("document");
  116.   objArray = DOM.body.getElementsByTagName(tag.toUpperCase());
  117.   if (objArray.length > 0) { //other images, check
  118.     for (i=0; i<objArray.length; i++) { //create list of all img names
  119.       name = objArray[i].getAttribute("name"); if (name) nameList += " " + name + " ";
  120.       name = objArray[i].getAttribute("id"); if (name) nameList += " " + name + " ";
  121.     }
  122.     while (nameList.indexOf(possName) != -1) possName = baseName+tagCounter++; //find 1st avail
  123.   }
  124.   return possName;
  125. }
  126.  
  127.  
  128. // Searches for a tag wrapping the current selection.
  129. // Returns true if the tag found. False if not found.
  130. function selectionInsideTag (tagName) {
  131.   var rtnBool = false;
  132.   var curObj = dw.getDocumentDOM('document').getSelectedNode();
  133.   
  134.   if (curObj.nodeType == Node.TEXT_NODE) {
  135.     curObj = curObj.parentNode; // Check the tag wrapping the current tag.
  136.   }
  137.   
  138.   // Look up the tree of tags (will be element nodes until document reached.
  139.   while (curObj.nodeType == Node.ELEMENT_NODE) {
  140.     if (curObj.tagName.toUpperCase() == tagName.toUpperCase()) {
  141.       rtnBool = true; // Found the tag, set return value true.
  142.       break; // No need to look further.
  143.     }
  144.     curObj = curObj.parentNode; // Check the tag wrapping the current tag.
  145.   }
  146.   return rtnBool;
  147. }
  148.