-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathjquery.smartmenu.js
1 lines (1 loc) · 2.57 KB
/
jquery.smartmenu.js
1
(function(a){var t=a(document).data("func",{});a.smartMenu=a.noop;a.fn.smartMenu=function(e,i){var n=a("body"),r={name:"",offsetX:2,offsetY:2,textLimit:6,beforeShow:a.noop,afterShow:a.noop};var s=a.extend(r,i||{});var o=function(i){var n=i||e,r=i?Math.random().toString():s.name,l="",c="",d="smart_menu_";if(a.isArray(n)&&n.length){l='<div id="smartMenu_'+r+'" class="'+d+'box">'+'<div class="'+d+'body">'+'<ul class="'+d+'ul">';a.each(n,function(e,i){if(e){l=l+'<li class="'+d+'li_separate"> </li>'}if(a.isArray(i)){a.each(i,function(e,i){var n=i.text,r="",c="",h=Math.random().toString().replace(".","");if(n){if(n.length>s.textLimit){n=n.slice(0,s.textLimit)+"…";c=' title="'+i.text+'"'}if(a.isArray(i.data)&&i.data.length){r='<li class="'+d+'li" data-hover="true">'+o(i.data)+'<a href="javascript:" class="'+d+'a"'+c+' data-key="'+h+'"><i class="'+d+'triangle"></i>'+n+"</a>"+"</li>"}else{r='<li class="'+d+'li">'+'<a href="javascript:" class="'+d+'a"'+c+' data-key="'+h+'">'+n+"</a>"+"</li>"}l+=r;var f=t.data("func");f[h]=i.func;t.data("func",f)}})}});l=l+"</ul>"+"</div>"+"</div>"}return l},l=function(){var e="#smartMenu_",i="smart_menu_",n=a(e+s.name);if(!n.size()){a("body").append(o());a(e+s.name+" a").bind("click",function(){var e=a(this).attr("data-key"),i=t.data("func")[e];if(a.isFunction(i)){i.call(t.data("trigger"))}a.smartMenu.hide();return false});a(e+s.name+" li").each(function(){var t=a(this).attr("data-hover"),e=i+"li_hover";a(this).hover(function(){var n=a(this).siblings("."+e);n.removeClass(e).children("."+i+"box").hide();n.children("."+i+"a").removeClass(i+"a_hover");if(t){a(this).addClass(e).children("."+i+"box").show();a(this).children("."+i+"a").addClass(i+"a_hover")}})});return a(e+s.name)}return n};a(this).each(function(){this.oncontextmenu=function(e){if(a.isFunction(s.beforeShow)){s.beforeShow.call(this)}e=e||window.event;e.cancelBubble=true;if(e.stopPropagation){e.stopPropagation()}a.smartMenu.hide();var i=t.scrollTop();var n=l();if(n){var r=a(".smart_menu_box").show();var o=e.clientX+s.offsetX,c=a(window).scrollTop()+e.clientY+s.offsetY,d={wh:a(window).height(),ww:a(window).width(),mh:r.height(),mw:r.width()};r.hide();c=c+d.mh>=d.wh?c-=d.mh:c;o=o+d.mw>=d.ww?o-=d.mw:o;n.css({display:"block",left:o,top:c});t.data("target",n);t.data("trigger",this);if(a.isFunction(s.afterShow)){s.afterShow.call(this)}return false}}});if(!n.data("bind")){n.bind("click",a.smartMenu.hide).data("bind",true)}};a.extend(a.smartMenu,{hide:function(){var a=t.data("target");if(a&&a.css("display")==="block"){a.hide()}},remove:function(){var a=t.data("target");if(a){a.remove()}}})})(jQuery);