/** * 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.1 * @date 2015.08.09 * @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,onOpen:function(){},onClose:function(){},onChangeMobile:function(t){t.parent().parent().addClass("vertical")},onChangeDesktop:function(t){t.parent().parent().removeClass("vertical")},onShowTitlebar:function(){},onHideTitlebar:function(){}},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(),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(){"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);