home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 June / comonline0602.iso / software / cogitum / cociter.exe / CogitumH.___ / HTML / CTTREE.HTC < prev    next >
Encoding:
HTML Component  |  2001-08-08  |  13.2 KB  |  589 lines

  1. <PUBLIC:COMPONENT NAME="ctTree" tagName="UL" lightWeight=true supportsEditMode=false>
  2. <PUBLIC:ATTACH EVENT="oncontentready" ONEVENT="Initialize()" FOR=element/>
  3. <PUBLIC:ATTACH EVENT="onclick" ONEVENT="OnClick()" />
  4. <PUBLIC:ATTACH EVENT="onselectstart" ONEVENT="OnSelectStart()" />
  5. <PUBLIC:METHOD NAME="Initialize" />
  6. <!-- PUBLIC:METHOD NAME="Expand" / -->
  7. <!-- PUBLIC:METHOD NAME="Collapse" / -->
  8. <!-- PUBLIC:METHOD NAME="OnClick" / -->
  9. <PUBLIC:METHOD NAME="GetCurrentNode" />
  10. <PUBLIC:METHOD NAME="GetCurrentNodeUIN" />
  11. <PUBLIC:METHOD NAME="GetCurrentNodeName" />
  12. <PUBLIC:METHOD NAME="CreateNewNode" />
  13. <PUBLIC:METHOD NAME="RenameNode" />
  14. <PUBLIC:METHOD NAME="RemoveNode" />
  15. <PUBLIC:METHOD NAME="MoveNode" />
  16. <PUBLIC:METHOD NAME="FindNodeByUIN" />
  17. <!-- PUBLIC:PROPERTY NAME="IsExpanded" / -->
  18. <PUBLIC:METHOD NAME="IsChild" />
  19. <PUBLIC:METHOD NAME="GetForegroundColor" />
  20. <PUBLIC:METHOD NAME="GetBackgroundColor" />
  21. <!-- Exposed custom events -->
  22. <PUBLIC:EVENT NAME="onSelectionChange" ID=selChangeID />
  23.  
  24. <SCRIPT LANGUAGE="Jscript">
  25.  
  26. function fnStringToBoolean(someString) {
  27.     if (someString == "true" || someString == true ||
  28.         someString == "1" || someString == 1 || someString == -1 ) {
  29.         return true;
  30.     }
  31.     else if (someString == "false" || someString == false ||
  32.         someString == "0" || someString == 0) {
  33.         return false;
  34.     }
  35.     else 
  36.         return someString;
  37.     
  38. };
  39.  
  40.  
  41. var oCurrentNode = null;
  42.  
  43. function GetCurrentNode() {
  44.     return oCurrentNode;
  45. };
  46.  
  47. function GetCurrentNodeUIN() {
  48.     if (oCurrentNode != null ) {
  49.         if (oCurrentNode.tagName == "NOBR") {
  50.             return oCurrentNode.parentElement.UIN;
  51.         }
  52.         else if (oCurrentNode.tagName == "LI") {
  53.             return oCurrentNode.UIN;
  54.             
  55.         }
  56.         else {
  57.             return null;
  58.         };
  59.     }
  60.     else {
  61.         return null;
  62.     };
  63. };
  64.  
  65. function GetCurrentNodeName() {
  66.     if (oCurrentNode != null ) {
  67.         if (oCurrentNode.tagName == "NOBR") {
  68.             return fnRemoveWhiteSpaces(oCurrentNode.innerText);
  69.         }
  70.         else if (oCurrentNode.tagName == "LI") {
  71.             return fnRemoveWhiteSpaces(oCurrentNode.firstChild.innerText);
  72.  
  73.         }
  74.         else {
  75.             return null;
  76.         };
  77.     }
  78.     else {
  79.         return null;
  80.     };
  81. };
  82.  
  83. function FireSelectionChangeEvent() {
  84.            var oEvent = createEventObject();
  85.  
  86.         selChangeID.fire(oEvent);
  87. };
  88.  
  89. function FocusNode(someNode) {
  90.     someNode.IsFocused = true;
  91.     someNode.className = "FolderNameFocused";
  92. };
  93.  
  94. function UnFocusNode(someNode) {
  95.     someNode.IsFocused = false;
  96.     someNode.className = "FolderNameNotFocused";
  97. };
  98.  
  99. function ExpandNode(someNode) {
  100.     if (fnStringToBoolean(someNode.IsExpandable)) {
  101.         someNode.IsExpanded = true;
  102.         someNode.className = "FolderExpanded";
  103.     };
  104. };
  105. function CollapseNode(someNode) {
  106.     if (fnStringToBoolean(someNode.IsExpandable)) {
  107.         someNode.IsExpanded = false;
  108.         someNode.className = "FolderCollapsed";
  109.     };
  110. };
  111.  
  112. function ExpandToNode(someNode) {
  113.     var oNode = someNode.parentElement;
  114.  
  115.     while (oNode != null) {
  116.         if (oNode.tagName == "LI") {
  117.             ExpandNode(oNode);
  118.         };
  119.  
  120.         oNode = oNode.parentElement;
  121.     };
  122. };
  123.  
  124. function CollapseFromNode(someNode) {
  125.     var i = 0;
  126.     if (someNode.tagName != "LI" &&
  127.         someNode.tagName != "UL") {
  128.  
  129.         return;
  130.     };
  131.     
  132.     if (someNode.tagName == "LI") {
  133.         CollapseNode(someNode);
  134.     };
  135.  
  136.     if (someNode.children.length > 0) {
  137.         for (i = 0; i < someNode.children.length; i++) {
  138.             if (someNode.children.item(i).tagName == "UL" ||
  139.                 someNode.children.item(i).tagName == "LI"
  140.                 ) {
  141.             
  142.                 CollapseFromNode(someNode.children.item(i));
  143.             };
  144.         };
  145.     };
  146. };
  147.  
  148. function Initialize() {
  149.  
  150.     var oListItems = this.element.getElementsByTagName("LI");
  151.     var i = 0;
  152.     for (i = 0; i < oListItems.length; i++) {
  153.         if ( ! fnStringToBoolean(oListItems[i].IsExpandable)) {
  154.             oListItems[i].className = "FolderNotExpandable";
  155.         };
  156.         if (fnStringToBoolean(oListItems[i].IsExpanded)) {
  157.             ExpandToNode(oListItems[i]);
  158.         };
  159.     };
  160.  
  161.     var oLabelItems = this.element.getElementsByTagName("NOBR");
  162.     for (i = 0; i < oLabelItems.length; i++) {
  163.         if (fnStringToBoolean(oLabelItems[i].IsFocused)) {
  164.             if (oCurrentNode != null) {
  165.                 //oCurrentNode.IsFocused = false;
  166.                 UnFocusNode(oCurrentNode);
  167.             };
  168.             oCurrentNode = oLabelItems[i];
  169.             ExpandToNode(oCurrentNode.parentElement);
  170.             FocusNode(oCurrentNode);
  171.                         ScrollToNode(oCurrentNode);
  172.         };
  173.     };
  174.  
  175.     if (oCurrentNode == null) {
  176.         oCurrentNode = this.element.firstChild.firstChild;
  177.         ExpandToNode(oCurrentNode.parentElement);
  178.         FocusNode(oCurrentNode);
  179.         ScrollToNode(oCurrentNode);
  180.     };
  181.  
  182.     FireSelectionChangeEvent();
  183.     
  184.     return true;
  185. };
  186.  
  187. function OnSelectStart() {
  188.     event.cancelBubble= true;
  189.     event.returnValue = false;
  190.     return false;
  191. };
  192.  
  193. function OnClick() {
  194.     if ( (event == null) || (event.srcElement == null) ) {
  195.         return ;
  196.     };
  197.  
  198.     var oldID = null;
  199.  
  200.     event.cancelBubble= true;
  201.     event.returnValue = false;
  202.     var oNode = null;
  203.     
  204.     var bDoExpandCollapse = false;
  205.     
  206.     if (event.srcElement.tagName == "UL") {
  207.         return;
  208.     };
  209.  
  210.     if (    ( event.srcElement.tagName == "LI" ) /*&& 
  211.         ( event.srcElement.className == "Folder")*/
  212.        ) {
  213.         oNode = event.srcElement;
  214.         
  215.         var rcLI = oNode.getBoundingClientRect();
  216.         var rcLabel = oNode.firstChild.getBoundingClientRect();
  217.  
  218.         if (event.offsetX > oNode.firstChild.offsetLeft + oNode.firstChild.offsetWidth)
  219.             return;
  220.         if (event.offsetY > oNode.firstChild.offsetTop + oNode.firstChild.offsetHeight)
  221.             return;
  222.         
  223.         rcLabel.left = rcLI.left;
  224.         //event.
  225.         
  226.         bDoExpandCollapse = fnStringToBoolean(oNode.IsExpandable);
  227.     };
  228.  
  229.  
  230.     if (    ( event.srcElement.tagName == "NOBR" ) && 
  231.         ( event.srcElement.parentElement.tagName == "LI" ) /*&&
  232.             ( event.srcElement.parentElement.className == "Folder")*/
  233.        ) {
  234.            
  235.         oNode = event.srcElement.parentElement;
  236.         
  237.         bDoExpandCollapse = false;
  238.     };
  239.  
  240. //    UnfocusAll(this.element);
  241.     
  242.     if (oCurrentNode != null) {
  243.         //oCurrentNode.IsFocused = false;
  244.         UnFocusNode(oCurrentNode);
  245.         oldID = oCurrentNode.parentElement.UIN;
  246.         oCurrentNode = null;
  247.  
  248.     };
  249. /*
  250.     var oFocused = this.element.document.all("Focused");
  251.     if (oFocused != null) {
  252.         try {
  253.             oFocused.id = "NotFocused";
  254.             oFocused.IsFocused = 0;
  255.         }
  256.         catch (err) {
  257.             try {
  258.                 while (oFocused.length) {
  259.                     oFocused.item(0).IsFocused = 0;
  260.                     oFocused.item(0).id = "NotFocused";
  261.                 };
  262.     //
  263.     //            for (i = 0; i < oFocused.length; i++) {
  264.     //                //oFocused.item(i).id = "NotFocused";
  265.     //                oFocused.item(i).IsFocused = 0;
  266.     //            };
  267.     //
  268.             }
  269.             catch (err) {
  270.             };
  271.         };
  272.     }
  273.     else {
  274.     };
  275. */    
  276.     
  277.     if (oNode != null && bDoExpandCollapse) {
  278.                 
  279.         if (fnStringToBoolean(oNode.IsExpanded) == false) {
  280.                     //oNode.className = "Open";
  281.                     //oNode.IsExpanded = true;
  282.                         ExpandNode(oNode);
  283.         }
  284.         else if (fnStringToBoolean(oNode.IsExpanded) == true) {
  285.                     //oNode.className = "Closed";
  286.                     //oNode.IsExpanded = false;
  287.  
  288.             CollapseNode(oNode);
  289.                     CollapseFromNode(oNode);
  290.         };
  291.     }
  292.  
  293.     if (oNode != null) {
  294.         if (oNode.tagName == "LI")
  295.             oNode = oNode.firstChild;
  296.         
  297.         //oNode.id = "Focused";
  298.         FocusNode(oNode);
  299.                 ScrollToNode(oNode);
  300.         //oNode.IsFocused = true;
  301.         oCurrentNode = oNode;
  302.  
  303.     };
  304.     
  305.     //this.element.document.recalc(true);
  306.  
  307.     if (oCurrentNode != null && oldID != null //&& oldID != ""
  308.         && oCurrentNode.parentNode.UIN != oldID) {
  309.  
  310.         FireSelectionChangeEvent();
  311.     };
  312.  
  313.     return false;
  314. };
  315. function GetForegroundColor () {
  316.     if (Expanded) {
  317.         return "white";
  318.     }
  319.     else {
  320.         return "black";
  321.     };
  322. };
  323.  
  324. function GetBackgroundColor () {
  325.     if (Expanded) {
  326.         return "blue";
  327.     }
  328.     else {
  329.         return "white";
  330.     };
  331. };
  332.  
  333. function ScrollToNode(someNode1) {
  334. //alert("ScrollToNode");
  335.     var oNode = someNode1;
  336.     if (oNode.tagName == "LI")
  337.         oNode = oNode.firstChild;
  338.  
  339.     var offTop    = oNode.offsetTop;
  340.     var offHeight    = oNode.offsetHeight;
  341.     var offLeft    = oNode.offsetLeft;
  342.     var offWidth    = oNode.offsetWidth;
  343.  
  344.         var offParent = oNode.offsetParent;
  345.  
  346.  
  347.     if (offParent.scrollTop > offTop) {
  348.  
  349.         //someNode.scrollIntoView(true);
  350.         offParent.scrollTop = offTop;
  351.     }
  352.     else if (offParent.scrollTop +
  353.         offParent.clientHeight  <
  354.         offTop + offHeight) {
  355.  
  356.                 offParent.scrollTop = offTop +
  357.                     offHeight - offParent.clientHeight;
  358.                 //someNode.scrollIntoView(false);
  359.     };
  360.  
  361.     if (offParent.scrollLeft > offLeft) {
  362.  
  363.         //someNode.scrollIntoView(true);
  364.                 offParent.scrollLeft = offLeft;
  365.     }
  366.     else if (offParent.scrollLeft +
  367.         offParent.clientWidth <
  368.         offLeft + offWidth) {
  369.  
  370.                 //someNode.scrollIntoView(false);
  371.                 offParent.scrollLeft = offLeft - 5;/* +
  372.                     offWidth - offParent.clientWidth*/;
  373.  
  374.     };
  375.  
  376.  
  377. };
  378.  
  379. function FindNodeByUIN(someUIN) {
  380.  
  381.     var oListItems = this.element.getElementsByTagName("LI");
  382.     var i = 0;
  383.  
  384.     for (i = 0; i < oListItems.length; i++) {
  385.         if (oListItems[i].UIN == someUIN)
  386.             return oListItems[i];
  387.     };
  388.  
  389.     return null;
  390.  
  391. };
  392.  
  393. function CreateNewNode(someParentFolderUIN,
  394.             someNewNodeName, someNewNodeUIN) {
  395.     
  396.  
  397.     var oParentNode = FindNodeByUIN(someParentFolderUIN);
  398.  
  399.     if (oParentNode == null) {
  400.         return false;
  401.     };
  402.  
  403.     var oUL = oParentNode.children.item("SubTree");//getElementsByTagName("UL");
  404.  
  405.     if (oUL == null || oUL.length == 0) {
  406.         oUL = this.element.document.createElement("UL");
  407.         oParentNode.appendChild(oUL);
  408.         oUL.className = oUL.name = oUL.id = "SubTree";
  409.  
  410.         //oUL = oParentNode.getElementsByTagName("UL");
  411.     }
  412.  
  413.     var oLI = oUL.document.createElement("LI");
  414.     oUL.appendChild(oLI);
  415.     oLI.className = "FolderNotExpandable";
  416.     oLI.IsExpanded = "false";
  417.     oLI.IsExpandable = 0;
  418.     oLI.UIN = someNewNodeUIN;
  419.  
  420.     var oNOBR = oUL.document.createElement("NOBR");
  421.     oLI.appendChild(oNOBR);
  422.     oNOBR.className = "FolderNameNotFocused";
  423.     oNOBR.name="FolderName";
  424.     oNOBR.IsFocused = "false";
  425.     oNOBR.innerText = someNewNodeName;
  426.  
  427.  
  428.     oParentNode.IsExpandable = -1;
  429.     ExpandNode(oParentNode);
  430.     ScrollToNode(oNOBR);
  431.  
  432.     return true;
  433.  
  434.  
  435. };
  436. function RenameNode(someNodeUIN, someNewNodeName) {
  437.     var oNode = FindNodeByUIN(someNodeUIN);
  438.  
  439.     if (oNode == null) {
  440.         return false;
  441.     };
  442.  
  443.     oNode.firstChild.innerText = someNewNodeName;
  444.  
  445.     return true;
  446.  
  447.  
  448.     
  449. };
  450.  
  451. function RemoveNode(someNodeUIN, bReSelectNode) {
  452.     var oNode = FindNodeByUIN(someNodeUIN);
  453.  
  454.     if (oNode == null) {
  455.         return null;
  456.     };
  457.  
  458.     if (oNode.UIN == oCurrentNode.parentElement.UIN) {
  459.         UnFocusNode(oNode);
  460.         oCurrentNode = null;
  461.     };
  462.  
  463.     var oParentUL = oNode.parentElement;
  464.     var oParentLI = oParentUL.parentElement;
  465.     var oNodeToFocus = oNode.nextSibling;
  466.     if (oNodeToFocus == null) {
  467.         oNodeToFocus = oNode.previousSibling;
  468.     }
  469.  
  470.     oParentUL.removeChild(oNode);
  471.     //oNode.firstChild.innerText = someNewNodeName;
  472.     
  473.     var oSubNodes = oParentUL.getElementsByTagName("LI");
  474.  
  475.     if (oSubNodes == null || oSubNodes.length == 0) {
  476.         // Removed node was the last child.
  477.         oParentLI.removeChild(oParentUL);
  478.         
  479.         oParentLI.className = "FolderNotExpandable";
  480.         oParentLI.IsExpanded = "false";
  481.         oParentLI.IsExpandable = 0;
  482.  
  483.                 oNodeToFocus = oParentLI;
  484.  
  485.     }
  486.     else {
  487.         // There still are other subnodes.
  488.  
  489.     };
  490.  
  491.     if (bReSelectNode) {
  492.         FocusNode(oNodeToFocus.firstChild);
  493.         oCurrentNode = oNodeToFocus.firstChild;
  494.                 ScrollToNode(oCurrentNode);
  495.         FireSelectionChangeEvent();
  496.     };
  497.  
  498.     return oNodeToFocus;
  499. };
  500.  
  501. function MoveNode(someDstNodeUIN, someSrcNodeUIN) {
  502.  
  503.     var oSrcNode = FindNodeByUIN(someSrcNodeUIN);
  504.     var oDstNode = FindNodeByUIN(someDstNodeUIN);
  505.  
  506.  
  507.     if (oSrcNode == null || oDstNode == null ||
  508.         someSrcNodeUIN == someDstNodeUIN) {
  509.  
  510.         return null;
  511.     };
  512.  
  513.     if (oSrcNode.UIN == oCurrentNode.parentElement.UIN) {
  514.         UnFocusNode(oSrcNode.firstChild);
  515.         oCurrentNode = null;
  516.     };
  517.  
  518.     var oSrcParentUL = oSrcNode.parentElement;
  519.     var oDstChildUL  = oDstNode.children.item("SubTree");
  520.  
  521.     var oSrcParentLI = oSrcParentUL.parentElement;
  522.  
  523.     var oSrcSubNodes = oSrcParentUL.getElementsByTagName("LI");
  524.  
  525.     if (oDstChildUL == null) {
  526.         oDstChildUL = this.element.document.createElement("UL");
  527.         oDstNode.appendChild(oDstChildUL);
  528.         oDstChildUL.id = oDstChildUL.name =
  529.             oDstChildUL.className = "SubTree";
  530.         
  531.         oDstNode.IsExpandable = true;
  532.         oDstNode.className = "FolderNotExpanded";
  533.         oDstNode.IsExpanded = "false";
  534.     };
  535.  
  536.     oDstChildUL.appendChild(oSrcNode = oSrcParentUL.removeChild(oSrcNode));
  537.  
  538.     if (oSrcSubNodes == null || oSrcSubNodes.length == 0) {
  539.         // Removed node was the last child.
  540.         oSrcParentLI.removeChild(oSrcParentUL);
  541.  
  542.         oSrcParentLI.className = "FolderNotExpandable";
  543.         oSrcParentLI.IsExpanded = "false";
  544.         oSrcParentLI.IsExpandable = 0;
  545.     }
  546.     else {
  547.         // There still are other subnodes.
  548.     };
  549.  
  550.  
  551.     //if (bReSelectNode) {
  552.         ExpandToNode(oSrcNode);
  553.         FocusNode(oSrcNode.firstChild);
  554.         oCurrentNode = oSrcNode.firstChild;
  555.                 ScrollToNode(oCurrentNode);
  556.         //FireSelectionChangeEvent();
  557.     //};
  558.  
  559.     return oCurrentNode;//oNodeToFocus;
  560. };
  561.  
  562. function IsChild(someParentUIN, someChildUIN) {
  563.     
  564.     var oParentNode = FindNodeByUIN(someParentUIN);
  565.     var oChildNode = FindNodeByUIN(someChildUIN);
  566.  
  567.  
  568.     if (oParentNode == null) {
  569.         return false;
  570.     };
  571.     if (someParentUIN == someChildUIN) {
  572.         return false;
  573.     };
  574.  
  575.     var oListItems = oParentNode.getElementsByTagName("LI");
  576.     var i = 0;
  577.  
  578.     for (i = 0; i < oListItems.length; i++) {
  579.         if (oListItems[i].UIN == someChildUIN)
  580.             return true;
  581.     };
  582.  
  583.     return false;
  584. };
  585.  
  586. </SCRIPT>
  587. </PUBLIC:COMPONENT>
  588.  
  589.