/** * available for use under the mit license (http://en.wikipedia.org/wiki/mit_license) * * copyright (c) 2014 - 2015 by adam banaszkiewicz * * permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "software"), to deal * in the software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the software, and to permit persons to whom the software is * furnished to do so, subject to the following conditions: * * the above copyright notice and this permission notice shall be included in * all copies or substantial portions of the software. * * the software is provided "as is", without warranty of any kind, express or * implied, including but not limited to the warranties of merchantability, * fitness for a particular purpose and noninfringement. in no event shall the * authors or copyright holders be liable for any claim, damages or other * liability, whether in an action of contract, tort or otherwise, arising from, * out of or in connection with the software or the use or other dealings in * the software. * * @version 0.1.2 * @date 2015.11.23 * @author adam banaszkiewicz */ !function(t){t.fn.materialmenu=function(i){var i=t.extend({mobilewidth:767,width:250,animationtime:200,overlayopacity:.4,buttonclass:"material-menu-button",buttonhtml:'
',title:"",showtitle:!1,titleshowon:40,hideonclick:!0,onopen:function(t){},onclose:function(t){},onchangemobile:function(t){t.parent().parent().addclass("vertical")},onchangedesktop:function(t){t.parent().parent().removeclass("vertical")},onshowtitlebar:function(t){},onhidetitlebar:function(t){}},i),e=function(i,e){this.options=e,this.element=i,this.overlay=t([]),this.title=t([]),this.istitleshowed=!1,this.isshowed=!1,this.changedonmobile=!1,this.changedondesktop=!1,this.touchposstart={y:0,x:0},this.touchposend={y:0,x:0},this.init=function(){var i=this;this.prepare(),this.showmenudependentbywidth(),this.bindevents(),t(window).resize(function(){i.showmenudependentbywidth()})},this.prepare=function(){if(this.element.wrap(t("
",{"class":"material-menu-wrapper"})),this.options.showtitle){var i=t(".material-menu-titlebar");0==i.length?(t("
",{"class":"material-menu-titlebar",style:"display:block;position:fixed;left:0px;top:0px;width:100%;height:55px;background-color:#fff;z-index:999998;"}).appendto("body"),this.title=t(".material-menu-titlebar"),this.title.append(t(this.options.buttonhtml).addclass(this.options.buttonclass).css({"float":"left",margin:"5px"})),this.title.append('
'+this.options.title+"
"),0==this.options.titleshowon?this.showtitle():this.hidetitle()):this.title=i,this.title.hide()}var e=t(".material-menu-overlay");1==e.length?this.overlay=e:(t("
",{"class":"material-menu-overlay",style:"display:block;position:fixed;left:0px;top:0px;width:100%;height:100%;z-index:999998;background-color:#000000;"}).appendto("body"),this.overlay=t(".material-menu-overlay")),this.overlay.fadeto(0,0).css("display","none")},this.bindevents=function(){var i=this;t("."+this.options.buttonclass).click(function(){i.isshowed?i.close():i.open()}),this.overlay.click(function(){i.close()}),t(window).scroll(function(){t(this).scrolltop()>=i.options.titleshowon?i.showtitle():i.hidetitle()}),t(window).scrolltop()>=i.options.titleshowon&&i.showtitle(),i.options.hideonclick&&i.element.find("a").click(function(){i.close()}),this.bindtouchclose()},this.showmenudependentbywidth=function(){this.getwindowwidth()<=this.options.mobilewidth?this.showformobile():this.showfordesktop(),this.isshowed&&this.close()},this.showformobile=function(){0==this.changedonmobile&&(this.element.parent().removeclass("material-menu-view-desktop").addclass("material-menu-view-mobile").css({display:"none",position:"fixed",top:"0px",zindex:"999999",overflow:"auto",height:"100%",width:this.getmenuwidth()+"px",left:"-"+this.getmenuwidth()+"px"}),this.options.showtitle&&this.showtitle(),this.changedondesktop=!1,this.changedonmobile=!0,this.options.onchangemobile(this.element))},this.showfordesktop=function(){0==this.changedondesktop&&(this.element.parent().removeclass("material-menu-view-mobile").addclass("material-menu-view-desktop").css({display:"block",position:"static",top:"auto",zindex:"auto",overflow:"visible",height:"auto",width:"auto",left:"auto"}),this.options.showtitle&&this.hidetitle(),this.changedondesktop=!0,this.changedonmobile=!1,this.options.onchangedesktop(this.element))},this.open=function(){var i=this;0==i.isshowed&&i.getwindowwidth()<=i.options.mobilewidth&&(i.isshowed=!0,i.element.parent().show().animate({left:"0px"},i.options.animationtime),i.overlay.css("display","block").fadeto(i.options.animationtime,i.options.overlayopacity),t("body").css("overflow","hidden"),i.options.onopen(i.element))},this.close=function(){var i=this;1==i.isshowed&&(i.isshowed=!1,i.element.parent().animate({left:"-"+i.getmenuwidth()+"px"},i.options.animationtime,function(){t(this).hide()}),i.overlay.fadeto(i.options.animationtime,0,function(){t(this).css("display","none"),i.options.onclose(i.element)}),t("body").css("overflow","auto"))},this.showtitle=function(){0==this.istitleshowed&&1==this.changedonmobile&&(this.title.show().fadeto(this.options.animationtime,1),this.istitleshowed=!0,this.options.onshowtitlebar(this.title))},this.hidetitle=function(){1==this.istitleshowed&&(this.title.fadeto(this.options.animationtime,0,function(){t(this).hide()}),this.istitleshowed=!1,this.options.onhidetitlebar(this.title))},this.bindtouchclose=function(){var t=this,i=[this.element,this.overlay];for(var e in i)i[e][0]&&i[e][0].addeventlistener&&!i[e].data("materialmenu-binded-touchclose")&&(i[e].data("materialmenu-binded-touchclose","1"),i[e][0].addeventlistener("touchstart",function(i){t.touchposstart.x=i.touches[0].pagex,t.touchposstart.y=i.touches[0].pagey},!1),i[e][0].addeventlistener("touchend",function(i){"left"==t.gettouchdirection()&&t.close()},!1),i[e][0].addeventlistener("touchmove",function(i){t.touchposend.x=i.touches[0].pagex,t.touchposend.y=i.touches[0].pagey},!1))},this.getmenuwidth=function(){return this.options.width},this.getwindowwidth=function(){return"number"==typeof window.innerwidth?window.innerwidth:document.documentelement&&(document.documentelement.clientwidth||document.documentelement.clientheight)?document.documentelement.clientwidth:document.body&&(document.body.clientwidth||document.body.clientheight)?document.body.clientwidth:t("body, html").width()},this.gettouchdirection=function(){var t=math.abs(this.touchposstart.x-this.touchposend.x),i=math.abs(this.touchposstart.y-this.touchposend.y);return t>i?this.touchposstart.x>this.touchposend.x?"left":"right":this.touchposstart.y>this.touchposend.y?"up":"down"}};return t(this).each(function(){var o=new e(t(this),i);o.init()})}}(jquery);