-
Notifications
You must be signed in to change notification settings - Fork 102
/
contextMenu.min.js
8 lines (8 loc) · 10.5 KB
/
contextMenu.min.js
1
2
3
4
5
6
7
8
/*
*contextMenu.js v 1.4.0
*Author: Sudhanshu Yadav
*s-yadav.github.com
*Copyright (c) 2013-2015 Sudhanshu Yadav.
*Dual licensed under the MIT and GPL licenses
*/
!function(a,b,c,d){"use strict";a.single=function(){var b=a({});return function(a){return b[0]=a,b}}(),a.fn.contextMenu=function(b,c,d){e[b]?c&&(c instanceof Array||"string"==typeof c||c.nodeType||c.jquery||(d=c,c=null)):(d=c,c=b,b="popup"),c instanceof Array&&"update"!=b&&(b="menu");var g=d;return a.inArray(b,["menu","popup","close","destroy"])>-1?(d=f.optionOtimizer(b,d),this.each(function(){var f=a(this);g=a.extend({},a.fn.contextMenu.defaults,d),g.baseTrigger||(g.baseTrigger=f),e[b].call(f,c,g)})):e[b].call(this,c,g),this},a.fn.contextMenu.defaults={triggerOn:"click",subMenuTriggerOn:"hover click",displayAround:"cursor",mouseClick:"left",verAdjust:0,horAdjust:0,top:"auto",left:"auto",closeOther:!0,containment:b,winEventClose:!0,position:"auto",closeOnClick:!0,onOpen:function(a,b){},afterOpen:function(a,b){},onClose:function(a,b){}};var e={menu:function(a,b){a=f.createMenuList(this,a,b),f.contextMenuBind.call(this,a,b,"menu")},popup:function(b,c){a(b).addClass("iw-contextMenu"),f.contextMenuBind.call(this,b,c,"popup")},update:function(b,c){var e=this;c=c||{},this.each(function(){var g=a(this),h=g.data("iw-menuData");h||(e.contextMenu("refresh"),h=g.data("iw-menuData"));var i=h.menu;if("object"==typeof b)for(var j=0;j<b.length;j++){var k=b[j].name,l=b[j].disable,m=b[j].fun,n=b[j].icon,o=b[j].img,p=b[j].title,q=b[j].className,r=i.children("li").filter(function(){return a(this).contents().filter(function(){return 3==this.nodeType}).text()==k}),s=b[j].subMenu;l!=d&&(l?r.addClass("iw-mDisable"):r.removeClass("iw-mDisable")),m&&r.unbind("click.contextMenu").bind("click.contextMenu",m),p!=d&&r.attr("title",p),q!=d&&r.attr("class",q);var t=r.find(".iw-mIcon");t.length&&t.remove(),o?r.prepend('<img src="'+o+'" align="absmiddle" class="iw-mIcon" />'):n&&r.prepend('<span align="absmiddle" class="iw-mIcon '+n+'" />'),s&&r.contextMenu("update",s)}f.onOff(i);var u=c.triggerOn;if(u){g.unbind(".contextMenu"),u=u.split(" ");for(var v=[],j=0,w=u.length;j<w;j++)v.push(u[j]+".contextMenu");g.bind(v.join(" "),f.eventHandler)}h.option=a.extend({},h.option,c),g.data("iw-menuData",h)})},refresh:function(){var b=this.filter(function(){return!!a(this).data("iw-menuData")}).data("iw-menuData"),c=this.filter(function(){return!a(this).data("iw-menuData")});b.option.baseTrigger=this,f.contextMenuBind.call(c,b.menuSelector,b.option)},open:function(b,c){c=c||{};var d=c.event||a.Event("click");c.top&&(d.clientY=c.top),c.left&&(d.clientX=c.left),this.each(function(){f.eventHandler.call(this,d)})},close:function(){var a=this.data("iw-menuData");a&&f.closeContextMenu(a.option,this,a.menu,null)},value:function(a){var b=this.data("iw-menuData");return b[a]?b[a]:b.option?b.option[a]:null},destroy:function(){var b=this,c=b.data("iw-menuData").menuId,d=a(".iw-contextMenu[menuId="+c+"]"),e=d.data("iw-menuData");e&&(1==e.noTrigger?d.hasClass("iw-created")?d.remove():(d.removeClass("iw-contextMenu "+c).removeAttr("menuId").removeData("iw-menuData"),d.find("li.iw-mTrigger").contextMenu("destroy")):(e.noTrigger--,d.data("iw-menuData",e)),b.unbind(".contextMenu").removeClass("iw-mTrigger").removeData("iw-menuData"))}},f={contextMenuBind:function(b,c,d){var e=this,g=a(b),h=g.data("iw-menuData");if(0!=g.length||(g=e.find(b),0!=g.length)){"menu"==d&&f.menuHover(g);var i=c.baseTrigger;if(h)h.noTrigger++,g.data("iw-menuData",h);else{var j;i.data("iw-menuData")?j=i.data("iw-menuData").menuId:(j=Math.ceil(1e5*Math.random()),i.data("iw-menuData",{menuId:j}));var k=g.clone();k.appendTo("body"),h={menuId:j,menuWidth:k.outerWidth(!0),menuHeight:k.outerHeight(!0),noTrigger:1,trigger:e},g.data("iw-menuData",h).attr("menuId",j),k.remove()}e.addClass("iw-mTrigger").data("iw-menuData",{menuId:h.menuId,option:c,menu:g,menuSelector:b,method:d});var l=c.triggerOn;l.indexOf("hover")!=-1&&(l=l.replace("hover","mouseenter"),i.index(e)!=-1&&i.add(g).bind("mouseleave.contextMenu",function(b){0==a(b.relatedTarget).closest(".iw-contextMenu").length&&a('.iw-contextMenu[menuId="'+h.menuId+'"]').fadeOut(100)})),e.delegate("input,a,.needs-click","click",function(a){a.stopImmediatePropagation()}),l=l.split(" ");for(var m=[],n=0,o=l.length;n<o;n++)m.push(l[n]+".contextMenu");e.bind(m.join(" "),f.eventHandler),g.bind("click mouseenter",function(a){a.stopPropagation()}),g.delegate("li","click",function(b){c.closeOnClick&&!a.single(this).hasClass("iw-has-submenu")&&f.closeContextMenu(c,e,g,b)})}},eventHandler:function(d){d.preventDefault();var e=a(this),g=e.data("iw-menuData"),h=g.menu,i=h.data("iw-menuData"),j=g.option,k=j.containment,l={trigger:e,menu:h},m=k==b,n=j.baseTrigger.index(e)==-1;!n&&j.closeOther&&a(".iw-contextMenu").css("display","none"),h.find(".iw-mSelected").removeClass("iw-mSelected"),j.onOpen.call(this,l,d);var v,w,z,A,o=a(k),p=o.innerHeight(),q=o.innerWidth(),r=0,s=0,t=i.menuHeight,u=i.menuWidth,x=0,y=0,B=v=parseInt(j.verAdjust),C=w=parseInt(j.horAdjust);if(m||(r=o.offset().top,s=o.offset().left,"static"==o.css("position")&&o.css("position","relative")),"cursor"==j.displayAround)x=m?d.clientX:d.clientX+a(b).scrollLeft()-s,y=m?d.clientY:d.clientY+a(b).scrollTop()-r,z=y+t,A=x+u,z>p&&(y-t<0?z-p<t-y?(y=p-t,v=-1*v):(y=0,v=0):(y-=t,v=-1*v)),A>q&&(x-u<0?A-q<u-x?(x=q-u,w=-1*w):(x=0,w=0):(x-=u,w=-1*w));else if("trigger"==j.displayAround){var D=e.outerHeight(!0),E=e.outerWidth(!0),F=m?e.offset().left-o.scrollLeft():e.offset().left-s,G=m?e.offset().top-o.scrollTop():e.offset().top-r,H=E;x=F+E,y=G,z=y+t,A=x+u,z>p&&(y-t<0?z-p<t-y?(y=p-t,v=-1*v):(y=0,v=0):(y=y-t+D,v=-1*v)),A>q&&(x-u<0?A-q<u-x?(x=q-u,w=-1*w,H=-E):(x=0,w=0,H=0):(x=x-u-E,w=-1*w,H=-E)),"top"==j.position?(y=G-t,v=B,x-=H):"left"==j.position?(x=F-u,w=C):"bottom"==j.position?(y=G+D,v=B,x-=H):"right"==j.position&&(x=F+E,w=C)}var I={position:m||n?"fixed":"absolute",display:"inline-block",height:"",width:""};if("auto"!=j.left&&(x=f.getPxSize(j.left,q)),"auto"!=j.top&&(y=f.getPxSize(j.top,p)),!m){var J=e.offsetParent().offset();n?(x=x+s-a(b).scrollLeft(),y=y+r-a(b).scrollTop()):(x-=s-J.left,y-=r-J.top)}I.left=x+w+"px",I.top=y+v+"px",h.css(I),j.afterOpen.call(this,l,d),0==e.closest(".iw-contextMenu").length&&(a(".iw-curMenu").removeClass("iw-curMenu"),h.addClass("iw-curMenu"));var K={trigger:e,menu:h,option:j,method:g.method};a("html").unbind("click",f.clickEvent).click(K,f.clickEvent),a(c).unbind("keydown",f.keyEvent).keydown(K,f.keyEvent),j.winEventClose&&a(b).bind("scroll resize",K,f.scrollEvent)},scrollEvent:function(a){f.closeContextMenu(a.data.option,a.data.trigger,a.data.menu,a)},clickEvent:function(b){var c=b.data.trigger.get(0);c!==b.target&&0==a(b.target).closest(".iw-contextMenu").length&&f.closeContextMenu(b.data.option,b.data.trigger,b.data.menu,b)},keyEvent:function(b){b.preventDefault();var c=b.data.menu,d=b.data.option,e=b.keyCode;if(27==e&&f.closeContextMenu(d,b.data.trigger,c,b),"menu"==b.data.method){var g=a(".iw-curMenu"),h=g.children("li:not(.iw-mDisable)"),i=h.filter(".iw-mSelected"),j=h.index(i),k=function(a){f.selectMenu(g,a);var c=a.data("iw-menuData");c&&f.eventHandler.call(a[0],b)},l=function(){k(h.filter(":first"))},m=function(){k(h.filter(":last"))},n=function(){k(h.filter(":eq("+(j+1)+")"))},o=function(){k(h.filter(":eq("+(j-1)+")"))},p=function(){var a=i.data("iw-menuData");if(a){f.eventHandler.call(i[0],b);var c=a.menu;c.addClass("iw-curMenu"),g.removeClass("iw-curMenu"),g=c,h=g.children("li:not(.iw-mDisable)"),i=h.filter(".iw-mSelected"),l()}},q=function(){var a=g.data("iw-menuData").trigger,b=a.closest(".iw-contextMenu");0!=b.length&&(g.removeClass("iw-curMenu").css("display","none"),b.addClass("iw-curMenu"))};switch(e){case 13:i.click();break;case 40:j==h.length-1||0==i.length?l():n();break;case 38:0==j||0==i.length?m():o();break;case 33:l();break;case 34:m();break;case 37:q();break;case 39:p()}}},closeContextMenu:function(d,e,g,h){a(c).unbind("keydown",f.keyEvent),a("html").unbind("click",f.clickEvent),a(b).unbind("scroll resize",f.scrollEvent),a(".iw-contextMenu").css("display","none"),a(c).focus(),d.onClose.call(this,{trigger:e,menu:g},h)},getPxSize:function(a,b){return isNaN(a)?a.indexOf("%")!=-1?parseInt(a)*b/100:parseInt(a):a},selectMenu:function(a,b){var c=a.find("li.iw-mSelected"),d=c.find(".iw-contextMenu");0!=d.length&&c[0]!=b[0]&&d.fadeOut(100),c.removeClass("iw-mSelected"),b.addClass("iw-mSelected")},menuHover:function(b){Date.now();b.children("li").bind("mouseenter.contextMenu click.contextMenu",function(c){a(".iw-curMenu").removeClass("iw-curMenu"),b.addClass("iw-curMenu"),f.selectMenu(b,a(this))})},createMenuList:function(c,d,e){var g=e.baseTrigger,h=Math.floor(1e4*Math.random());if("object"==typeof d&&!d.nodeType&&!d.jquery){var i=a('<ul class="iw-contextMenu iw-created iw-cm-menu" id="iw-contextMenu'+h+'"></ul>');if(a.each(d,function(b,c){var d=c.name,h=c.fun||function(){},j=c.subMenu,k=c.img||"",l=c.icon||"",m=c.title||"",n=c.className||"",o=c.disable,p=a('<li title="'+m+'" class="'+n+'">'+d+"</li>");k?p.prepend('<img src="'+k+'" align="absmiddle" class="iw-mIcon" />'):l&&p.prepend('<span align="absmiddle" class="iw-mIcon '+l+'" />'),o&&p.addClass("iw-mDisable"),j||p.bind("click.contextMenu",function(a){h.call(this,{trigger:g,menu:i},a)}),i.append(p),j&&(p.addClass("iw-has-submenu").append('<div class="iw-cm-arrow-right" />'),f.subMenu(p,j,g,e))}),g.index(c[0])==-1)c.append(i);else{var j=e.containment==b?"body":e.containment;a(j).append(i)}return f.onOff(a("#iw-contextMenu"+h)),"#iw-contextMenu"+h}if(0!=a(d).length){var k=a(d);return k.removeClass("iw-contextMenuCurrent").addClass("iw-contextMenu iw-cm-menu iw-contextMenu"+h).attr("menuId","iw-contextMenu"+h).css("display","none"),k.find("ul").each(function(b,c){var d=a(this),h=d.parent("li");h.append('<div class="iw-cm-arrow-right" />'),d.addClass("iw-contextMenuCurrent"),f.subMenu(h,".iw-contextMenuCurrent",g,e)}),f.onOff(a(".iw-contextMenu"+h)),".iw-contextMenu"+h}},subMenu:function(a,b,c,d){a.contextMenu("menu",b,{triggerOn:d.subMenuTriggerOn,displayAround:"trigger",position:"auto",mouseClick:"left",baseTrigger:c,containment:d.containment})},onOff:function(b){b.find(".iw-mOverlay").remove(),b.find(".iw-mDisable").each(function(){var b=a(this);b.append('<div class="iw-mOverlay"/>'),b.find(".iw-mOverlay").bind("click mouseenter",function(a){a.stopPropagation()})})},optionOtimizer:function(b,c){if(c)return"menu"==b&&(c.mouseClick||(c.mouseClick="right")),"right"==c.mouseClick&&"click"==c.triggerOn&&(c.triggerOn="contextmenu"),a.inArray(c.triggerOn,["hover","mouseenter","mouseover","mouseleave","mouseout","focusin","focusout"])!=-1&&(c.displayAround="trigger"),c}}}(jQuery,window,document);