0

I'm trying to add a JavaScript in Drupal for a mobile navigation for the Zen 7.x-5.x theme. The JavaScript I'm trying to add can be found on http://codepen.io/micahgodbolt/pen/czwer

I understand that I need to use the JavaScript like described here: https://stackoverflow.com/a/14526812

But being new to JavaScript I can't get it to work. My latest try gives "undefined is not a function" on row 18 in the following (where the adjustMenu variable is declared):

(function($) {
    Drupal.behaviors.mybehavior = {
        attach: function () {
        var ww = $(window).width();

        $(document).ready(function() {
            $(".nav li a").each(function() {
                if ($(this).next().length > 0) {
                    $(this).addClass("parent");
                    };
                })

                $(".toggleMenu").click(function(e) {
                    e.preventDefault();
                    $(this).toggleClass("active");
                    $(".nav").toggle();
                });
                adjustMenu();
            })

            $(window).bind('resize orientationchange', function() {
                ww = $(window).width();
                adjustMenu();
            });

            var adjustMenu = function() {
                if (ww < 768) {
                // if "more" link not in DOM, add it
                if (!$(".more")[0]) {
                $('<div class="more">&nbsp;</div>').insertBefore($('.parent')); 
                }
                    $(".toggleMenu").css("display", "inline-block");
                    if (!$(".toggleMenu").hasClass("active")) {
                        $(".nav").hide();
                    } else {
                        $(".nav").show();
                    }
                    $(".nav li").unbind('mouseenter mouseleave');
                    $(".nav li a.parent").unbind('click');
                $(".nav li .more").unbind('click').bind('click', function() {

                        $(this).parent("li").toggleClass("hover");
                    });
                } 
                else if (ww >= 768) {
                // remove .more link in desktop view
                $('.more').remove(); 
                    $(".toggleMenu").css("display", "none");
                    $(".nav").show();
                    $(".nav li").removeClass("hover");
                    $(".nav li a").unbind('click');
                    $(".nav li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
                        // must be attached to li so that mouseleave is not triggered when hover over submenu
                        $(this).toggleClass('hover');
                });
            }
        }
    }
    };
})(jQuery);

I would really appreciate any help, as there is no mobile navigation in the Zen 7.x-5.x theme by default, and adding a JavaScript that is not written for Drupal is not very easy.

Community
  • 1
  • 1

1 Answers1

0

I got it working by a few adjustments, including moving the declaration fo rhte adjustMenu variable to above where it's being called. Entire javscript now looks like this:

(function ($) {
  Drupal.behaviors.mobile_menu_toggle = {
    attach: function (context, settings) {
        var ww = $(window).width();

        var adjustMenu = function() {
                if (ww < 768) {
                // if "more" link not in DOM, add it
                if (!$(".more")[0]) {
                $('<div class="more">&nbsp;</div>').insertBefore($('.parent')); 
                }
                    $(".toggleMenu").css("display", "inline-block");
                    if (!$(".toggleMenu").hasClass("active")) {
                        $(".menu").hide();
                    } else {
                        $(".menu").show();
                    }
                    $(".menu li").unbind('mouseenter mouseleave');
                    $(".menu li a.parent").unbind('click');
                $(".menu li .more").unbind('click').bind('click', function() {

                        $(this).parent("li").toggleClass("hover");
                    });
                } 
                else if (ww >= 768) {
                // remove .more link in desktop view
                $('.more').remove(); 
                    $(".toggleMenu").css("display", "none");
                    $(".menu").show();
                    $(".menu li").removeClass("hover");
                    $(".menu li a").unbind('click');
                    $(".menu li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
                        // must be attached to li so that mouseleave is not triggered when hover over submenu
                        $(this).toggleClass('hover');
                    });
                }
            }

        $(document).ready(function() {
            $(".menu li a").each(function() {
                if ($(this).next().length > 0) {
                    $(this).addClass("parent");
                    };
                })

                $(".toggleMenu").click(function(e) {
                    e.preventDefault();
                    $(this).toggleClass("active");
                    $(".menu").toggle();
                });
                adjustMenu();
            })

            $(window).bind('resize orientationchange', function() {
                ww = $(window).width();
                adjustMenu();
            });


        }
    };
})(jQuery);