systemInit.js 4.93 KB
// 菜单
(function() {


    $("table").parent().addClass("table-responsive");

    var menuInit = false;
    var openUrl = null;

    // Makes .page-inner height same as .page-sidebar height
    var sidebarAndContentHeight = function() {
        var content = $('.page-inner'),
            sidebar = $('.page-sidebar'),
            body = $('body'),
            height,
            footerHeight = $('.page-footer').outerHeight(),
            pageContentHeight = $('.page-content').height();

        content.attr('style', 'min-height:' + sidebar.height() + 'px !important');

        if (body.hasClass('page-sidebar-fixed')) {
            height = sidebar.height() + footerHeight;
        } else {
            height = sidebar.height() + footerHeight;
            if (height < $(window).height()) {
                height = $(window).height();
            }
        }

        if (height >= content.height()) {
            content.attr('style', 'min-height:' + height + 'px !important');
        }
    };

    function init() {
        $(".admin").show();
        // Sidebar Menu
        var parent, ink, d, x, y;
        $('.sidebar .accordion-menu li .sub-menu').slideUp(0);
        $('.sidebar .accordion-menu li.open .sub-menu').slideDown(0);
        $('.small-sidebar .sidebar .accordion-menu li.open .sub-menu').hide(0);
        $('.sidebar .accordion-menu > li.droplink > a').click(function() {

            if ($('body').hasClass('.small-sidebar')) {
                return;
            };

            if ($('body').hasClass('.page-horizontal-bar')) {
                return;
            };

            if ($('body').hasClass('.hover-menu')) {
                return;
            };

            var menu = $('.sidebar .menu'),
                sidebar = $('.page-sidebar-inner'),
                page = $('.page-content'),
                sub = $(this).next(),
                el = $(this);

            menu.find('li').removeClass('open');
            $('.sub-menu').slideUp(200, function() {
                sidebarAndContentHeight();
            });
            sidebarAndContentHeight();

            if (!sub.is(':visible')) {
                $(this).parent('li').addClass('open');
                $(this).next('.sub-menu').slideDown(200, function() {
                    sidebarAndContentHeight();
                });
            } else {
                sub.slideUp(200, function() {
                    sidebarAndContentHeight();
                });
            }
            return false;
        });

        // Push Menu
        $('.push-sidebar').click(function() {
            var hidden = $('.sidebar');

            if (hidden.hasClass('visible')) {
                hidden.removeClass('visible');
                $('.page-inner').removeClass('sidebar-visible');
            } else {
                hidden.addClass('visible');
                $('.page-inner').addClass('sidebar-visible');
            }
        });

        $('.sidebar .accordion-menu .sub-menu li.droplink > a').click(function() {
            var menu = $(this).parent().parent(),
                sidebar = $('.page-sidebar-inner'),
                page = $('.page-content'),
                sub = $(this).next(),
                el = $(this);

            menu.find('li').removeClass('open');
            sidebarAndContentHeight();

            if (!sub.is(':visible')) {
                $(this).parent('li').addClass('open');
                $(this).next('.sub-menu').slideDown(200, function() {
                    sidebarAndContentHeight();
                });
            } else {
                sub.slideUp(200, function() {
                    sidebarAndContentHeight();
                });
            }
            return false;
        });
        sidebarAndContentHeight();
        window.onresize = sidebarAndContentHeight;
        menuInit = true;
        doOpenMenu();
    }

    // 打开菜单
    function doOpenMenu() {
        if (openUrl && menuInit) {
            function active(dom) {
                var li = dom.parent();
                li.addClass("active");
                if (li.parent().hasClass("sub-menu")) {
                    li.parent().parent().addClass("open");
                    li.parent().slideDown(200, function() {
                        sidebarAndContentHeight();
                    });
                }
            }
            $(".accordion-menu a").each(function() {
                var url = $(this).attr("href");
                if (openUrl.indexOf(url) >= 0) {
                    active($(this));
                    return;
                }
            });
        }
    }

    function _openMenu(url) {
        if (!url) {
            url = location.href;
        }
        openUrl = url;
        doOpenMenu();
    }

    // 加载菜单
    $.ajax({
        url: "tmpl/menu.html",
        dataType: "html",
        success: function(data) {
            $(".accordion-menu").html(data);
            init();
            openMenu();
        }
    });
    window.openMenu = _openMenu;
})();