123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779 |
- * jQuery mmenu 1.3.1
- *
- * Copyright (c) 2013 Fred Heusschen
- * www.frebsite.nl
- *
- * Dual licensed under the MIT and GPL licenses.
- * http://en.wikipedia.org/wiki/MIT_License
- * http://en.wikipedia.org/wiki/GNU_General_Public_License
- */
- (function( $ ) {
-
- var $wndw = null,
- $html = null,
- $body = null,
- $page = null,
- $blck = null;
- var $scrollTopNode = null;
-
- var _serialnr = 0;
- $.fn.mmenu = function( opts )
- {
- if ( !$wndw )
- {
- $wndw = $(window);
- $html = $('html');
- $body = $('body');
- }
- opts = extendOptions( opts );
- opts = $.extend( true, {}, $.fn.mmenu.defaultOptions, opts );
- opts = complementOptions( opts );
- $html[ opts.configuration.hardwareAcceleration ? 'addClass' : 'removeClass' ]( cls( 'accelerated' ) );
- return this.each(
- function()
- {
-
- var $menu = $(this),
- _opened = false,
- _direction = ( opts.slidingSubmenus ) ? 'horizontal' : 'vertical';
- _serialnr++;
-
- $page = _initPage( $page, opts.configuration );
- $blck = _initBlocker( $blck, $menu, opts.configuration );
- $menu = _initMenu( $menu, opts.configuration );
- _initSubmenus( $menu, _direction, _serialnr );
- _initLinks( $menu, opts.onClick, opts.configuration );
- _initCounters( $menu, opts.counters, opts.configuration );
- _initSearch( $menu, opts.searchfield );
- _initOpenClose( $menu, $page );
-
- var $subs = $menu.find( 'ul' );
- $menu.add( $subs )
- .bind(
- evt( 'toggle' ) + ' ' + evt( 'open' ) + ' ' + evt( 'close' ),
- function( e )
- {
- e.preventDefault();
- e.stopPropagation();
- }
- );
-
- $menu
- .bind(
- evt( 'toggle' ),
- function( e )
- {
- return $menu.triggerHandler( evt( _opened ? 'close' : 'open' ) );
- }
- )
- .bind(
- evt( 'open' ),
- function( e )
- {
- if ( _opened )
- {
- return false;
- }
- _opened = true;
- return openMenu( $menu, opts.position );
- }
- )
- .bind(
- evt( 'close' ),
- function( e )
- {
- if ( !_opened )
- {
- return false;
- }
- _opened = false;
- return closeMenu( $menu, opts );
- }
- );
-
- if ( _direction == 'horizontal' )
- {
- $subs
- .bind(
- evt( 'toggle' ),
- function( e )
- {
- return $(this).triggerHandler( evt( 'open' ) );
- }
- )
- .bind(
- evt( 'open' ),
- function( e )
- {
- return openSubmenuHorizontal( $(this), opts );
- }
- )
- .bind(
- evt( 'close' ),
- function( e )
- {
- return closeSubmenuHorizontal( $(this), opts );
- }
- );
- }
- else
- {
- $subs
- .bind(
- evt( 'toggle' ),
- function( e )
- {
- var $t = $(this);
- return $t.triggerHandler( evt( ( $t.parent().hasClass( cls( 'opened' ) ) ) ? 'close' : 'open' ) );
- }
- )
- .bind(
- evt( 'open' ),
- function( e )
- {
- $(this).parent().addClass( cls( 'opened' ) );
- return 'open';
- }
- )
- .bind(
- evt( 'close' ),
- function( e )
- {
- $(this).parent().removeClass( cls( 'opened' ) );
- return 'close';
- }
- );
- }
- }
- );
- };
- $.fn.mmenu.defaultOptions = {
- position : 'right',
- slidingSubmenus : true,
- counters : {
- add : false,
- count : true
- },
- searchfield : {
- add : false,
- search : true,
- showLinksOnly : true,
- placeholder : 'Search',
- noResults : 'No results found.'
- },
- onClick : {
- close : true,
- delayPageload : true,
- blockUI : false
- },
- configuration : {
- hardwareAcceleration: true,
- selectedClass : 'Selected',
- labelClass : 'Label',
- counterClass : 'Counter',
- pageNodetype : 'div',
- menuNodetype : 'nav',
- slideDuration : 500
- }
- };
- $.fn.mmenu.debug = function( msg )
- {
- if ( typeof console != 'undefined' && typeof console.log != 'undefined' )
- {
- console.log( 'MMENU: ' + msg );
- }
- };
- $.fn.mmenu.deprecated = function( depr, repl )
- {
- if ( typeof console != 'undefined' && typeof console.warn != 'undefined' )
- {
- console.warn( 'MMENU: ' + depr + ' is deprecated, use ' + repl + ' instead.' );
- }
- };
- function extendOptions( o )
- {
- if ( typeof o == 'string' )
- {
- if ( o == 'left' || o == 'right' )
- {
- o = {
- position: o
- };
- }
- }
- else if ( typeof o != 'object' )
- {
- o = {};
- }
-
- if ( typeof o.addCounters != 'undefined' )
- {
- $.fn.mmenu.deprecated( 'addCounters-option', 'counters.add-option' );
- o.counters = {
- add: o.addCounters
- };
- }
- if ( typeof o.closeOnClick != 'undefined' )
- {
- $.fn.mmenu.deprecated( 'closeOnClick-option', 'onClick.close-option' );
- o.onClick = {
- close: o.closeOnClick
- };
- }
-
-
- if ( typeof o.counters == 'boolean' )
- {
- o.counters = {
- add : o.counters,
- count : o.counters
- };
- }
- else if ( typeof o.counters != 'object' )
- {
- o.counters = {};
- }
-
- if ( typeof o.onClick == 'boolean' )
- {
- o.onClick = {
- close : o.onClick
- };
- }
- else if ( typeof o.onClick != 'object' )
- {
- o.onClick = {};
- }
-
- if ( typeof o.searchfield == 'boolean' )
- {
- o.searchfield = {
- add : o.searchfield,
- search : o.searchfield
- };
- }
- else if ( typeof o.searchfield == 'string' )
- {
- o.searchfield = {
- add : true,
- search : true,
- placeholder : o.searchfield
- };
- }
- else if ( typeof o.searchfield != 'object' )
- {
- o.searchfield = {};
- }
- return o;
- }
- function complementOptions( o )
- {
- if ( typeof o.onClick.delayPageload == 'boolean' )
- {
- o.onClick.delayPageload = ( o.onClick.delayPageload ) ? o.configuration.slideDuration : 0;
- }
- return o;
- }
- function _initPage( $p, conf )
- {
- if ( !$p )
- {
- $p = $('> ' + conf.pageNodetype, $body);
- if ( $p.length > 1 )
- {
- $p = $p.wrapAll( '<' + conf.pageNodetype + ' />' ).parent();
- }
- $p.addClass( cls( 'page' ) );
- }
- return $p;
- }
- function _initMenu( $m, conf )
- {
- if ( !$m.is( conf.menuNodetype ) )
- {
- $m = $( '<' + conf.menuNodetype + ' />' ).append( $m );
- }
-
- $m.addClass( cls( '' ).slice( 0, -1 ) ).prependTo( 'body' );
-
- $('li.' + conf.selectedClass, $m).removeClass( conf.selectedClass ).addClass( cls( 'selected' ) );
-
- $('li.' + conf.labelClass, $m).removeClass( conf.labelClass ).addClass( cls( 'label' ) );
- return $m;
- }
- function _initSubmenus( $m, direction, serial )
- {
- $m.addClass( cls( direction ) );
- $( 'ul ul', $m )
- .addClass( cls( 'submenu' ) )
- .each(
- function( i )
- {
- var $t = $(this)
- $a = $t.parent().find( '> a, > span' ),
- id = $t.attr( 'id' ) || cls( 's' + serial + '-' + i );
- $t.attr( 'id', id );
- var $btn = $( '<a class="' + cls( 'subopen' ) + '" href="#' + id + '" />' ).insertBefore( $a );
- if ( !$a.is( 'a' ) )
- {
- $btn.addClass( cls( 'fullsubopen' ) );
- }
- if ( direction == 'horizontal' )
- {
- var $p = $t.parent().parent(),
- id = $p.attr( 'id' ) || cls( 'p' + serial + '-' + i );
-
- $p.attr( 'id', id );
- $t.prepend( '<li class="' + cls( 'subtitle' ) + '"><a class="' + cls( 'subclose' ) + '" href="#' + id + '">' + $a.text() + '</a></li>' );
- }
- }
- );
- if ( direction == 'horizontal' )
- {
-
- $('li.' + cls( 'selected' ), $m)
- .parents( 'li.' + cls( 'selected' ) ).removeClass( cls( 'selected' ) )
- .end().each(
- function()
- {
- var $t = $(this),
- $u = $t.find( '> ul' );
-
- if ( $u.length )
- {
- $t.parent().addClass( cls( 'subopened' ) );
- $u.addClass( cls( 'opened' ) );
- }
- }
- )
- .parent().addClass( cls( 'opened' ) )
- .parents( 'ul' ).addClass( cls( 'subopened' ) );
- if ( !$('ul.' + cls( 'opened' ), $m).length )
- {
- $('ul', $m).not( '.' + cls( 'submenu' ) ).addClass( cls( 'opened' ) );
- }
-
- $('ul ul', $m).appendTo( $m );
- }
- else
- {
-
- $('li.' + cls( 'selected' ), $m)
- .addClass( cls( 'opened' ) )
- .parents( '.' + cls( 'selected' ) ).removeClass( cls( 'selected' ) );
- }
- }
- function _initBlocker( $b, $m, conf )
- {
- if ( !$b )
- {
- $b = $( '<div id="' + cls( 'blocker' ) + '" />' ).appendTo( $body );
- }
- click( $b,
- function()
- {
- $m.trigger( evt( 'close' ) );
- }
- );
- return $b;
- }
- function _initLinks( $m, onClick, conf )
- {
- if ( onClick.close )
- {
- var $a = $('a', $m)
- .not( '.' + cls( 'subopen' ) )
- .not( '.' + cls( 'subclose' ) );
- click( $a,
- function()
- {
- var $t = $(this),
- href = $t.attr( 'href' );
-
- $m.trigger( evt( 'close' ) );
- $a.parent().removeClass( cls( 'selected' ) );
- $t.parent().addClass( cls( 'selected' ) );
- if ( onClick.blockUI && href.slice( 0, 1 ) != '#' )
- {
- $html.addClass( cls( 'blocking' ) );
- }
- if ( href != '#' )
- {
- setTimeout(
- function()
- {
- window.location.href = href;
- }, onClick.delayPageload
- );
- }
- }
- );
- }
- }
- function _initCounters( $m, counters, conf )
- {
-
- $('em.' + conf.counterClass, $m).removeClass( conf.counterClass ).addClass( cls( 'counter' ) );
-
- if ( counters.add )
- {
- $('.' + cls( 'submenu' ), $m).each(
- function()
- {
- var $s = $(this),
- id = $s.attr( 'id' );
-
- if ( id && id.length )
- {
- var $c = $( '<em class="' + cls( 'counter' ) + '" />' ),
- $a = $('a.' + cls( 'subopen' ), $m).filter( '[href="#' + id + '"]' );
- if ( !$a.parent().find( 'em.' + cls( 'counter' ) ).length )
- {
- $a.before( $c );
- }
- }
- }
- );
- }
-
- if ( counters.count )
- {
- $('em.' + cls( 'counter' ), $m).each(
- function()
- {
- var $c = $(this),
- $s = $('ul' + $c.next().attr( 'href' ), $m);
- $c.bind(
- evt( 'count' ),
- function( e )
- {
- e.preventDefault();
- e.stopPropagation();
- var $lis = $s.children()
- .not( '.' + cls( 'label' ) )
- .not( '.' + cls( 'subtitle' ) )
- .not( '.' + cls( 'noresult' ) )
- .not( '.' + cls( 'noresults' ) );
- $c.html( $lis.length );
- }
- );
- }
- ).trigger( evt( 'count' ) );
- }
- }
- function _initSearch( $m, search )
- {
- if ( search.add )
- {
- var $s = $( '<div class="' + cls( 'search' ) + '" />' ).prependTo( $m );
- $s.append( '<input placeholder="' + search.placeholder + '" type="text" autocomplete="off" />' );
- if ( search.noResults )
- {
- $('ul', $m).not( '.' + cls( 'submenu' ) ).append( '<li class="' + cls( 'noresults' ) + '">' + search.noResults + '</li>' );
- }
- }
- if ( search.search )
- {
- var $s = $('div.' + cls( 'search' ), $m),
- $i = $('input', $s);
- var $labels = $('li.' + cls( 'label' ), $m),
- $counters = $('em.' + cls( 'counter' ), $m),
- $items = $('li', $m)
- .not( '.' + cls( 'subtitle' ) )
- .not( '.' + cls( 'label' ) )
- .not( '.' + cls( 'noresults' ) );
- var _searchText = '> a';
- if ( !search.showLinksOnly )
- {
- _searchText += ', > span';
- }
- $i.bind(
- evt( 'keyup' ),
- function()
- {
- var query = $i.val().toLowerCase();
-
- $items.add( $labels ).addClass( cls( 'noresult' ) );
- $items.each(
- function()
- {
- var $t = $(this);
- if ( $(_searchText, $t).text().toLowerCase().indexOf( query ) > -1 )
- {
- $t.add( $t.prevAll( '.' + cls( 'label' ) ).first() ).removeClass( cls( 'noresult' ) );
- }
- }
- );
-
- $( $('ul.' + cls( 'submenu' ), $m).get().reverse() ).each(
- function()
- {
- var $t = $(this),
- $p = null,
- id = $t.attr( 'id' ),
- $i = $t.find( 'li' )
- .not( '.' + cls( 'subtitle' ) )
- .not( '.' + cls( 'label' ) )
- .not( '.' + cls( 'noresult' ) );
- if ( id && id.length )
- {
- var $p = $('a.' + cls( 'subopen' ), $m).filter( '[href="#' + id + '"]' ).parent();
- }
- if ( $i.length )
- {
- if ( $p )
- {
- $p.removeClass( cls( 'noresult' ) );
- $p.removeClass( cls( 'nosubresult' ) );
- }
- }
- else
- {
- $t.trigger( evt( 'close' ) );
- if ( $p )
- {
- $p.addClass( cls( 'nosubresult' ) );
- }
- }
- }
- );
-
- $m[ $items.not( '.' + cls( 'noresult' ) ).length ? 'removeClass' : 'addClass' ]( cls( 'noresults' ) );
-
- $counters.trigger( evt( 'count' ) );
- }
- );
- }
- }
- function _initOpenClose( $m, $p )
- {
-
- var id = $m.attr( 'id' );
- if ( id && id.length )
- {
- click( 'a[href="#' + id + '"]',
- function()
- {
- $m.trigger( evt( 'toggle' ) );
- }
- );
- }
-
- var id = $p.attr( 'id' );
- if ( id && id.length )
- {
- click( 'a[href="#' + id + '"]',
- function()
- {
- $m.trigger( evt( 'close' ) );
- }
- );
- }
-
- click( $('a.' + cls( 'subopen' ) + ', ' + 'a.' + cls( 'subclose' ), $m),
- function()
- {
- $( $(this).attr( 'href' ) ).trigger( evt( 'toggle' ) );
- }
- );
- }
- function openMenu( $m, p )
- {
- var _scrollTop = findScrollTop();
-
- var _w = 0;
- $wndw.bind(
- evt( 'resize' ),
- function( e )
- {
- var nw = $wndw.width();
- if ( nw != _w )
- {
- _w = nw;
- $page
- .attr( 'style', $page.data( dta( 'style' ) ) )
- .width( nw );
- }
- }
- );
-
- $page
- .data( dta( 'style' ), $page.attr( 'style' ) || '' )
- .data( dta( 'scrollTop' ), _scrollTop )
- .width( $page.outerWidth() )
- .css( 'top', -_scrollTop );
-
- $m.addClass( cls( 'opened' ) );
- $html.addClass( cls( 'opened' ) ).addClass( cls( p ) );
- setTimeout(
- function()
- {
-
- $html.addClass( cls( 'opening' ) );
- }, 25
- );
- return 'open';
- }
- function closeMenu( $m, o )
- {
-
- $html.removeClass( cls( 'opening' ) );
- setTimeout(
- function()
- {
-
- $html.removeClass( cls( 'opened' ) ).removeClass( cls( o.position ) );
- $m.removeClass( cls( 'opened' ) );
-
- $page.attr( 'style', $page.data( dta( 'style' ) ) );
- $wndw.unbind( evt( 'resize' ) );
- if ( $scrollTopNode )
- {
- $scrollTopNode.scrollTop( $page.data( dta( 'scrollTop' ) ) );
- }
- }, o.configuration.slideDuration + 25
- );
- return 'close';
- }
- function openSubmenuHorizontal( $t, o )
- {
- $t.prevAll( 'ul' ).addClass( cls( 'subopened' ) );
- $t.nextAll( 'ul' ).removeClass( cls( 'subopened' ) );
- $t.removeClass( cls( 'subopened' ) ).addClass( cls( 'opened' ) );
- setTimeout(
- function()
- {
- $t.nextAll( 'ul' ).removeClass( cls( 'opened' ) );
- }, o.configuration.slideDuration + 25
- );
- return 'open';
- }
- function closeSubmenuHorizontal( $t, o )
- {
- $t.prevAll( 'ul.' + cls( 'opened' ) ).first().trigger( evt( 'open' ) );
- return 'close';
- }
- function findScrollTop()
- {
- if ( !$scrollTopNode )
- {
- if ( $('html').scrollTop() != 0 )
- {
- $scrollTopNode = $('html');
- }
- else if ( $('body').scrollTop() != 0 )
- {
- $scrollTopNode = $('body');
- }
- }
- return ( $scrollTopNode ) ? $scrollTopNode.scrollTop() - 1 : 0;
- }
- function click( $b, fn )
- {
- if ( typeof $b == 'string' )
- {
- $b = $( $b );
- }
- $b.bind(
- evt( 'click' ),
- function( e )
- {
- e.preventDefault();
- e.stopPropagation();
- fn.call( this, e );
- }
- );
- }
- function cls( c )
- {
- return 'mmenu-' + c;
- }
- function dta( d )
- {
- return 'mmenu-' + d;
- }
- function evt( e )
- {
- return e + '.mmenu';
- }
- })( jQuery );
|