From 5c81ced09fdf7c5a44e04415a845f2283456eb0b Mon Sep 17 00:00:00 2001 From: chriskl Date: Mon, 2 Feb 2004 06:32:18 +0000 Subject: [PATCH] update to TreeMenu 1.1.9 --- classes/HTML_TreeMenu/TreeMenu.js | 98 ++++++++++------ classes/HTML_TreeMenu/TreeMenu.php | 174 ++++++++++++++++++----------- 2 files changed, 172 insertions(+), 100 deletions(-) diff --git a/classes/HTML_TreeMenu/TreeMenu.js b/classes/HTML_TreeMenu/TreeMenu.js index 1986696c..7a7d73d5 100755 --- a/classes/HTML_TreeMenu/TreeMenu.js +++ b/classes/HTML_TreeMenu/TreeMenu.js @@ -32,8 +32,26 @@ // | Harald Radi | // +-----------------------------------------------------------------------+ // -// $Id: TreeMenu.js,v 1.4 2004/01/04 08:13:41 chriskl Exp $ +// $Id: TreeMenu.js,v 1.5 2004/02/02 06:32:18 chriskl Exp $ +/** +* Function to create copies of objects which are +* normally passed around by references (Arrays for example) +*/ +function arrayCopy(input) +{ + var output = new Array(input.length); + + for (i in input) { + if (typeof(input[i]) == 'array') { + output[i] = arrayCopy(input[i]); + } else { + output[i] = input[i]; + } + } + + return output; +} /** * TreeMenu class @@ -48,7 +66,8 @@ this.usePersistence = usePersistence; this.noTopLevelImages = noTopLevelImages; this.n = new Array(); - + this.output = ''; + this.nodeRefs = new Array(); this.branches = new Array(); this.branchStatus = new Array(); @@ -114,14 +133,14 @@ var expanded = arguments[3] ? arguments[3] : false; var visibility = arguments[4] ? arguments[4] : 'inline'; var parentLayerID = arguments[5] ? arguments[5] : null; - + var currentlevel = level.length; - + for (var i=0; i', this.iconpath, nodes[i].icon, layerID) : ''; + var gifname = nodes[i].n.length && this.doesMenu() && nodes[i].isDynamic ? (expanded ? 'minus' : 'plus') : 'branch'; + var iconName = expanded && nodes[i].expandedIcon ? nodes[i].expandedIcon : nodes[i].icon; + var iconimg = nodes[i].icon ? this.stringFormat('', this.iconpath, iconName, layerID) : ''; /** * Add event handlers @@ -197,16 +217,17 @@ * IMPORTANT: * document.write()ing the string: '
', layerID, visibility, (nodes[i].cssClass ? nodes[i].cssClass : this.defaultClass)) : this.stringFormat('
', nodes[i].cssClass ? nodes[i].cssClass : this.defaultClass); - var onMDown = this.doesMenu() && nodes[i].n.length && nodes[i].isDynamic ? this.stringFormat('onmousedown="{0}.toggleBranch(\'{1}\', true)" style="cursor: pointer; cursor: hand"', this.myname, layerID) : ''; - var imgTag = this.stringFormat('', this.iconpath, gifname, modifier, layerID, onMDown); - var linkTarget = nodes[i].linkTarget ? nodes[i].linkTarget : this.linkTarget; - var linkStart = nodes[i].link ? this.stringFormat('', nodes[i].link, linkTarget) : ''; - var linkEnd = nodes[i].link ? '' : ''; - var browseStart = nodes[i].iconLink ? '' : ''; - var browseEnd = nodes[i].iconLink ? '' : ''; - - output = this.stringFormat('{0}{1}{2}{3}{4}{5}{6}{8}{9}
', + var layerTag = this.doesMenu() ? this.stringFormat('
', layerID, visibility, (nodes[i].cssClass ? nodes[i].cssClass : this.defaultClass)) : this.stringFormat('
', nodes[i].cssClass ? nodes[i].cssClass : this.defaultClass); + var onMDown = this.doesMenu() && nodes[i].n.length && nodes[i].isDynamic ? this.stringFormat('onmousedown="{0}.toggleBranch(\'{1}\', true)" style="cursor: pointer; cursor: hand"', this.myname, layerID) : ''; + var imgTag = this.stringFormat('', this.iconpath, gifname, modifier, layerID, onMDown); + var linkTarget= nodes[i].linkTarget ? nodes[i].linkTarget : this.linkTarget; + var linkStart = nodes[i].link ? this.stringFormat('', nodes[i].link, linkTarget) : ''; + + var linkEnd = nodes[i].link ? '' : ''; + var browseStart = nodes[i].iconLink ? '' : ''; + var browseEnd = nodes[i].iconLink ? '' : ''; + + this.output += this.stringFormat('{0}{1}{2}{3}{4}{5}{6}{8}{9}
', layerTag, prepend, parentLayerID == null && (nodes.length == 1 || this.noTopLevelImages) ? '' : imgTag, @@ -217,14 +238,6 @@ eventHandlers, nodes[i].title, linkEnd); - - /** - * Write out the HTML. Uses document.write for speed over layers and - * innerHTML. This however means no dynamic adding/removing nodes on - * the client side. This could be conditional I guess if dynamic - * adding/removing is required. - */ - document.write(output + "\r\n"); /** * Traverse sub nodes ? @@ -244,9 +257,9 @@ } else { var newPrepend = prepend + this.stringFormat('', this.iconpath); } - + this.drawMenu(nodes[i].n, - level, + arrayCopy(level), newPrepend, nodes[i].expanded, expanded ? 'inline' : 'none', @@ -255,6 +268,14 @@ } } +/** +* Writes the output generated by drawMenu() to the page +*/ + TreeMenu.prototype.writeOutput = function () + { + document.write(this.output); + } + /** * Toggles a branches visible status. Called from resetBranches() * and also when a +/- graphic is clicked. @@ -264,9 +285,9 @@ var currentDisplay = this.getLayer(layerID).style.display; var newDisplay = (this.branchStatus[layerID] && currentDisplay == 'inline') ? 'none' : 'inline'; var fireEvents = arguments[2] != null ? arguments[2] : true; - + for (var i=0; i end hide JavaScript diff --git a/classes/HTML_TreeMenu/TreeMenu.php b/classes/HTML_TreeMenu/TreeMenu.php index 93cfa9e9..17314957 100755 --- a/classes/HTML_TreeMenu/TreeMenu.php +++ b/classes/HTML_TreeMenu/TreeMenu.php @@ -33,7 +33,7 @@ // | Harald Radi | // +-----------------------------------------------------------------------+ // -// $Id: TreeMenu.php,v 1.3 2004/01/04 08:13:41 chriskl Exp $ +// $Id: TreeMenu.php,v 1.4 2004/02/02 06:32:18 chriskl Exp $ /** * HTML_TreeMenu Class @@ -100,7 +100,7 @@ class HTML_TreeMenu * are Wolfram Kriesings' PEAR Tree class, and Richard Heyes' (me!) * Tree class (available here: http://www.phpguru.org/). This * method is intended to be used statically, eg: - * $treeMenu = &HTML_TreeMenu::import($myTreeStructureObj); + * $treeMenu = &HTML_TreeMenu::createFromStructure($myTreeStructureObj); * * @param array $params An array of parameters that determine * how the import happens. This can consist of: @@ -171,7 +171,38 @@ class HTML_TreeMenu break; /** - * Richard Heyes' (me!) Tree class + * Richard Heyes' (me!) second (array based) Tree class + */ + case 'heyes_array': + // Need to create a HTML_TreeMenu object ? + if (!isset($params['treeMenu'])) { + $treeMenu = &new HTML_TreeMenu(); + $parentID = 0; + } else { + $treeMenu = &$params['treeMenu']; + $parentID = $params['parentID']; + } + + // Loop thru the trees nodes + foreach ($params['structure']->getChildren($parentID) as $nodeID) { + $data = $params['structure']->getData($nodeID); + $parentNode = &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions'], $data))); + + // Recurse ? + if ($params['structure']->hasChildren($nodeID)) { + $recurseParams['type'] = 'heyes_array'; + $recurseParams['parentID'] = $nodeID; + $recurseParams['nodeOptions'] = $params['nodeOptions']; + $recurseParams['structure'] = &$params['structure']; + $recurseParams['treeMenu'] = &$parentNode; + HTML_TreeMenu::createFromStructure($recurseParams); + } + } + + break; + + /** + * Richard Heyes' (me!) original OO based Tree class */ case 'heyes': default: @@ -201,55 +232,55 @@ class HTML_TreeMenu return $treeMenu; } - - /** + + /** * Creates a treeMenu from XML. The structure of your XML should be - * like so: - * - * - * - * - * - * - * - * - * - * Any of the options you can supply to the HTML_TreeNode constructor can be supplied as - * attributes to the tag. If there are no subnodes for a particular node, you can - * use the XML shortcut instead of . The $xml argument can - * be either the XML as a string, or an pre-created XML_Tree object. Also, this method - * REQUIRES my own Tree class to work (http://phpguru.org/tree.html). If this has not - * been include()ed or require()ed this method will die(). - * - * @param mixed $xml This can be either a string containing the XML, or an XML_Tree object - * (the PEAR::XML_Tree package). - * @return object The HTML_TreeMenu object - */ - function createFromXML($xml) - { - if (!class_exists('Tree')) { - die('Could not find Tree class'); - } - - // Supplied $xml is a string - if (is_string($xml)) { - require_once('XML/Tree.php'); - $xmlTree = &new XML_Tree(); - $xmlTree->getTreeFromString($xml); - - // Supplied $xml is an XML_Tree object - } else { - $xmlTree = $xml; - } - - // Now process the XML_Tree object, setting the XML attributes - // to be the tag data (with out the XML tag name or contents). - $treeStructure = Tree::createFromXMLTree($xmlTree, true); - $treeStructure->nodes->traverse(create_function('&$node', '$tagData = $node->getTag(); $node->setTag($tagData["attributes"]);')); - - - return HTML_TreeMenu::createFromStructure(array('structure' => $treeStructure)); - } + * like so: + * + * + * + * + * + * + * + * + * + * Any of the options you can supply to the HTML_TreeNode constructor can be supplied as + * attributes to the tag. If there are no subnodes for a particular node, you can + * use the XML shortcut instead of . The $xml argument can + * be either the XML as a string, or an pre-created XML_Tree object. Also, this method + * REQUIRES my own Tree class to work (http://phpguru.org/tree.html). If this has not + * been include()ed or require()ed this method will die(). + * + * @param mixed $xml This can be either a string containing the XML, or an XML_Tree object + * (the PEAR::XML_Tree package). + * @return object The HTML_TreeMenu object + */ + function createFromXML($xml) + { + if (!class_exists('Tree')) { + die('Could not find Tree class'); + } + + // Supplied $xml is a string + if (is_string($xml)) { + require_once('XML/Tree.php'); + $xmlTree = &new XML_Tree(); + $xmlTree->getTreeFromString($xml); + + // Supplied $xml is an XML_Tree object + } else { + $xmlTree = $xml; + } + + // Now process the XML_Tree object, setting the XML attributes + // to be the tag data (with out the XML tag name or contents). + $treeStructure = Tree::createFromXMLTree($xmlTree, true); + $treeStructure->nodes->traverse(create_function('&$node', '$tagData = $node->getTag(); $node->setTag($tagData["attributes"]);')); + + + return HTML_TreeMenu::createFromStructure(array('structure' => $treeStructure)); + } } // HTML_TreeMenu @@ -354,11 +385,13 @@ class HTML_TreeNode * o link The link for the node, defaults to blank * o icon The icon for the node, defaults to blank * o expandedIcon The icon to show when the node is expanded - * o class The CSS class for this node, defaults to blank + * o cssClass The CSS class for this node, defaults to blank * o expanded The default expanded status of this node, defaults to false * This doesn't affect non dynamic presentation types * o linkTarget Target for the links. Defaults to linkTarget of the * HTML_TreeMenu_Presentation. + * o iconLink Target for the icon link. Defaults to iconLink of the + * HTML_TreeMenu_Presentation. * o isDynamic If this node is dynamic or not. Only affects * certain presentation types. * o ensureVisible If true this node will be made visible despite the expanded @@ -380,7 +413,7 @@ class HTML_TreeNode $this->isDynamic = true; $this->ensureVisible = false; $this->linkTarget = null; - $this->iconLink = ''; + $this->iconLink = null; $this->parent = null; $this->events = $events; @@ -552,6 +585,9 @@ class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation * is achieved using cookies. Default is true. * o noTopLevelImages - Whether to skip displaying the first level of images if * there is multiple top level branches. + * o maxDepth - The maximum depth of indentation. Useful for ensuring + * deeply nested trees don't go way off to the right of your + * page etc. Defaults to no limit. * * And also a boolean for whether the entire tree is dynamic or not. * This overrides any perNode dynamic settings. @@ -567,6 +603,7 @@ class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation // Defaults $this->images = 'images'; + $this->maxDepth = 0; // No limit $this->linkTarget = '_self'; $this->defaultClass = ''; $this->usePersistence = true; @@ -588,7 +625,7 @@ class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation function toHTML() { static $count = 0; - $menuObj = 'objTreeMenu_' . ++$count; + $menuObj = 'objTreeMenu_' . ++$count; $html = "\n"; $html .= '