home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2002 August / INTERNET94.ISO / pc / software / windows / mail / ft_gate_office / jscript / dhtmllib.js next >
Encoding:
JavaScript  |  2001-02-16  |  11.1 KB  |  432 lines

  1. /* dhtmllib.js Copyright 1999 by Mike Hall. Web address: http://www.brainjar.com
  2. Last update: February 26, 2000.
  3. Provides basic functions for DHTML positioned elements which will work on 
  4. both Netscape Communicator and Internet Explorer browsers (version 4.0 and up).*/
  5. // Determine browser.
  6. var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 &&
  7.                 parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
  8. var isMinIE4 = (document.all) ? 1 : 0;
  9. var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.") >= 0) ? 1 : 0;
  10. //-----------------------------------------------------------------------------
  11. // Layer visibility.
  12. //-----------------------------------------------------------------------------
  13. function hideLayer(layer) {
  14.   if (isMinNS4)
  15.     layer.visibility = "hide";
  16.   if (isMinIE4)
  17.     layer.style.visibility = "hidden";
  18. }
  19. function showLayer(layer) {
  20.   if (isMinNS4)
  21.     layer.visibility = "show";
  22.   if (isMinIE4)
  23.     layer.style.visibility = "visible";
  24. }
  25. function inheritLayer(layer) {
  26.   if (isMinNS4)
  27.     layer.visibility = "inherit";
  28.   if (isMinIE4)
  29.     layer.style.visibility = "inherit";
  30. }
  31. function getVisibility(layer) {
  32.   if (isMinNS4) {
  33.     if (layer.visibility == "show")
  34.       return "visible";
  35.     if (layer.visibility == "hide")
  36.       return "hidden";
  37.     return layer.visibility;
  38.   }
  39.   if (isMinIE4)
  40.     return layer.style.visibility;
  41.   return "";
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Layer positioning.
  45. //-----------------------------------------------------------------------------
  46. function moveLayerTo(layer, x, y) {
  47.   if (isMinNS4)
  48.     layer.moveTo(x, y);
  49.   if (isMinIE4) {
  50.     layer.style.left = x;
  51.     layer.style.top  = y;
  52.   }
  53. }
  54. function moveLayerBy(layer, dx, dy) {
  55.   if (isMinNS4)
  56.     layer.moveBy(dx, dy);
  57.   if (isMinIE4) {
  58.     layer.style.pixelLeft += dx;
  59.     layer.style.pixelTop  += dy;
  60.   }
  61. }
  62. function getLeft(layer) {
  63.   if (isMinNS4)
  64.     return layer.left;
  65.   if (isMinIE4)
  66.     return layer.style.pixelLeft;
  67.   return -1;
  68. }
  69. function getTop(layer) {
  70.   if (isMinNS4)
  71.     return layer.top;
  72.   if (isMinIE4)
  73.     return layer.style.pixelTop;
  74.   return -1;
  75. }
  76. function getRight(layer) {
  77.   if (isMinNS4)
  78.     return layer.left + getWidth(layer);
  79.   if (isMinIE4)
  80.     return layer.style.pixelLeft + getWidth(layer);
  81.   return -1;
  82. }
  83. function getBottom(layer) {
  84.   if (isMinNS4)
  85.     return layer.top + getHeight(layer);
  86.   if (isMinIE4)
  87.     return layer.style.pixelTop + getHeight(layer);
  88.   return -1;
  89. }
  90. function getPageLeft(layer) {
  91.   var x;
  92.   if (isMinNS4)
  93.     return layer.pageX;
  94.   if (isMinIE4) {
  95.     x = 0;
  96.     while (layer.offsetParent != null) {
  97.       x += layer.offsetLeft;
  98.       layer = layer.offsetParent;
  99.     }
  100.     x += layer.offsetLeft;
  101.     return x;
  102.   }
  103.   return -1;
  104. }
  105. function getPageTop(layer) {
  106.   var y;
  107.   if (isMinNS4)
  108.     return layer.pageY;
  109.   if (isMinIE4) {
  110.     y = 0;
  111.     while (layer.offsetParent != null) {
  112.       y += layer.offsetTop;
  113.       layer = layer.offsetParent;
  114.     }
  115.     y += layer.offsetTop;
  116.     return y;
  117.   }
  118.   return -1;
  119. }
  120. function getWidth(layer) {
  121.   if (isMinNS4) {
  122.     if (layer.document.width)
  123.       return layer.document.width;
  124.     else
  125.       return layer.clip.right - layer.clip.left;
  126.   }
  127.   if (isMinIE4) {
  128.     if (layer.style.pixelWidth)
  129.       return layer.style.pixelWidth;
  130.     else
  131.       return layer.clientWidth;
  132.   }
  133.   return -1;
  134. }
  135. function getHeight(layer) {
  136.   if (isMinNS4) {
  137.     if (layer.document.height)
  138.       return layer.document.height;
  139.     else
  140.       return layer.clip.bottom - layer.clip.top;
  141.   }
  142.   if (isMinIE4) {
  143.     if (layer.style.pixelHeight)
  144.       return layer.style.pixelHeight;
  145.     else
  146.       return layer.clientHeight;
  147.   }
  148.   return -1;
  149. }
  150. function getzIndex(layer) {
  151.   if (isMinNS4)
  152.     return layer.zIndex;
  153.   if (isMinIE4)
  154.     return layer.style.zIndex;
  155.   return -1;
  156. }
  157. function setzIndex(layer, z) {
  158.   if (isMinNS4)
  159.     layer.zIndex = z;
  160.   if (isMinIE4)
  161.     layer.style.zIndex = z;
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Layer clipping.
  165. //-----------------------------------------------------------------------------
  166. function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {
  167.   if (isMinNS4) {
  168.     layer.clip.left   = clipleft;
  169.     layer.clip.top    = cliptop;
  170.     layer.clip.right  = clipright;
  171.     layer.clip.bottom = clipbottom;
  172.   }
  173.   if (isMinIE4)
  174.     layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
  175. }
  176. function getClipLeft(layer) {
  177.   if (isMinNS4)
  178.     return layer.clip.left;
  179.   if (isMinIE4) {
  180.     var str =  layer.style.clip;
  181.     if (!str)
  182.       return 0;
  183.     var clip = getIEClipValues(layer.style.clip);
  184.     return(clip[3]);
  185.   }
  186.   return -1;
  187. }
  188. function getClipTop(layer) {
  189.   if (isMinNS4)
  190.     return layer.clip.top;
  191.   if (isMinIE4) {
  192.     var str =  layer.style.clip;
  193.     if (!str)
  194.       return 0;
  195.     var clip = getIEClipValues(layer.style.clip);
  196.     return clip[0];
  197.   }
  198.   return -1;
  199. }
  200. function getClipRight(layer) {
  201.   if (isMinNS4)
  202.     return layer.clip.right;
  203.   if (isMinIE4) {
  204.     var str =  layer.style.clip;
  205.     if (!str)
  206.       return layer.style.pixelWidth;
  207.     var clip = getIEClipValues(layer.style.clip);
  208.     return clip[1];
  209.   }
  210.   return -1;
  211. }
  212. function getClipBottom(layer) {
  213.   if (isMinNS4)
  214.     return layer.clip.bottom;
  215.   if (isMinIE4) {
  216.     var str =  layer.style.clip;
  217.     if (!str)
  218.       return layer.style.pixelHeight;
  219.     var clip = getIEClipValues(layer.style.clip);
  220.     return clip[2];
  221.   }
  222.   return -1;
  223. }
  224. function getClipWidth(layer) {
  225.   if (isMinNS4)
  226.     return layer.clip.width;
  227.   if (isMinIE4) {
  228.     var str = layer.style.clip;
  229.     if (!str)
  230.       return layer.style.pixelWidth;
  231.     var clip = getIEClipValues(layer.style.clip);
  232.     return clip[1] - clip[3];
  233.   }
  234.   return -1;
  235. }
  236. function getClipHeight(layer) {
  237.   if (isMinNS4)
  238.     return layer.clip.height;
  239.   if (isMinIE4) {
  240.     var str =  layer.style.clip;
  241.     if (!str)
  242.       return layer.style.pixelHeight;
  243.     var clip = getIEClipValues(layer.style.clip);
  244.     return clip[2] - clip[0];
  245.   }
  246.   return -1;
  247. }
  248. function getIEClipValues(str) {
  249.   var clip = new Array();
  250.   var i;
  251.   // Parse out the clipping values for IE layers.
  252.   i = str.indexOf("(");
  253.   clip[0] = parseInt(str.substring(i + 1, str.length), 10);
  254.   i = str.indexOf(" ", i + 1);
  255.   clip[1] = parseInt(str.substring(i + 1, str.length), 10);
  256.   i = str.indexOf(" ", i + 1);
  257.   clip[2] = parseInt(str.substring(i + 1, str.length), 10);
  258.   i = str.indexOf(" ", i + 1);
  259.   clip[3] = parseInt(str.substring(i + 1, str.length), 10);
  260.   return clip;
  261. }
  262. //-----------------------------------------------------------------------------
  263. // Layer scrolling.
  264. //-----------------------------------------------------------------------------
  265. function scrollLayerTo(layer, x, y, bound) {
  266.   var dx = getClipLeft(layer) - x;
  267.   var dy = getClipTop(layer) - y;
  268.   scrollLayerBy(layer, -dx, -dy, bound);
  269. }
  270. function scrollLayerBy(layer, dx, dy, bound) {
  271.   var cl = getClipLeft(layer);
  272.   var ct = getClipTop(layer);
  273.   var cr = getClipRight(layer);
  274.   var cb = getClipBottom(layer);
  275.   if (bound) {
  276.     if (cl + dx < 0)
  277.       dx = -cl;
  278.     else if (cr + dx > getWidth(layer))
  279.       dx = getWidth(layer) - cr;
  280.     if (ct + dy < 0)
  281.       dy = -ct;
  282.     else if (cb + dy > getHeight(layer))
  283.       dy = getHeight(layer) - cb;
  284.   }
  285.   clipLayer(layer, cl + dx, ct + dy, cr + dx, cb + dy);
  286.   moveLayerBy(layer, -dx, -dy);
  287. }
  288. //-----------------------------------------------------------------------------
  289. // Layer background.
  290. //-----------------------------------------------------------------------------
  291. function setBgColor(layer, color) {
  292.   if (isMinNS4)
  293.     layer.bgColor = color;
  294.   if (isMinIE4)
  295.     layer.style.backgroundColor = color;
  296. }
  297. function setBgImage(layer, src) {
  298.   if (isMinNS4)
  299.     layer.background.src = src;
  300.   if (isMinIE4)
  301.     layer.style.backgroundImage = "url(" + src + ")";
  302. }
  303. //-----------------------------------------------------------------------------
  304. // Layer utilities.
  305. //-----------------------------------------------------------------------------
  306. function getLayer(name) {
  307.   if (isMinNS4)
  308.     return findLayer(name, document);
  309.   if (isMinIE4)
  310.     return eval('document.all.' + name);
  311.   return null;
  312. }
  313. function findLayer(name, doc) {
  314.   var i, layer;
  315.   for (i = 0; i < doc.layers.length; i++) {
  316.     layer = doc.layers[i];
  317.     if (layer.name == name)
  318.       return layer;
  319.     if (layer.document.layers.length > 0)
  320.       if ((layer = findLayer(name, layer.document)) != null)
  321.         return layer;
  322.   }
  323.   return null;
  324. }
  325. //-----------------------------------------------------------------------------
  326. // Image utilities.
  327. //-----------------------------------------------------------------------------
  328. function getImage(name) {
  329.   if (isMinNS4) {
  330.     return findImage(name, document);
  331.   }
  332.   if (isMinIE4)
  333.     return eval('document.all.' + name);
  334.   return null;
  335. }
  336. function findImage(name, doc) {
  337.   var i, img;
  338.   for (i = 0; i < doc.images.length; i++)
  339.     if (doc.images[i].name == name)
  340.       return doc.images[i];
  341.   for (i = 0; i < doc.layers.length; i++)
  342.     if ((img = findImage(name, doc.layers[i].document)) != null) {
  343.       img.container = doc.layers[i];
  344.       return img;
  345.     }
  346.   return null;
  347. }
  348. function getImagePageLeft(img) {
  349.   var x, obj;
  350.   if (isMinNS4) {
  351.     if (img.container != null)
  352.       return img.container.pageX + img.x;
  353.     else
  354.       return img.x;
  355.   }
  356.   if (isMinIE4) {
  357.     x = 0;
  358.     obj = img;
  359.     while (obj.offsetParent != null) {
  360.       x += obj.offsetLeft;
  361.       obj = obj.offsetParent;
  362.     }
  363.     x += obj.offsetLeft;
  364.     return x;
  365.   }
  366.   return -1;
  367. }
  368. function getImagePageTop(img) {
  369.   var y, obj;
  370.   if (isMinNS4) {
  371.     if (img.container != null)
  372.       return img.container.pageY + img.y;
  373.     else
  374.       return img.y;
  375.   }
  376.   if (isMinIE4) {
  377.     y = 0;
  378.     obj = img;
  379.     while (obj.offsetParent != null) {
  380.       y += obj.offsetTop;
  381.       obj = obj.offsetParent;
  382.     }
  383.     y += obj.offsetTop;
  384.     return y;
  385.   }
  386.   return -1;
  387. }
  388. //-----------------------------------------------------------------------------
  389. // Window and page properties.
  390. //-----------------------------------------------------------------------------
  391. function getWindowWidth() {
  392.   if (isMinNS4)
  393.     return window.innerWidth;
  394.   if (isMinIE4)
  395.     return document.body.clientWidth;
  396.   return -1;
  397. }
  398. function getWindowHeight() {
  399.   if (isMinNS4)
  400.     return window.innerHeight;
  401.   if (isMinIE4)
  402.     return document.body.clientHeight;
  403.   return -1;
  404. }
  405. function getPageWidth() {
  406.   if (isMinNS4)
  407.     return document.width;
  408.   if (isMinIE4)
  409.     return document.body.scrollWidth;
  410.   return -1;
  411. }
  412. function getPageHeight() {
  413.   if (isMinNS4)
  414.     return document.height;
  415.   if (isMinIE4)
  416.     return document.body.scrollHeight;
  417.   return -1;
  418. }
  419. function getPageScrollX() {
  420.   if (isMinNS4)
  421.     return window.pageXOffset;
  422.   if (isMinIE4)
  423.     return document.body.scrollLeft;
  424.   return -1;
  425. }
  426. function getPageScrollY() {
  427.   if (isMinNS4)
  428.     return window.pageYOffset;
  429.   if (isMinIE4)
  430.     return document.body.scrollTop;
  431.   return -1;
  432. }