foundation.topbar.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*jslint unparam: true, browser: true, indent: 2 */
  2. ;(function ($, window, document, undefined) {
  3. 'use strict';
  4. Foundation.libs.topbar = {
  5. name : 'topbar',
  6. version : '4.2.0',
  7. settings : {
  8. index : 0,
  9. stickyClass : 'sticky',
  10. custom_back_text: true,
  11. back_text: 'Back',
  12. is_hover: true,
  13. scrolltop : true, // jump to top when sticky nav menu toggle is clicked
  14. init : false
  15. },
  16. init : function (section, method, options) {
  17. Foundation.inherit(this, 'data_options');
  18. var self = this;
  19. if (typeof method === 'object') {
  20. $.extend(true, this.settings, method);
  21. } else if (typeof options !== 'undefined') {
  22. $.extend(true, this.settings, options);
  23. }
  24. if (typeof method != 'string') {
  25. $('.top-bar, [data-topbar]').each(function () {
  26. $.extend(true, self.settings, self.data_options($(this)));
  27. self.settings.$w = $(window);
  28. self.settings.$topbar = $(this);
  29. self.settings.$section = self.settings.$topbar.find('section');
  30. self.settings.$titlebar = self.settings.$topbar.children('ul').first();
  31. self.settings.$topbar.data('index', 0);
  32. var breakpoint = $("<div class='top-bar-js-breakpoint'/>").insertAfter(self.settings.$topbar);
  33. self.settings.breakPoint = breakpoint.width();
  34. breakpoint.remove();
  35. self.assemble();
  36. if (self.settings.$topbar.parent().hasClass('fixed')) {
  37. $('body').css('padding-top', self.outerHeight(self.settings.$topbar));
  38. }
  39. });
  40. if (!self.settings.init) {
  41. this.events();
  42. }
  43. return this.settings.init;
  44. } else {
  45. // fire method
  46. return this[method].call(this, options);
  47. }
  48. },
  49. events : function () {
  50. var self = this;
  51. var offst = this.outerHeight($('.top-bar, [data-topbar]'));
  52. $(this.scope)
  53. .off('.fndtn.topbar')
  54. .on('click.fndtn.topbar', '.top-bar .toggle-topbar, [data-topbar] .toggle-topbar', function (e) {
  55. var topbar = $(this).closest('.top-bar, [data-topbar]'),
  56. section = topbar.find('section, .section'),
  57. titlebar = topbar.children('ul').first();
  58. e.preventDefault();
  59. if (self.breakpoint()) {
  60. if (!self.rtl) {
  61. section.css({left: '0%'});
  62. section.find('>.name').css({left: '100%'});
  63. } else {
  64. section.css({right: '0%'});
  65. section.find('>.name').css({right: '100%'});
  66. }
  67. section.find('li.moved').removeClass('moved');
  68. topbar.data('index', 0);
  69. topbar
  70. .toggleClass('expanded')
  71. .css('min-height', '');
  72. }
  73. if (!topbar.hasClass('expanded')) {
  74. if (topbar.hasClass('fixed')) {
  75. topbar.parent().addClass('fixed');
  76. topbar.removeClass('fixed');
  77. $('body').css('padding-top',offst);
  78. }
  79. } else if (topbar.parent().hasClass('fixed')) {
  80. topbar.parent().removeClass('fixed');
  81. topbar.addClass('fixed');
  82. $('body').css('padding-top','0');
  83. if (self.settings.scrolltop) {
  84. window.scrollTo(0,0);
  85. }
  86. }
  87. })
  88. .on('mouseenter mouseleave', '.top-bar li', function (e) {
  89. if (!self.settings.is_hover) return;
  90. if (/enter|over/i.test(e.type)) {
  91. $(this).addClass('hover');
  92. } else {
  93. $(this).removeClass('hover');
  94. }
  95. })
  96. .on('click.fndtn.topbar', '.top-bar li.has-dropdown', function (e) {
  97. if (self.breakpoint()) return;
  98. var li = $(this),
  99. target = $(e.target),
  100. topbar = li.closest('[data-topbar], .top-bar'),
  101. is_hover = topbar.data('topbar');
  102. if (self.settings.is_hover && !Modernizr.touch) return;
  103. e.stopImmediatePropagation();
  104. if (target[0].nodeName === 'A' && target.parent().hasClass('has-dropdown')) {
  105. e.preventDefault();
  106. }
  107. if (li.hasClass('hover')) {
  108. li
  109. .removeClass('hover')
  110. .find('li')
  111. .removeClass('hover');
  112. } else {
  113. li.addClass('hover');
  114. }
  115. })
  116. .on('click.fndtn.topbar', '.top-bar .has-dropdown>a, [data-topbar] .has-dropdown>a', function (e) {
  117. if (self.breakpoint()) {
  118. e.preventDefault();
  119. var $this = $(this),
  120. topbar = $this.closest('.top-bar, [data-topbar]'),
  121. section = topbar.find('section, .section'),
  122. titlebar = topbar.children('ul').first(),
  123. dropdownHeight = $this.next('.dropdown').outerHeight(),
  124. $selectedLi = $this.closest('li');
  125. topbar.data('index', topbar.data('index') + 1);
  126. $selectedLi.addClass('moved');
  127. if (!self.rtl) {
  128. section.css({left: -(100 * topbar.data('index')) + '%'});
  129. section.find('>.name').css({left: 100 * topbar.data('index') + '%'});
  130. } else {
  131. section.css({right: -(100 * topbar.data('index')) + '%'});
  132. section.find('>.name').css({right: 100 * topbar.data('index') + '%'});
  133. }
  134. topbar.css('min-height', self.height($this.siblings('ul')) + self.outerHeight(titlebar, true));
  135. }
  136. });
  137. $(window).on('resize.fndtn.topbar', function () {
  138. if (!self.breakpoint()) {
  139. $('.top-bar, [data-topbar]')
  140. .css('min-height', '')
  141. .removeClass('expanded')
  142. .find('li')
  143. .removeClass('hover');
  144. }
  145. }.bind(this));
  146. $('body').on('click.fndtn.topbar', function (e) {
  147. var parent = $(e.target).closest('[data-topbar], .top-bar');
  148. if (parent.length > 0) {
  149. return;
  150. }
  151. $('.top-bar li, [data-topbar] li').removeClass('hover');
  152. });
  153. // Go up a level on Click
  154. $(this.scope).on('click.fndtn', '.top-bar .has-dropdown .back, [data-topbar] .has-dropdown .back', function (e) {
  155. e.preventDefault();
  156. var $this = $(this),
  157. topbar = $this.closest('.top-bar, [data-topbar]'),
  158. titlebar = topbar.children('ul').first(),
  159. section = topbar.find('section, .section'),
  160. $movedLi = $this.closest('li.moved'),
  161. $previousLevelUl = $movedLi.parent();
  162. topbar.data('index', topbar.data('index') - 1);
  163. if (!self.rtl) {
  164. section.css({left: -(100 * topbar.data('index')) + '%'});
  165. section.find('>.name').css({left: 100 * topbar.data('index') + '%'});
  166. } else {
  167. section.css({right: -(100 * topbar.data('index')) + '%'});
  168. section.find('>.name').css({right: 100 * topbar.data('index') + '%'});
  169. }
  170. if (topbar.data('index') === 0) {
  171. topbar.css('min-height', 0);
  172. } else {
  173. topbar.css('min-height', self.height($previousLevelUl) + self.outerHeight(titlebar, true));
  174. }
  175. setTimeout(function () {
  176. $movedLi.removeClass('moved');
  177. }, 300);
  178. });
  179. },
  180. breakpoint : function () {
  181. return $(window).width() <= this.settings.breakPoint || $('html').hasClass('lt-ie9');
  182. },
  183. assemble : function () {
  184. var self = this;
  185. // Pull element out of the DOM for manipulation
  186. this.settings.$section.detach();
  187. this.settings.$section.find('.has-dropdown>a').each(function () {
  188. var $link = $(this),
  189. $dropdown = $link.siblings('.dropdown'),
  190. url = $link.attr('href');
  191. if (url && url.length > 1) {
  192. var $titleLi = $('<li class="title back js-generated"><h5><a href="#"></a></h5></li><li><a class="parent-link js-generated" href="' + url + '">' + $link.text() +'</a></li>');
  193. } else {
  194. var $titleLi = $('<li class="title back js-generated"><h5><a href="#"></a></h5></li>');
  195. }
  196. // Copy link to subnav
  197. if (self.settings.custom_back_text == true) {
  198. $titleLi.find('h5>a').html('&laquo; ' + self.settings.back_text);
  199. } else {
  200. $titleLi.find('h5>a').html('&laquo; ' + $link.html());
  201. }
  202. $dropdown.prepend($titleLi);
  203. });
  204. // Put element back in the DOM
  205. this.settings.$section.appendTo(this.settings.$topbar);
  206. // check for sticky
  207. this.sticky();
  208. },
  209. height : function (ul) {
  210. var total = 0,
  211. self = this;
  212. ul.find('> li').each(function () { total += self.outerHeight($(this), true); });
  213. return total;
  214. },
  215. sticky : function () {
  216. var klass = '.' + this.settings.stickyClass;
  217. if ($(klass).length > 0) {
  218. var distance = $(klass).length ? $(klass).offset().top: 0,
  219. $window = $(window);
  220. var offst = this.outerHeight($('.top-bar'));
  221. $window.scroll(function() {
  222. if ($window.scrollTop() >= (distance)) {
  223. $(klass).addClass("fixed");
  224. $('body').css('padding-top',offst);
  225. }
  226. else if ($window.scrollTop() < distance) {
  227. $(klass).removeClass("fixed");
  228. $('body').css('padding-top','0');
  229. }
  230. });
  231. }
  232. },
  233. off : function () {
  234. $(this.scope).off('.fndtn.topbar');
  235. $(window).off('.fndtn.topbar');
  236. },
  237. reflow : function () {}
  238. };
  239. }(Foundation.zj, this, this.document));