foundation.topbar.js 13 KB

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