home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 April / VPR9804B.ISO / Netscape / NETCAST.Z / ncjs10.jar / scroll.js < prev    next >
Text File  |  1997-09-03  |  8KB  |  260 lines

  1. //*****************************
  2. // VARIABLES                  *
  3. //*****************************
  4.  
  5. var contentWindowLayer;
  6. var contentClipperLayer;
  7. var contentAreaLayer;
  8. var verticalTrackLayer;
  9. var upArrowLayer;
  10. var downArrowLayer;
  11. var verticalTabLayer;
  12.  
  13. var itemID = 1;
  14. var snapTimerID = 0;
  15. var beginSnapTimerID = 0;
  16. var itemHeight;
  17. var    beginRepeatDelay = 300;    //length of delay (in milliseconds) before start of repeated execution of snapContent() upon clicking arrow buttons
  18. var contentWindowY;
  19. var contentW;
  20. var contentH;
  21. var arrowH;
  22. var vTabH;
  23. var vTabTopLimit;
  24. var vTabBottomLimit;
  25. var vTabRange;
  26. var vddMultiplier;
  27. var tabSnapIncrement;
  28.  
  29.  
  30. //*****************************
  31. // FUNCTIONS                  *
  32. //*****************************
  33.  
  34. // use this if the entire content area resizes, such as on initial load.
  35. // configures the appropriate position for all of the scrollbar elements.
  36.  
  37. function scrollbarSetup() {
  38.  
  39.     repeatDelay = 150;
  40.  
  41.     contentWindowLayer = document.layers["contentWindow"];
  42.     contentClipperLayer = contentWindowLayer.layers["contentClipper"];
  43.     contentAreaLayer = contentClipperLayer.layers["contentArea"];
  44.     verticalTrackLayer = contentWindowLayer.layers["verticalTrack"];
  45.     upArrowLayer = contentWindowLayer.layers["upArrow"];
  46.     downArrowLayer = contentWindowLayer.layers["downArrow"];
  47.     verticalTabLayer = contentWindowLayer.layers["verticalTab"];
  48.  
  49.     // First, compute the appropriate positions
  50.  
  51.     itemHeight = contentAreaLayer.itemHeight;
  52.  
  53.     // Then, move the scrollbar elements around
  54.  
  55.     //set down arrow location relative to content clipper layer height.
  56.     downArrowLayer.moveTo(175, (contentClipperLayer.clip.height - 15));
  57.     downArrowLayer.zIndex=800;
  58.  
  59.     //vertical scrollbar tab bottom edge position limit
  60.     vTabBottomLimit = downArrowLayer.top;
  61.  
  62.     //vertical scrollbar tab top edge position limit
  63.     //the magic constant is the height of visible part (less than 15 pixels by 15 pixels) of the arrow button images
  64.     vTabTopLimit = 14;
  65.  
  66.     //vertical scrollbar tab height
  67.     vTabH = verticalTabLayer.document.images[0].height;
  68.  
  69.     //vertical scrollbar tab track length
  70.     vTabRange = vTabBottomLimit - vTabH - vTabTopLimit; //don't ask. it works.
  71.  
  72. //content window top edge
  73.     contentWindowY = contentWindowLayer.top;
  74.  
  75. //content area document width
  76.     contentW = contentAreaLayer.document.width;
  77.  
  78. //content area document height
  79.     contentH = contentAreaLayer.document.height;
  80.  
  81. //arrow height
  82.     arrowH = upArrowLayer.document.images[0].height;
  83.  
  84.     // remember to resize the scrollbar itself based on the present contents
  85.     sizeScrollbar();
  86. }
  87.  
  88. // sizeScrollbar gets called whenever the content area changes size, so that the
  89. // scrollbar can recompute its size, position, and the like.  We take the height
  90. // and number of items from the appropriately set variables in the content area.
  91.  
  92. function sizeScrollbar() {
  93. //visibility of track, buttons and tab defaults to hide.
  94. //the track layer and image height is greater than we will ever use.
  95. //up arrow location is constant; we don't change it here.
  96.  
  97.     contentH = contentAreaLayer.topPos;
  98.     maxItems = contentAreaLayer.itemCount;
  99.  
  100.     //value by which to multiply mouse position delta
  101.     //for use in contentAreaLayer.moveTo() in tabScrollVertically(), in vertical_scroll.js
  102.     vddMultiplier = ((contentH - ((vTabRange + (arrowH * 2)))));
  103.  
  104.     //distance of tab movement when up arrow is clicked
  105.     tabSnapIncrement = vTabRange / (maxItems - 2); //don't ask. it works.
  106.  
  107.     // make track, buttons and tab visible, but only if the content space is large enough
  108.     // to accomodate them
  109.  
  110.     if (contentH > contentClipperLayer.clip.height) {
  111. //        verticalTrackLayer.visibility = "show";
  112.         upArrowLayer.visibility = "show";
  113.         downArrowLayer.visibility = "show";
  114. //        verticalTabLayer.visibility = "show";
  115.     } else {
  116.         verticalTrackLayer.visibility = "hide";
  117.         upArrowLayer.visibility = "hide";
  118.         downArrowLayer.visibility = "hide";
  119.         verticalTabLayer.visibility = "hide";
  120.     }
  121. }
  122.  
  123. function arrowTimer(direction) {
  124.     //if the mouse is held down for N milliseconds, start repeating the incremental scrolling
  125.     if(direction == "up") {
  126.         //scroll the content up one increment
  127.         snapContent('up');
  128.         
  129.         //beginSnapTimerID is cleared in arrowBehavior()
  130.         beginSnapTimerID = setTimeout(repeatSnapContent, beginRepeatDelay, "up");
  131.     }
  132.  
  133.     else if(direction == "down") {
  134.         //scroll the content down one increment
  135.         snapContent('down');
  136.  
  137.         //beginSnapTimerID is cleared in arrowBehavior()
  138.         beginSnapTimerID = setTimeout(repeatSnapContent, beginRepeatDelay, "down");
  139.     }
  140. }
  141.  
  142. function repeatSnapContent(direction) {
  143. // java.lang.System.out.println("RepeatSnapContent called");
  144.     
  145.     snapContent(direction);
  146.  
  147.     //if the mouse down persists, repeat the incremental scrolling
  148.     //snapTimerID is cleared in arrowBehavior()
  149.     snapTimerID = setTimeout(repeatSnapContent, repeatDelay, direction);
  150. }
  151.  
  152. function snapContent(direction) {
  153. // java.lang.System.out.println("SnapContent called (" + direction + "), itemID = " + itemID + ", max = " + maxItems);
  154.     //the "down" arrow was clicked
  155.     if(direction == "down") {
  156.         //if the item number has incremented beyond the last item
  157.         if(itemID > maxItems) {
  158.             //reset the item number to the last item and do nothing
  159.             itemID = maxItems;
  160.             return;
  161.         }
  162.  
  163.         //otherwise
  164.         else {
  165.             //if reasonable, increment the item number
  166.             if((itemID + 1) > maxItems) return;
  167.             else itemID += 1;
  168.  
  169.             //update tab layer properties to reflect new position
  170.             verticalTabLayer.tabOffset = 0;
  171.             verticalTabLayer.setPreviousTabPosition(verticalTabLayer.DRAG_Y);
  172.             verticalTabLayer.recalcTabPosition(verticalTabLayer.top, vTabTopLimit);
  173.  
  174.             //move tab only if it is within bounds
  175.             if(itemID == (maxItems - 1)) verticalTabLayer.moveTo(verticalTabLayer.left, (vTabBottomLimit - vTabH));
  176.             else if(itemID < maxItems) verticalTabLayer.checkTabBounds(tabSnapIncrement, vTabTopLimit);
  177.             else if(itemID == maxItems) return;
  178.  
  179.             //update tab layer properties
  180. //            verticalTabLayer.recalcVerticalDragDelta();
  181. //            verticalTabLayer.recalcContentY();
  182.  
  183.             //move the content area layer down by one item length
  184.             contentAreaLayer.moveBy(0, -itemHeight);
  185.         }
  186.     }
  187.     
  188.     //the "up" arrow was clicked
  189.     if(direction == "up") {
  190.         //if the item number has decremented beyond the first item
  191.         if(itemID < 1) {
  192.             //reset the item number to the first item and do nothing
  193.             itemID = 1;
  194.             return;
  195.         }
  196.  
  197.         //otherwise
  198.         else {
  199.             //if reasonable, decrement the item number
  200.             if((itemID - 1) < 1) return;
  201.             else itemID -= 1;
  202.  
  203.             //update tab layer properties to reflect new position
  204.             verticalTabLayer.tabOffset = 0;
  205.             verticalTabLayer.setPreviousTabPosition();
  206.             verticalTabLayer.recalcTabPosition(verticalTabLayer.top, vTabTopLimit);
  207.  
  208.             //move tab only if it is within bounds
  209.             if(itemID == 1) verticalTabLayer.moveTo(verticalTabLayer.left, vTabTopLimit);
  210.             else verticalTabLayer.checkTabBounds(-tabSnapIncrement, vTabTopLimit);
  211.  
  212.             //update tab layer properties
  213.             verticalTabLayer.recalcVerticalDragDelta();
  214.             verticalTabLayer.recalcContentY();
  215.  
  216.             //if it isn't already at the top,
  217.             if(contentAreaLayer.top >= 0) {
  218.                 contentAreaLayer.top = 0;
  219.                 itemID = 1;
  220.                 return;
  221.             }
  222.             else {
  223.                 //move the content area layer down by one item length
  224.                 contentAreaLayer.moveBy(0, itemHeight);
  225.             }
  226.         }
  227.     }
  228. }
  229.  
  230. //depression behavior for scrollbar arrow buttons, 
  231. //and clearTimeout() for setTimeout() in arrowTimer() and repeatSnapContent()
  232. function arrowBehavior(direction,state) {
  233.     if(direction == "up") {
  234.         if(state == "in") {
  235.             upArrowLayer.document.images["upArrowImage"].src = "images/i_uparr.gif";
  236.         }
  237.         else if(state == "out") {
  238.             upArrowLayer.document.images["upArrowImage"].src = "images/o_uparr.gif";
  239.             
  240.             //clear timer IDs
  241.             clearTimeout(snapTimerID);
  242.             clearTimeout(beginSnapTimerID);
  243.         }
  244.     }
  245.  
  246.     else if(direction == "down") {
  247.         if(state == "in") {
  248.             downArrowLayer.document.images["downArrowImage"].src = "images/i_dnarr.gif";
  249.         }
  250.         else if(state == "out") {
  251.             downArrowLayer.document.images["downArrowImage"].src = "images/o_dnarr.gif";
  252.             
  253.             //clear timer IDs
  254.             clearTimeout(snapTimerID);
  255.             clearTimeout(beginSnapTimerID);
  256.         }
  257.     }
  258. }
  259.  
  260.