Difference between revisions of "Template:Slovenia/Javascript"

(Created page with "<!-- Javascript template, ki ga bomo lahko uporabili na vsaki strani našega wikija --> <html> <head> <meta charset="UTF-8" /> </head> <script> days = ["Mon", "Tue", "Wed"...")
 
Line 116: Line 116:
 
         this.cCode = html;
 
         this.cCode = html;
 
     }
 
     }
 +
 +
function isScrolledIntoView(elem) {
 +
var docViewTop = $(window).scrollTop();
 +
var docViewBottom = docViewTop + $(window).height() - $("#menucomb").height();
 +
 +
var elemTop = $(elem).offset().top;
 +
var elemBottom = elemTop + $(elem).height();
 +
 +
if(elemTop >= docViewTop) {
 +
return true;
 +
}
 +
else if(elemBottom >= docViewBottom) {
 +
return true;
 +
}
 +
return false; // || elemTop >= docViewTop) ;
 +
}
 +
 +
    calendar = new Calendar();
 +
    calendar.build();
 +
    document.getElementById("calendarField").innerHTML = calendar.getCode();
 +
 +
    //add event listener
 +
    document.addEventListener("click", changeMonth, false);
 +
 +
    function changeMonth(e) {
 +
        if (e.target.getAttribute("id") == "left") {
 +
            calendar.takeMonth();
 +
            calendar.build();
 +
            document.getElementById("calendarField").innerHTML = calendar.getCode();
 +
        }
 +
        if (e.target.getAttribute("id") == "right") {
 +
            calendar.addMonth();
 +
            calendar.build();
 +
            document.getElementById("calendarField").innerHTML = calendar.getCode();
 +
        }
 +
        if (e.target.parentNode.getAttribute("class") == "week") {
 +
            location.href = "#" + calendar.getWeek(e.target.getAttribute("id"));
 +
        }
 +
    }
 +
    $(document).ready(function () {
 +
        $("#calendarField").on('click', '#calendarHead', function (e) {
 +
            var width = $("#calendarTable").width();
 +
            //console.log(width);
 +
            $("#calendarTable").find("tbody").toggle(); //toggle all body
 +
            $("#calendarTable").find(".chead").toggle();
 +
            $("#calendarTable").width(width);
 +
        });
 +
        $(window).scroll(function () {
 +
            console.log("event fired");
 +
            var id = null;
 +
            $(".weekRow").each(function () {
 +
                if (isScrolledIntoView($(this))) {
 +
                    if (id == null) {
 +
id = $("span", this).attr("id");
 +
                        //id = $(this).attr("id")
 +
                    }
 +
                }
 +
            });
 +
 +
            day = id.split("-")[0].split(".")[0];
 +
            month = id.split("-")[0].split(".")[1];
 +
 +
            calendar = new Calendar(month - 1, 2016);
 +
            calendar.build();
 +
            document.getElementById("calendarField").innerHTML = calendar.getCode();
 +
            //console.log(id);
 +
 +
            var parent = null;
 +
 +
            //clear all tr classes
 +
            $("tr").each(function () {
 +
                if ($(this).attr("class") == "weekHighLight") {
 +
                    $(this).attr("class") = "week";
 +
                }
 +
            });
 +
 +
            $("td").each(function () {
 +
                if ($(this).attr("class") != "weak") {
 +
                    if ($(this).html() == day) {
 +
                        if (parent == null) {
 +
                            parent = $(this).parent().attr("id")
 +
                        }
 +
                    }
 +
                }
 +
            });
 +
            $("#" + parent).removeClass("week");
 +
            $("#" + parent).addClass("weekHighLight");
 +
        });
 +
    });
 +
 +
 
</script>
 
</script>
 
</html>
 
</html>

Revision as of 11:05, 14 October 2016