Advertisement
humayun180

Slick Nav ( jquery.slicknav.min.js )

Feb 27th, 2020
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 21.17 KB | None | 0 0
  1. SlickNav -
  2.  
  3. /*!
  4.  * SlickNav Responsive Mobile Menu v1.0.10
  5.  * (c) 2016 Josh Cope
  6.  * licensed under MIT
  7.  */
  8. ;(function ($, document, window) {
  9.     var
  10.     // default settings object.
  11.         defaults = {
  12.             label: 'MORE',
  13.             duplicate: true,
  14.             duration: 200,
  15.             easingOpen: 'swing',
  16.             easingClose: 'swing',
  17.             closedSymbol: '►',
  18.             openedSymbol: '▼',
  19.             prependTo: 'body',
  20.             appendTo: '',
  21.             parentTag: 'a',
  22.             closeOnClick: false,
  23.             allowParentLinks: false,
  24.             nestedParentLinks: true,
  25.             showChildren: false,
  26.             removeIds: true,
  27.             removeClasses: false,
  28.             removeStyles: false,
  29.             brand: '',
  30.             animations: 'jquery',
  31.             init: function () {},
  32.             beforeOpen: function () {},
  33.             beforeClose: function () {},
  34.             afterOpen: function () {},
  35.             afterClose: function () {}
  36.         },
  37.         mobileMenu = 'slicknav',
  38.         prefix = 'slicknav',
  39.  
  40.         Keyboard = {
  41.             DOWN: 40,
  42.             ENTER: 13,
  43.             ESCAPE: 27,
  44.             LEFT: 37,
  45.             RIGHT: 39,
  46.             SPACE: 32,
  47.             TAB: 9,
  48.             UP: 38,
  49.         };
  50.  
  51.     function Plugin(element, options) {
  52.         this.element = element;
  53.  
  54.         // jQuery has an extend method which merges the contents of two or
  55.         // more objects, storing the result in the first object. The first object
  56.         // is generally empty as we don't want to alter the default options for
  57.         // future instances of the plugin
  58.         this.settings = $.extend({}, defaults, options);
  59.  
  60.         // Don't remove IDs by default if duplicate is false
  61.         if (!this.settings.duplicate && !options.hasOwnProperty("removeIds")) {
  62.           this.settings.removeIds = false;
  63.         }
  64.  
  65.         this._defaults = defaults;
  66.         this._name = mobileMenu;
  67.  
  68.         this.init();
  69.     }
  70.  
  71.     Plugin.prototype.init = function () {
  72.         var $this = this,
  73.             menu = $(this.element),
  74.             settings = this.settings,
  75.             iconClass,
  76.             menuBar;
  77.  
  78.         // clone menu if needed
  79.         if (settings.duplicate) {
  80.             $this.mobileNav = menu.clone();
  81.         } else {
  82.             $this.mobileNav = menu;
  83.         }
  84.  
  85.         // remove IDs if set
  86.         if (settings.removeIds) {
  87.           $this.mobileNav.removeAttr('id');
  88.           $this.mobileNav.find('*').each(function (i, e) {
  89.               $(e).removeAttr('id');
  90.           });
  91.         }
  92.  
  93.         // remove classes if set
  94.         if (settings.removeClasses) {
  95.             $this.mobileNav.removeAttr('class');
  96.             $this.mobileNav.find('*').each(function (i, e) {
  97.                 $(e).removeAttr('class');
  98.             });
  99.         }
  100.  
  101.         // remove styles if set
  102.         if (settings.removeStyles) {
  103.             $this.mobileNav.removeAttr('style');
  104.             $this.mobileNav.find('*').each(function (i, e) {
  105.                 $(e).removeAttr('style');
  106.             });
  107.         }
  108.  
  109.         // styling class for the button
  110.         iconClass = prefix + '_icon';
  111.  
  112.         if (settings.label === '') {
  113.             iconClass += ' ' + prefix + '_no-text';
  114.         }
  115.  
  116.         if (settings.parentTag == 'a') {
  117.             settings.parentTag = 'a href="#"';
  118.         }
  119.  
  120.         // create menu bar
  121.         $this.mobileNav.attr('class', prefix + '_nav');
  122.         menuBar = $('<div class="' + prefix + '_menu"></div>');
  123.         if (settings.brand !== '') {
  124.             var brand = $('<div class="' + prefix + '_brand">'+settings.brand+'</div>');
  125.             $(menuBar).append(brand);
  126.         }
  127.         $this.btn = $(
  128.             ['<' + settings.parentTag + ' aria-haspopup="true" role="button" tabindex="0" class="' + prefix + '_btn ' + prefix + '_collapsed">',
  129.                 '<span class="' + prefix + '_menutxt">' + settings.label + '</span>',
  130.                 '<span class="' + iconClass + '">',
  131.                     '<span class="' + prefix + '_icon-bar"></span>',
  132.                     '<span class="' + prefix + '_icon-bar"></span>',
  133.                     '<span class="' + prefix + '_icon-bar"></span>',
  134.                 '</span>',
  135.             '</' + settings.parentTag + '>'
  136.             ].join('')
  137.         );
  138.         $(menuBar).append($this.btn);
  139.         if(settings.appendTo !== '') {
  140.             $(settings.appendTo).append(menuBar);
  141.         } else {
  142.             $(settings.prependTo).prepend(menuBar);
  143.         }
  144.         menuBar.append($this.mobileNav);
  145.  
  146.         // iterate over structure adding additional structure
  147.         var items = $this.mobileNav.find('li');
  148.         $(items).each(function () {
  149.             var item = $(this),
  150.                 data = {};
  151.             data.children = item.children('ul').attr('role', 'menu');
  152.             item.data('menu', data);
  153.  
  154.             // if a list item has a nested menu
  155.             if (data.children.length > 0) {
  156.  
  157.                 // select all text before the child menu
  158.                 // check for anchors
  159.  
  160.                 var a = item.contents(),
  161.                     containsAnchor = false,
  162.                     nodes = [];
  163.  
  164.                 $(a).each(function () {
  165.                     if (!$(this).is('ul')) {
  166.                         nodes.push(this);
  167.                     } else {
  168.                         return false;
  169.                     }
  170.  
  171.                     if($(this).is("a")) {
  172.                         containsAnchor = true;
  173.                     }
  174.                 });
  175.  
  176.                 var wrapElement = $(
  177.                     '<' + settings.parentTag + ' role="menuitem" aria-haspopup="true" tabindex="-1" class="' + prefix + '_item"/>'
  178.                 );
  179.  
  180.                 // wrap item text with tag and add classes unless we are separating parent links
  181.                 if ((!settings.allowParentLinks || settings.nestedParentLinks) || !containsAnchor) {
  182.                     var $wrap = $(nodes).wrapAll(wrapElement).parent();
  183.                     $wrap.addClass(prefix+'_row');
  184.                 } else
  185.                     $(nodes).wrapAll('<span class="'+prefix+'_parent-link '+prefix+'_row"/>').parent();
  186.  
  187.                 if (!settings.showChildren) {
  188.                     item.addClass(prefix+'_collapsed');
  189.                 } else {
  190.                     item.addClass(prefix+'_open');
  191.                 }
  192.  
  193.                 item.addClass(prefix+'_parent');
  194.  
  195.                 // create parent arrow. wrap with link if parent links and separating
  196.                 var arrowElement = $('<span class="'+prefix+'_arrow">'+(settings.showChildren?settings.openedSymbol:settings.closedSymbol)+'</span>');
  197.  
  198.                 if (settings.allowParentLinks && !settings.nestedParentLinks && containsAnchor)
  199.                     arrowElement = arrowElement.wrap(wrapElement).parent();
  200.  
  201.                 //append arrow
  202.                 $(nodes).last().after(arrowElement);
  203.  
  204.  
  205.             } else if ( item.children().length === 0) {
  206.                  item.addClass(prefix+'_txtnode');
  207.             }
  208.  
  209.             // accessibility for links
  210.             item.children('a').attr('role', 'menuitem').click(function(event){
  211.                 //Ensure that it's not a parent
  212.                 if (settings.closeOnClick && !$(event.target).parent().closest('li').hasClass(prefix+'_parent')) {
  213.                         //Emulate menu close if set
  214.                         $($this.btn).click();
  215.                     }
  216.             });
  217.  
  218.             //also close on click if parent links are set
  219.             if (settings.closeOnClick && settings.allowParentLinks) {
  220.                 item.children('a').children('a').click(function (event) {
  221.                     //Emulate menu close
  222.                     $($this.btn).click();
  223.                 });
  224.  
  225.                 item.find('.'+prefix+'_parent-link a:not(.'+prefix+'_item)').click(function(event){
  226.                     //Emulate menu close
  227.                         $($this.btn).click();
  228.                 });
  229.             }
  230.         });
  231.  
  232.         // structure is in place, now hide appropriate items
  233.         $(items).each(function () {
  234.             var data = $(this).data('menu');
  235.             if (!settings.showChildren){
  236.                 $this._visibilityToggle(data.children, null, false, null, true);
  237.             }
  238.         });
  239.  
  240.         // finally toggle entire menu
  241.         $this._visibilityToggle($this.mobileNav, null, false, 'init', true);
  242.  
  243.         // accessibility for menu button
  244.         $this.mobileNav.attr('role','menu');
  245.  
  246.         // outline prevention when using mouse
  247.         $(document).mousedown(function(){
  248.             $this._outlines(false);
  249.         });
  250.  
  251.         $(document).keyup(function(){
  252.             $this._outlines(true);
  253.         });
  254.  
  255.         // menu button click
  256.         $($this.btn).click(function (e) {
  257.             e.preventDefault();
  258.             $this._menuToggle();
  259.         });
  260.  
  261.         // click on menu parent
  262.         $this.mobileNav.on('click', '.' + prefix + '_item', function (e) {
  263.             e.preventDefault();
  264.             $this._itemClick($(this));
  265.         });
  266.  
  267.         // check for keyboard events on menu button and menu parents
  268.         $($this.btn).keydown(function (e) {
  269.             var ev = e || event;
  270.  
  271.             switch(ev.keyCode) {
  272.                 case Keyboard.ENTER:
  273.                 case Keyboard.SPACE:
  274.                 case Keyboard.DOWN:
  275.                     e.preventDefault();
  276.                     if (ev.keyCode !== Keyboard.DOWN || !$($this.btn).hasClass(prefix+'_open')){
  277.                         $this._menuToggle();
  278.                     }
  279.                    
  280.                     $($this.btn).next().find('[role="menuitem"]').first().focus();
  281.                     break;
  282.             }
  283.  
  284.            
  285.         });
  286.  
  287.         $this.mobileNav.on('keydown', '.'+prefix+'_item', function(e) {
  288.             var ev = e || event;
  289.  
  290.             switch(ev.keyCode) {
  291.                 case Keyboard.ENTER:
  292.                     e.preventDefault();
  293.                     $this._itemClick($(e.target));
  294.                     break;
  295.                 case Keyboard.RIGHT:
  296.                     e.preventDefault();
  297.                     if ($(e.target).parent().hasClass(prefix+'_collapsed')) {
  298.                         $this._itemClick($(e.target));
  299.                     }
  300.                     $(e.target).next().find('[role="menuitem"]').first().focus();
  301.                     break;
  302.             }
  303.         });
  304.  
  305.         $this.mobileNav.on('keydown', '[role="menuitem"]', function(e) {
  306.             var ev = e || event;
  307.  
  308.             switch(ev.keyCode){
  309.                 case Keyboard.DOWN:
  310.                     e.preventDefault();
  311.                     var allItems = $(e.target).parent().parent().children().children('[role="menuitem"]:visible');
  312.                     var idx = allItems.index( e.target );
  313.                     var nextIdx = idx + 1;
  314.                     if (allItems.length <= nextIdx) {
  315.                         nextIdx = 0;
  316.                     }
  317.                     var next = allItems.eq( nextIdx );
  318.                     next.focus();
  319.                 break;
  320.                 case Keyboard.UP:
  321.                     e.preventDefault();
  322.                     var allItems = $(e.target).parent().parent().children().children('[role="menuitem"]:visible');
  323.                     var idx = allItems.index( e.target );
  324.                     var next = allItems.eq( idx - 1 );
  325.                     next.focus();
  326.                 break;
  327.                 case Keyboard.LEFT:
  328.                     e.preventDefault();
  329.                     if ($(e.target).parent().parent().parent().hasClass(prefix+'_open')) {
  330.                         var parent = $(e.target).parent().parent().prev();
  331.                         parent.focus();
  332.                         $this._itemClick(parent);
  333.                     } else if ($(e.target).parent().parent().hasClass(prefix+'_nav')){
  334.                         $this._menuToggle();
  335.                         $($this.btn).focus();
  336.                     }
  337.                     break;
  338.                 case Keyboard.ESCAPE:
  339.                     e.preventDefault();
  340.                     $this._menuToggle();
  341.                     $($this.btn).focus();
  342.                     break;    
  343.             }
  344.         });
  345.  
  346.         // allow links clickable within parent tags if set
  347.         if (settings.allowParentLinks && settings.nestedParentLinks) {
  348.             $('.'+prefix+'_item a').click(function(e){
  349.                     e.stopImmediatePropagation();
  350.             });
  351.         }
  352.     };
  353.  
  354.     //toggle menu
  355.     Plugin.prototype._menuToggle = function (el) {
  356.         var $this = this;
  357.         var btn = $this.btn;
  358.         var mobileNav = $this.mobileNav;
  359.  
  360.         if (btn.hasClass(prefix+'_collapsed')) {
  361.             btn.removeClass(prefix+'_collapsed');
  362.             btn.addClass(prefix+'_open');
  363.         } else {
  364.             btn.removeClass(prefix+'_open');
  365.             btn.addClass(prefix+'_collapsed');
  366.         }
  367.         btn.addClass(prefix+'_animating');
  368.         $this._visibilityToggle(mobileNav, btn.parent(), true, btn);
  369.     };
  370.  
  371.     // toggle clicked items
  372.     Plugin.prototype._itemClick = function (el) {
  373.         var $this = this;
  374.         var settings = $this.settings;
  375.         var data = el.data('menu');
  376.         if (!data) {
  377.             data = {};
  378.             data.arrow = el.children('.'+prefix+'_arrow');
  379.             data.ul = el.next('ul');
  380.             data.parent = el.parent();
  381.             //Separated parent link structure
  382.             if (data.parent.hasClass(prefix+'_parent-link')) {
  383.                 data.parent = el.parent().parent();
  384.                 data.ul = el.parent().next('ul');
  385.             }
  386.             el.data('menu', data);
  387.         }
  388.         if (data.parent.hasClass(prefix+'_collapsed')) {
  389.             data.arrow.html(settings.openedSymbol);
  390.             data.parent.removeClass(prefix+'_collapsed');
  391.             data.parent.addClass(prefix+'_open');
  392.             data.parent.addClass(prefix+'_animating');
  393.             $this._visibilityToggle(data.ul, data.parent, true, el);
  394.         } else {
  395.             data.arrow.html(settings.closedSymbol);
  396.             data.parent.addClass(prefix+'_collapsed');
  397.             data.parent.removeClass(prefix+'_open');
  398.             data.parent.addClass(prefix+'_animating');
  399.             $this._visibilityToggle(data.ul, data.parent, true, el);
  400.         }
  401.     };
  402.  
  403.     // toggle actual visibility and accessibility tags
  404.     Plugin.prototype._visibilityToggle = function(el, parent, animate, trigger, init) {
  405.         var $this = this;
  406.         var settings = $this.settings;
  407.         var items = $this._getActionItems(el);
  408.         var duration = 0;
  409.         if (animate) {
  410.             duration = settings.duration;
  411.         }
  412.        
  413.         function afterOpen(trigger, parent) {
  414.             $(trigger).removeClass(prefix+'_animating');
  415.             $(parent).removeClass(prefix+'_animating');
  416.  
  417.             //Fire afterOpen callback
  418.             if (!init) {
  419.                 settings.afterOpen(trigger);
  420.             }
  421.         }
  422.        
  423.         function afterClose(trigger, parent) {
  424.             el.attr('aria-hidden','true');
  425.             items.attr('tabindex', '-1');
  426.             $this._setVisAttr(el, true);
  427.             el.hide(); //jQuery 1.7 bug fix
  428.  
  429.             $(trigger).removeClass(prefix+'_animating');
  430.             $(parent).removeClass(prefix+'_animating');
  431.  
  432.             //Fire init or afterClose callback
  433.             if (!init){
  434.                 settings.afterClose(trigger);
  435.             } else if (trigger == 'init'){
  436.                 settings.init();
  437.             }
  438.         }
  439.  
  440.         if (el.hasClass(prefix+'_hidden')) {
  441.             el.removeClass(prefix+'_hidden');
  442.              //Fire beforeOpen callback
  443.             if (!init) {
  444.                 settings.beforeOpen(trigger);
  445.             }
  446.             if (settings.animations === 'jquery') {
  447.                 el.stop(true,true).slideDown(duration, settings.easingOpen, function(){
  448.                     afterOpen(trigger, parent);
  449.                 });
  450.             } else if(settings.animations === 'velocity') {
  451.                 el.velocity("finish").velocity("slideDown", {
  452.                     duration: duration,
  453.                     easing: settings.easingOpen,
  454.                     complete: function() {
  455.                         afterOpen(trigger, parent);
  456.                     }
  457.                 });
  458.             }
  459.             el.attr('aria-hidden','false');
  460.             items.attr('tabindex', '0');
  461.             $this._setVisAttr(el, false);
  462.         } else {
  463.             el.addClass(prefix+'_hidden');
  464.  
  465.             //Fire init or beforeClose callback
  466.             if (!init){
  467.                 settings.beforeClose(trigger);
  468.             }
  469.  
  470.             if (settings.animations === 'jquery') {
  471.                 el.stop(true,true).slideUp(duration, this.settings.easingClose, function() {
  472.                     afterClose(trigger, parent)
  473.                 });
  474.             } else if (settings.animations === 'velocity') {
  475.                
  476.                 el.velocity("finish").velocity("slideUp", {
  477.                     duration: duration,
  478.                     easing: settings.easingClose,
  479.                     complete: function() {
  480.                         afterClose(trigger, parent);
  481.                     }
  482.                 });
  483.             }
  484.         }
  485.     };
  486.  
  487.     // set attributes of element and children based on visibility
  488.     Plugin.prototype._setVisAttr = function(el, hidden) {
  489.         var $this = this;
  490.  
  491.         // select all parents that aren't hidden
  492.         var nonHidden = el.children('li').children('ul').not('.'+prefix+'_hidden');
  493.  
  494.         // iterate over all items setting appropriate tags
  495.         if (!hidden) {
  496.             nonHidden.each(function(){
  497.                 var ul = $(this);
  498.                 ul.attr('aria-hidden','false');
  499.                 var items = $this._getActionItems(ul);
  500.                 items.attr('tabindex', '0');
  501.                 $this._setVisAttr(ul, hidden);
  502.             });
  503.         } else {
  504.             nonHidden.each(function(){
  505.                 var ul = $(this);
  506.                 ul.attr('aria-hidden','true');
  507.                 var items = $this._getActionItems(ul);
  508.                 items.attr('tabindex', '-1');
  509.                 $this._setVisAttr(ul, hidden);
  510.             });
  511.         }
  512.     };
  513.  
  514.     // get all 1st level items that are clickable
  515.     Plugin.prototype._getActionItems = function(el) {
  516.         var data = el.data("menu");
  517.         if (!data) {
  518.             data = {};
  519.             var items = el.children('li');
  520.             var anchors = items.find('a');
  521.             data.links = anchors.add(items.find('.'+prefix+'_item'));
  522.             el.data('menu', data);
  523.         }
  524.         return data.links;
  525.     };
  526.  
  527.     Plugin.prototype._outlines = function(state) {
  528.         if (!state) {
  529.             $('.'+prefix+'_item, .'+prefix+'_btn').css('outline','none');
  530.         } else {
  531.             $('.'+prefix+'_item, .'+prefix+'_btn').css('outline','');
  532.         }
  533.     };
  534.  
  535.     Plugin.prototype.toggle = function(){
  536.         var $this = this;
  537.         $this._menuToggle();
  538.     };
  539.  
  540.     Plugin.prototype.open = function(){
  541.         var $this = this;
  542.         if ($this.btn.hasClass(prefix+'_collapsed')) {
  543.             $this._menuToggle();
  544.         }
  545.     };
  546.  
  547.     Plugin.prototype.close = function(){
  548.         var $this = this;
  549.         if ($this.btn.hasClass(prefix+'_open')) {
  550.             $this._menuToggle();
  551.         }
  552.     };
  553.  
  554.     $.fn[mobileMenu] = function ( options ) {
  555.         var args = arguments;
  556.  
  557.         // Is the first parameter an object (options), or was omitted, instantiate a new instance
  558.         if (options === undefined || typeof options === 'object') {
  559.             return this.each(function () {
  560.  
  561.                 // Only allow the plugin to be instantiated once due to methods
  562.                 if (!$.data(this, 'plugin_' + mobileMenu)) {
  563.  
  564.                     // if it has no instance, create a new one, pass options to our plugin constructor,
  565.                     // and store the plugin instance in the elements jQuery data object.
  566.                     $.data(this, 'plugin_' + mobileMenu, new Plugin( this, options ));
  567.                 }
  568.             });
  569.  
  570.         // If is a string and doesn't start with an underscore or 'init' function, treat this as a call to a public method.
  571.         } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
  572.  
  573.             // Cache the method call to make it possible to return a value
  574.             var returns;
  575.  
  576.             this.each(function () {
  577.                 var instance = $.data(this, 'plugin_' + mobileMenu);
  578.  
  579.                 // Tests that there's already a plugin-instance and checks that the requested public method exists
  580.                 if (instance instanceof Plugin && typeof instance[options] === 'function') {
  581.  
  582.                     // Call the method of our plugin instance, and pass it the supplied arguments.
  583.                     returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
  584.                 }
  585.             });
  586.  
  587.             // If the earlier cached method gives a value back return the value, otherwise return this to preserve chainability.
  588.             return returns !== undefined ? returns : this;
  589.         }
  590.     };
  591. }(jQuery, document, window));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement