home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / w9x / visdhtml.exe / visual / components / menubar / menu.js < prev    next >
Encoding:
JavaScript  |  1998-05-19  |  9.9 KB  |  297 lines

  1. /**
  2.  * Menu Class 0.3
  3.  * by gary smith July 1997
  4.  * Copyright (c) 1997-98 Netscape Communications Corp.
  5.  *
  6.  * Netscape grants you a royalty free license to use or modify this
  7.  * software provided that this copyright notice appears on all copies.
  8.  * This software is provided "AS IS," without a warranty of any kind.
  9.  *
  10.  * see also menubar.js, which can be used with or w/o menu.js
  11.  */
  12.  
  13. function Menu(label) {
  14.     this.version = "0.3 [menu.js 980210]";
  15.     this.label = label;
  16.     this.menuBgColor = '#C1CDCD';
  17.     this.separators = new Array();
  18.     this.subMenus = new Array();
  19.     this.subMenuIcons = new Array();
  20.     this.countItems = 0;
  21.     this.items = new Array();
  22.  
  23.     this.menuLayer = makeLayer(label, "", null, 5, 5, 0,-1, '#000000', "hide");
  24.     this.menuLayer.label = this.label;
  25.     this.menuLayer.edge   = makeLayer('m', "", this.menuLayer, this.menuLayer.clip.width -1, this.menuLayer.clip.height -1, -1,-1, '#DDDDDD');
  26.     this.menuLayer.dark   = makeLayer('m', "", this.menuLayer, this.menuLayer.clip.width -1, this.menuLayer.clip.height -1, 1,1, '#888888');
  27.     this.menuLayer.lite   = makeLayer('m', "", this.menuLayer, this.menuLayer.clip.width -2, this.menuLayer.clip.height -2, 1,1, '#FFFFFF');
  28.     this.menuLayer.main   = makeLayer('m', "", this.menuLayer, this.menuLayer.clip.width -3, this.menuLayer.clip.height -3, 2,2, this.menuBgColor);
  29.     this.menuLayer.hilite = makeLayer('m', "", this.menuLayer, 0,0, 2,2);
  30.     this.menuLayer.focus  = makeLayer('m', "", this.menuLayer, 0,0, 2,2, null, "hide");
  31.     this.menuLayer.focus.captureEvents(Event.MOUSEDOWN);
  32.     this.menuLayer.focus.onmousedown = onMenuItem;
  33.     this.menuLayer.focus.onMouseOut = onMenuItem_MouseOut;
  34.     this.menuLayer.main.saveBgColor = this.menuBgColor;
  35.     this.menuLayer.that = this;
  36.  
  37.     this.addMenuItem = addMenuItem;
  38.     this.addSeparator = addSeparator;
  39.     this.updateMenu = updateMenu;
  40.     this.setMenuBgColor = setMenuBgColor;
  41.     this.menuLayeroveMenu = moveMenu;
  42.     this.onMenuItem = onMenuItem;
  43.     this.onMenuItem_MouseOver = onMenuItem_MouseOver;
  44.     this.nsMenuObject = nsMenuObject;
  45.     this.showMenu = showMenu;
  46.     this.hideMenu = hideMenu;
  47.     this.hideMenus = hideMenus;
  48.     this.hideMenuItems = hideMenuItems;
  49.     if (!window.NActiveMenus) {
  50.         window.NActiveMenus = new Array();
  51.     }
  52. }
  53.  
  54. function addMenuItem(label, action) {
  55.     var trueLabel = label;
  56.     var label_focus = label;
  57.     if (label.menuLayer) {
  58.         label = trueLabel.menuLayer.label;
  59.         label_focus = label;
  60.         var subMenu = true;
  61.     }
  62.     if (label.indexOf("<DIV ID") == -1 || label != "<HR>") {
  63.        label = '<NOBR><DIV ID=sysFont>    '+ label +'    </DIV></NOBR>';
  64.        label_focus = '<NOBR><DIV ID=sysFont><FONT COLOR="white">    '+ label_focus +'    </FONT></DIV></NOBR>';
  65.     }
  66.     var l = makeLayer('m', label, this.menuLayer, 0,0, 2,2);
  67.     l.focus = makeLayer('m', label_focus, this.menuLayer, 0,0, 1,1);
  68.     l.label = label;
  69.     l.onMouseOver = onMenuItem_MouseOver;
  70.     l.action = action;
  71.     l.focus.visibility = "hide";
  72.     if (this.items[this.countItems -1]) {
  73.         l.top = this.items[this.countItems -1].top + this.items[this.countItems -1].clip.height +5;
  74.     }
  75.     if (l.clip.width > this.menuLayer.clip.width) {
  76.         this.menuLayer.clip.width = l.clip.width;
  77.     }
  78.     if (subMenu) {
  79.         l.subMenuIcon = makeLayer('m', '<IMG SRC="images/gs.gif">', this.menuLayer, 0,0, 1,1);
  80.         l.subMenuIcon.clip.left += 3;
  81.         l.subMenuIcon.top = l.top -2;
  82.         l.subMenuIcon.subMenu = trueLabel.menuLayer;
  83.         l.subMenu = trueLabel.menuLayer;
  84.         l.subMenu.parentMenu = this.menuLayer;
  85.         this.subMenus[this.subMenus.length] = trueLabel;
  86.         this.subMenuIcons[this.subMenuIcons.length] = l.subMenuIcon;
  87.         this.menuLayer.clip.width += 10;
  88.     }
  89.     l.clip.width -= 4;
  90.     l.focus.top = l.top;
  91.     this.menuLayer.clip.height = l.top + l.clip.height +5;
  92.     this.menuLayer.edge.clip.height = this.menuLayer.clip.height;
  93.     this.menuLayer.edge.clip.width = this.menuLayer.clip.width;
  94.     this.menuLayer.dark.clip.height = this.menuLayer.clip.height -2;
  95.     this.menuLayer.dark.clip.width = this.menuLayer.clip.width -2;
  96.     this.menuLayer.lite.clip.height = this.menuLayer.clip.height -3;
  97.     this.menuLayer.lite.clip.width = this.menuLayer.clip.width -3;
  98.     this.menuLayer.main.clip.height = this.menuLayer.clip.height -4;
  99.     this.menuLayer.main.clip.width = this.menuLayer.clip.width -4;
  100.     this.menuLayer.hilite.clip.height = l.clip.height;
  101.     this.menuLayer.hilite.clip.width = this.menuLayer.clip.width -4;
  102.     this.menuLayer.focus.clip.height = l.clip.height;
  103.     this.menuLayer.focus.clip.width = this.menuLayer.clip.width -4;
  104.     this.menuLayer.focus.zIndex = l.focus.zIndex +1;
  105.     this.items[trueLabel] = l;
  106.     this.items[this.countItems] = l;
  107.     this.countItems++;
  108.     this.updateMenu();
  109. }
  110.  
  111. function updateMenu() {
  112.     for (var i in this.items) {
  113.         this.items[i].clip.width = this.menuLayer.clip.width -6;
  114.     }
  115.     for (var i in this.separators) {
  116.         this.separators[i].clip.width = this.menuLayer.clip.width -6;
  117.         this.separators[i].dark.clip.width = this.menuLayer.clip.width -6;
  118.     }
  119.     for (var i in this.subMenuIcons) {
  120.         this.subMenuIcons[i].left = this.menuLayer.clip.width -15;
  121.     }
  122. }
  123.  
  124.  
  125. function addSeparator(label) {
  126.     var l = makeLayer('m', "", this.menuLayer, 2,2, 3,2, "#FFFFFF");
  127.     l.focus = makeLayer('m', "", this.menuLayer, 2,2, 3,2, "#FFFFFF", "hide");
  128.     if (this.items[this.countItems -1])
  129.         l.top = this.items[this.countItems -1].top + this.items[this.countItems -1].clip.height +5;
  130.     if (l.clip.width > this.menuLayer.clip.width)
  131.         this.menuLayer.clip.width = l.clip.width;
  132.     l.dark   = makeLayer('m', "", l, l.clip.width, 1, 0,0, '#999999');
  133.     l.clip.width -= 4;
  134.     this.items[label || "separator" + this.countItems] = l;
  135.     this.separators[this.separators.length] = l;
  136.     this.items[this.countItems] = l;
  137.     this.countItems++;
  138. }
  139.  
  140. function onMenuItem(e, l, p) {
  141.     if (!l) {
  142.         l = e.target.parentLayer || this.parentLayer;
  143.     }
  144.     if (window.NActiveMenuBarItem) {
  145.         window.NActiveMenuBarItem.focus.visibility = "hide";
  146.     }
  147.     l.visibility = "hide";
  148.     l.that.hideMenuItems(l.that,true);
  149.     if (window.NActiveMenuItem) {
  150.         eval( window.NActiveMenuItem.action );
  151.     }
  152.     window.NActiveMenuItem = null;
  153.     window.NMenuHide = true;
  154. }
  155.  
  156. function onMenuItem_MouseOver(e, l) {
  157.     if (!l) {
  158.         var l = e.target;
  159.     }
  160.     if (!l) return;
  161.     l.parentLayer.activeMenuItem = l;
  162.     l.parentLayer.that.hideMenuItems(l.parentLayer.that);
  163.     l.focus.visibility = "inherit";
  164.     l.parentLayer.hilite.bgColor = "#000084";
  165.     l.parentLayer.hilite.top = l.top;
  166.     l.parentLayer.focus.top = l.top;
  167.     l.parentLayer.focus.visibility = "inherit";
  168.     l.parentLayer.action = l;
  169.     window.NActiveMenuItem = l;
  170.     if (l.subMenu) {
  171.         l.subMenu.zIndex = l.parentLayer.zIndex +1;
  172.         l.subMenu.top = l.top + l.parentLayer.top;
  173.         l.subMenu.left = l.parentLayer.left + l.parentLayer.clip.width -3;
  174.         if (l.subMenu.left + l.subMenu.clip.width > window.innerWidth) {
  175.             l.subMenu.left = l.parentLayer.left - l.subMenu.clip.width +15;
  176.         }
  177.         l.subMenu.visibility = "inherit";
  178.         window.NActiveMenus[window.NActiveMenus.length] = l.subMenu;
  179.     }
  180. }
  181.  
  182. function onMenuItem_MouseOut(e,l) {
  183.     l = l || this;
  184.     l.parentLayer.hilite.bgColor = null;
  185.     l.parentLayer.hilite.top = l.top;
  186.     l.parentLayer.focus.top = l.top;
  187.     l.visibility = "hide";
  188.     if (window.NActiveMenuItem) {
  189.         window.NActiveMenuItem.focus.visibility = "hide";
  190.     }
  191. }
  192.  
  193. function hideMenuItems(menu, parents) {
  194.     if (!menu && window.NActiveMenus) {
  195.         for (var i=0; i < window.NActiveMenus.length; i++) {
  196.             window.NActiveMenus[i].visibility = "hide";
  197.         }
  198.     } else {
  199.         for (var i=0; i < menu.countItems; i++) {
  200.             menu.items[i].focus.visibility = "hide";
  201.         } 
  202.         for (var i=0; i < menu.subMenuIcons.length; i++) {
  203.             menu.subMenuIcons[i].subMenu.visibility = "hide";
  204.             if (menu.subMenuIcons[i].subMenu.that.subMenuIcons.length) {
  205.                 menu.subMenuIcons[i].subMenu.that.hideMenuItems(menu.subMenuIcons[i].subMenu.that);
  206.             }
  207.         }
  208.         if (parents && menu.menuLayer.parentMenu) {
  209.             menu.menuLayer.parentMenu.visibility = "hide";
  210.             menu.menuLayer.parentMenu.that.hideMenuItems(menu.menuLayer.parentMenu.that,true);
  211.         }
  212.     }
  213. }
  214.  
  215. function hideMenus() {
  216.     for(var i=0; i < window.NActiveMenus.length; i++) {
  217.         window.NActiveMenus[i].visibility = "hide";
  218.     }
  219.     window.NActiveMenu = null;
  220.     window.NActiveMenus.length = 0;
  221. }
  222.  
  223. function moveMenu(x,y) {
  224.     this.menuLayer.left = x;
  225.     this.menuLayer.top = y;
  226. }
  227.  
  228. function setMenuBgColor(str) {
  229.     this.menuLayer.main.document.bgColor = str;
  230.     this.menuLayer.main.saveBgColor = str;
  231.     this.menuBgColor = str;
  232. }
  233.  
  234. function nsMenuObject() {
  235.     if (!window.nsMenuObjects) {
  236.         window.nsMenuObjects = new Array();
  237.         window.nsMenuObjects[0] = this;
  238.     } else {
  239.         window.nsMenuObjects[window.nsMenuObjects.length++] = this;
  240.     }
  241. }
  242.  
  243. function hideMenu(menu) {
  244.     if (menu.menuLayer) {
  245.         menu.menuLayer.visibility = "hide";
  246.     } else {
  247.         window.NMenuHide = true; 
  248.         if (window.NActiveMenu) {
  249.             window.NActiveMenu.visibility = "hide";
  250.             window.NActiveMenu.that.hideMenuItems(window.NActiveMenu.that, true);
  251.         }
  252.         if (window.NActiveMenuBarItem) {
  253.             window.NActiveMenuBarItem.focus.visibility = "hide";
  254.         }
  255.     }
  256. }
  257.  
  258. function showMenu(menu) {
  259.     if (menu.menuLayer) {
  260.         menu.menuLayer.visibility = "inherit";
  261.     } else {
  262.         window.NMenuHide = false; 
  263.         if (window.NActiveMenu) 
  264.             window.NActiveMenu.visibility = "inherit";
  265.         if (window.NActiveMenuBarItem)
  266.             window.NActiveMenuBarItem.focus.visibility = "inherit";
  267.     }
  268. }
  269.  
  270. function makeLayer(label, content, parentLyr, width, height, x, y, color, viso) {
  271.     content = content || " ";
  272.     if (parentLyr) {
  273.         var l = new Layer(width || 0, parentLyr);
  274.     } else {
  275.         var l = new Layer(width || 0);
  276.     }
  277.     l.document.ids.font14.fontSize = 14;
  278.     l.document.ids.sysFont.fontSize = 12;
  279.     l.document.ids.sysFont.fontFamily = "Arial, Espy, sans-serif";
  280.     l.document.open("text/html");
  281.     l.document.writeln(content);
  282.     l.document.close();
  283.     l.label = label;
  284.     if (x)
  285.         l.left += x;
  286.     if (y)
  287.         l.top += y;
  288.     if (color)
  289.         l.bgColor = color;
  290.     if (width)
  291.         l.clip.width = width;
  292.     if (height)
  293.         l.clip.height = height;
  294.     if (!viso)
  295.         l.visibility = "inherit";
  296.     return l;
  297. }