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

  1. /**
  2.  * MenuBar 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 menu.js, which can be used with or w/o menubar.js
  11.  */
  12.  
  13. function MenuBar(width, height) {
  14.     this.version = "0.3 [menubar.js 980210]";
  15.     this.menuBarWidth = width;
  16.     this.menuBarHeight = height;
  17.     this.menuBarBgColor = '#C1CDCD'; //CDB79E
  18.     this.menuBarParentLayer = null;
  19.  
  20.     this.setMenuBar = setMenuBar;
  21.     this.addMenuBarItem = addMenuBarItem;
  22.     this.moveMenuBarItem = moveMenuBarItem;
  23.     this.moveMenuBar = moveMenuBar;
  24.     this.onMenuBar = onMenuBar;
  25.     this.setMenuBarWidth = setMenuBarWidth;
  26.     this.setMenuBarHeight = setMenuBarHeight;
  27.     this.setMenuBarBgColor = setMenuBarBgColor;
  28.     this.resizeMenuBar = resizeMenuBar;
  29.     this.hideMenuBarItems = hideMenuBarItems;
  30. }
  31.  
  32. function setMenuBar() {
  33.     if (this.menuBarParentLayer)
  34.         this.bar = new Layer(0);
  35.     else 
  36.         this.bar = new Layer(0);
  37.     this.bar.document.bgColor = "#000000";
  38.     this.bar.document.writeln(' ');
  39.     this.bar.document.close();
  40.     this.bar.clip.width = this.menuBarWidth || window.innerWidth || 100;
  41.     this.bar.clip.height = this.menuBarHeight || 20;
  42.     this.barWidth = this.bar.clip.width;
  43.     this.bar.clip.height += 3;
  44.     this.barHeight = this.bar.clip.height;
  45.     this.bar.visibility = 'inherit';
  46.     this.bar.shadow = makeLayer('m', ' ', this.bar, this.barWidth -1, this.barHeight -1, 0,0, '#888888');
  47.     this.bar.lite   = makeLayer('m', ' ', this.bar, this.barWidth -2, this.barHeight -2, 0,0, '#EEEEEE');
  48.     this.bar.gray   = makeLayer('m', ' ', this.bar, this.barWidth -3, this.barHeight -3, 1,1, this.menuBarBgColor);
  49.     this.bar.focus  = makeLayer('m', "", this.bar, 0,0, 2,2);
  50.     this.bar.gray.saveBgColor = this.menuBarBgColor;
  51.     this.bar.focus.captureEvents(Event.MOUSEUP);
  52.      this.bar.focus.onmouseup = this.onMenuBar;
  53.     this.bar.focus.ID = "focus";
  54.     this.bar.focus.that = this;
  55.     this.countItems = 0;
  56.     this.items = new Array();
  57.     this.activeMenuBarItems = new Array();
  58.     if (!window.NMenuHide) {
  59.         window.NMenuHide = true;
  60.     }
  61. }
  62.  
  63. function addMenuBarItem(item, action) {
  64.     if (typeof(item) != 'string') {
  65.         var label = item.menuLayer.label;
  66.         this.items[label] = item;
  67.     } else {
  68.         var label = item;
  69.         item = new Layer(0);
  70.         this.items[label] = item;
  71.     }
  72.     if (label.indexOf("IMG") == -1) {
  73.         var m = label.substring(0,1);
  74.         label = label.substring(1);
  75.         label = '<SPAN ID=sysFont> '+ '<U>' + m + '</U>' + label +' </SPAN>';
  76.     }
  77.     item.labelLayer = makeLayer('label', label, this.bar, 0,0, 1,1, null, "hide");
  78.     item.labelLayer.focus = makeLayer('label', '<FONT COLOR="#FFFFFF">'+ label +'</FONT>', this.bar, 0,0, 1,1, '#000084');
  79.     item.labelLayer.bar = this.bar;    
  80.     item.labelLayer.focus.item = item;    
  81.     if (this.items[this.countItems -1])
  82.         item.labelLayer.left = this.items[this.countItems -1].left + this.items[this.countItems -1].clip.width +5;
  83.     item.labelLayer.focus.left = item.labelLayer.left;
  84.     item.labelLayer.visibility = 'inherit';
  85.     item.labelLayer.focus.visibility = 'hide';
  86.     item.labelLayer.onMouseOver = onMenuBarItem_MouseOver;
  87.     item.labelLayer.onFocus = this.onMenuBar;
  88.     item.labelLayer.focus.captureEvents(Event.MOUSEUP);
  89.      item.labelLayer.focus.onmouseup = this.onMenuBar;
  90.     item.labelLayer.focus.action = action;
  91.     item.labelLayer.that = this;
  92.     item.labelLayer.menuLayer = item.menuLayer;
  93.     this.bar.focus.zIndex = item.labelLayer.focus.zIndex +1;
  94.     this.items[this.countItems] = item.labelLayer;
  95.     this.countItems++;
  96. }
  97.  
  98. function onMenuBarItem_MouseOver(e) {
  99.     if (window.hideMenus) {
  100.         window.hideMenus();
  101.     }
  102.     this.parentLayer.focus.left = this.left;
  103.     this.parentLayer.focus.clip.width = this.clip.width;
  104.     this.parentLayer.focus.clip.height = this.clip.height;
  105.     window.NActiveMenuBarItem = this;
  106.     if (window.NMenuHide) {
  107.         return;
  108.     }
  109.     this.focus.visibility = "inherit";
  110.     onMenuBar(null,this);
  111. }
  112.  
  113. function onMenuBar(e,l) {
  114.     if (window.hideMenus) {
  115.         window.hideMenus();
  116.     }
  117.     if (!l) {
  118.         if (window.NActiveMenuBarItem) {
  119.             l = window.NActiveMenuBarItem;
  120.         } else {
  121.             l = this;
  122.         }
  123.         if (window.NMenuHide) {
  124.             window.NMenuHide = false;
  125.         }
  126.         if (l.action) {
  127.             eval( l.action );
  128.         }
  129.     }
  130.     if (l.that) {
  131.         if (window.NActiveMenuBarItem) {
  132.             l = window.NActiveMenuBarItem;
  133.         } else if (l.that.hideMenuBarItems) {
  134.             l.that.hideMenuBarItems();
  135.         }
  136.     }
  137.     if (l.menuLayer) {
  138.         var m = l.menuLayer;
  139.         m.left = l.left + l.bar.left;
  140.         m.top = l.top + l.clip.height + l.bar.top;
  141.         if (l.that.previousLayer) {
  142.             var p = l.that.previousLayer;
  143.             if (p != l) {
  144.                 p.menuLayer.visibility = "hide";
  145.                 p.focus.visibility = "hide";
  146.                 m.visibility = "inherit";
  147.                 l.focus.visibility = "inherit";
  148.                 window.NMenuHide = false;
  149.             } else if (m.visibility == "hide") {
  150.                 m.visibility = "inherit";
  151.                 l.focus.visibility = "inherit";
  152.             } else {
  153.                 m.visibility = "hide";
  154.                 p.focus.visibility = "hide";
  155.                 window.NMenuHide = true;
  156.             }
  157.         } else {
  158.             m.visibility = "inherit";
  159.             l.focus.visibility = "inherit";
  160.         }
  161.         l.that.previousLayer = l;
  162.         window.NActiveMenu = l.menuLayer;
  163.  
  164.     }
  165. }
  166.  
  167. function hideMenuBarItems(menu) {
  168.     menu = menu || this;
  169.     for(var i=0; i < menu.activeMenuBarItems.length; i++) {
  170.         menu.hideMenuBarItems[i].visibility = "hide";
  171.     }
  172.     menu.activeMenuBarItems.length = 0;
  173.     if (window.NActiveMenuBarItem) {
  174.         window.NActiveMenuBarItem.focus.visibility = "hide";
  175.         window.NActiveMenuBarItem = null;
  176.     }
  177. }
  178.  
  179. function moveMenuBarItem(item, x, y) {
  180.     if (typeof(item) == 'string') {
  181.         item = this.items[item];
  182.     }
  183.     if (x) {
  184.         item.labelLayer.left = x;
  185.         item.labelLayer.focus.left = x;
  186.     }
  187.     if (y) {
  188.        item.labelLayer.top = y;
  189.        item.labelLayer.focus.top = y;
  190.     }
  191. }
  192.  
  193. function moveMenuBar(x,y) {
  194.     if (x) this.bar.left = x;
  195.     if (y) this.bar.top = y;
  196. }
  197.  
  198. function resizeMenuBar(w) {
  199.     this.bar.clip.width = w;
  200.     this.bar.shadow.clip.width = w -1;
  201.     this.bar.lite.clip.width = w -2;
  202.     this.bar.gray.clip.width = w -3;
  203. }
  204.  
  205. function setMenuBarWidth(i) {
  206.     this.menuBarWidth = i;
  207. }
  208.  
  209. function setMenuBarHeight(i) {
  210.     this.menuBarHeight = i;
  211. }
  212.  
  213. function setMenuBarBgColor(str) {
  214.     this.menuBarBgColor = str;
  215. }
  216.  
  217. function setMenuBarParentLayer(l) {
  218.     this.menuBarParentLayer = l;
  219. }
  220.  
  221. function makeLayer(label, content, parentLyr, width, height, x, y, color, viso) {
  222.     content = content || " ";
  223.     if (parentLyr) {
  224.         var l = new Layer(width || 0, parentLyr);
  225.     } else {
  226.         var l = new Layer(width || 0);
  227.     }
  228.     l.document.ids.font14.fontSize = 14;
  229.     l.document.ids.sysFont.fontSize = 12;
  230.     l.document.ids.sysFont.fontFamily = "Arial, Espy, sans-serif";
  231.     l.document.open("text/html");
  232.     l.document.writeln(content);
  233.     l.document.close();
  234.     l.label = label;
  235.     if (x)
  236.         l.left += x;
  237.     if (y)
  238.         l.top += y;
  239.     if (color)
  240.         l.bgColor = color;
  241.     if (width)
  242.         l.clip.width = width;
  243.     if (height)
  244.         l.clip.height = height;
  245.     if (!viso)
  246.         l.visibility = "inherit";
  247.     return l;
  248. }