-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoonmenu.min.js
28 lines (28 loc) · 5.23 KB
/
moonmenu.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* ----------------------------- Moon Menu -------------------------------------
* Circular (Moon) JavaScript based menu that can be attached to elements or
* as a context menu. Supported by all modern browsers: IE9+, Firefox, Safari,
* Chrome and Opera
* Prerequisite: JQuery
*
* Copyright (c) 2013 - 2014 Liron Aichel, liron_wb@walla.com
*
* Licensed under MIT-style license:
*
* 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 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.
*
*/
(function(){function c(){this.version="1.0.2";this.options={openTrigger:"click touchend",closeTrigger:undefined,closeOnLostFocus:true,radius:40,position:"top right",reverseOrder:false,densityFactor:2,allowMultiple:false};this.sel={moons:".moonmenu"};this.events={beforeOpen:"beforeOpen",afterOpen:"opened",beforeClose:"beforeClose",afterClose:"afterClose",itemClick:"itemClick"}}var a=function(d){return d*(Math.PI/180)};var b=function(e,g){var o=this;var l=g.options.radius;var k=g.options.radius*2;var f=k;var n=k;var h=(2*Math.PI)/((e.length*g.options.densityFactor-2));var j={top:g.options.position.indexOf("top")!=-1,left:g.options.position.indexOf("left")!=-1,right:g.options.position.indexOf("right")!=-1,bottom:g.options.position.indexOf("bottom")!=-1,center:g.options.position.indexOf("center")!=-1,middle:g.options.position.indexOf("middle")!=-1};var i;if(j.top&&j.left){i=a(125)}else{if(j.bottom&&j.left){i=a(45)}else{if(j.bottom&&j.right){i=a(-45)}else{if(j.middle&&j.left){i=a(90)}else{if(j.middle&&j.right){i=a(-90)}else{if(j.center&&j.top){i=a(180)}else{if(j.center&&j.bottom){i=a(0)}else{i=a(-125)}}}}}}}var d=0;if(j.top){d=0}else{if(j.middle){d=g.attachedTo?g.attachedTo.height()/2:0}else{d=g.attachedTo?g.attachedTo.height():0}}var m=0;if(j.left){m=0}else{if(j.center){m=g.attachedTo?g.attachedTo.width()/2:0}else{m=g.attachedTo?g.attachedTo.width():0}}d-=(n/1.5);m-=(f/2);if(g.options.reverseOrder){e=$(e.get().reverse())}e.each(function(){var p=Math.round(f/2+l*Math.cos(i)-$(this).width()/2);var q=Math.round(n/2+l*Math.sin(i)-$(this).height()/2);$(this).css("top",(q+d)+"px");$(this).css("left",(p+m)+"px");i+=h})};c.prototype.init=function(g,f){var e=this;this.element=g;this.attachedTo=f;this.element.css("position","absolute");this.element.children().css("position","absolute");this.element.css("list-style","none");this.element.addClass("moonmenu");var d=this.element.find("li");d.addClass("moonMenuItem");d.hide();d.on("click touchend",function(h){$(e).trigger({type:e.events.itemClick,clickedItem:this})});if(this.attachedTo){if(!this.options.closeTrigger){this.attachedTo.on(this.options.openTrigger,function(){e.toggle()})}else{this.attachedTo.on(this.options.openTrigger,function(){e.open()});this.attachedTo.on(this.options.closeTrigger,function(){e.close()})}}if(this.options.closeOnLostFocus){$(document).on("mousedown",function(h){if(e.isActive()){if(e.attachedTo&&$(h.target)[0]!==e.attachedTo[0]){e.close()}if(!e.attachedTo&&h.button==0){e.close()}}})}};c.prototype.isActive=function(d){return this.element.is(".active")};c.prototype.toggle=function(d){if(this.isActive()){this.close(d)}else{this.open(d)}};c.prototype.open=function(g){$(this).trigger(this.events.beforeOpen,this);if(this.attachedTo&&this.isActive()){return}if(!this.options.allowMultiple){$(".moonmenu").each(function(){var e=$(this).data("moonMenu");if(e&&e.isActive()){e.close(g)}})}var d={x:0,y:0};if(this.attachedTo){d={x:this.attachedTo.offset().left,y:this.attachedTo.offset().top}}else{if(g.pageX&&g.pageY){d={x:g.pageX,y:g.pageY}}else{return}}this.element.css("top",d.y+"px");this.element.css("left",d.x+"px");var f=this.element.children();f.hide();b(f,this);$(f).each(function(e){$(this).stop(true);$(this).delay(40*e).fadeIn("fast")});this.element.addClass("active");$(this).trigger(this.events.afterOpen,this)};c.prototype.close=function(){$(this).trigger(this.events.beforeClose,this);var d=this.element.children();$(d).each(function(e){$(this).delay(40*e).fadeOut("fast")});this.element.removeClass("active");$(this).trigger(this.events.afterClose,this)};$.fn.moonMenu=function(f){if(this.length>1){var e=[];this.each(function(){e.push($(this).moonMenu(f))});return e}if(this.data("moonMenu")){return this.data("moonMenu")}if(this&&this.is("ul")){var d=new c();$.extend(d.options,f);if(this.attr("for")&&$(this.attr("for")).length>0){d.init(this,$(this.attr("for")))}else{d.init(this)}this.data("moonMenu",d);return d}};$(document).ready(function(){$("body [data-role='moonMenu']").moonMenu()})})();