foundation.topbar.js 15 KB

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