foundation.topbar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.topbar = {
  4. name : 'topbar',
  5. version: '5.0.1',
  6. settings : {
  7. index : 0,
  8. sticky_class : 'sticky',
  9. custom_back_text: true,
  10. back_text: 'Back',
  11. is_hover: true,
  12. mobile_show_parent_link: false,
  13. scrolltop : true // jump to top when sticky nav menu toggle is clicked
  14. },
  15. init : function (section, method, options) {
  16. Foundation.inherit(this, 'addCustomRule register_media throttle');
  17. var self = this;
  18. self.register_media('topbar', 'foundation-mq-topbar');
  19. this.bindings(method, options);
  20. $('[data-topbar]', this.scope).each(function () {
  21. var topbar = $(this),
  22. settings = topbar.data('topbar-init'),
  23. section = $('section', this),
  24. titlebar = $('> ul', this).first();
  25. topbar.data('index', 0);
  26. var topbarContainer = topbar.parent();
  27. if(topbarContainer.hasClass('fixed') || topbarContainer.hasClass(settings.sticky_class)) {
  28. self.settings.sticky_class = settings.sticky_class;
  29. self.settings.stick_topbar = topbar;
  30. topbar.data('height', topbarContainer.outerHeight());
  31. topbar.data('stickyoffset', topbarContainer.offset().top);
  32. } else {
  33. topbar.data('height', topbar.outerHeight());
  34. }
  35. if (!settings.assembled) self.assemble(topbar);
  36. if (settings.is_hover) {
  37. $('.has-dropdown', topbar).addClass('not-click');
  38. } else {
  39. $('.has-dropdown', topbar).removeClass('not-click');
  40. }
  41. // Pad body when sticky (scrolled) or fixed.
  42. self.addCustomRule('.f-topbar-fixed { padding-top: ' + topbar.data('height') + 'px }');
  43. if (topbarContainer.hasClass('fixed')) {
  44. $('body').addClass('f-topbar-fixed');
  45. }
  46. });
  47. },
  48. toggle: function (toggleEl) {
  49. var self = this;
  50. if (toggleEl) {
  51. var topbar = $(toggleEl).closest('[data-topbar]');
  52. } else {
  53. var topbar = $('[data-topbar]');
  54. }
  55. var settings = topbar.data('topbar-init');
  56. var section = $('section, .section', topbar);
  57. if (self.breakpoint()) {
  58. if (!self.rtl) {
  59. section.css({left: '0%'});
  60. $('>.name', section).css({left: '100%'});
  61. } else {
  62. section.css({right: '0%'});
  63. $('>.name', section).css({right: '100%'});
  64. }
  65. $('li.moved', section).removeClass('moved');
  66. topbar.data('index', 0);
  67. topbar
  68. .toggleClass('expanded')
  69. .css('height', '');
  70. }
  71. if (settings.scrolltop) {
  72. if (!topbar.hasClass('expanded')) {
  73. if (topbar.hasClass('fixed')) {
  74. topbar.parent().addClass('fixed');
  75. topbar.removeClass('fixed');
  76. $('body').addClass('f-topbar-fixed');
  77. }
  78. } else if (topbar.parent().hasClass('fixed')) {
  79. if (settings.scrolltop) {
  80. topbar.parent().removeClass('fixed');
  81. topbar.addClass('fixed');
  82. $('body').removeClass('f-topbar-fixed');
  83. window.scrollTo(0,0);
  84. } else {
  85. topbar.parent().removeClass('expanded');
  86. }
  87. }
  88. } else {
  89. if(topbar.parent().hasClass(self.settings.sticky_class)) {
  90. topbar.parent().addClass('fixed');
  91. }
  92. if(topbar.parent().hasClass('fixed')) {
  93. if (!topbar.hasClass('expanded')) {
  94. topbar.removeClass('fixed');
  95. topbar.parent().removeClass('expanded');
  96. self.update_sticky_positioning();
  97. } else {
  98. topbar.addClass('fixed');
  99. topbar.parent().addClass('expanded');
  100. }
  101. }
  102. }
  103. },
  104. timer : null,
  105. events : function (bar) {
  106. var self = this;
  107. $(this.scope)
  108. .off('.topbar')
  109. .on('click.fndtn.topbar', '[data-topbar] .toggle-topbar', function (e) {
  110. e.preventDefault();
  111. self.toggle(this);
  112. })
  113. .on('click.fndtn.topbar', '[data-topbar] li.has-dropdown', function (e) {
  114. var li = $(this),
  115. target = $(e.target),
  116. topbar = li.closest('[data-topbar]'),
  117. settings = topbar.data('topbar-init');
  118. if(target.data('revealId')) {
  119. self.toggle();
  120. return;
  121. }
  122. if (self.breakpoint()) return;
  123. if (settings.is_hover && !Modernizr.touch) return;
  124. e.stopImmediatePropagation();
  125. if (li.hasClass('hover')) {
  126. li
  127. .removeClass('hover')
  128. .find('li')
  129. .removeClass('hover');
  130. li.parents('li.hover')
  131. .removeClass('hover');
  132. } else {
  133. li.addClass('hover');
  134. if (target[0].nodeName === 'A' && target.parent().hasClass('has-dropdown')) {
  135. e.preventDefault();
  136. }
  137. }
  138. })
  139. .on('click.fndtn.topbar', '[data-topbar] .has-dropdown>a', function (e) {
  140. if (self.breakpoint()) {
  141. e.preventDefault();
  142. var $this = $(this),
  143. topbar = $this.closest('[data-topbar]'),
  144. section = topbar.find('section, .section'),
  145. dropdownHeight = $this.next('.dropdown').outerHeight(),
  146. $selectedLi = $this.closest('li');
  147. topbar.data('index', topbar.data('index') + 1);
  148. $selectedLi.addClass('moved');
  149. if (!self.rtl) {
  150. section.css({left: -(100 * topbar.data('index')) + '%'});
  151. section.find('>.name').css({left: 100 * topbar.data('index') + '%'});
  152. } else {
  153. section.css({right: -(100 * topbar.data('index')) + '%'});
  154. section.find('>.name').css({right: 100 * topbar.data('index') + '%'});
  155. }
  156. topbar.css('height', $this.siblings('ul').outerHeight(true) + topbar.data('height'));
  157. }
  158. });
  159. $(window).off('.topbar').on('resize.fndtn.topbar', self.throttle(function () {
  160. self.resize.call(self);
  161. }, 50)).trigger('resize');
  162. $('body').off('.topbar').on('click.fndtn.topbar touchstart.fndtn.topbar', function (e) {
  163. var parent = $(e.target).closest('li').closest('li.hover');
  164. if (parent.length > 0) {
  165. return;
  166. }
  167. $('[data-topbar] li').removeClass('hover');
  168. });
  169. // Go up a level on Click
  170. $(this.scope).on('click.fndtn.topbar', '[data-topbar] .has-dropdown .back', function (e) {
  171. e.preventDefault();
  172. var $this = $(this),
  173. topbar = $this.closest('[data-topbar]'),
  174. section = topbar.find('section, .section'),
  175. settings = topbar.data('topbar-init'),
  176. $movedLi = $this.closest('li.moved'),
  177. $previousLevelUl = $movedLi.parent();
  178. topbar.data('index', topbar.data('index') - 1);
  179. if (!self.rtl) {
  180. section.css({left: -(100 * topbar.data('index')) + '%'});
  181. section.find('>.name').css({left: 100 * topbar.data('index') + '%'});
  182. } else {
  183. section.css({right: -(100 * topbar.data('index')) + '%'});
  184. section.find('>.name').css({right: 100 * topbar.data('index') + '%'});
  185. }
  186. if (topbar.data('index') === 0) {
  187. topbar.css('height', '');
  188. } else {
  189. topbar.css('height', $previousLevelUl.outerHeight(true) + topbar.data('height'));
  190. }
  191. setTimeout(function () {
  192. $movedLi.removeClass('moved');
  193. }, 300);
  194. });
  195. },
  196. resize : function () {
  197. var self = this;
  198. $('[data-topbar]').each(function () {
  199. var topbar = $(this),
  200. settings = topbar.data('topbar-init');
  201. var stickyContainer = topbar.parent('.' + self.settings.sticky_class);
  202. var stickyOffset;
  203. if (!self.breakpoint()) {
  204. var doToggle = topbar.hasClass('expanded');
  205. topbar
  206. .css('height', '')
  207. .removeClass('expanded')
  208. .find('li')
  209. .removeClass('hover');
  210. if(doToggle) {
  211. self.toggle(topbar);
  212. }
  213. }
  214. if(stickyContainer.length > 0) {
  215. if(stickyContainer.hasClass('fixed')) {
  216. // Remove the fixed to allow for correct calculation of the offset.
  217. stickyContainer.removeClass('fixed');
  218. stickyOffset = stickyContainer.offset().top;
  219. if($(document.body).hasClass('f-topbar-fixed')) {
  220. stickyOffset -= topbar.data('height');
  221. }
  222. topbar.data('stickyoffset', stickyOffset);
  223. stickyContainer.addClass('fixed');
  224. } else {
  225. stickyOffset = stickyContainer.offset().top;
  226. topbar.data('stickyoffset', stickyOffset);
  227. }
  228. }
  229. });
  230. },
  231. breakpoint : function () {
  232. return !matchMedia(Foundation.media_queries['topbar']).matches;
  233. },
  234. assemble : function (topbar) {
  235. var self = this,
  236. settings = topbar.data('topbar-init'),
  237. section = $('section', topbar),
  238. titlebar = $('> ul', topbar).first();
  239. // Pull element out of the DOM for manipulation
  240. section.detach();
  241. $('.has-dropdown>a', section).each(function () {
  242. var $link = $(this),
  243. $dropdown = $link.siblings('.dropdown'),
  244. url = $link.attr('href');
  245. if (settings.mobile_show_parent_link && url && url.length > 1) {
  246. 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>');
  247. } else {
  248. var $titleLi = $('<li class="title back js-generated"><h5><a href="#"></a></h5></li>');
  249. }
  250. // Copy link to subnav
  251. if (settings.custom_back_text == true) {
  252. $('h5>a', $titleLi).html(settings.back_text);
  253. } else {
  254. $('h5>a', $titleLi).html('&laquo; ' + $link.html());
  255. }
  256. $dropdown.prepend($titleLi);
  257. });
  258. // Put element back in the DOM
  259. section.appendTo(topbar);
  260. // check for sticky
  261. this.sticky();
  262. this.assembled(topbar);
  263. },
  264. assembled : function (topbar) {
  265. topbar.data('topbar-init', $.extend({}, topbar.data('topbar-init'), {assembled: true}));
  266. },
  267. height : function (ul) {
  268. var total = 0,
  269. self = this;
  270. $('> li', ul).each(function () { total += $(this).outerHeight(true); });
  271. return total;
  272. },
  273. sticky : function () {
  274. var $window = $(window),
  275. self = this;
  276. $(window).on('scroll', function() {
  277. self.update_sticky_positioning();
  278. });
  279. },
  280. update_sticky_positioning: function() {
  281. var klass = '.' + this.settings.sticky_class;
  282. var $window = $(window);
  283. if ($(klass).length > 0) {
  284. var distance = this.settings.sticky_topbar.data('stickyoffset');
  285. if (!$(klass).hasClass('expanded')) {
  286. if ($window.scrollTop() > (distance)) {
  287. if (!$(klass).hasClass('fixed')) {
  288. $(klass).addClass('fixed');
  289. $('body').addClass('f-topbar-fixed');
  290. }
  291. } else if ($window.scrollTop() <= distance) {
  292. if ($(klass).hasClass('fixed')) {
  293. $(klass).removeClass('fixed');
  294. $('body').removeClass('f-topbar-fixed');
  295. }
  296. }
  297. }
  298. }
  299. },
  300. off : function () {
  301. $(this.scope).off('.fndtn.topbar');
  302. $(window).off('.fndtn.topbar');
  303. },
  304. reflow : function () {}
  305. };
  306. }(jQuery, this, this.document));