/*
*
* Truncates/expands the menu
*
* Author:Abdullah Aman
* From: Rogers Digital Media
*
*/
function MinifyMenu( main_nav_id, minify_menu_name, extra_spaces, mobile_width) {
this.main_nav_id = main_nav_id;
this.main_menu_ul = $(main_nav_id);
this.more_menu_ul = null;
//this.main_menu_childs = $(this.main_menu_ul).children();
this.prev_width = null;
this.current_width = $(window).width();
this.allowed_items_in_first_row = null;
this.restore_point =null;
this.extra_spaces = extra_spaces;
this.minify_menu_name = null;
this.mobile_width = mobile_width;
if ( minify_menu_name.length == 0 ) {
this.minify_menu_name = 'More+';
}
else {
this.minify_menu_name = minify_menu_name;
}
};
MinifyMenu.prototype.updateMenu = function(first_pass) {
//var current_item = null;
var item_to_add = null;//when the menu is shrinking
var item_to_restore = null;//when the menu is expanding
//var more_menu_ul = null;
var max_items = $(this.main_menu_ul).children().length;
this.prev_width = this.current_width;
this.current_width = $(window).width();
//if the menu is set to display none/hidden then lets show it
if ( first_pass ) {
this.showmenu();
this.remove_sub_nav_style();
this.make_more_menu_clickable();
}
//exit out if we are in mobile, there is no need to minify and expand
if (this.current_width <= this.mobile_width) {
return;
}
this.menu_ul_height = $(this.main_nav_id).height();
this.menu_ul_width = $(this.main_nav_id).width();
this.menu_li_height = ($(this.main_nav_id + " li").first()).height();
if ( this.menu_ul_height > ((+this.menu_li_height) + this.extra_spaces) ) {
if ( this.more_menu_ul == null ) {
this.main_menu_ul = $(this.main_menu_ul).append("
"+this.minify_menu_name+"");
this.more_menu_ul = $(this.main_menu_ul).find('.more ul');
}
this.minify(max_items);
}
else {
if (this.prev_width < this.current_width) {
this.expand(max_items);
}
}
};
MinifyMenu.prototype.minify = function( max_items ) {
this.menu_ul_height = $(this.main_menu_ul).height();
var current_item = $(this.main_menu_ul).find('.more');
current_count = 0;
while(this.menu_ul_height > ((+this.menu_li_height) + this.extra_spaces) && current_count < max_items) {
item_to_add = current_item.prev();
//console.log(item_to_add.text());
$(item_to_add).find('.dropdown-menu').css('display','none');//hide the submenu
//fix up the li height if we are first loaded in the mobile menu and start moving to bigger breaking points
if((+this.menu_li_height) == 0)
{
//alert("need updates"+current_item.prev().height());
this.menu_li_height = current_item.prev().height();
}
//alert(item_to_add.text());
$(this.more_menu_ul).prepend(item_to_add);
this.menu_ul_height = $(this.main_menu_ul).height();
current_count++;
}
}
MinifyMenu.prototype.expand = function( max_items ) {
var current_item = $(this.main_menu_ul).find('.more');
var current_item_next = "";
if ( current_item.length != 0 ) {
var more_ul = current_item.find('ul');
var more_menu_childrens = more_ul.children();
var item_to_restore = more_ul.children().first();
$(item_to_restore).find('.dropdown-menu').removeAttr("style");//restore menu setting
current_item_next = current_item.prev();//get the menu item just before the +more
// If the "MORE+" menu has no children, remove the unnecessary parent "MORE+" item.
if ( more_menu_childrens.length == 0 ) {
$(current_item).remove();
this.more_menu_ul = null;
}
current_item_next.after(item_to_restore);//restoring the item back in the menu
this.minify(max_items);
$(this.main_menu_ul).find('.more > .dropdown-menu').removeAttr("style");
}
}
MinifyMenu.prototype.showmenu = function() {
if ( !$(this.main_menu_ul).is(':visible') ) {
$(this.main_menu_ul).css('display','block');
}
}
/**
* On mobile if a sub menu item has child items in it then just make the sub menu clickable
*/
MinifyMenu.prototype.remove_sub_nav_style = function() {
var anchors = $(this.main_menu_ul).find('.dropdown-menu .dropdown-accordion');
$(anchors).parent().removeClass('dropdown')
$(anchors).parent().addClass('dropdown-no-children');
anchors.click(function(event){
window.location.href = $(this).attr("href");
return false;
});
}
/**
* -Makes the items in the "more+" menu clickable
* -Make main nav menu items clickable on desktop
*/
MinifyMenu.prototype.make_more_menu_clickable = function() {
$('.dropdown-accordion').click(function(event){
var window_width = $(window).width();
if(window_width >= 1024)
{
if($(this).attr("href") != "#" )
{
event.stopPropagation();//make the anchor ('this') follow link
}
else
{
event.stopPropagation();
return;//stop sub-nav from showing (for 'more+' menu item)
}
}
else if(window_width > 767 && window_width < 1024)
{
if($(this).attr("href") != "#" )
{
event.stopPropagation();//make the anchor ('this') follow link
}
}
});
}
menu_nav = new MinifyMenu("#menu-main-navigation",'',5,766);
$( window ).resize(function(){menu_nav.updateMenu()});
menu_nav.updateMenu(true);
sub_nav = new MinifyMenu("#genesis-sub-nav",'',5,1920);
$( window ).resize(function(){sub_nav.updateMenu()});
sub_nav.updateMenu(true);