home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2011 October / PCgo_1011_CD.iso / interface / js / sitemap.js < prev    next >
Encoding:
Text File  |  2007-11-14  |  19.8 KB  |  647 lines

  1. <!--//--><![CDATA[//><!--
  2.  
  3. /**
  4.  * singleton pattern for low memory usage
  5.  */
  6. function SiteMapDataLoader( fileName ) {
  7.     var xmlDoc = null;
  8.  
  9.     /**
  10.      * init method
  11.      */
  12.     this.init = function( fileName) {
  13.         
  14.         try {            
  15.             xmlDoc = loadXML( fileName );                                                
  16.         } catch (e) {
  17.             throw e;
  18.         }
  19.     }
  20.     
  21.         
  22.     /**
  23.      * load data from xml file method
  24.      */
  25.     var loadXML  = function( fileName )
  26.     {
  27.         xmlDoc = null;
  28.         
  29.         if (document.implementation && document.implementation.createDocument)
  30.         {
  31.             xmlDoc = document.implementation.createDocument("", "", null);
  32.         }
  33.         else if (window.ActiveXObject)
  34.         {
  35.             xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  36.          }
  37.         else
  38.         {
  39.             alert('Your browser can\'t handle this script');
  40.         }
  41.  
  42.                 
  43.         xmlDoc.async = false;
  44.         xmlDoc.load( fileName );        
  45.         
  46.         if( xmlDoc == null ) {
  47.             throw "could not create xmlDoc object";
  48.         }
  49.         
  50.         return xmlDoc;
  51.     }
  52.     
  53.     /**
  54.      * 
  55.      */
  56.     this.getData = function() {
  57.         return xmlDoc;
  58.     }
  59.  
  60.     this.init( fileName );
  61.  
  62. }
  63.  
  64. SiteMapDataLoader.__instance__ = null;
  65.  
  66. SiteMapDataLoader.getInstance = function( fileName ) {
  67.     if( this.__instance__ == null ) {
  68.         this.__instance__ = new SiteMapDataLoader( fileName );        
  69.     }
  70.     
  71.     return this.__instance__;
  72. }
  73.  
  74. function SiteMap() {
  75.     
  76.     var xmlDoc = null;
  77.     
  78.     var containerId = "siteMap";
  79.     
  80.     var rootElementId = "siteMapContainer";
  81.     
  82.     var startVisible = true;
  83.     
  84.     var startClosed = false;
  85.     
  86.     var startSectionsClosed = false;
  87.      
  88.     var labelTitle = "SiteMap";
  89.     
  90.     var labelBySection = false;
  91.     
  92.     var cssClass = "siteMapContainer";
  93.     
  94.     /**
  95.      * init method
  96.      */
  97.     this.init = function( title ) {
  98.         
  99.         try {
  100.             
  101.             if( title != null ) {
  102.                 labelTitle = title;
  103.             }
  104.             
  105.             var loader = SiteMapDataLoader.getInstance( "sitemap.xml" );
  106.  
  107.             xmlDoc = loader.getData();
  108.         } catch (e) {
  109.             throw e;
  110.         }
  111.     }        
  112.     
  113.    this.setCSS = function ( css ) {
  114.        cssClass = css;
  115.    }
  116.    
  117.    this.setLabelBySection = function ( bySection ) {       
  118.        labelBySection = bySection;
  119.    }
  120.    
  121.    this.setTitle = function( title ) {
  122.        labelTitle = title;       
  123.    }
  124.    
  125.    this.getTitle = function() {
  126.        return labelTitle;
  127.    }
  128.    
  129.    /**
  130.     * 
  131.     */
  132.    var setLabelBySection = function( section, element ) {
  133.        
  134.        if( element != null ) {
  135.            
  136.            if( section != null ) {
  137.                
  138.                var title = section.getAttribute("title");
  139.                
  140.                var textElement = document.createTextNode(title);
  141.                
  142.                element.replaceChild( textElement, element.firstChild );
  143.                element.appendChild( textElement );
  144.            }
  145.        }
  146.    }
  147.    
  148.    /**
  149.     * 
  150.     */
  151.    this.process = function( id, container ) {
  152.        
  153.        if( xmlDoc != null ) {
  154.                       
  155.            var element = HTMLCreateRootElement( rootElementId );
  156.            
  157.            var labelElement = HTMLCreateLabelFragment( element );
  158.            
  159.            var containerElement = HTMLCreateContainerFragment( labelElement );
  160.            
  161.            element.appendChild( labelElement );
  162.            
  163.            element.appendChild( containerElement );
  164.  
  165.            var array = xmlDoc.getElementsByTagName( "smc" );
  166.            
  167.            var createLabel = true;
  168.            
  169.            if( array != null ) {
  170.                
  171.                for( var index=0 ; index < array.length ; index++ ) { 
  172.                    
  173.                    if( id != null ) {
  174.                        if( array[index].getAttribute("id") == id ) {                       
  175.                            
  176.                            if( labelBySection ) {
  177.                                setLabelBySection( array[index], labelElement );
  178.                                createLabel = false;
  179.                            }
  180.  
  181.                            processSection( array[index], null, containerElement, createLabel );  
  182.                        }                                
  183.                    } else {
  184.                            processSection( array[index], null, containerElement, createLabel );                         
  185.                    }
  186.                }
  187.  
  188.                if( container != null ) {
  189.                    containerId = container;
  190.                }
  191.                               
  192.                // append to Id
  193.                 var container = document.getElementById( containerId );                
  194.                 
  195.                 if( container ) {                    
  196.                     container.appendChild( element );
  197.                 }            
  198.                
  199.                
  200.            } else {
  201.                throw "SiteMap::process() -> no root sections found";
  202.            }
  203.            
  204.            
  205.        } else {
  206.            throw "SiteMap::process() -> xmlDoc is null";
  207.        }
  208.    }
  209.  
  210.  
  211.    var processSection = function( section, parentSection, container, createLabel ) {
  212.        
  213.        if( section != null ) {
  214.            
  215.            var sectionHTMLContainer = HTMLCreateSection( section, container, createLabel );
  216.            
  217.            // process titles
  218.            var titles = section.getElementsByTagName( "st" );
  219.            var arrayTitles = new Array();
  220.            
  221.            for( var index = 0 ; index < titles.length ; index++ ) {
  222.                
  223.                if( titles[index].parentNode == section ) {
  224.                    arrayTitles.push( titles[index] );
  225.                }
  226.            }
  227.            
  228.            if( arrayTitles.length > 0 ) {
  229.                HTMLCreateTitles( arrayTitles, sectionHTMLContainer );
  230.            }
  231.            
  232.            // process sub sections
  233.            
  234.            var sections = section.getElementsByTagName( "sms" );
  235.            
  236.            var title = section.getAttribute( "title" );
  237.            
  238.            if( sections != null ) {
  239.                           
  240.                for( var index = 0; index < sections.length ; index ++ ) {
  241.                    
  242.                    // getElementsByTagName returns all elements at all depths below so bypass this
  243.                    // check if parent element is at same depth
  244.                    if( sections[index].parentNode == section ) {                   
  245.                        processSection( sections[index], section, sectionHTMLContainer, true );                   
  246.                    } 
  247.                }
  248.                
  249.            } else {
  250.                // no sub sections in section
  251.            }
  252.        } else {
  253.            throw "SiteMap::processSection() -> section parameter is null";
  254.        }
  255.        
  256.    }
  257.    
  258.    
  259.    /**
  260.     * 
  261.     */
  262.    var HTMLCreateRootElement = function( idPrefix ) {
  263.  
  264.        var element = document.createElement( "div" );
  265.        
  266.        if( element == null ) {
  267.            throw "SiteMap::HTMLCreateRootElement() -> could not create root element";
  268.        }
  269.  
  270.        // visibility
  271.        if( startVisible == true ) {
  272.           element.style.display = "block";
  273.        } else {
  274.           element.style.display = "none";
  275.        }
  276.        
  277.        // id + unique random number
  278.        var attribute = document.createAttribute( "id" );
  279.        
  280.        if( attribute != null ) {
  281.          attribute.nodeValue = createUniqueRandomId( idPrefix );
  282.          
  283.          element.setAttributeNode( attribute );
  284.        } else {
  285.            throw "SiteMap::HTMLCreateRootElement() -> could not create id attribute for root element";
  286.        }
  287.  
  288.        attribute = document.createAttribute( "class" );
  289.        
  290.        if( attribute != null ) {
  291.        
  292.          attribute.nodeValue = cssClass;
  293.          
  294.          element.setAttributeNode( attribute );
  295.        } else {
  296.            throw "SiteMap::HTMLCreateRootElement() ->  could not create style attribute for root element";
  297.        }
  298.        
  299.        return element;
  300.    }
  301.  
  302.    /**
  303.     * 
  304.     */
  305.    var HTMLCreateLabelFragment = function( element ) {
  306.        
  307.            var container = document.createElement("div");
  308.            var h1Element = document.createElement( "h1" );
  309.            var textElement = null;;
  310.            
  311.            if( setLabelBySection == true ) {
  312.                 textElement = document.createTextNode( getTitleOfFirstSection() );
  313.                
  314.            } else {
  315.                 textElement = document.createTextNode( labelTitle );
  316.            }
  317.     
  318.            var attribute = document.createAttribute( "id" );
  319.            
  320.            if( attribute != null ) {
  321.              
  322.              if( element != null ) {
  323.                  attribute.nodeValue = element.getAttribute("id") + "_label";
  324.              } else {
  325.                  attribute.nodeValue = createUniqueRandomId( "randomId" );
  326.              }
  327.              
  328.              h1Element.setAttributeNode( attribute );
  329.              
  330.            } else {
  331.                throw "SiteMap::HTMLCreateLabelFragment() -> could not create id attribute for h1 element";
  332.            }
  333.     
  334.            var attribute = document.createAttribute( "class" );
  335.            
  336.            if( attribute != null ) {
  337.              
  338.              attribute.nodeValue = "opened";
  339.              
  340.              h1Element.setAttributeNode( attribute );
  341.              
  342.            } else {
  343.                throw "SiteMap::HTMLCreateLabelFragment() -> could not create class attribute for h1 element";
  344.            }
  345.            
  346.            h1Element.appendChild( textElement );
  347.  
  348.            var closeElement = document.createElement( "a" );
  349.            textElement = document.createTextNode( "X" );
  350.            closeElement.appendChild( textElement );
  351.            attribute = document.createAttribute( "id" );
  352.            
  353.            if( attribute != null ) {
  354.              
  355.              if( element != null ) {
  356.                  attribute.nodeValue = element.getAttribute("id") + "_label_close";
  357.              } else {
  358.                  attribute.nodeValue = createUniqueRandomId( "randomId" );
  359.              }
  360.            } else {
  361.                throw "SiteMap::HTMLCreateLabelFragment() -> could not create id attribute for close element";
  362.            }
  363.            
  364.            attribute = document.createAttribute( "href" );
  365.            
  366.            if( attribute != null ) {
  367.              
  368.              attribute.nodeValue = createUniqueRandomId( "javascript:void(0);" );
  369.              closeElement.setAttributeNode( attribute );
  370.              
  371.            } else {
  372.                throw "SiteMap::HTMLCreateLabelFragment() -> could not create href attribute for close element";
  373.            }
  374.            
  375.            closeElement.className = "close";
  376.            
  377.            closeElement.onclick = function( event ) {
  378.                
  379.                // do not propagate event
  380.                 if (!event) var event = window.event;
  381.                 event.cancelBubble = true;
  382.                 if (event.stopPropagation) event.stopPropagation();
  383.              
  384.                 var element = this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode );
  385.                 
  386.                 element.style.display = "none";
  387.            };
  388.                
  389. //           h1Element.appendChild( closeElement );
  390.            
  391.            return h1Element;
  392.    } 
  393.    
  394.    /**
  395.     * 
  396.     */
  397.     var HTMLCreateContainerFragment = function( element ) {
  398.         
  399.         if( element != null ) {
  400.             
  401.            var container = document.createElement( "div" );
  402.     
  403.            if( container == null ) {
  404.                throw "could not create container element";
  405.            }
  406.  
  407.            var attribute = document.createAttribute( "id" );
  408.            
  409.            if( attribute != null ) {               
  410.              
  411.              attribute.nodeValue = element.getAttribute("id") + "_container";
  412.              
  413.              container.setAttributeNode( attribute );
  414.            } else {
  415.                throw "could not create id attribute for container element";
  416.            }
  417.  
  418.            attribute = document.createAttribute( "class" );
  419.            
  420.            if( attribute != null ) {
  421.              attribute.nodeValue = "siteMapContainer_container";
  422.              
  423.              container.setAttributeNode( attribute );
  424.            } else {
  425.                throw "could not create style attribute for root element";
  426.            }
  427.            
  428.            if( startClosed == true ) {
  429.                container.style.display = "none";
  430.                element.className = "closed";
  431.            } else {
  432.                container.style.display = "block";
  433.                element.className = "opened";               
  434.            }
  435.            
  436.            element.onclick = function()  { 
  437.                
  438.                
  439.                
  440.             };
  441.              
  442.            return container;            
  443.         } else {
  444.             throw "either rootElement or xmlDoc is null maybe even both ;)";
  445.         }
  446.     }   
  447.  
  448.  
  449.  
  450.    /**
  451.     * 
  452.     */
  453.    var HTMLCreateSection = function( section, parentContainer, createLabel ) {
  454.        
  455.        if( section != null ) {
  456.            
  457.            var labelType = "h2";
  458.            
  459.            if( parentContainer != null ) {
  460.                labelType = "h3";
  461.            }
  462.  
  463.            var container = document.createElement( "dl" );           
  464.            var containerElements = document.createElement( "dt" );           
  465.            
  466.            if( createLabel ) {
  467.                var title = section.getAttribute( "title" );
  468.                var containerLabel = document.createElement( labelType );
  469.                
  470.                var indicatorLabel = document.createTextNode( "- " );
  471.                
  472.                var labelText = document.createTextNode( title );
  473.            
  474.                containerLabel.onclick = function( event ) {
  475.                
  476.                    var container = this.parentNode.childNodes[1];
  477.                    var indicator = this.childNodes[0];                
  478.                                   
  479.                    if( container != null ) {
  480.                    
  481.                        if( container.style.display == "block" ) {
  482.                            container.style.display = "none";
  483.                            indicator.nodeValue = "+ ";
  484.                        } else {
  485.                            container.style.display = "block";
  486.                            indicator.nodeValue = "- ";
  487.                        }
  488.                    
  489.                    }
  490.                
  491.                };        
  492.  
  493.                containerLabel.appendChild( indicatorLabel );
  494.                containerLabel.appendChild( labelText );
  495.                container.appendChild( containerLabel );
  496.            }
  497.            
  498.            container.appendChild( containerElements );
  499.            
  500.            if( startSectionsClosed == true ) {
  501.                containerElements.style.display = "none";
  502.            } else {
  503.                containerElements.style.display = "block";               
  504.            }
  505.            
  506.            
  507.            if( parentContainer == null ) {
  508.                if( containerElement != null ) {
  509.                    
  510.                    containerElement.appendChild( container );
  511.                    
  512.                    return containerElements;
  513.                    
  514.                } else {
  515.                    throw "SiteMap::createHTMLSection() -> container element is null";
  516.                }
  517.            } else {
  518.                    parentContainer.appendChild( container );
  519.                    
  520.                    return containerElements;               
  521.            }
  522.            
  523.        } else {
  524.            throw "SiteMap::createHTMLSection() -> section parameter is null";
  525.        }
  526.    }
  527.  
  528.  
  529.     this.closeAll = function(elemId) {
  530.         var element = document.getElementById(elemId);
  531.         if (element != null) {
  532.           var nodes = element.getElementsByTagName('h3');
  533.           var node = null;
  534.           for (var i=0; i<nodes.length; i++) {
  535.           
  536.             node = nodes[i];
  537.             node.onclick();
  538.           }
  539.           
  540.           var nodes = element.getElementsByTagName('h2');
  541.           var node = null;
  542.           for (var i=0; i<nodes.length; i++) {
  543.             node = nodes[i];
  544.             node.onclick();
  545.           }          
  546.         }      
  547.     }
  548.     
  549.     /**
  550.      * 
  551.      */
  552.     var HTMLCreateTitles = function( titles, container ) {
  553.        
  554.        if( titles != null ) {
  555.            
  556.            if( titles.length > 0 ) {
  557.                
  558.                if( container != null ) {
  559.                    
  560.                    var title = null;
  561.                    var containerElement = null;
  562.                    var labelElement = null;
  563.                    var labelTextElement = null;
  564.                    var attribute = null;
  565.                    var st = null;
  566.                    
  567.                    for( var index = 0 ; index < titles.length ; index++ ) {
  568.                        
  569.                        title = titles[index].getElementsByTagName( "t" )[0].firstChild.data;
  570.                        st = titles[index];
  571.                        
  572.                        if( title != null ) {
  573.                            
  574.                            containerElement = document.createElement( "dt" );
  575.                            labelElement = document.createElement( "a" );
  576.                            
  577.                            /* 2022 2027 8226 */
  578.                            indicatorTextElement = document.createTextNode( "\u2022 " );
  579.                            
  580.                            labelTextElement = document.createTextNode( title );
  581.                            
  582.                            attribute = document.createAttribute("href");
  583.                            attribute.nodeValue = "javascript:void(0);";
  584.                            
  585.                            labelElement.setAttributeNode( attribute );
  586.  
  587.                            attribute = document.createAttribute("name");
  588.                            attribute.nodeValue = st.getAttribute("href");
  589.                            
  590.                            labelElement.setAttributeNode( attribute );
  591.  
  592.                            labelElement.appendChild( indicatorTextElement );
  593.                            labelElement.appendChild( labelTextElement );
  594.                            
  595.                            labelElement.onclick = function() {
  596.                              switch_iframe_and_close_sitemap( this.getAttribute("name") );
  597.         
  598.                            }
  599.                            containerElement.appendChild( labelElement );
  600.                            container.appendChild( containerElement );
  601.                            
  602.                        }
  603.                        
  604.                    }
  605.                    
  606.                }
  607.                
  608.            }
  609.            
  610.        } else {
  611.            
  612.        }
  613.    }
  614.         
  615.     /**
  616.      * create unique random id making sure it does not belong to an attachted dom element
  617.      */
  618.    var createUniqueRandomId = function( prefix ) {
  619.         // avoid creating element with existing id
  620.         var rand;
  621.         
  622.         var found = true;
  623.         var foundElement = null;
  624.         var tempId = prefix;
  625.         
  626.         while( found ) {
  627.             rand = Math.round( 1000 * Math.random() );
  628.             
  629.             tempId = prefix + "_" + rand;
  630.             
  631.             foundElement = document.getElementById( tempId );
  632.             
  633.             if( foundElement != null ) {
  634.                found = true;
  635.             } else {
  636.                 found = false;
  637.             }
  638.         }
  639.         
  640.         return tempId;
  641.    }
  642.    
  643.     
  644. }
  645.  
  646. //--><!]]>
  647.