home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 89 / PIWD89.iso / pc / CONTENTS / DREAMWEAVER / UTILITIES / pawluk / FRAMESTUFFER / FRAMESTUFFER.JS < prev    next >
Text File  |  2003-10-05  |  11KB  |  309 lines

  1. // Hal Pawluk 1999-2003    http://www.pawluk.com/public/
  2.  
  3.     var pageDOM="";
  4.     var Myscript = "";
  5.     var itsHere = false;        // tests for script in page
  6.     var itsThere = false;    // tests for script in frameset
  7.     var thisFrameset = "";
  8.     var thisFrame = "";
  9.     var preFix="";
  10.     var activePage="";
  11.  
  12.     function isItHere(){        // called by onLoad in body tag
  13.         checkForDWfile();
  14.         pageDOM = dreamweaver.getDocumentDOM("document");
  15.         activePage = fileOnly(pageDOM.URL);
  16.         var theHeadNode = pageDOM.getElementsByTagName("HEAD");
  17.         if (theHeadNode.item(0).innerHTML.toLowerCase().indexOf(end.toLowerCase()) != -1){
  18.  
  19.             //--    look up frameset and frame names specified
  20.             var allScripts = pageDOM.getElementsByTagName("SCRIPT");
  21.             var jamScript = "";
  22.             if (allScripts) {
  23.                 for (i=0; i<allScripts.length; i++) {
  24.                     if (allScripts[i].innerHTML.toLowerCase().indexOf(end.toLowerCase()) != -1){
  25.                         jamScript = allScripts[i].innerHTML;
  26.                         break;
  27.                 }    }    }            // end if-allscripts
  28.             jamScript = jamScript.substring(jamScript.indexOf('replace')+9);
  29.             var theSplit = jamScript.indexOf('?');
  30.             thisFrameset = jamScript.substring(0,theSplit);
  31.             
  32.             //var hasAnchor = jamScript.indexOf('@');
  33.             //if (hasAnchor!=-1){
  34.             //    anchorName = jamScript.substring(hasAnchor+1);
  35.             //    anchorName = anchorName.substring(0,anchorName.indexOf('~'));
  36.             //    document.entryForm.getAnchor.value = anchorName;
  37.             //    }
  38.             
  39.             jamScript = jamScript.substring(jamScript.lastIndexOf('~'));
  40.             thisFrame = jamScript.substring(1,jamScript.indexOf("'"));
  41.             //-- end of names lookup
  42.  
  43.             theChoices = document.entryForm.getFrame
  44.             for (var i=theChoices.options.length-1; i > 0 ;i--)        // reduce list to one item
  45.                 theChoices.options[i] = null;
  46.             theChoices.options[0].text = thisFrame;
  47.             
  48.             document.entryForm.getFrameset.options[0].text = thisFrameset;
  49.             document.entryForm.ldescrip.visibility = "visible";
  50.             itsHere = true;
  51.         }else{
  52.             thisFrameset = '';
  53.             thisFrame = '';
  54.             }
  55.         return itsHere;
  56.         }                                // end isItHere ---
  57.  
  58.     var jamFrameset = "";
  59.     var jamFramesetHome = "";
  60.     var jamFrame ="";
  61.     
  62.     function insertScript(){    // insert in page and handler in frameset
  63.         if (itsHere){
  64.             openDocRemove();
  65.             getFramesetHome();    
  66.             }
  67.         jamFrameset = document.entryForm.getFrameset.options[0].text;
  68.         theChoices = document.entryForm.getFrame
  69.     
  70.         if (jamFrameset=='Pick a target frameset for this page'){
  71.             alert('Please pick a target frameset.');
  72.             return;
  73.         }else if (theChoices.selectedIndex==0 && theChoices.options.length>1){
  74.             alert('Please specify a frame for this page.');
  75.             return;
  76.         }else{                        //we're good to go
  77.             jamFrame = theChoices.options[theChoices.selectedIndex].text;
  78.             if (jamFrame.indexOf("_") != -1)
  79.                 jamFrame = jamFrame.substring(0,jamFrame.indexOf("_"));
  80.  
  81.             //could add a form field for the anchor, but distracting, seldom used
  82.             var theAnchor = '';    //keep this var, ignore the next two lines
  83.             //var theAnchor = noSpaces(document.entryForm.getAnchor.value);
  84.             //theAnchor = theAnchor.length>0? '@'+theAnchor:'';
  85.             
  86.             Myscript = "\r"+ start + thisVersion +"\r"
  87.             + "if (window.name!='"+ jamFrame +"' && window.name!='booker_'\r"
  88.             + "         && !((self.innerHeight == 0) && (self.innerWidth == 0)))\r"
  89.  
  90.             // activePage is updated in isItHere() and getFramesetHome()
  91.             + "        top.location.replace('"+ jamFrameset +"?"+ activePage + theAnchor +"~"+ jamFrame +"');\r"
  92.  
  93.             + end + "\r"
  94.             openDocInsert();        // script into page
  95.  
  96.             // Assemble the script to handle jammed pages in the target frameset
  97.             
  98.             Myscript = "\r"+ start + thisVersion +"\r"
  99.             + "if (self != top)     // frame buster, may be removed\r"
  100.             + "    top.location.replace(self.location);\r\r"
  101.             + "var theDefault = '"+ activePage +"';\r"
  102.             + "var theFrame = '"+ jamFrame +"';\r"
  103.             + "var thePage = theDefault;\r"
  104.             + "var framedPage = location.search;\r"
  105.             + "if (framedPage.length > 1 && framedPage.indexOf(\"://\") == -1){\r"
  106.             + "    framedPage = framedPage.substring(1);\r"
  107.             + "    var theSplit = framedPage.lastIndexOf('~');\r"
  108.             + "    if (theSplit != -1){\r"
  109.             + "        thePage = framedPage.substring(0,theSplit);\r"
  110.             + "        theFrame = framedPage.substring(theSplit+1);\r"
  111.             + "    }else\r"
  112.             + "        thePage = framedPage;\r"
  113.             + "    // uncomment anchor code if required (see Help)\r"
  114.             + "/*\r"
  115.             + "    if (thePage.indexOf('@') !=-1){  // checks for anchor\r"
  116.             + "        re=/(\\S+)@(\\S+)/\r"
  117.             + "        theArray = re.exec(thePage);\r"
  118.             + "        thePage=theArray[1]+'#'+theArray[2];\r"
  119.             + "        }\r"
  120.             + "*/\r"
  121.             + "    }\r\r"
  122.             
  123.             + "function frameStuffer(){\r"
  124.             + "    eval('top.frames[\"'+theFrame+'\"].location.replace(\"'+ thePage +'\");');\r"
  125.             + "    if (theFrame != '"+ jamFrame +"'){\r"
  126.             + "        top.frames[\""+ jamFrame +"\"].location.replace(theDefault);\r"
  127.             + "        theFrame = '"+ jamFrame +"';\r"
  128.             + "        }\r"
  129.             + "    }\r"
  130.             + end + "\r";
  131.             
  132.             // Now open the frameset target, write the function, then save the frameset page.
  133.             
  134.             
  135.             pageDOM = dreamweaver.getDocumentDOM(jamFramesetHome);
  136.             closedDocInsert(pageDOM);
  137.             if (!itsThere)
  138.                 pageDOM.body.setAttribute('onLoad','frameStuffer()');
  139.             dreamweaver.saveDocument(pageDOM,pageDOM.URL);
  140.             window.close();
  141.             }                            // end good to go
  142.         }                              // end insertScript
  143.  
  144.     function getFramesetHome(){        // gets path to frameset, confirms it exists
  145.         // called by browseFile and insertScript, 'jamFramesetHome' used by checkFrames
  146.         activePage = fileOnly(pageDOM.URL);        
  147.         var pathToPage = pageDOM.URL;
  148.         pathToPage = pathToPage.substring(0,pathToPage.lastIndexOf("/"));
  149.         jamFramesetHome = noSpaces(document.entryForm.getFrameset.options[0].text);
  150.         preFix="";
  151.         while (jamFramesetHome.charAt(0) == "."){
  152.             jamFramesetHome = jamFramesetHome.substring(jamFramesetHome.indexOf("/")+1);
  153.             preFix = pathToPage.substring(pathToPage.lastIndexOf("/")) + preFix;
  154.             pathToPage = pathToPage.substring(0,pathToPage.lastIndexOf("/"));    
  155.             }
  156.         jamFramesetHome = pathToPage +"/"+ jamFramesetHome;
  157.  
  158.         // added two lines
  159.         preFix = preFix.charAt(0)=='/'? preFix.substring(1)+"/" : preFix;
  160.         activePage = preFix + activePage;
  161.  
  162.         if ( !DWfile.exists( jamFramesetHome ) ){
  163.             alert("Sorry, can't find the file '"+ jamFrameset+"'");
  164.             return false;
  165.         }else 
  166.             return true;
  167.         }
  168.  
  169.  
  170.     var isDefault = -1;
  171.     function checkFrames(){
  172.         theChoices = document.entryForm.getFrame
  173.         for (var i=theChoices.options.length-1; i > 0 ;i--)
  174.             theChoices.options[i] = null;
  175.         var framesDOM = dreamweaver.getDocumentDOM(jamFramesetHome);
  176.         theFrames = framesDOM.getElementsByTagName('FRAME')
  177.         if (theFrames.length == 0){
  178.             alert('Sorry, can\'t find any frames in this page');
  179.             document.entryForm.getFrameset.options[0].text = "Pick a target frameset for this page";
  180.             theChoices.options[0].text = '--- Frame names will be listed here ---';
  181.         }else{
  182.             theChoices.options[0].text = 'Now pick a frame for this page:';
  183.             var theCut = pageDOM.URL.lastIndexOf('/') + 1;
  184.             var thisPage = pageDOM.URL.substring(theCut);
  185.             isDefault = -1;
  186.             for (var i=0; i< theFrames.length; i++){
  187.                 if (theFrames[i].getAttribute('SRC').indexOf(thisPage) != -1){
  188.                     isDefault = i;
  189.                     break;
  190.                 }    }    // end else theFrames.length
  191.             if (isDefault >= 0){
  192.                 theChoices.options[0].text = theFrames[isDefault].getAttribute('NAME');
  193.                 theChoices.options[0].selected = true;
  194.             }else{
  195.                 for (var i=0; i< theFrames.length; i++)
  196.                     theChoices.options[i+1] = new Option ( theFrames[i].getAttribute('NAME') );
  197.                     theChoices.options[0].selected = true;
  198.             }    }    //end isDefault
  199.         document.entryForm.getFrameset.options[0].selected = true; // 'prints' frameset name
  200.         }    //end checkFrames
  201.  
  202.  
  203.     function removeScript(){
  204.         if (itsHere){
  205.             openDocRemove();
  206.             window.close();
  207.         }else{
  208.             alert(thisFnName + ' is not in this page.');
  209.             return;
  210.         }    }
  211.  
  212.     //---------------------
  213.     
  214. function openDocInsert(){
  215.     theLocalDom=dreamweaver.getDocumentDOM("document");
  216.     scriptinsert(theLocalDom);
  217.     }
  218.     
  219. function openDocRemove(){
  220.     theLocalDom=dreamweaver.getDocumentDOM("document");
  221.     scriptdelete(theLocalDom);
  222.     }
  223.  
  224. function closedDocInsert(otherDom){
  225.     scriptinsert(otherDom);
  226.     }
  227.     
  228. function closedDocRemove(otherDom){
  229.     scriptdelete(otherDom);
  230.     }
  231.  
  232. function scriptinsert(thePageDom){
  233.     theHeadNode=thePageDom.getElementsByTagName("HEAD");
  234.     itsThere=theHeadNode.item(0).innerHTML.toLowerCase().indexOf(codeToCheck.toLowerCase())!="-1";
  235.     if (itsThere);
  236.     else
  237.           theHeadNode.item(0).innerHTML += '<script language="javascript" type="text/javascript">' + Myscript + '</s'+'cript>';
  238.     }
  239.  
  240. function scriptdelete(thePageDom){
  241.     theHeadNode = thePageDom.getElementsByTagName("HEAD");
  242.     itsThere = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(codeToCheck.toLowerCase()) != -1;    
  243.     if(itsThere){
  244.         theScriptNodes=thePageDom.getElementsByTagName("SCRIPT");
  245.         for(var i=0;i<theScriptNodes.length;i++){
  246.             var innerScript = theScriptNodes[i].innerHTML.toLowerCase();
  247.             if (innerScript.indexOf(codeToCheck.toLowerCase()) != -1){
  248.                 var beginsAt = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(start.toLowerCase());
  249.                 var endsAt   = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(end.toLowerCase());
  250.                 var theChunk = theHeadNode.item(0).innerHTML.substring(beginsAt,endsAt+end.length);
  251.                 theHeadNode.item(0).innerHTML = subtractString(theHeadNode.item(0).innerHTML, theChunk);
  252.                 var leavings = noSpaces(theScriptNodes[i].innerHTML);
  253.                 if (leavings=="<!--//-->")        // need to separate the tests or DW breaks
  254.                     theScriptNodes[i].outerHTML = "" ;
  255.                 else if (leavings=="")            // removes empty script tags
  256.                     theScriptNodes[i].outerHTML = "" ;
  257.                 break;
  258.                 }
  259.             }
  260.         }
  261.     }
  262.  
  263. // ---- support functions ----
  264.  
  265. function noSpaces(theString){
  266.     var theEnd = theString.length;
  267.     for (var i=0; i< theEnd; i++){
  268.         while ( theString.charCodeAt(i)<33 && i<theEnd){
  269.             theString = theString.substring(0,i) + theString.substring(i+1);
  270.             theEnd-- ;
  271.             }
  272.         }
  273.     return theString;
  274.     }
  275.     
  276. function subtractString(theString,theTakeaway){
  277.     var theSplit = theString.indexOf(theTakeaway);
  278.     if (theSplit != -1);{
  279.         var theLength = theTakeaway.length;
  280.         theString = theString.substring(0,theSplit) + theString.substring(theSplit+theLength);
  281.         }
  282.     return theString;
  283.     }
  284.     
  285. function padRight(theString,thePad,howLong){
  286.     while(theString.length < howLong)
  287.         theString += thePad;
  288.     return theString;
  289.     }
  290. function fileOnly(theString) {
  291.     var theSplit = theString.lastIndexOf ('/');
  292.     if (theSplit != -1)
  293.         return theString.substring(theSplit+1);
  294.     else
  295.         return "";
  296.     }
  297.     
  298. function checkForDWfile(){
  299.     if(typeof(DWfile)=="undefined"){
  300.         var message="Sorry, you need the DWfile library\r";
  301.         message+="You can download it from Macromedia's website";
  302.         alert(message);
  303.         dreamweaver.browseDocument("http://www.macromedia.com/support/dreamweaver/extend/dwfile/");
  304.         window.close();
  305.         return false;
  306.     }else
  307.         return nada="";
  308.     }
  309.