foundation.topbar.js 12 KB

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