You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(function(){"use strict";//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// H E L P E R F U N C T I O N S///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////** * Function to check if we clicked inside an element with a particular class * name. * * @param {Object} e The event * @param {String} className The class name to check against * @return {Boolean} */functionclickInsideElement(e,className){varel=e.srcElement||e.target;if(el.classList.contains(className)){returnel;}else{while(el=el.parentNode){if(el.classList&&el.classList.contains(className)){returnel;}}}returnfalse;}/** * Get's exact position of event. * * @param {Object} e The event passed in * @return {Object} Returns the x and y position */functiongetPosition(e){varposx=0;varposy=0;if(!e)vare=window.event;if(e.pageX||e.pageY){posx=e.pageX;posy=e.pageY;}elseif(e.clientX||e.clientY){posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;}return{x: posx,y: posy}}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// C O R E F U N C T I O N S///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////** * Variables. */varcontextMenuClassName="context-menu";varcontextMenuItemClassName="context-menu__item";varcontextMenuLinkClassName="context-menu__link";varcontextMenuActive="context-menu--active";vartaskItemClassName="task";vartaskItemInContext;varclickCoords;varclickCoordsX;varclickCoordsY;varmenu=document.querySelector("#context-menu");varmenuItems=menu.querySelectorAll(".context-menu__item");varmenuState=0;varmenuWidth;varmenuHeight;varmenuPosition;varmenuPositionX;varmenuPositionY;varwindowWidth;varwindowHeight;/** * Initialise our application's code. */functioninit(){contextListener();clickListener();keyupListener();resizeListener();}/** * Listens for contextmenu events. */functioncontextListener(){document.addEventListener("contextmenu",function(e){taskItemInContext=clickInsideElement(e,taskItemClassName);if(taskItemInContext){e.preventDefault();toggleMenuOn();positionMenu(e);}else{taskItemInContext=null;toggleMenuOff();}});}/** * Listens for click events. */functionclickListener(){document.addEventListener("click",function(e){varclickeElIsLink=clickInsideElement(e,contextMenuLinkClassName);if(clickeElIsLink){e.preventDefault();menuItemListener(clickeElIsLink);}else{varbutton=e.which||e.button;if(button===1){toggleMenuOff();}}});}/** * Listens for keyup events. */functionkeyupListener(){window.onkeyup=function(e){if(e.keyCode===27){toggleMenuOff();}}}/** * Window resize event listener */functionresizeListener(){window.onresize=function(e){toggleMenuOff();};}/** * Turns the custom context menu on. */functiontoggleMenuOn(){if(menuState!==1){menuState=1;menu.classList.add(contextMenuActive);}}/** * Turns the custom context menu off. */functiontoggleMenuOff(){if(menuState!==0){menuState=0;menu.classList.remove(contextMenuActive);}}/** * Positions the menu properly. * * @param {Object} e The event */functionpositionMenu(e){clickCoords=getPosition(e);clickCoordsX=clickCoords.x;clickCoordsY=clickCoords.y;menuWidth=menu.offsetWidth+4;menuHeight=menu.offsetHeight+4;windowWidth=window.innerWidth;windowHeight=window.innerHeight;if((windowWidth-clickCoordsX)<menuWidth){menu.style.left=windowWidth-menuWidth+"px";}else{menu.style.left=clickCoordsX+"px";}if((windowHeight-clickCoordsY)<menuHeight){menu.style.top=windowHeight-menuHeight+"px";}else{menu.style.top=clickCoordsY+"px";}}/** * Dummy action function that logs an action when a menu item link is clicked * * @param {HTMLElement} link The link that was clicked */functionmenuItemListener(link){console.log("Task ID - "+taskItemInContext.getAttribute("data-id")+", Task action - "+link.getAttribute("data-action"));toggleMenuOff();}/** * Run the app. */init();})();
The text was updated successfully, but these errors were encountered:
js & right click menu
https://stackoverflow.com/questions/4909167/how-to-add-a-custom-right-click-menu-to-a-webpage
// todo
tutorials
https://www.sitepoint.com/building-custom-right-click-context-menu-javascript/
https://codepen.io/SitePoint/pen/MYLoWY
https://codepen.io/webgeeker/pen/bOQvaN
https://github.com/callmenick/Custom-Context-Menu/blob/master/main.js
The text was updated successfully, but these errors were encountered: