home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / browser / ns405lyc / netcast.z / ncjs10.jar / selector.js < prev    next >
Encoding:
JavaScript  |  1998-03-06  |  16.6 KB  |  616 lines

  1. // (c) Copyright 1997 Netscape Communications, Corp.
  2.  
  3. var gNumLayers     = document.layers.length;
  4. var layerLevel       = null;
  5. var statusLocked   = false;
  6. var enablePopups    = 0;
  7. var propID          = null;
  8.  
  9. var expandedLayer    = null;
  10. var numContainers     = 0;
  11. var currentHeight    = 0;
  12.  
  13. var currentItem         = null;
  14.  
  15. var newWorld         = 1;
  16.  
  17. function onload()
  18. {
  19.     var startup=1;
  20.  
  21.     new LayerManager(window);
  22.  
  23.     captureEvents (Event.CLICK);
  24.     window.onClick = itemPopupMenu;
  25.  
  26.     depth.onmousedown = itemPopupMenu;
  27. }
  28.  
  29. function onblur()
  30. {
  31.     hideMenu();
  32. }
  33.  
  34. function finishLoading()
  35. {
  36.     var ready = parent.titlebar.document.demo.isSelectorReady();
  37.  
  38.     if (!ready) {
  39. //        java.lang.System.out.println("selector: Not finished loading ");
  40.         setTimeout(finishLoading, 750);
  41.         return;
  42.     } else {
  43. //        java.lang.System.out.println("selector: Finished loading ");
  44.     }
  45.  
  46.     var    lastOpened = "" + depth.GetNetscapePref("netcaster.drawer.lastOpen", "finder", false);
  47.  
  48.     toggleContainer(lastOpened);
  49.  
  50.  
  51. }
  52.  
  53. function setFocusedItem(dataItem)
  54. {
  55.     currentItem = dataItem;
  56. }
  57.  
  58. /***************** Data Item Initialization Routines *******************/
  59.  
  60. function layerLoaded(layerObj, parameter)
  61. {
  62.     layerObj.fontFace = "arial,geneva,helvetica";
  63.     layerObj.fontSize = "+0";
  64.     layerObj.y        = currentHeight;
  65.     layerObj.type     = parameter.getType();
  66.     layerObj.itemName = parameter.getItemName();
  67.     layerObj.descName = parameter.getDescription();
  68.     layerObj.url      = parameter.getURL();
  69.     layerObj.dataItem = parameter;
  70.     layerObj.clip.height = 30;
  71.     layerObj.clip.width = 200;
  72.  
  73.     parameter.setItemView(layerObj);
  74.  
  75.     currentHeight += 30;
  76.     layerObj.visibility = "show";
  77.  
  78. //    java.lang.System.out.println("selector: Layer Loaded (" + layerObj.itemName + ") - height " + currentHeight);
  79.  
  80. }
  81.  
  82. function createCategoryLayer(dataItem)
  83. {
  84.     var newLoadObj = new LayerLoadObject(null, "headtmpl.htm",
  85.         window, this, dataItem, false);
  86.  
  87.     window.layerManager.layerLoad(newLoadObj);
  88.     window.numContainers++;
  89.  
  90.     return;
  91. }
  92.  
  93. function contextCreateItemLayer(dataItem)
  94. {
  95.     setTimeout(createItemLayer, 10, dataItem);
  96. }
  97.  
  98. function createItemLayer(dataItem)
  99. {
  100.     // wait until the parent data item's layer is created
  101.  
  102.     var itemParent = dataItem.getParent();
  103.     var parentLayer = itemParent.getItemView();
  104.  
  105.     if (!parentLayer || (parentLayer == null) || parentLayer.container == null) {
  106.         
  107. //        java.lang.System.out.println("parent layer not available");
  108.         setTimeout(createItemLayer, 200, dataItem);
  109.         return;
  110.     }
  111.  
  112. //    java.lang.System.out.println("CreateItem: parent layer: " + parentLayer);
  113. //    java.lang.System.out.println("CreateItem: parent container: " + parentLayer.container);
  114.  
  115.     var newLoadObj = new LayerLoadObject(null, "itemtmpl.htm",
  116.         parentLayer.container, parentLayer.container, dataItem, false);
  117.  
  118.     window.layerManager.layerLoad(newLoadObj);
  119.  
  120.     parentLayer.container.itemCount++;
  121.     return;
  122. }
  123.  
  124. function createPageLayer(dataItem, url)
  125. {
  126.     // wait until the parent data item's layer is created
  127.  
  128.     var itemParent = dataItem.getParent();
  129.     var parentLayer = itemParent.getItemView();
  130.  
  131.     if (!parentLayer || (parentLayer == null) || parentLayer.container == null) {
  132.         
  133. //        java.lang.System.out.println("parent layer not available");
  134.         setTimeout(createPageLayer, 200, dataItem, url);
  135.         return;
  136.     }
  137.  
  138. /*    java.lang.System.out.println("CreatePage: parent layer: " + parentLayer);
  139.     java.lang.System.out.println("CreatePage: parent container: " + parentLayer.container);
  140.     java.lang.System.out.println("CreatePage: url: " + url);
  141. */
  142.  
  143.     var newLoadObj = new LayerLoadObject(null, url,
  144.         parentLayer.container, parentLayer.container, dataItem, true);
  145.  
  146.     window.layerManager.layerLoad(newLoadObj);
  147.  
  148.     parentLayer.container.itemCount++;
  149.     return;
  150. }
  151.  
  152. function refreshItemLayer(dataItem, layer)
  153. {
  154.     layer.type     = dataItem.getType();
  155.     layer.itemName = dataItem.getItemName();
  156.     layer.descName = dataItem.getDescription();
  157.     layer.url      = dataItem.getURL();
  158.  
  159.     layer.refreshChannelInfo();
  160. }
  161.  
  162.  
  163.  
  164. function contextDeleteItemLayer(dataItem, layer)
  165. {
  166.     setTimeout(deleteItemLayer, 10, dataItem, layer);
  167. }
  168.  
  169. function deleteItemLayer(dataItem, layer)
  170. {
  171.     var    itemHeight = 30;        // THIS IS A HARDCODED VALUE!
  172.     var    parentArray = layer.parentLayer.document.layers;
  173.  
  174.     // First, adjust the selector
  175.     layer.visibility = "hide";
  176.  
  177.     for (var i=0; i < parentArray.length; i++) {
  178.         if (parentArray[i].top > layer.top) {
  179.  
  180. //            parentArray[i].top -= layer.clip.height;
  181.             parentArray[i].top -= itemHeight;
  182.          }
  183.     }
  184.  
  185.     // Then adjust the parent so subsequent adds happen in the right place
  186.     layer.parentLayer.topPos -= itemHeight;
  187.  
  188.     // Finally, delete the layer
  189. }
  190.  
  191. //*******************************************************************************************************
  192. // Open a webtop
  193. //*******************************************************************************************************
  194.  
  195. function openWebtopURL(siteURL, dataItem)
  196. {
  197.     parent.titlebar.OpenWebtop(siteURL, dataItem, true);
  198. }
  199.  
  200. //*******************************************************************************************************
  201. // Open a normal channel
  202. //*******************************************************************************************************
  203.  
  204. function openSiteURL(fullscreen, siteURL)
  205. {
  206.     parent.titlebar.OpenSite(fullscreen, siteURL, true);
  207. }
  208.   
  209. //*******************************************************************************************************
  210. // Open a castanet channel
  211. //*******************************************************************************************************
  212.  
  213. function openCastanetURL(siteURL)
  214. {
  215.     parent.titlebar.document.demo.openCastanetURL(siteURL);
  216. //    parent.titlebar.AutoHide();
  217. }
  218.  
  219. //*******************************************************************************************************
  220. // Handle the expand/collapse behavior
  221. //*******************************************************************************************************
  222.  
  223. function toggleContainer(containerName)
  224. {
  225.     var container =    null;
  226.     var containerLayer = null;
  227.  
  228.     if (containerName == null || containerName == "")
  229.         return;
  230.  
  231.     // First, get the layer corresponding to this container
  232.     container = parent.titlebar.document.demo.findContainerByName(containerName)
  233.     if (container != null) {
  234.         containerLayer = container.getItemView()
  235. // java.lang.System.out.println("Toggling container: " + containerLayer);
  236.     }
  237.  
  238.     if (containerLayer == null) {
  239.         return;
  240.     }
  241.  
  242.     var parentArray = containerLayer.parentLayer.document.layers;
  243.  
  244.     if (expandedLayer == null) {
  245.         expandedLayer = containerLayer;
  246.         expandedLayer.clip.height = containerGetHeight() + 30;
  247.         expandedLayer.showContainer();
  248.  
  249.         for (var i=0; i < parentArray.length; i++) {
  250.             if (parentArray[i].top > expandedLayer.top) {
  251.                 parentArray[i].top += containerGetHeight();
  252.             }
  253.             
  254.             if (parentArray[i] != expandedLayer) {
  255.                 parentArray[i].clip.bottom = 30;
  256.             }
  257.         }
  258.  
  259.     }
  260.  
  261.     // if the currently visible container is higher than the selected one
  262.  
  263.     else if (expandedLayer.top < containerLayer.top) {
  264.         var moveArray = new Array();
  265.         var    itemCount = 0;
  266.  
  267. //        java.lang.System.out.println("ToggleContainer: expanded is higher");
  268.  
  269.         for (var i=0; i < parentArray.length; i++) {
  270.             if ((parentArray[i].top > expandedLayer.top) &&
  271.                 (parentArray[i].top <= containerLayer.top)) {
  272.                 moveArray[itemCount++] = parentArray[i];
  273.             }
  274.         }
  275.  
  276. /*        java.lang.System.out.println("Calling reposition with source layer:");
  277.         java.lang.System.out.println(expandedLayer);
  278.         java.lang.System.out.println("and destination Layer");
  279.         java.lang.System.out.println(containerLayer);
  280. */
  281.         repositionContainers(-250, moveArray, expandedLayer, 30, containerLayer);
  282.  
  283.         expandedLayer.hiding();
  284.         expandedLayer = containerLayer;
  285.         containerLayer.showing();
  286.     }
  287.  
  288.     // if the currently visible container is lower than the selected one
  289.  
  290.     else if (expandedLayer.top > containerLayer.top) {
  291.         var moveArray = new Array;
  292.         var    itemCount = 0;
  293.         var parentArray = containerLayer.parentLayer.document.layers;
  294.  
  295.         for (var i=0; i < parentArray.length; i++) {
  296.             if ((parentArray[i].top > containerLayer.top) &&
  297.                 (parentArray[i].top <= expandedLayer.top)) {
  298.                 moveArray[itemCount++] = parentArray[i];
  299.             }
  300.         }
  301.  
  302.  
  303. /*        java.lang.System.out.println("Calling reposition with source layer:");
  304.         java.lang.System.out.println(containerLayer);
  305.         java.lang.System.out.println("and destination Layer");
  306.         java.lang.System.out.println(expandedLayer);
  307. */
  308.         var newBottom = containerGetHeight() + 30;
  309.         repositionContainers(250, moveArray, containerLayer, newBottom, expandedLayer);
  310.  
  311.         expandedLayer.hiding();
  312.         expandedLayer = containerLayer;
  313.         containerLayer.showing();
  314.     }
  315.  
  316. }
  317.  
  318. function repositionContainers(delta, moveArray, sourceLayer, sourceClip, destLayer)
  319. {
  320.     var bottom = sourceLayer.clip.height;
  321.  
  322.     if (((delta > 0) && (bottom < sourceClip)) ||
  323.         ((delta < 0) && (bottom > sourceClip))) {
  324.     
  325.         // adjust delta so that the last jump doesn't exceed our range
  326.  
  327.         var pinnedDelta = Math.abs(bottom - sourceClip);
  328.  
  329.         if (Math.abs(delta) > pinnedDelta) {
  330.  
  331.             if (delta < 0) {
  332.                 pinnedDelta *= -1;
  333.             }
  334. //            java.lang.System.out.println("Adjusting delta from " + delta + " to " + pinnedDelta);
  335. //            java.lang.System.out.println("Source clip reset should reset to exactly sourceClip");
  336.  
  337.             delta = pinnedDelta;
  338.         }
  339.  
  340.  
  341.         for (var i=0; i < moveArray.length; i++) {
  342.             moveArray[i].top += delta;
  343. //            java.lang.System.out.println("Adjusted position of layer " + i + " to " + moveArray[i].top);
  344.         }
  345.  
  346.         if (delta < 0) {
  347. //        java.lang.System.out.println("Resetting source clip height from " + sourceLayer.clip.height);
  348.             sourceLayer.clip.height += delta;
  349. //        java.lang.System.out.println("to " + sourceLayer.clip.height);
  350.         } else {
  351. //        java.lang.System.out.println("Resetting source clip from " + sourceLayer.clip.bottom);
  352.             sourceLayer.clip.bottom += delta;
  353. //        java.lang.System.out.println("to " + sourceLayer.clip.bottom);
  354.         }
  355.  
  356. //        java.lang.System.out.println("Resetting destination clipping: clip.height = " + destLayer.clip.height +
  357. //            " with delta " + delta + " and source clip " + sourceClip);
  358.         destLayer.clip.height -= delta;
  359.  
  360.         if (sourceLayer.clip.height != sourceClip) {
  361. //            java.lang.System.out.println("Resetting reposition containers: clip.height = " + sourceLayer.clip.height +
  362. //                "target is " + sourceClip + " with delta " + delta);
  363.  
  364.             setTimeout(repositionContainers, 40, delta, moveArray, sourceLayer, sourceClip, destLayer);
  365.         } else {
  366. /*            java.lang.System.out.println("Finished positioning:");
  367.             java.lang.System.out.println("*** Source layer: " + sourceLayer);
  368.             java.lang.System.out.println("*** Destination layer: " + destLayer);
  369. */
  370.         }
  371.  
  372.     }
  373.  
  374.     return;
  375. }
  376.  
  377.  
  378. /***************** other stuff ******************/
  379.  
  380. function handleProperties(item)
  381. {
  382.     if ( null != item ) 
  383.     {
  384.         editing = 1;
  385.         var propWin = depth.SecureWindowOpen (window, 'addc.htm', 
  386.                 'prefs', 
  387.                 'left=150,top=200,width=427,height=247,titlebar=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,modal=yes,alwaysRaised=yes,z-lock=no,dependent=yes,hotkeys=no');
  388.  
  389.         propWin.depth = depth;
  390.         propID = item;
  391.     }
  392. }
  393.  
  394. function PropertiesCallback(propsWindow)
  395. {
  396.     propsWindow.selector  = this;
  397.     propsWindow.container = depth.getDefaultContainer();
  398.  
  399.     if (editing == 1)
  400.         propsWindow.editItem(propID);
  401.  
  402.     propsWindow.callback = self;
  403.  
  404. }
  405.  
  406. //*******************************************************************************************************
  407. // Handling for the popup menu
  408. //*******************************************************************************************************
  409.  
  410. function hiliteMenuItem(e) {
  411.     e.target.bgColor="#808080";
  412. }
  413.  
  414. function showMenu(dataItem, menuLeft, menuTop)
  415. {
  416.     var menu = window.document.layers['menu'];
  417.     var menuCommands = dataItem.getMenuItems();
  418.     var currentDoc = menu.layers['item_name'].document;
  419.  
  420.     menu.clip.height = ((menuCommands.length + 1) * 25) + 3;    // leave a 3 pixel margin
  421.     currentDoc.open("text/html");
  422.     currentDoc.write("<center><font size=-1 face='arial,helvetica'><b>");
  423.     currentDoc.write("" + dataItem.getItemName() + "</b></font></center>");
  424.     currentDoc.close();
  425.  
  426.     for (var i=0; i < menuCommands.length; i++) {
  427.         var currentDoc = menu.layers["item"+i].document;
  428.         currentDoc.open("text/html");
  429.         currentDoc.write("<font size=-1 face='arial,helvetica'>");
  430.         if (("" + menuCommands[i][0]) == "-") {
  431.             menu.layers["item"+i].onmouseover=null;
  432.             currentDoc.write("<hr width=80%></font>");
  433.         } else {
  434.             menu.layers["item"+i].onmouseover=hiliteMenuItem;
  435.             currentDoc.write("<spacer type=block height=2 width=200><spacer type=block height=1 width=5>");
  436.             currentDoc.write(menuCommands[i][0] + "</font>");
  437.             currentDoc.write("<layer top=0 left=0 clip=150,25>");
  438.             currentDoc.write("<a href='about:blank' " +
  439.                 "onclick='parentLayer.doMenuItem(\"" + menuCommands[i][1] + "\");return false;'>" +
  440.                 "<img src='images/blank.gif' border=none width=150 height=25>" +
  441.                 "</a></layer>");
  442.         }
  443.         currentDoc.close();
  444.     }
  445.  
  446.     menu.layers["item"+menuCommands.length].onmouseover=null;
  447.  
  448.     // the selector is 200 pixels wide
  449.     var overflow = menuLeft + menu.clip.width- 200;
  450.  
  451.     if (overflow > 0) {
  452.         menu.left = menuLeft - overflow;
  453.     } else {
  454.         menu.left = menuLeft;
  455.     }
  456.  
  457.     var overflow = menuTop + menu.clip.height - (containerGetHeight() + 30);
  458.     if (overflow > 0) {
  459.         menu.top = menuTop - overflow;
  460.     } else {
  461.         menu.top = menuTop;
  462.     }
  463.  
  464.     menu.visibility = "show";
  465.     menu.menuTarget = dataItem;
  466.     menu.zIndex=1000;
  467.  
  468. }
  469.  
  470. /*
  471. function showMenu(dataItem, menuTop, menuLeft)
  472. {
  473.     var menu = window.document.layers['menu'];
  474.     var menuCommands = dataItem.getMenuItems();
  475.     var currentDoc = menu.layers['item_name'].document;
  476.  
  477. //    menu.clip.height = (menuCommands.length + 1) * 25;
  478.     menu.clip.height = 6 * 25;
  479.  
  480.     menu.top = menuTop;
  481.     menu.left= menuLeft;
  482.     menu.visibility = "show";
  483.     menu.menuTarget = dataItem;
  484.     menu.zIndex=1000;
  485.  
  486. }
  487. */
  488.  
  489. function hideMenu()
  490. {
  491.     var menu = window.document.layers['menu'];
  492.  
  493.     if (menu.visibility = "show") {
  494.         menu.visibility="hide";
  495.     }    
  496. }
  497.  
  498. function itemPopupMenu(e)
  499. {
  500.     var menu = window.document.layers['menu'];
  501.  
  502.     if (e.which > 1 || (depth.isMac() && (e.modifiers & Event.CONTROL_MASK)))
  503.     {
  504.         if (currentItem == null) {
  505.             menu.visibility = "hide";
  506.         } else {
  507.             setTimeout(showMenu, 10, currentItem, e.pageX, e.pageY);
  508.         }
  509.  
  510.         return false;
  511.     }
  512.     else
  513.     {
  514.         if (menu.visibility == "show") {
  515.  
  516.             if ((e.pageX >= menu.left) && (e.pageX <= (menu.left + menu.clip.width))
  517.                 && (e.pageY >= menu.top) && (e.pageY <= (menu.top + menu.clip.height))) {
  518.  
  519.                 menu.visibility = "hide";
  520.                 return routeEvent(e);
  521.             }
  522.  
  523.             menu.visibility = "hide";
  524.             return false;
  525.         }
  526.         
  527.         return routeEvent(e);
  528.     }
  529. }
  530.  
  531. //*******************************************************************************************************
  532. // Show an alert to the user with the given string.
  533. //*******************************************************************************************************
  534. function showError(error) {
  535.  if (error != null)
  536.     alert(error);
  537. }
  538.  
  539. function winOpen(url, name, options) {
  540.     return window.open(url, name, options);
  541. }
  542.  
  543. function winAlert(string) {
  544.     return window.alert(string);
  545. }
  546.  
  547. function winConfirm(string) {
  548.     return window.confirm(string);
  549. }
  550.  
  551. function winEscape(string) {
  552.     return window.escape(string);
  553. }
  554.  
  555. function winUnescape(string) {
  556.     return window.unescape(string);
  557. }
  558.  
  559. function containerIsOpen(containerName) {
  560.     return expandedContainer == containerName;
  561. }
  562.  
  563. function containerGetHeight() {
  564.     return window.innerHeight - (window.numContainers * 30);    
  565. }
  566.  
  567. function containerGetParameter(containerName) {
  568.     container = parent.titlebar.document.demo.findContainerByName(containerName)
  569.  
  570.     if (container != null) {
  571.         return "" + container.getUserData();
  572.     }
  573.  
  574.     return "";
  575. }
  576.  
  577. function containerSetParameter(containerName, data) {
  578.     // First, get the layer corresponding to this container
  579.     container = parent.titlebar.document.demo.findContainerByName(containerName)
  580.     if (container != null) {
  581.         container.setUserData(data);
  582.     }
  583.  
  584.     return;
  585. }
  586.  
  587. function selectorOpen() {
  588.     parent.selectorTab.ShowSelector();
  589.     return;
  590. }
  591.  
  592. function selectorClose() {
  593.     parent.selectorTab.HideSelector();
  594.     return;
  595. }
  596.  
  597. function containerPrintln(outputString) {
  598.     java.lang.System.out.println(outputString);
  599.     return;
  600. }
  601.  
  602. export winOpen;
  603. export winAlert;
  604. export winConfirm;
  605. export winEscape;
  606. export winUnescape;
  607. export containerIsOpen;
  608. export containerGetHeight;
  609. export containerGetParameter;
  610. export containerSetParameter;
  611. export selectorClose;
  612. export selectorOpen;
  613. export containerPrintln;
  614.  
  615. void(0);
  616.