PageSourceSearch

https://cinclips.net/wp-content/themes/baw-premium/framework/mobile-menu/mobile-menu.js

js cinclips.net collected 2026-10-07 18:59:52 UTC 22,325 bytes, 612 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.