123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- ;(function ($, document, window) {
- var
-
- defaults = {
- label: 'MENU',
- duplicate: true,
- duration: 200,
- easingOpen: 'swing',
- easingClose: 'swing',
- closedSymbol: '►',
- openedSymbol: '▼',
- prependTo: 'body',
- appendTo: '',
- parentTag: 'a',
- closeOnClick: false,
- allowParentLinks: false,
- nestedParentLinks: true,
- showChildren: false,
- removeIds: true,
- removeClasses: false,
- removeStyles: false,
- brand: '',
- animations: 'jquery',
- init: function () {},
- beforeOpen: function () {},
- beforeClose: function () {},
- afterOpen: function () {},
- afterClose: function () {}
- },
- mobileMenu = 'slicknav',
- prefix = 'slicknav',
- Keyboard = {
- DOWN: 40,
- ENTER: 13,
- ESCAPE: 27,
- LEFT: 37,
- RIGHT: 39,
- SPACE: 32,
- TAB: 9,
- UP: 38,
- };
- function Plugin(element, options) {
- this.element = element;
-
-
-
-
- this.settings = $.extend({}, defaults, options);
-
- if (!this.settings.duplicate && !options.hasOwnProperty("removeIds")) {
- this.settings.removeIds = false;
- }
- this._defaults = defaults;
- this._name = mobileMenu;
- this.init();
- }
- Plugin.prototype.init = function () {
- var $this = this,
- menu = $(this.element),
- settings = this.settings,
- iconClass,
- menuBar;
-
- if (settings.duplicate) {
- $this.mobileNav = menu.clone();
- } else {
- $this.mobileNav = menu;
- }
-
- if (settings.removeIds) {
- $this.mobileNav.removeAttr('id');
- $this.mobileNav.find('*').each(function (i, e) {
- $(e).removeAttr('id');
- });
- }
-
- if (settings.removeClasses) {
- $this.mobileNav.removeAttr('class');
- $this.mobileNav.find('*').each(function (i, e) {
- $(e).removeAttr('class');
- });
- }
-
- if (settings.removeStyles) {
- $this.mobileNav.removeAttr('style');
- $this.mobileNav.find('*').each(function (i, e) {
- $(e).removeAttr('style');
- });
- }
-
- iconClass = prefix + '_icon';
- if (settings.label === '') {
- iconClass += ' ' + prefix + '_no-text';
- }
- if (settings.parentTag == 'a') {
- settings.parentTag = 'a href="#"';
- }
-
- $this.mobileNav.attr('class', prefix + '_nav');
- menuBar = $('<div class="' + prefix + '_menu"></div>');
- if (settings.brand !== '') {
- var brand = $('<div class="' + prefix + '_brand">'+settings.brand+'</div>');
- $(menuBar).append(brand);
- }
- $this.btn = $(
- ['<' + settings.parentTag + ' aria-haspopup="true" role="button" tabindex="0" class="' + prefix + '_btn ' + prefix + '_collapsed">',
- '<span class="' + prefix + '_menutxt">' + settings.label + '</span>',
- '<span class="' + iconClass + '">',
- '<span class="' + prefix + '_icon-bar"></span>',
- '<span class="' + prefix + '_icon-bar"></span>',
- '<span class="' + prefix + '_icon-bar"></span>',
- '</span>',
- '</' + settings.parentTag + '>'
- ].join('')
- );
- $(menuBar).append($this.btn);
- if(settings.appendTo !== '') {
- $(settings.appendTo).append(menuBar);
- } else {
- $(settings.prependTo).prepend(menuBar);
- }
- menuBar.append($this.mobileNav);
-
- var items = $this.mobileNav.find('li');
- $(items).each(function () {
- var item = $(this),
- data = {};
- data.children = item.children('ul').attr('role', 'menu');
- item.data('menu', data);
-
- if (data.children.length > 0) {
-
-
- var a = item.contents(),
- containsAnchor = false,
- nodes = [];
- $(a).each(function () {
- if (!$(this).is('ul')) {
- nodes.push(this);
- } else {
- return false;
- }
- if($(this).is("a")) {
- containsAnchor = true;
- }
- });
- var wrapElement = $(
- '<' + settings.parentTag + ' role="menuitem" aria-haspopup="true" tabindex="-1" class="' + prefix + '_item"/>'
- );
-
- if ((!settings.allowParentLinks || settings.nestedParentLinks) || !containsAnchor) {
- var $wrap = $(nodes).wrapAll(wrapElement).parent();
- $wrap.addClass(prefix+'_row');
- } else
- $(nodes).wrapAll('<span class="'+prefix+'_parent-link '+prefix+'_row"/>').parent();
- if (!settings.showChildren) {
- item.addClass(prefix+'_collapsed');
- } else {
- item.addClass(prefix+'_open');
- }
- item.addClass(prefix+'_parent');
-
- var arrowElement = $('<span class="'+prefix+'_arrow">'+(settings.showChildren?settings.openedSymbol:settings.closedSymbol)+'</span>');
- if (settings.allowParentLinks && !settings.nestedParentLinks && containsAnchor)
- arrowElement = arrowElement.wrap(wrapElement).parent();
-
- $(nodes).last().after(arrowElement);
- } else if ( item.children().length === 0) {
- item.addClass(prefix+'_txtnode');
- }
-
- item.children('a').attr('role', 'menuitem').click(function(event){
-
- if (settings.closeOnClick && !$(event.target).parent().closest('li').hasClass(prefix+'_parent')) {
-
- $($this.btn).click();
- }
- });
-
- if (settings.closeOnClick && settings.allowParentLinks) {
- item.children('a').children('a').click(function (event) {
-
- $($this.btn).click();
- });
- item.find('.'+prefix+'_parent-link a:not(.'+prefix+'_item)').click(function(event){
-
- $($this.btn).click();
- });
- }
- });
-
- $(items).each(function () {
- var data = $(this).data('menu');
- if (!settings.showChildren){
- $this._visibilityToggle(data.children, null, false, null, true);
- }
- });
-
- $this._visibilityToggle($this.mobileNav, null, false, 'init', true);
-
- $this.mobileNav.attr('role','menu');
-
- $(document).mousedown(function(){
- $this._outlines(false);
- });
- $(document).keyup(function(){
- $this._outlines(true);
- });
-
- $($this.btn).click(function (e) {
- e.preventDefault();
- $this._menuToggle();
- });
-
- $this.mobileNav.on('click', '.' + prefix + '_item', function (e) {
- e.preventDefault();
- $this._itemClick($(this));
- });
-
- $($this.btn).keydown(function (e) {
- var ev = e || event;
- switch(ev.keyCode) {
- case Keyboard.ENTER:
- case Keyboard.SPACE:
- case Keyboard.DOWN:
- e.preventDefault();
- if (ev.keyCode !== Keyboard.DOWN || !$($this.btn).hasClass(prefix+'_open')){
- $this._menuToggle();
- }
- $($this.btn).next().find('[role="menuitem"]').first().focus();
- break;
- }
- });
- $this.mobileNav.on('keydown', '.'+prefix+'_item', function(e) {
- var ev = e || event;
- switch(ev.keyCode) {
- case Keyboard.ENTER:
- e.preventDefault();
- $this._itemClick($(e.target));
- break;
- case Keyboard.RIGHT:
- e.preventDefault();
- if ($(e.target).parent().hasClass(prefix+'_collapsed')) {
- $this._itemClick($(e.target));
- }
- $(e.target).next().find('[role="menuitem"]').first().focus();
- break;
- }
- });
- $this.mobileNav.on('keydown', '[role="menuitem"]', function(e) {
- var ev = e || event;
- switch(ev.keyCode){
- case Keyboard.DOWN:
- e.preventDefault();
- var allItems = $(e.target).parent().parent().children().children('[role="menuitem"]:visible');
- var idx = allItems.index( e.target );
- var nextIdx = idx + 1;
- if (allItems.length <= nextIdx) {
- nextIdx = 0;
- }
- var next = allItems.eq( nextIdx );
- next.focus();
- break;
- case Keyboard.UP:
- e.preventDefault();
- var allItems = $(e.target).parent().parent().children().children('[role="menuitem"]:visible');
- var idx = allItems.index( e.target );
- var next = allItems.eq( idx - 1 );
- next.focus();
- break;
- case Keyboard.LEFT:
- e.preventDefault();
- if ($(e.target).parent().parent().parent().hasClass(prefix+'_open')) {
- var parent = $(e.target).parent().parent().prev();
- parent.focus();
- $this._itemClick(parent);
- } else if ($(e.target).parent().parent().hasClass(prefix+'_nav')){
- $this._menuToggle();
- $($this.btn).focus();
- }
- break;
- case Keyboard.ESCAPE:
- e.preventDefault();
- $this._menuToggle();
- $($this.btn).focus();
- break;
- }
- });
-
- if (settings.allowParentLinks && settings.nestedParentLinks) {
- $('.'+prefix+'_item a').click(function(e){
- e.stopImmediatePropagation();
- });
- }
- };
-
- Plugin.prototype._menuToggle = function (el) {
- var $this = this;
- var btn = $this.btn;
- var mobileNav = $this.mobileNav;
- if (btn.hasClass(prefix+'_collapsed')) {
- btn.removeClass(prefix+'_collapsed');
- btn.addClass(prefix+'_open');
- } else {
- btn.removeClass(prefix+'_open');
- btn.addClass(prefix+'_collapsed');
- }
- btn.addClass(prefix+'_animating');
- $this._visibilityToggle(mobileNav, btn.parent(), true, btn);
- };
-
- Plugin.prototype._itemClick = function (el) {
- var $this = this;
- var settings = $this.settings;
- var data = el.data('menu');
- if (!data) {
- data = {};
- data.arrow = el.children('.'+prefix+'_arrow');
- data.ul = el.next('ul');
- data.parent = el.parent();
-
- if (data.parent.hasClass(prefix+'_parent-link')) {
- data.parent = el.parent().parent();
- data.ul = el.parent().next('ul');
- }
- el.data('menu', data);
- }
- if (data.parent.hasClass(prefix+'_collapsed')) {
- data.arrow.html(settings.openedSymbol);
- data.parent.removeClass(prefix+'_collapsed');
- data.parent.addClass(prefix+'_open');
- data.parent.addClass(prefix+'_animating');
- $this._visibilityToggle(data.ul, data.parent, true, el);
- } else {
- data.arrow.html(settings.closedSymbol);
- data.parent.addClass(prefix+'_collapsed');
- data.parent.removeClass(prefix+'_open');
- data.parent.addClass(prefix+'_animating');
- $this._visibilityToggle(data.ul, data.parent, true, el);
- }
- };
-
- Plugin.prototype._visibilityToggle = function(el, parent, animate, trigger, init) {
- var $this = this;
- var settings = $this.settings;
- var items = $this._getActionItems(el);
- var duration = 0;
- if (animate) {
- duration = settings.duration;
- }
- function afterOpen(trigger, parent) {
- $(trigger).removeClass(prefix+'_animating');
- $(parent).removeClass(prefix+'_animating');
-
- if (!init) {
- settings.afterOpen(trigger);
- }
- }
- function afterClose(trigger, parent) {
- el.attr('aria-hidden','true');
- items.attr('tabindex', '-1');
- $this._setVisAttr(el, true);
- el.hide();
- $(trigger).removeClass(prefix+'_animating');
- $(parent).removeClass(prefix+'_animating');
-
- if (!init){
- settings.afterClose(trigger);
- } else if (trigger == 'init'){
- settings.init();
- }
- }
- if (el.hasClass(prefix+'_hidden')) {
- el.removeClass(prefix+'_hidden');
-
- if (!init) {
- settings.beforeOpen(trigger);
- }
- if (settings.animations === 'jquery') {
- el.stop(true,true).slideDown(duration, settings.easingOpen, function(){
- afterOpen(trigger, parent);
- });
- } else if(settings.animations === 'velocity') {
- el.velocity("finish").velocity("slideDown", {
- duration: duration,
- easing: settings.easingOpen,
- complete: function() {
- afterOpen(trigger, parent);
- }
- });
- }
- el.attr('aria-hidden','false');
- items.attr('tabindex', '0');
- $this._setVisAttr(el, false);
- } else {
- el.addClass(prefix+'_hidden');
-
- if (!init){
- settings.beforeClose(trigger);
- }
- if (settings.animations === 'jquery') {
- el.stop(true,true).slideUp(duration, this.settings.easingClose, function() {
- afterClose(trigger, parent)
- });
- } else if (settings.animations === 'velocity') {
- el.velocity("finish").velocity("slideUp", {
- duration: duration,
- easing: settings.easingClose,
- complete: function() {
- afterClose(trigger, parent);
- }
- });
- }
- }
- };
-
- Plugin.prototype._setVisAttr = function(el, hidden) {
- var $this = this;
-
- var nonHidden = el.children('li').children('ul').not('.'+prefix+'_hidden');
-
- if (!hidden) {
- nonHidden.each(function(){
- var ul = $(this);
- ul.attr('aria-hidden','false');
- var items = $this._getActionItems(ul);
- items.attr('tabindex', '0');
- $this._setVisAttr(ul, hidden);
- });
- } else {
- nonHidden.each(function(){
- var ul = $(this);
- ul.attr('aria-hidden','true');
- var items = $this._getActionItems(ul);
- items.attr('tabindex', '-1');
- $this._setVisAttr(ul, hidden);
- });
- }
- };
-
- Plugin.prototype._getActionItems = function(el) {
- var data = el.data("menu");
- if (!data) {
- data = {};
- var items = el.children('li');
- var anchors = items.find('a');
- data.links = anchors.add(items.find('.'+prefix+'_item'));
- el.data('menu', data);
- }
- return data.links;
- };
- Plugin.prototype._outlines = function(state) {
- if (!state) {
- $('.'+prefix+'_item, .'+prefix+'_btn').css('outline','none');
- } else {
- $('.'+prefix+'_item, .'+prefix+'_btn').css('outline','');
- }
- };
- Plugin.prototype.toggle = function(){
- var $this = this;
- $this._menuToggle();
- };
- Plugin.prototype.open = function(){
- var $this = this;
- if ($this.btn.hasClass(prefix+'_collapsed')) {
- $this._menuToggle();
- }
- };
- Plugin.prototype.close = function(){
- var $this = this;
- if ($this.btn.hasClass(prefix+'_open')) {
- $this._menuToggle();
- }
- };
- $.fn[mobileMenu] = function ( options ) {
- var args = arguments;
-
- if (options === undefined || typeof options === 'object') {
- return this.each(function () {
-
- if (!$.data(this, 'plugin_' + mobileMenu)) {
-
-
- $.data(this, 'plugin_' + mobileMenu, new Plugin( this, options ));
- }
- });
-
- } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
-
- var returns;
- this.each(function () {
- var instance = $.data(this, 'plugin_' + mobileMenu);
-
- if (instance instanceof Plugin && typeof instance[options] === 'function') {
-
- returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
- }
- });
-
- return returns !== undefined ? returns : this;
- }
- };
- }(jQuery, document, window));
|