wizefunc.js 6.62 KB
/* Go to top of page */
    function topFunction() {
        document.body.scrollTop = 0;
        document.documentElement.scrollTop = 0;
    }
    
    /* open/close navigation optionally slide page */
    function openNav(slide) {
        if (document.getElementById("mySidenav").getAttribute("class").split(' ')[1] == "menuClosed") {
            document.getElementById("mySidenav").style.width = "300px";
            if (slide==1) {
                document.getElementById("main").style.marginLeft = "300px";
            }
            document.getElementById("mySidenav").setAttribute("class","menu menuOpen");
            /* Scroll to top when opening menu */
            topFunction();
        } else {
            document.getElementById("mySidenav").style.width = "0";
            if (slide==1) {
                document.getElementById("main").style.marginLeft = "0";
            }
            document.getElementById("mySidenav").setAttribute("class","menu menuClosed");            
        }
    }
    
    /* when scrolling down 20 pixel display upButton */
    function scrollFunction() {
        if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
            upButton.style.display = "block";
        } else {
            upButton.style.display = "none";
        }
    }

    /* Test if the logo is present (to avoid displaying broken link icon on chrome) */
    function testLogo() {
        logo=document.getElementById("logoImgId");
        logodiv=document.getElementById("logoDivId");
        if (logo.naturalHeight == 0) {
            logodiv.setAttribute("style","display:none;");
        }
    }

    
    function tagToInfo(s5=false) {
        /* All H4 tags */
        var atags = document.getElementsByTagName('H4');
        /* extract configuration tags */
        tags=[];
        for (var i=0; i<atags.length;i++) {
            var s = atags[i].innerHTML;
            if (s.match(/^#/)) {
                atags[i].innerHTML = s.replace('#','');
                tags.push(atags[i]);
            }
        }
                
        var n = tags.length < 3 ? tags.length : 2;
        for (var i=1; i <= n; i++) {
            /* first make the tag not displayed */
            tags[i-1].setAttribute("style","display:none;");
            /* If next sibling is a <br> make it display:none too */
            if (tags[i-1].nextElementSibling && tags[i-1].nextElementSibling.tagName == 'BR') {            
                tags[i-1].nextElementSibling.setAttribute("style","display:none;");
            }
            if (s5) {
                var s = tags[i-1].innerHTML;
                document.getElementById("footinfo"+i).innerHTML = s;
            }
        }
    }
    
    /* Convert the two first tags into info */
    /* use tags for image/table/object attributes        */
    function tagToStyle() {
        /* All H5 tags */
        var atags = document.getElementsByTagName('H5');
        /* extract configuration tags */
        tags=[];
        for (var i=0; i<atags.length;i++) {
            var s = atags[i].innerHTML;
            if (s.match(/^#/)) {
                atags[i].innerHTML = s.replace('#','');
                tags.push(atags[i]);
            }
        }
                
        if (tags.length > 0) {
            /* Image tags */
            for (var i=0; i < tags.length; i++) {
                var ctag = tags[i];
                ctag.setAttribute("style","display:none;");
                /* If next sibling is a <br> make it display:none too */
                if (ctag.nextElementSibling && ctag.nextElementSibling.tagName == 'BR') {
                    ctag.nextElementSibling.setAttribute("style","display:none;");
                }
                var s = ctag.innerHTML;
                /* find next img/table/zim-object */                
                ctag = findNextObject(ctag);
                
                /* either we found something either null */
                if (ctag!=null) {
                    var style = ctag.getAttribute("style");
                    var res;
                    style = style == null ? '' : style;
                    if (s == "left") {
                        style = style + "float:left; margin-left:0; margin-right:1em;";
                    }
                    if (s == "right") {
                        style = style + "float:right; margin-left:1em; margin-right:0;";
                    }                    
                    if (res = s.match(/^height(.+)$/)) {
                        style = style + "height:"+res[1]+";";
                    }
                    if (res = s.match(/^width(.+)$/)) {
                        style = style + "width:"+res[1]+"; height:auto;";
                    }
                    if (res = s.match(/^fontsize(.+)$/)) {
                        style = style + "font-size:"+res[1]+"%;";
                    }
                    ctag.setAttribute("style",style);
                }
                
            }
            
        }
    }

    function findNextObject(tag) {
        /* try to found in current */
        var objectsTagClassArray=['IMG','','TABLE','','DIV','zim-object','UL','','OL',''];
        ob = findTagInSiblings(tag,objectsTagClassArray);
        if (ob == null) {
            ob = findTagInSiblings(tag.parentElement,objectsTagClassArray);
        }
        return ob;
    }
    
    function findTagInSiblings(tag,tagsToFind) {
        var notFound = true;
        var ctag = tag;
        var cctag;
        while (notFound) {
            ctag = ctag.nextElementSibling
            if (ctag == null) {
                notFound = false;
                break;
            }
            if (isTagInList(ctag,tagsToFind)) {
                notFound = false;
                break;
            }
            /* now search into children */
            if (ctag.childElementCount != 0) {
                for (var i=0;i<ctag.childElementCount;i++) {
                    cctag=ctag.children[i];
                    if (isTagInList(cctag,tagsToFind)) {
                        notFound = false;
                        ctag=cctag;
                        break;
                    }
                }
            }
        }        
        return ctag;
    }

    function isTagInList(tag,tagsToFind) {
        yes = false;
        for (var i=0; i < tagsToFind.length/2 ; i++) {
            if (tag.tagName == tagsToFind[i*2]) {
                if (tagsToFind[i*2+1]=='') {
                    yes = true;
                    break;
                } else if (tag.className == tagsToFind[i*2+1]) {
                    yes = true;
                    break;
                }                
            }
        }                
        return yes;
    }