This repository was archived by the owner on Jul 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcontextmenu.min.js
More file actions
1 lines (1 loc) · 3.43 KB
/
Copy pathcontextmenu.min.js
File metadata and controls
1 lines (1 loc) · 3.43 KB
1
function ContextMenu(a,b){function c(h){var j=document.createElement("ul");return h.forEach(function(k){var l=document.createElement("li");if(l.menu=f,"undefined"==typeof k.type){var m=document.createElement("span");m.className="cm_icon_span",m.innerHTML=""==ContextUtil.getProperty(k,"icon","")?ContextUtil.getProperty(b,"default_icon",""):ContextUtil.getProperty(k,"icon","");var n=document.createElement("span");n.className="cm_text",n.innerHTML=""==ContextUtil.getProperty(k,"text","")?ContextUtil.getProperty(b,"default_text","item"):ContextUtil.getProperty(k,"text","");var o=document.createElement("span");if(o.className="cm_sub_span","undefined"!=typeof k.sub&&(""==ContextUtil.getProperty(b,"sub_icon","")?o.innerHTML="›":o.innerHTML=ContextUtil.getProperty(b,"sub_icon","")),l.appendChild(m),l.appendChild(n),l.appendChild(o),!ContextUtil.getProperty(k,"enabled",!0))l.setAttribute("disabled","");else{if("object"==typeof k.events)for(var p=Object.keys(k.events),q=0;q<p.length;q++)l.addEventListener(p[q],k.events[p[q]]);"undefined"!=typeof k.sub&&l.appendChild(c(k.sub))}}else k.type==ContextMenu.DIVIDER&&(l.className="cm_divider");j.appendChild(l)}),j}function d(){f.hide()}var f=this,g=ContextMenu.count++;if(this.menu=a,this.contextTarget=null,!(a instanceof Array))throw new Error("Parameter 1 must be of type Array");if("undefined"==typeof b)b={};else if("object"!=typeof b)throw new Error("Parameter 2 must be of type object");window.addEventListener("resize",function(){ContextUtil.getProperty(b,"close_on_resize",!0)&&f.hide()}),this.setOptions=function(h){if("object"==typeof h)b=h;else throw new Error("Parameter 1 must be of type object")},this.changeOption=function(h,j){if("string"!=typeof h)throw new Error("Parameter 1 must be of type string");else if("undefined"!=typeof j)b[h]=j;else throw new Error("Parameter 2 must be set")},this.getOptions=function(){return b},this.reload=function(){if(null==document.getElementById("cm_"+g)){var h=document.createElement("div");h.className="cm_container",h.id="cm_"+g,document.body.appendChild(h)}var j=document.getElementById("cm_"+g);j.innerHTML="",j.appendChild(c(a))},this.display=function(h,j){f.contextTarget="undefined"==typeof j?h.target:j;var k=document.getElementById("cm_"+g),l={x:h.clientX,y:h.clientY},m=l.x,n=l.y,o=k.offsetWidth+4,p=k.offsetHeight+4,q=window.innerWidth,r=window.innerHeight,s=parseInt(ContextUtil.getProperty(b,"mouse_offset",2));k.style.left=q-m<o?q-o+"px":m+s+"px",k.style.top=r-n<p?r-p+"px":n+s+"px";var t=ContextUtil.getSizes(k);q-m<t.width?k.classList.add("cm_border_right"):k.classList.remove("cm_border_right"),r-n<t.height?k.classList.add("cm_border_bottom"):k.classList.remove("cm_border_bottom"),k.classList.add("display"),ContextUtil.getProperty(b,"close_on_click",!0)&&window.addEventListener("click",d),h.preventDefault()},this.hide=function(){document.getElementById("cm_"+g).classList.remove("display"),window.removeEventListener("click",d)},this.reload()}ContextMenu.count=0,ContextMenu.DIVIDER="cm_divider";const ContextUtil={getProperty:function(a,b,c){return"undefined"==typeof a[b]?c:a[b]},getSizes:function(a){for(var g,b=a.getElementsByTagName("li"),c=0,d=0,f=0;f<b.length;f++)g=b[f],g.offsetWidth>c&&(c=g.offsetWidth),g.offsetHeight>d&&(d=g.offsetHeight);for(var h=c,j=d,f=0;f<b.length;f++){var g=b[f],k=g.getElementsByTagName("ul");if("undefined"!=typeof k[0]){var l=ContextUtil.getSizes(k[0]);c+l.width>h&&(h=c+l.width),d+l.height>j&&(j=d+l.height)}}return{width:h,height:j}}};