/sites/all/themes/omega/gamma/js/gamma.js

https://github.com/nicolasnm/divingbook · JavaScript · 131 lines · 76 code · 8 blank · 47 comment · 6 complexity · 171f734677f7fd1549be2dbc1f2bfc1a MD5 · raw file

  1. // declare the gamma variable for jQuery usage in the gamma theme.
  2. (function ($) {
  3. /**
  4. * @todo Must fix the interaction between active menu items
  5. * @todo Change the order of the hover out fade back to normal, and sub-menu hiding
  6. * This should be set to close the menu fully (after short delay) then fade the menu color back to normal
  7. * @todo Make sure the menus accomodate menus that are only 2 wide on the top level.
  8. */
  9. gamma = jQuery.noConflict();
  10. gamma(document).ready(function(){
  11. // even up the menus to a non decimal width value
  12. // this happens because we don't define specific CSS width
  13. // and rely on padding to give us a nice width
  14. gamma('#site-menu .menu:first > li').each(function(){
  15. var currentWidth = gamma(this).innerWidth();
  16. gamma(this).width(Math.round(currentWidth));
  17. });
  18. // make the menu sexy
  19. gamma('#site-menu .menu:first > li').hover(
  20. // hover in
  21. function(){
  22. if(!gamma(this).hasClass('active-trail')) {
  23. gamma(this).children('a').stop().animate({
  24. "color": "#454545",
  25. }, 1000, function() {
  26. });
  27. gamma(this).addClass('hovering').stop().animate({
  28. "backgroundColor": "#DDD",
  29. }, 150, function() {
  30. });
  31. }
  32. // find the position of the parent LI in the menu
  33. var parentIndex = gamma('#site-menu .menu:first > li').index(this);
  34. // find the number of total items in the menu
  35. var totalNum = gamma('#site-menu .menu:first > li').size();
  36. // each dropdown is the width of the next 3 menu items by default
  37. // if the menu item is one of the last two, we need to change the way
  38. // the width and positioning is calculated.
  39. if(totalNum - parentIndex < 3) {
  40. // this menu item is one of the last two, and we need to calculate differently.
  41. // find offset of the parent LI
  42. var leftItem = totalNum - 3;
  43. var rightItem = totalNum - 1;
  44. var offset = gamma('#site-menu .menu:first > li:eq('+leftItem+')').offset();
  45. // get the left offset of the parent LI
  46. var leftOffset = offset.left - 1;
  47. // grab the appropriate indexes for the left and right positions of the menu
  48. var rightIndex = rightItem;
  49. }
  50. else {
  51. // Default implementation
  52. // find offset of the parent LI
  53. var offset = gamma(this).offset();
  54. // get the left offset of the parent LI
  55. var leftOffset = offset.left - 1;
  56. // grab the appropriate indexes for the left and right positions of the menu
  57. var rightIndex = parentIndex + 2;
  58. }
  59. // get the left offset of the last menu item to line up with
  60. var offsetLast = gamma('#site-menu .menu:first > li:eq('+rightIndex+')').offset();
  61. var lastLeftOffset = offsetLast.left;
  62. // get the width of the last item
  63. var rightWidth = gamma('#site-menu .menu:first > li:eq('+rightIndex+')').outerWidth();
  64. // create the "right offset" of the last item
  65. var rightOffset = lastLeftOffset + rightWidth;
  66. // determine the true width of the dropdown menu
  67. var menuWidth = rightOffset - leftOffset - 1;
  68. // move the child menu to the appropriate position and size
  69. gamma(this).addClass('isPositioned').children('ul').css('width', menuWidth).css('left', leftOffset).slideDown('normal');
  70. },
  71. // hover out
  72. function(){
  73. gamma(this).stop().children('ul').slideUp('normal').end();
  74. // don't re-animate the active item
  75. if(!gamma(this).hasClass('active-trail')) {
  76. gamma(this).children('a').stop().animate({
  77. "color": "#EEE",
  78. }, 1000, function() {
  79. });
  80. gamma(this).animate({
  81. "backgroundColor": "#8d8c8c",
  82. }, 1000, function() {
  83. gamma(this).removeClass('hovering');
  84. });
  85. }
  86. });
  87. /**
  88. * Fade in the user menu on page load and match the line height to the height of the #branding element
  89. */
  90. var logo_height = gamma('#branding').height();
  91. var user_menu_height = gamma('#user-menu').height();
  92. if (logo_height < user_menu_height) {
  93. logo_height = 60;
  94. }
  95. var useMargin = Math.round((logo_height - user_menu_height) / 2);
  96. gamma('#user-menu').css('margin-top', useMargin).fadeIn('fast');
  97. /**
  98. * Respect primary menu trails, regardless of menu structure
  99. */
  100. var currentURL = document.location.pathname;
  101. var activeTopLevelPageArray = currentURL.split('/');
  102. var activeTopLevelPage = activeTopLevelPageArray[1];
  103. // applying the class to what we find
  104. gamma('#site-menu ul li a[href^=/'+activeTopLevelPage+']')
  105. .addClass('active');
  106. // now making the nav menu do what we want with styles for the active element
  107. gamma('#site-menu ul li a.active:first')
  108. .addClass('current-main') // assign main active bg to active link
  109. .parent('li') // select the parent list item
  110. .addClass('active-trail'); // then assign a class to it for manipulation
  111. /* If there is no match, just activate the "home" menu item/tab */
  112. var isActive = gamma('#site-menu li.active-trail').size();
  113. if (isActive < 1) {
  114. gamma('#site-menu li:first').addClass('active-trail');
  115. }
  116. // make the service links images sexy
  117. /*
  118. gamma('.service-links img').each(function(){
  119. initImage(this);
  120. });
  121. */
  122. });
  123. })(jQuery);