Link to home
Start Free TrialLog in
Avatar of Cyber-Drugs
Cyber-DrugsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Dynamic Context Menu

Hi guys,

I'm looking to create a Dynamic Context menu, but Dynamic I mean I can have loads of arrays, eg:

myArrayA[0].Title='File';
myArrayA[0].Func='runFunctionA();';
myArrayA[1].Title='Save';
myArrayA[1].Func='runFunctionB();';
myArrayA[2].Title='Load';
myArrayA[2].Func='runFunctionC();';
myArrayA[3].Title='Exit';
myArrayA[3].Func='return false;';

myArrayB[0].Title='Copy';
myArrayB[0].Func='window.clipboardData.setData("Text", trgObj.value);';
myArrayB[1].Title='Paste';
myArrayB[1].Func='trgObj.value = window.clipboardData.getData("Text")';


And I can set different elements of the screen with different menus, eg:


document.getElementById('tagA').oncontextmenu = displayMenu('myArrayA');
document.getElementById('tagB').oncontextmenu = displayMenu('myArrayB');


Any ideas on how I can get this to work guys?

Cheers!
Avatar of bruno
bruno
Flag of United States of America image

whenever i've done a context menu, i've had a div in the code that i show when the user clicks.  you could write your div out dynamically use JS if you want, with those items.
Avatar of Cyber-Drugs

ASKER

Hi brunobear,

I have some code which does that already, but unfortunately, I don't see how I could make it work with more than one group of div's. I'm going to post the code on here, and if you can see how to alter it so I can change it to be dynamic to do something like this:

document.getElementById('tagA').oncontextmenu = displayMenu('myDivA');
document.getElementById('tagB').oncontextmenu = displayMenu('myDivB');


Cheers!


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ContextMenu </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
<!--

.skin0{
     position:absolute;
     width:140px;
     border:1pt solid #808080;
     background-color:#cccccc;
     font-family:Verdana;
     line-height:20px;
     cursor:default;
     font-size:14px;
     z-index:100;
     visibility:hidden;
}

.menuitems{
     padding-left:10px;
     padding-right:10px;
     margin:2px;
     font-family:Arial;
     font-size:8pt;
     border:1pt solid #cccccc;
}
.hritems{
     padding: -10px;
     margin : 0px;
}
-->
</style>
</head>
<body>
<div id="ie5menu" class="skin0" onMouseover="highlightie5(event)" onMouseout="lowlightie5(event)" onClick="jumptoie5(event)" display:none>
     <div class="menuitems" url="http://dynamicdrive.com" onclick="CopyToClipBoard();">Copy</div>
     <div class="menuitems" url="http://dynamicdrive.com" onclick="PasteFromClipBoard();">Paste</div>
</div>

<script language="JavaScript1.2">
function GetWindowSize()
{
     this.myWidth = 0;
     this.myHeight = 0;
     if( typeof( window.innerWidth ) == 'number' ) {
          //Non-IE
          this.myWidth = window.innerWidth;
          this.myHeight = window.innerHeight;
     }
     else if( document.documentElement &&
            (document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
          //IE 6+ in 'standards compliant mode'
          this.myWidth = document.documentElement.clientWidth;
          this.myHeight = document.documentElement.clientHeight;
     }
     else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
          //IE 4 compatible
          this.myWidth = document.body.clientWidth;
          this.myHeight = document.body.clientHeight;
     }
}
function test() {
     alert('this is a test');
}

//set this variable to 1 if you wish the URLs of the highlighted menu to be displayed in the status bar
var display_url=0

var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all
if (ie5||ns6)
var menuobj=document.getElementById("ie5menu")

function showmenuie5(e){
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? document.body.clientWidth-event.clientX : window.innerWidth-e.clientX
var bottomedge=ie5? document.body.clientHeight-event.clientY : window.innerHeight-e.clientY

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? document.body.scrollLeft+event.clientX-menuobj.offsetWidth : window.pageXOffset+e.clientX-menuobj.offsetWidth
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? document.body.scrollLeft+event.clientX : window.pageXOffset+e.clientX

//same concept with the vertical position
if (bottomedge<menuobj.offsetHeight)
menuobj.style.top=ie5? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY;
else
menuobj.style.top=ie5? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY;

menuobj.style.visibility="visible"
var posY = parseInt (menuobj.style.top) + menuobj.offsetHeight;
var wsize = new GetWindowSize();
window.status = posY + ' ' + wsize.myHeight + ' ' + menuobj.offsetHeight;  

if (posY > wsize.myHeight) {
   posY =  parseInt(wsize.myHeight) - menuobj.offsetHeight - 20;
   window.status = posY ;  
   menuobj.style.top = posY;
}
return false
}

var trgObj = null;
var srcObj = null;
function getTarget(evt) {
  if (!evt) { evt = window.event; }
  if (document.all) { return (evt.srcElement); }
  return (evt.target);
}

function CopyToClipBoard() {
      clipboard = trgObj.value;
      window.clipboardData.setData("Text", clipboard);
}
function PasteFromClipBoard() {
      clipboard = window.clipboardData.getData("Text");
      if (!clipboard || !trgObj.value) { return; }
      trgObj.value = clipboard;
}

function hidemenuie5(e){
menuobj.style.visibility="hidden"

trgObj = getTarget (e);
if (trgObj.tagName != 'INPUT' && trgObj.type != 'text') { trgObj = null; return; }
if (trgObj) { window.status = 'Target is ' + trgObj.tagName; }
}

function highlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode //up one node
firingobj.style.backgroundColor="#CFD6E8"
firingobj.style.border="1pt solid #737B92"
firingobj.style.color="black"
if (display_url==1)
window.status=event.srcElement.url
}
}

function lowlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode //up one node
firingobj.style.backgroundColor="#cccccc"
firingobj.style.border="1pt solid #cccccc"
firingobj.style.color="black"
window.status=''
}
}

function jumptoie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode
if (firingobj.getAttribute("target"))
window.open(firingobj.getAttribute("url"),firingobj.getAttribute("target"))
}
}

if (ie5||ns6){
menuobj.style.display=''
document.oncontextmenu=showmenuie5
document.onclick=hidemenuie5
}

</script>

<input type="text" name="txt1" value="Text Value1">
<input type="text" name="txt2">

</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of bruno
bruno
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am more than happy to re-do all the positioning, but that seems to be exactly what I am after, cheers!
glad i could help, thanks for the A.  :-)