diff --git a/bower.json b/bower.json index 3a36ebc..3ca53d0 100644 --- a/bower.json +++ b/bower.json @@ -6,7 +6,7 @@ "name": "Sudhanshu Yadav", "email": "sudhanshuyadav2@gmail.com" }, - "version": "1.2.3", + "version": "1.3.0", "repository": { "type": "git", "url": "https://github.com/s-yadav/contextMenu.js" diff --git a/contextMenu.js b/contextMenu.js index 06aec96..0ccb3c1 100644 --- a/contextMenu.js +++ b/contextMenu.js @@ -1,5 +1,5 @@ /* - *contextMenu.js v 1.2.3 + *contextMenu.js v 1.3.0 *Author: Sudhanshu Yadav *s-yadav.github.com *Copyright (c) 2013 Sudhanshu Yadav. @@ -157,6 +157,15 @@ menuData.option.baseTrigger = this; iMethods.contextMenuBind.call(newElm, menuData.menuSelector, menuData.option); }, + open: function(sel,data){ + data = data || {}; + var e = data.event || new Event('click'); + if(data.top) e.clientY = data.top; + if(data.left) e.clientX = data.left; + this.each(function(){ + iMethods.eventHandler.call(this,e); + }); + }, //to force context menu to close close: function () { var menuData = this.data('iw-menuData'); diff --git a/contextMenu.js.jquery.json b/contextMenu.js.jquery.json index 5da1749..7dd52de 100644 --- a/contextMenu.js.jquery.json +++ b/contextMenu.js.jquery.json @@ -1,6 +1,6 @@ { "name": "contextMenu.js", - "version": "1.2.3", + "version": "1.3.0", "title": "A light weight multi functional context menu plugin", "author": { "name": "Sudhanshu Yadav", diff --git a/contextMenu.min.js b/contextMenu.min.js index 991a201..c101135 100644 --- a/contextMenu.min.js +++ b/contextMenu.min.js @@ -1,8 +1,8 @@ /* - *contextMenu.js v 1.2.3 + *contextMenu.js v 1.3.0 *Author: Sudhanshu Yadav *s-yadav.github.com *Copyright (c) 2013 Sudhanshu Yadav. *Dual licensed under the MIT and GPL licenses */ -;(function(e,t,n,r){"use strict";e.fn.contextMenu=function(t,n,r){if(!i[t]){r=n;n=t;t="popup"}else if(n){if(!(n instanceof Array||typeof n==="string"||n.nodeType||n.jquery)){r=n;n=null}}if(n instanceof Array&&t!="update"){t="menu"}var o=r;if(t!="update"){r=s.optionOtimizer(t,r);o=e.extend({},e.fn.contextMenu.defaults,r);if(!o.baseTrigger){o.baseTrigger=this}}i[t].call(this,n,o);return this};e.fn.contextMenu.defaults={triggerOn:"click",displayAround:"cursor",mouseClick:"left",verAdjust:0,horAdjust:0,top:"auto",left:"auto",closeOther:true,containment:t,winEventClose:true,sizeStyle:"auto",position:"auto",closeOnClick:true,onOpen:function(e,t){},afterOpen:function(e,t){},onClose:function(e,t){}};var i={menu:function(t,n){var r=e(this);t=s.createMenuList(r,t,n);s.contextMenuBind.call(this,t,n,"menu")},popup:function(t,n){e(t).addClass("iw-contextMenu");s.contextMenuBind.call(this,t,n,"popup")},update:function(t,n){var i=this;this.each(function(){var o=e(this),u=o.data("iw-menuData");if(!u){i.contextMenu("refresh");u=o.data("iw-menuData")}var a=u.menu;if(typeof t==="object"){for(var f=0;f')}}if(g){m.contextMenu("update",g)}}}s.onOff(a);u.option=e.extend({},u.option,n);o.data("iw-menuData",u);var b=u.option.triggerOn;if(n){if(b!=n.triggerOn){o.unbind(".contextMenu");o.bind(b+".contextMenu",s.eventHandler)}}})},refresh:function(){var t=this.filter(function(){return!!e(this).data("iw-menuData")}).data("iw-menuData"),n=this.filter(function(){return!e(this).data("iw-menuData")});t.option.baseTrigger=this;s.contextMenuBind.call(n,t.menuSelector,t.option)},close:function(){var e=this.data("iw-menuData");if(e){s.closeContextMenu(e.option,this,e.menu,null)}},value:function(e){var t=this.data("iw-menuData");if(t[e]){return t[e]}else if(t.option){return t.option[e]}return null},destroy:function(){this.each(function(){var t=e(this),n=t.data("iw-menuData").menuId,r=e(".iw-contextMenu[menuId="+n+"]"),i=r.data("iw-menuData");if(!i)return;if(i.noTrigger==1){if(r.hasClass("iw-created")){r.remove()}else{r.removeClass("iw-contextMenu "+n).removeAttr("menuId").removeData("iw-menuData");r.find("li.iw-mTrigger").contextMenu("destroy")}}else{i.noTrigger--;r.data("iw-menuData",i)}t.unbind(".contextMenu").removeClass("iw-mTrigger").removeData("iw-menuData")})}};var s={contextMenuBind:function(t,n,r){var i=this,o=e(t),u=o.data("iw-menuData");if(o.length==0){o=i.find(t);if(o.length==0){return}}if(r=="menu"){s.menuHover(o)}var a=n.baseTrigger;if(!u){var f;if(!a.data("iw-menuData")){f=Math.ceil(Math.random()*1e5);a.data("iw-menuData",{menuId:f})}else{f=a.data("iw-menuData").menuId}var l=o.clone();l.appendTo("body");u={menuId:f,menuWidth:l.outerWidth(true),menuHeight:l.outerHeight(true),noTrigger:1,trigger:i};o.data("iw-menuData",u).attr("menuId",f);l.remove()}else{u.noTrigger++;o.data("iw-menuData",u)}i.addClass("iw-mTrigger").data("iw-menuData",{menuId:u.menuId,option:n,menu:o,menuSelector:t,method:r});var c;if(n.triggerOn=="hover"){c="mouseenter";if(a.index(i)!=-1){a.add(o).bind("mouseleave.contextMenu",function(t){if(e(t.relatedTarget).closest(".iw-contextMenu").length==0){e('.iw-contextMenu[menuId="'+u.menuId+'"]').hide(100)}})}}else{c=n.triggerOn}i.delegate("input,a,.needs-click","click",function(e){e.stopImmediatePropagation()});i.bind(c+".contextMenu",s.eventHandler);o.bind("click mouseenter",function(e){e.stopPropagation()});o.delegate("li","click",function(e){if(n.closeOnClick)s.closeContextMenu(n,i,o,e)})},eventHandler:function(r){r.preventDefault();var i=e(this),o=i.data("iw-menuData"),u=o.menu,a=u.data("iw-menuData"),f=o.option,l=f.containment,c={trigger:i,menu:u},h=l==t,p=f.baseTrigger.index(i)==-1;if(!p&&f.closeOther){e(".iw-contextMenu").css("display","none")}u.find(".iw-mSelected").removeClass("iw-mSelected");f.onOpen.call(this,c,r);var d=e(l),v=d.innerHeight(),m=d.innerWidth(),g=0,y=0,b=a.menuHeight,w=a.menuWidth,E,S,x=0,T=0,N,C,k=E=parseInt(f.verAdjust),L=S=parseInt(f.horAdjust);if(!h){g=d.offset().top;y=d.offset().left;if(d.css("position")=="static"){d.css("position","relative")}}if(f.sizeStyle=="auto"){b=Math.min(b,v);w=Math.min(w,m);w=w+20}if(f.displayAround=="cursor"){x=h?r.clientX:r.clientX+e(t).scrollLeft()-y;T=h?r.clientY:r.clientY+e(t).scrollTop()-g;N=T+b;C=x+w;if(N>v){if(T-b<0){if(N-vm){if(x-w<0){if(C-mv){if(T-b<0){if(N-vm){if(x-w<0){if(C-m');for(var f=0;f'+c+"");if(d){y.prepend('')}if(g){y.addClass("iw-mDisable")}y.bind("click.contextMenu",h);a.append(y);if(p){y.append('
');s.subMenu(y,p,o,i)}}if(o.index(n[0])==-1){n.append(a)}else{var b=i.containment==t?"body":i.containment;e(b).append(a)}s.onOff(e("#iw-contextMenu"+u));return"#iw-contextMenu"+u}else if(e(r).length!=0){var w=e(r);w.removeClass("iw-contextMenuCurrent").addClass("iw-contextMenu iw-cm-menu iw-contextMenu"+u).attr("menuId","iw-contextMenu"+u).css("display","none");w.find("ul").each(function(t,n){var r=e(this),u=r.parent("li");u.append('
');r.addClass("iw-contextMenuCurrent");s.subMenu(u,".iw-contextMenuCurrent",o,i)});s.onOff(e(".iw-contextMenu"+u));return".iw-contextMenu"+u}},subMenu:function(e,t,n,r){e.contextMenu("menu",t,{triggerOn:"hover",displayAround:"trigger",position:"auto",baseTrigger:n,containment:r.containment})},onOff:function(t){t.find(".iw-mOverlay").remove();t.find(".iw-mDisable").each(function(){var t=e(this);t.append('
');t.find(".iw-mOverlay").bind("click mouseenter",function(e){e.stopPropagation()})})},optionOtimizer:function(t,n){if(!n){return}if(t=="menu"){if(!n.mouseClick){n.mouseClick="right"}}if(n.mouseClick=="right"&&n.triggerOn=="click"){n.triggerOn="contextmenu"}if(e.inArray(n.triggerOn,["hover","mouseenter","mouseover","mouseleave","mouseout","focusin","focusout"])!=-1){n.displayAround="trigger"}return n}}})(jQuery,window,document); \ No newline at end of file +;!function(e,t,n,i){"use strict";e.fn.contextMenu=function(t,n,i){a[t]?n&&(n instanceof Array||"string"==typeof n||n.nodeType||n.jquery||(i=n,n=null)):(i=n,n=t,t="popup"),n instanceof Array&&"update"!=t&&(t="menu");var r=i;return"update"!=t&&(i=o.optionOtimizer(t,i),r=e.extend({},e.fn.contextMenu.defaults,i),r.baseTrigger||(r.baseTrigger=this)),a[t].call(this,n,r),this},e.fn.contextMenu.defaults={triggerOn:"click",displayAround:"cursor",mouseClick:"left",verAdjust:0,horAdjust:0,top:"auto",left:"auto",closeOther:!0,containment:t,winEventClose:!0,sizeStyle:"auto",position:"auto",closeOnClick:!0,onOpen:function(){},afterOpen:function(){},onClose:function(){}};var a={menu:function(t,n){var i=e(this);t=o.createMenuList(i,t,n),o.contextMenuBind.call(this,t,n,"menu")},popup:function(t,n){e(t).addClass("iw-contextMenu"),o.contextMenuBind.call(this,t,n,"popup")},update:function(t,n){var a=this;this.each(function(){var r=e(this),u=r.data("iw-menuData");u||(a.contextMenu("refresh"),u=r.data("iw-menuData"));var c=u.menu;if("object"==typeof t)for(var s=0;s')}w&&p.contextMenu("update",w)}o.onOff(c),u.option=e.extend({},u.option,n),r.data("iw-menuData",u);var M=u.option.triggerOn;n&&M!=n.triggerOn&&(r.unbind(".contextMenu"),r.bind(M+".contextMenu",o.eventHandler))})},refresh:function(){var t=this.filter(function(){return!!e(this).data("iw-menuData")}).data("iw-menuData"),n=this.filter(function(){return!e(this).data("iw-menuData")});t.option.baseTrigger=this,o.contextMenuBind.call(n,t.menuSelector,t.option)},open:function(e,t){t=t||{};var n=t.event||new Event("click");t.top&&(n.clientY=t.top),t.left&&(n.clientX=t.left),this.each(function(){o.eventHandler.call(this,n)})},close:function(){var e=this.data("iw-menuData");e&&o.closeContextMenu(e.option,this,e.menu,null)},value:function(e){var t=this.data("iw-menuData");return t[e]?t[e]:t.option?t.option[e]:null},destroy:function(){this.each(function(){var t=e(this),n=t.data("iw-menuData").menuId,i=e(".iw-contextMenu[menuId="+n+"]"),a=i.data("iw-menuData");a&&(1==a.noTrigger?i.hasClass("iw-created")?i.remove():(i.removeClass("iw-contextMenu "+n).removeAttr("menuId").removeData("iw-menuData"),i.find("li.iw-mTrigger").contextMenu("destroy")):(a.noTrigger--,i.data("iw-menuData",a)),t.unbind(".contextMenu").removeClass("iw-mTrigger").removeData("iw-menuData"))})}},o={contextMenuBind:function(t,n,i){var a=this,r=e(t),u=r.data("iw-menuData");if(0!=r.length||(r=a.find(t),0!=r.length)){"menu"==i&&o.menuHover(r);var c=n.baseTrigger;if(u)u.noTrigger++,r.data("iw-menuData",u);else{var s;c.data("iw-menuData")?s=c.data("iw-menuData").menuId:(s=Math.ceil(1e5*Math.random()),c.data("iw-menuData",{menuId:s}));var l=r.clone();l.appendTo("body"),u={menuId:s,menuWidth:l.outerWidth(!0),menuHeight:l.outerHeight(!0),noTrigger:1,trigger:a},r.data("iw-menuData",u).attr("menuId",s),l.remove()}a.addClass("iw-mTrigger").data("iw-menuData",{menuId:u.menuId,option:n,menu:r,menuSelector:t,method:i});var d;"hover"==n.triggerOn?(d="mouseenter",-1!=c.index(a)&&c.add(r).bind("mouseleave.contextMenu",function(t){0==e(t.relatedTarget).closest(".iw-contextMenu").length&&e('.iw-contextMenu[menuId="'+u.menuId+'"]').hide(100)})):d=n.triggerOn,a.delegate("input,a,.needs-click","click",function(e){e.stopImmediatePropagation()}),a.bind(d+".contextMenu",o.eventHandler),r.bind("click mouseenter",function(e){e.stopPropagation()}),r.delegate("li","click",function(e){n.closeOnClick&&o.closeContextMenu(n,a,r,e)})}},eventHandler:function(i){i.preventDefault();var a=e(this),r=a.data("iw-menuData"),u=r.menu,c=u.data("iw-menuData"),s=r.option,l=s.containment,d={trigger:a,menu:u},m=l==t,f=-1==s.baseTrigger.index(a);!f&&s.closeOther&&e(".iw-contextMenu").css("display","none"),u.find(".iw-mSelected").removeClass("iw-mSelected"),s.onOpen.call(this,d,i);var g,h,p,w,v=e(l),M=v.innerHeight(),x=v.innerWidth(),b=0,C=0,k=c.menuHeight,y=c.menuWidth,D=0,O=0,T=g=parseInt(s.verAdjust),I=h=parseInt(s.horAdjust);if(m||(b=v.offset().top,C=v.offset().left,"static"==v.css("position")&&v.css("position","relative")),"auto"==s.sizeStyle&&(k=Math.min(k,M),y=Math.min(y,x),y+=20),"cursor"==s.displayAround)D=m?i.clientX:i.clientX+e(t).scrollLeft()-C,O=m?i.clientY:i.clientY+e(t).scrollTop()-b,p=O+k,w=D+y,p>M&&(0>O-k?k-O>p-M?(O=M-k,g=-1*g):(O=0,g=0):(O-=k,g=-1*g)),w>x&&(0>D-y?y-D>w-x?(D=x-y,h=-1*h):(D=0,h=0):(D-=y,h=-1*h));else if("trigger"==s.displayAround){var S=a.outerHeight(!0),H=a.outerWidth(!0),E=m?a.offset().left-v.scrollLeft():a.offset().left-C,A=m?a.offset().top-v.scrollTop():a.offset().top-b,z=H;D=E+H,O=A,p=O+k,w=D+y,p>M&&(0>O-k?k-O>p-M?(O=M-k,g=-1*g):(O=0,g=0):(O=O-k+S,g=-1*g)),w>x&&(0>D-y?y-D>w-x?(D=x-y,h=-1*h,z=-H):(D=0,h=0,z=0):(D=D-y-H,h=-1*h,z=-H)),"top"==s.position?(k=Math.min(c.menuHeight,A),O=A-k,g=T,D-=z):"left"==s.position?(y=Math.min(c.menuWidth,E),D=E-y,h=I):"bottom"==s.position?(k=Math.min(c.menuHeight,M-A-S),O=A+S,g=T,D-=z):"right"==s.position&&(y=Math.min(c.menuWidth,x-E-H),D=E+H,h=I)}var j=u.outerWidth(!0)-u.width(),W=u.outerHeight(!0)-u.height(),P={position:m||f?"fixed":"absolute",display:"inline-block",height:"",width:"","overflow-y":k!=c.menuHeight?"auto":"hidden","overflow-x":y!=c.menuWidth?"auto":"hidden"};if("auto"==s.sizeStyle&&(P.height=k-W+"px",P.width=y-j+"px"),"auto"!=s.left&&(D=o.getPxSize(s.left,x)),"auto"!=s.top&&(O=o.getPxSize(s.top,M)),!m){var L=a.offsetParent().offset();f?(D=D+C-e(t).scrollLeft(),O=O+b-e(t).scrollTop()):(D-=C-L.left,O-=b-L.top)}P.left=D+h+"px",P.top=O+g+"px",u.css(P),s.afterOpen.call(this,d,i),0==a.closest(".iw-contextMenu").length&&(e(".iw-curMenu").removeClass("iw-curMenu"),u.addClass("iw-curMenu"));var q={trigger:a,menu:u,option:s,method:r.method};e("html").unbind("click",o.clickEvent).click(q,o.clickEvent),e(n).unbind("keydown",o.keyEvent).keydown(q,o.keyEvent),s.winEventClose&&e(t).bind("scroll resize",q,o.scrollEvent)},scrollEvent:function(e){o.closeContextMenu(e.data.option,e.data.trigger,e.data.menu,e)},clickEvent:function(t){var n=t.data.trigger.get(0);n!==t.target&&0==e(t.target).closest(".iw-contextMenu").length&&o.closeContextMenu(t.data.option,t.data.trigger,t.data.menu,t)},keyEvent:function(t){t.preventDefault();var n=t.data.menu,i=t.data.option,a=t.keyCode;if(27==a&&o.closeContextMenu(i,t.data.trigger,n,t),"menu"==t.data.method){var r=e(".iw-curMenu"),u=r.children("li:not(.iw-mDisable)"),c=u.filter(".iw-mSelected"),s=u.index(c),l=function(e){c.removeClass("iw-mSelected"),e.addClass("iw-mSelected")},d=function(){l(u.filter(":first"))},m=function(){l(u.filter(":last"))},f=function(){l(u.filter(":eq("+(s+1)+")"))},g=function(){l(u.filter(":eq("+(s-1)+")"))},h=function(){var e=c.data("iw-menuData");if(e){c.triggerHandler("mouseenter.contextMenu");var t=e.menu;t.addClass("iw-curMenu"),r.removeClass("iw-curMenu"),r=t,u=r.children("li:not(.iw-mDisable)"),c=u.filter(".iw-mSelected"),d()}},p=function(){var e=r.data("iw-menuData").trigger,t=e.closest(".iw-contextMenu");0!=t.length&&(r.removeClass("iw-curMenu").css("display","none"),t.addClass("iw-curMenu"))};switch(a){case 13:c.click();break;case 40:s==u.length-1||0==c.length?d():f();break;case 38:0==s||0==c.length?m():g();break;case 33:d();break;case 34:m();break;case 37:p();break;case 39:h()}}},closeContextMenu:function(i,a,r,u){e(n).unbind("keydown",o.keyEvent),e("html").unbind("click",o.clickEvent),e(t).unbind("scroll resize",o.scrollEvent),e(".iw-contextMenu").hide(),e(n).focus(),i.onClose.call(this,{trigger:a,menu:r},u)},getPxSize:function(e,t){return isNaN(e)?-1!=e.indexOf("%")?parseInt(e)*t/100:parseInt(e):e},menuHover:function(t){t.children("li").bind("mouseenter",function(){e(".iw-curMenu").removeClass("iw-curMenu"),t.addClass("iw-curMenu");var n=t.find("li.iw-mSelected"),i=n.find(".iw-contextMenu");0!=i.length&&n[0]!=this&&i.hide(100),n.removeClass("iw-mSelected"),e(this).addClass("iw-mSelected")})},createMenuList:function(n,i,a){var r=a.baseTrigger,u=Math.floor(1e4*Math.random());if("object"==typeof i&&!i.nodeType&&!i.jquery){for(var c=e('
    '),s=0;s'+d+"");g&&v.prepend(''),w&&v.addClass("iw-mDisable"),v.bind("click.contextMenu",m),c.append(v),f&&(v.append('
    '),o.subMenu(v,f,r,a))}if(-1==r.index(n[0]))n.append(c);else{var M=a.containment==t?"body":a.containment;e(M).append(c)}return o.onOff(e("#iw-contextMenu"+u)),"#iw-contextMenu"+u}if(0!=e(i).length){var x=e(i);return x.removeClass("iw-contextMenuCurrent").addClass("iw-contextMenu iw-cm-menu iw-contextMenu"+u).attr("menuId","iw-contextMenu"+u).css("display","none"),x.find("ul").each(function(){var t=e(this),n=t.parent("li");n.append('
    '),t.addClass("iw-contextMenuCurrent"),o.subMenu(n,".iw-contextMenuCurrent",r,a)}),o.onOff(e(".iw-contextMenu"+u)),".iw-contextMenu"+u}},subMenu:function(e,t,n,i){e.contextMenu("menu",t,{triggerOn:"hover",displayAround:"trigger",position:"auto",baseTrigger:n,containment:i.containment})},onOff:function(t){t.find(".iw-mOverlay").remove(),t.find(".iw-mDisable").each(function(){var t=e(this);t.append('
    '),t.find(".iw-mOverlay").bind("click mouseenter",function(e){e.stopPropagation()})})},optionOtimizer:function(t,n){return n?("menu"==t&&(n.mouseClick||(n.mouseClick="right")),"right"==n.mouseClick&&"click"==n.triggerOn&&(n.triggerOn="contextmenu"),-1!=e.inArray(n.triggerOn,["hover","mouseenter","mouseover","mouseleave","mouseout","focusin","focusout"])&&(n.displayAround="trigger"),n):void 0}}}(jQuery,window,document); \ No newline at end of file