123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- /*jslint unparam: true, browser: true, indent: 2 */
- ;(function ($, window, document, undefined) {
- 'use strict';
- Foundation.libs.topbar = {
- name : 'topbar',
- version : '4.2.0',
- settings : {
- index : 0,
- stickyClass : 'sticky',
- custom_back_text: true,
- back_text: 'Back',
- is_hover: true,
- scrolltop : true, // jump to top when sticky nav menu toggle is clicked
- init : false
- },
- init : function (section, method, options) {
- Foundation.inherit(this, 'data_options');
- var self = this;
- if (typeof method === 'object') {
- $.extend(true, this.settings, method);
- } else if (typeof options !== 'undefined') {
- $.extend(true, this.settings, options);
- }
- if (typeof method != 'string') {
- $('.top-bar, [data-topbar]').each(function () {
- $.extend(true, self.settings, self.data_options($(this)));
- self.settings.$w = $(window);
- self.settings.$topbar = $(this);
- self.settings.$section = self.settings.$topbar.find('section');
- self.settings.$titlebar = self.settings.$topbar.children('ul').first();
- self.settings.$topbar.data('index', 0);
- var breakpoint = $("<div class='top-bar-js-breakpoint'/>").insertAfter(self.settings.$topbar);
- self.settings.breakPoint = breakpoint.width();
- breakpoint.remove();
- self.assemble();
- if (self.settings.$topbar.parent().hasClass('fixed')) {
- $('body').css('padding-top', self.outerHeight(self.settings.$topbar));
- }
- });
- if (!self.settings.init) {
- this.events();
- }
- return this.settings.init;
- } else {
- // fire method
- return this[method].call(this, options);
- }
- },
- events : function () {
- var self = this;
- var offst = this.outerHeight($('.top-bar, [data-topbar]'));
- $(this.scope)
- .off('.fndtn.topbar')
- .on('click.fndtn.topbar', '.top-bar .toggle-topbar, [data-topbar] .toggle-topbar', function (e) {
- var topbar = $(this).closest('.top-bar, [data-topbar]'),
- section = topbar.find('section, .section'),
- titlebar = topbar.children('ul').first();
- e.preventDefault();
- if (self.breakpoint()) {
- if (!self.rtl) {
- section.css({left: '0%'});
- section.find('>.name').css({left: '100%'});
- } else {
- section.css({right: '0%'});
- section.find('>.name').css({right: '100%'});
- }
- section.find('li.moved').removeClass('moved');
- topbar.data('index', 0);
- topbar
- .toggleClass('expanded')
- .css('min-height', '');
- }
- if (!topbar.hasClass('expanded')) {
- if (topbar.hasClass('fixed')) {
- topbar.parent().addClass('fixed');
- topbar.removeClass('fixed');
- $('body').css('padding-top',offst);
- }
- } else if (topbar.parent().hasClass('fixed')) {
- topbar.parent().removeClass('fixed');
- topbar.addClass('fixed');
- $('body').css('padding-top','0');
- if (self.settings.scrolltop) {
- window.scrollTo(0,0);
- }
- }
- })
- .on('mouseenter mouseleave', '.top-bar li', function (e) {
- if (!self.settings.is_hover) return;
- if (/enter|over/i.test(e.type)) {
- $(this).addClass('hover');
- } else {
- $(this).removeClass('hover');
- }
- })
- .on('click.fndtn.topbar', '.top-bar li.has-dropdown', function (e) {
- if (self.breakpoint()) return;
- var li = $(this),
- target = $(e.target),
- topbar = li.closest('[data-topbar], .top-bar'),
- is_hover = topbar.data('topbar');
- if (self.settings.is_hover && !Modernizr.touch) return;
- e.stopImmediatePropagation();
- if (target[0].nodeName === 'A' && target.parent().hasClass('has-dropdown')) {
- e.preventDefault();
- }
- if (li.hasClass('hover')) {
- li
- .removeClass('hover')
- .find('li')
- .removeClass('hover');
- } else {
- li.addClass('hover');
- }
- })
- .on('click.fndtn.topbar', '.top-bar .has-dropdown>a, [data-topbar] .has-dropdown>a', function (e) {
- if (self.breakpoint()) {
- e.preventDefault();
- var $this = $(this),
- topbar = $this.closest('.top-bar, [data-topbar]'),
- section = topbar.find('section, .section'),
- titlebar = topbar.children('ul').first(),
- dropdownHeight = $this.next('.dropdown').outerHeight(),
- $selectedLi = $this.closest('li');
- topbar.data('index', topbar.data('index') + 1);
- $selectedLi.addClass('moved');
- if (!self.rtl) {
- section.css({left: -(100 * topbar.data('index')) + '%'});
- section.find('>.name').css({left: 100 * topbar.data('index') + '%'});
- } else {
- section.css({right: -(100 * topbar.data('index')) + '%'});
- section.find('>.name').css({right: 100 * topbar.data('index') + '%'});
- }
- topbar.css('min-height', self.height($this.siblings('ul')) + self.outerHeight(titlebar, true));
- }
- });
- $(window).on('resize.fndtn.topbar', function () {
- if (!self.breakpoint()) {
- $('.top-bar, [data-topbar]')
- .css('min-height', '')
- .removeClass('expanded')
- .find('li')
- .removeClass('hover');
- }
- }.bind(this));
- $('body').on('click.fndtn.topbar', function (e) {
- var parent = $(e.target).closest('[data-topbar], .top-bar');
- if (parent.length > 0) {
- return;
- }
- $('.top-bar li, [data-topbar] li').removeClass('hover');
- });
- // Go up a level on Click
- $(this.scope).on('click.fndtn', '.top-bar .has-dropdown .back, [data-topbar] .has-dropdown .back', function (e) {
- e.preventDefault();
- var $this = $(this),
- topbar = $this.closest('.top-bar, [data-topbar]'),
- titlebar = topbar.children('ul').first(),
- section = topbar.find('section, .section'),
- $movedLi = $this.closest('li.moved'),
- $previousLevelUl = $movedLi.parent();
- topbar.data('index', topbar.data('index') - 1);
- if (!self.rtl) {
- section.css({left: -(100 * topbar.data('index')) + '%'});
- section.find('>.name').css({left: 100 * topbar.data('index') + '%'});
- } else {
- section.css({right: -(100 * topbar.data('index')) + '%'});
- section.find('>.name').css({right: 100 * topbar.data('index') + '%'});
- }
- if (topbar.data('index') === 0) {
- topbar.css('min-height', 0);
- } else {
- topbar.css('min-height', self.height($previousLevelUl) + self.outerHeight(titlebar, true));
- }
- setTimeout(function () {
- $movedLi.removeClass('moved');
- }, 300);
- });
- },
- breakpoint : function () {
- return $(window).width() <= this.settings.breakPoint || $('html').hasClass('lt-ie9');
- },
- assemble : function () {
- var self = this;
- // Pull element out of the DOM for manipulation
- this.settings.$section.detach();
- this.settings.$section.find('.has-dropdown>a').each(function () {
- var $link = $(this),
- $dropdown = $link.siblings('.dropdown'),
- url = $link.attr('href');
- if (url && url.length > 1) {
- 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>');
- } else {
- var $titleLi = $('<li class="title back js-generated"><h5><a href="#"></a></h5></li>');
- }
- // Copy link to subnav
- if (self.settings.custom_back_text == true) {
- $titleLi.find('h5>a').html('« ' + self.settings.back_text);
- } else {
- $titleLi.find('h5>a').html('« ' + $link.html());
- }
- $dropdown.prepend($titleLi);
- });
- // Put element back in the DOM
- this.settings.$section.appendTo(this.settings.$topbar);
- // check for sticky
- this.sticky();
- },
- height : function (ul) {
- var total = 0,
- self = this;
- ul.find('> li').each(function () { total += self.outerHeight($(this), true); });
- return total;
- },
- sticky : function () {
- var klass = '.' + this.settings.stickyClass;
- if ($(klass).length > 0) {
- var distance = $(klass).length ? $(klass).offset().top: 0,
- $window = $(window);
- var offst = this.outerHeight($('.top-bar'));
- $window.scroll(function() {
- if ($window.scrollTop() >= (distance)) {
- $(klass).addClass("fixed");
- $('body').css('padding-top',offst);
- }
- else if ($window.scrollTop() < distance) {
- $(klass).removeClass("fixed");
- $('body').css('padding-top','0');
- }
- });
- }
- },
- off : function () {
- $(this.scope).off('.fndtn.topbar');
- $(window).off('.fndtn.topbar');
- },
- reflow : function () {}
- };
- }(Foundation.zj, this, this.document));
|