PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/assets/javascripts/foundation/jquery.foundation.topbar.js

https://bitbucket.org/viswanath608/foundation
JavaScript | 177 lines | 128 code | 35 blank | 14 comment | 20 complexity | 0678990d78b862c3e2afaa208a08b84e MD5 | raw file
  1. /*
  2. * jQuery Foundation Top Bar 2.0.4
  3. * http://foundation.zurb.com
  4. * Copyright 2012, ZURB
  5. * Free to use under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. /*jslint unparam: true, browser: true, indent: 2 */
  9. ;(function ($, window, undefined) {
  10. 'use strict';
  11. var settings = {
  12. index : 0,
  13. initialized : false
  14. },
  15. methods = {
  16. init : function (options) {
  17. return this.each(function () {
  18. settings = $.extend(settings, options);
  19. settings.$w = $(window),
  20. settings.$topbar = $('nav.top-bar'),
  21. settings.$section = settings.$topbar.find('section'),
  22. settings.$titlebar = settings.$topbar.children('ul:first');
  23. var breakpoint = $("<div class='top-bar-js-breakpoint'/>").appendTo("body");
  24. settings.breakPoint = breakpoint.width();
  25. breakpoint.remove();
  26. if (!settings.initialized) {
  27. methods.assemble();
  28. settings.initialized = true;
  29. }
  30. if (!settings.height) {
  31. methods.largestUL();
  32. }
  33. if (settings.$topbar.parent().hasClass('fixed')) {
  34. $('body').css('padding-top',settings.$topbar.outerHeight())
  35. }
  36. $('.top-bar .toggle-topbar').off('click.fndtn').on('click.fndtn', function (e) {
  37. e.preventDefault();
  38. if (methods.breakpoint()) {
  39. settings.$topbar.toggleClass('expanded');
  40. settings.$topbar.css('min-height', '');
  41. }
  42. if (!settings.$topbar.hasClass('expanded')) {
  43. settings.$section.css({left: '0%'});
  44. settings.$section.find('>.name').css({left: '100%'});
  45. settings.$section.find('li.moved').removeClass('moved');
  46. settings.index = 0;
  47. }
  48. });
  49. // Show the Dropdown Levels on Click
  50. $('.top-bar .has-dropdown>a').off('click.fndtn').on('click.fndtn', function (e) {
  51. if (Modernizr.touch || methods.breakpoint())
  52. e.preventDefault();
  53. if (methods.breakpoint()) {
  54. var $this = $(this),
  55. $selectedLi = $this.closest('li');
  56. settings.index += 1;
  57. $selectedLi.addClass('moved');
  58. settings.$section.css({left: -(100 * settings.index) + '%'});
  59. settings.$section.find('>.name').css({left: 100 * settings.index + '%'});
  60. $this.siblings('ul').height(settings.height + settings.$titlebar.outerHeight(true));
  61. settings.$topbar.css('min-height', settings.height + settings.$titlebar.outerHeight(true) * 2)
  62. }
  63. });
  64. $(window).on('resize.fndtn.topbar',function() {
  65. if (!methods.breakpoint()) {
  66. settings.$topbar.css('min-height', '');
  67. }
  68. });
  69. // Go up a level on Click
  70. $('.top-bar .has-dropdown .back').off('click.fndtn').on('click.fndtn', function (e) {
  71. e.preventDefault();
  72. var $this = $(this),
  73. $movedLi = $this.closest('li.moved'),
  74. $previousLevelUl = $movedLi.parent();
  75. settings.index -= 1;
  76. settings.$section.css({left: -(100 * settings.index) + '%'});
  77. settings.$section.find('>.name').css({'left': 100 * settings.index + '%'});
  78. if (settings.index === 0) {
  79. settings.$topbar.css('min-height', 0);
  80. }
  81. setTimeout(function () {
  82. $movedLi.removeClass('moved');
  83. }, 300);
  84. });
  85. });
  86. },
  87. breakpoint : function () {
  88. return settings.$w.width() < settings.breakPoint;
  89. },
  90. assemble : function () {
  91. // Pull element out of the DOM for manipulation
  92. settings.$section.detach();
  93. settings.$section.find('.has-dropdown>a').each(function () {
  94. var $link = $(this),
  95. $dropdown = $link.siblings('.dropdown'),
  96. $titleLi = $('<li class="title back js-generated"><h5><a href="#"></a></h5></li>');
  97. // Copy link to subnav
  98. $titleLi.find('h5>a').html($link.html());
  99. $dropdown.prepend($titleLi);
  100. });
  101. // Put element back in the DOM
  102. settings.$section.appendTo(settings.$topbar);
  103. },
  104. largestUL : function () {
  105. var uls = settings.$topbar.find('section ul ul'),
  106. largest = uls.first(),
  107. total = 0;
  108. uls.each(function () {
  109. if ($(this).children('li').length > largest.children('li').length) {
  110. largest = $(this);
  111. }
  112. });
  113. largest.children('li').each(function () { total += $(this).outerHeight(true); });
  114. settings.height = total;
  115. }
  116. };
  117. $.fn.foundationTopBar = function (method) {
  118. if (methods[method]) {
  119. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  120. } else if (typeof method === 'object' || !method) {
  121. return methods.init.apply(this, arguments);
  122. } else {
  123. $.error('Method ' + method + ' does not exist on jQuery.foundationTopBar');
  124. }
  125. };
  126. // Monitor scroll position for sticky
  127. if ($('.sticky').length > 0) {
  128. var distance = $('.sticky').length ? $('.sticky').offset().top: 0,
  129. $window = $(window);
  130. var offst = $('nav.top-bar').outerHeight()+20;
  131. $window.scroll(function() {
  132. if ( $window.scrollTop() >= ( distance ) ) {
  133. $(".sticky").addClass("fixed");
  134. $('body').css('padding-top',offst);
  135. }
  136. else if ( $window.scrollTop() < distance ) {
  137. $(".sticky").removeClass("fixed");
  138. $('body').css('padding-top','0');
  139. }
  140. });
  141. }
  142. }(jQuery, this));