cvs: pear /HTML_TreeMenu TreeMenu.js TreeMenu.php /HTML_TreeMenu/docs example.php /HTML_TreeMenu/images branch.gif branchbottom.gif branchtop.gif
folder.gif line.gif linebottom.gif minus.gif minusbottom.gif minustop.gif plus.gif plusbottom.gif plustop.gif /HTML_TreeMenu/imagesAlt branch.gif
branchbottom.gif branchtop.gif folder.gif line.gif linebottom.gif minus.gif minusbottom.gif minustop.gif plus.gif plusbottom.gif plustop.gif

From: Date: Sat, 15 Jun 2002 18:24:07 +0000
Subject: cvs: pear /HTML_TreeMenu TreeMenu.js TreeMenu.php /HTML_TreeMenu/docs example.php /HTML_TreeMenu/images branch.gif branchbottom.gif branchtop.gif
folder.gif line.gif linebottom.gif minus.gif minusbottom.gif minustop.gif plus.gif plusbottom.gif plustop.gif /HTML_TreeMenu/imagesAlt branch.gif
branchbottom.gif branchtop.gif folder.gif line.gif linebottom.gif minus.gif minusbottom.gif minustop.gif plus.gif plusbottom.gif plustop.gif
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4632@lists.php.net to get a copy of this message
richard Sat Jun 15 14:24:07 2002 EDT Added files: /pear/HTML_TreeMenu TreeMenu.js TreeMenu.php /pear/HTML_TreeMenu/docs example.php /pear/HTML_TreeMenu/images branch.gif branchbottom.gif branchtop.gif folder.gif line.gif linebottom.gif minus.gif minusbottom.gif minustop.gif plus.gif plusbottom.gif plustop.gif /pear/HTML_TreeMenu/imagesAlt branch.gif branchbottom.gif branchtop.gif folder.gif line.gif linebottom.gif minus.gif minusbottom.gif minustop.gif plus.gif plusbottom.gif plustop.gif Log: * Initial commit of HTML_TreeMenu

Index: pear/HTML_TreeMenu/TreeMenu.js +++ pear/HTML_TreeMenu/TreeMenu.js // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Richard Heyes <richard@phpguru.org> | // | Harald Radi <harald.radi@nme.at> | // +----------------------------------------------------------------------+ // // $Id: TreeMenu.js,v 1.1 2002/06/15 18:24:06 richard Exp $ function TreeMenu(layer, iconpath, myname, linkTarget) { // Properties this.layer = layer; this.iconpath = iconpath; this.myname = myname; this.linkTarget = linkTarget; this.n = new Array(); this.branches = new Array(); this.branchStatus = new Array(); this.layerRelations = new Array(); // Methods this.drawMenu = drawMenu; this.toggleBranch = toggleBranch; this.doesMenu = doesMenu; this.saveExpandedStatus = saveExpandedStatus; this.loadExpandedStatus = loadExpandedStatus; this.resetBranches = resetBranches; } function TreeNode(title, icon, link, expanded) { this.title = title; this.icon = icon; this.link = link; this.expanded = expanded; this.n = new Array(); } function drawMenu()// OPTIONAL ARGS: nodes = [], level = [], prepend = '', expanded = false, visbility = 'inline', parentLayerID = null { /** * Necessary variables */ var output = ''; var modifier = ''; var layerID = ''; var parentLayerID = ''; /** * Parse any optional arguments */ var nodes = arguments[0] ? arguments[0] : this.n var level = arguments[1] ? arguments[1] : []; var prepend = arguments[2] ? arguments[2] : ''; 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<nodes.length; i++) { level[currentlevel] = i+1; layerID = 'node_' + implode('_', level); /** * Gif modifier */ if (i == 0 && parentLayerID == null) { modifier = nodes.length > 1 ? "top" : 'single'; } else if(i == (nodes.length-1)) { modifier = "bottom"; } else { modifier = ""; } /** * Single root branch is always expanded */ if (!doesMenu() || (parentLayerID == null && nodes.length == 1)) { expanded = true; } /** * Setup branch status and build an indexed array * of branch layer ids */ if (nodes[i].n.length > 1) { this.branchStatus[layerID] = expanded; this.branches[this.branches.length] = layerID; } /** * Setup toggle relationship */ if (!this.layerRelations[parentLayerID]) { this.layerRelations[parentLayerID] = new Array(); } this.layerRelations[parentLayerID][this.layerRelations[parentLayerID].length] = layerID; /** * Branch images */ var gifname = nodes[i].n.length && this.doesMenu() ? (expanded ? 'minus' : 'plus') : 'branch'; var iconimg = nodes[i].icon ? sprintf('<img src="%s/%s" align="top">', this.iconpath, nodes[i].icon) : ''; /** * Build the html to write to the document */ var divTag = sprintf('<div id="%s" style="display: %s; behavior: url(#default#userdata)">', layerID, visibility); var onClick = doesMenu() && nodes[i].n.length ? sprintf('onclick="%s.toggleBranch(\'%s\', true)" style="cursor: hand"', this.myname, layerID) : ''; var imgTag = sprintf('<img src="%s/%s%s.gif" align="top" border="0" name="img_%s" %s />', this.iconpath, gifname, modifier, layerID, onClick); var linkStart = nodes[i].link ? sprintf('<a href="%s" target="%s">', nodes[i].link, this.linkTarget) : ''; var linkEnd = nodes[i].link ? '</a>' : ''; output = sprintf('%s<nobr>%s%s%s%s%s%s</nobr><br></div>', divTag, prepend, parentLayerID == null && nodes.length == 1 ? '' : imgTag, iconimg, linkStart, nodes[i].title, linkEnd); /** * Write out the HTML */ if (doesMenu()) { document.all(this.layer).innerHTML += output; } else if(document.getElementById(this.layer)) { document.getElementById(this.layer).innerHTML += output; } else { document.write(output); } if (nodes[i].n.length) { /** * Determine what to prepend. If there is only one root * node then the prepend to pass to children is nothing. * Otherwise it depends on where we are in the tree. */ if (parentLayerID == null && nodes.length == 1) { var newPrepend = ''; } else if (i < (nodes.length - 1)) { var newPrepend = prepend + sprintf('<img src="%s/line.gif" align="top">', this.iconpath); } else { var newPrepend = prepend + sprintf('<img src="%s/linebottom.gif" align="top">', this.iconpath); } this.drawMenu(nodes[i].n, explode('_', implode('_', level)), // Seemingly necessary to enforce passing by value newPrepend, nodes[i].expanded, expanded ? 'inline' : 'none', layerID); } } } function toggleBranch(layerID, updateStatus) // OPTIONAL ARGS: noSave = false { var currentDisplay = document.all(layerID).style.display; var newDisplay = (this.branchStatus[layerID] && currentDisplay == 'inline') ? 'none' : 'inline' for (var i=0; i<this.layerRelations[layerID].length; i++) { if (this.branchStatus[this.layerRelations[layerID][i]]) { this.toggleBranch(this.layerRelations[layerID][i], false); } document.all(this.layerRelations[layerID][i]).style.display = newDisplay; } if (updateStatus) { this.branchStatus[layerID] = !this.branchStatus[layerID]; /** * Persistence */ if (!arguments[2]) { this.saveExpandedStatus(layerID, this.branchStatus[layerID]); } // Swap image: imgSrc = document.images['img_' + layerID].src; re = /^(.*)(plus|minus)(bottom|top|single)?.gif$/ if (matches = imgSrc.match(re)) { document.images['img_' + layerID].src = sprintf('%s%s%s%s', matches[1], matches[2] == 'plus' ? 'minus' : 'plus', matches[3], '.gif'); } } } /** * Can the browser handle the dynamic menu? */ function doesMenu() { var agt = navigator.userAgent.toLowerCase(); var is_major = parseInt(navigator.appVersion); var is_minor = parseFloat(navigator.appVersion); var is_ie = (agt.indexOf("msie") != -1 && agt.indexOf("opera") == -1); var is_ie3 = (is_ie && (is_major < 4)); var is_ie4 = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) ); var is_ie5up = (is_ie && !is_ie3 && !is_ie4); return is_ie5up; } /** * Save the status of the layer */ function saveExpandedStatus(layerID, expanded) { document.all(layerID).setAttribute("expandedStatus", expanded); document.all(layerID).save(layerID); } /** * Load the status of the layer */ function loadExpandedStatus(layerID) { document.all(layerID).load(layerID); if (val = document.all(layerID).getAttribute("expandedStatus")) { return val; } else { return null; } } /** * Reset branch status */ function resetBranches() { for (var i=0; i<this.branches.length; i++) { var status = this.loadExpandedStatus(this.branches[i]); // Only update if it's supposed to be expanded and it's not already if (status == 'true' && this.branchStatus[this.branches[i]] != true) { this.toggleBranch(this.branches[i], true, true); } } } /** * Javascript mini implementation of sprintf() */ function sprintf(strInput) { var strOutput = ''; var currentArg = 1; for (var i=0; i<strInput.length; i++) { if (strInput.charAt(i) == '%' && i != (strInput.length - 1) && typeof(arguments[currentArg]) != 'undefined') { switch (strInput.charAt(++i)) { case 's': strOutput += arguments[currentArg]; break; case '%': strOutput += '%'; break; } currentArg++; } else { strOutput += strInput.charAt(i); } } return strOutput; } function explode(seperator, input) { var output = []; var tmp = ''; skipEmpty = arguments[2] ? true : false; for (var i=0; i<input.length; i++) { if (input.charAt(i) == seperator) { if (tmp == '' && skipEmpty) { continue; } else { output[output.length] = tmp; tmp = ''; } } else { tmp += input.charAt(i); } } if (tmp != '' || !skipEmpty) { output[output.length] = tmp; } return output; } function implode(seperator, input) { var output = ''; for (var i=0; i<input.length; i++) { if (i == 0) { output += input[i]; } else { output += seperator + input[i]; } } return output; } function in_array(item, arr) { for (var i=0; i<arr.length; i++) { if (arr[i] == item) { return true; } } return false; } Index: pear/HTML_TreeMenu/TreeMenu.php +++ pear/HTML_TreeMenu/TreeMenu.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Richard Heyes <richard@phpguru.org> | // | Harald Radi <harald.radi@nme.at> | // +----------------------------------------------------------------------+ // // $Id: TreeMenu.php,v 1.1 2002/06/15 18:24:06 richard Exp $ /** * HTML_TreeMenu Class * * A simple couple of PHP classes and some not so simple * Jabbascript which produces a tree menu. In IE this menu * is dynamic, with branches being collapsable. In IE5+ the * status of the collapsed/open branches persists across page * refreshes.In any other browser the tree is static. Code is * based on work of Harald Radi. * * Usage. * * After installing the package, copy the example php script to * your servers document. Also place the TreeMenu.js and the images * folder in the same place. Running the script should then produce * the tree. * * @author Richard Heyes <richard@php.net> * @author Harald Radi <harald.radi@nme.at> * @access public * @version 1.0 * @package HTML_TreeMenu */ class HTML_TreeMenu { /** * Indexed array of subnodes * @var array */ var $items; /** * The layer ID * @var string */ var $layer; /** * Path to the images * @var string */ var $images; /** * Name of the object * This should not be changed without changing * the javascript. * @var string */ var $menuobj; /** * Constructor * * @access public * @param string $layer The name of the layer to add the HTML to. * In browsers that do not support document.all * or document.getElementById(), document.write() * is used, and thus this layer name has no effect. * @param string $images The path to the images folder. */ function HTML_TreeMenu($layer, $images, $linkTarget = '_self') { $this->menuobj = 'objTreeMenu'; $this->layer = $layer; $this->images = $images; $this->linkTarget = $linkTarget; } /** * This function adds an item to the the tree. * * @access public * @param object $menu The node to add. This object should be * a HTML_TreeNode object. */ function addItem($menu) { $this->items[] = $menu; } /** * This function prints the menu Jabbascript code. Should * be called *AFTER* your layer tag has been printed. In the * case of older browsers, eg Navigator 4, The menu HTML will * appear where this function is called. * * @access public */ function printMenu() { echo "\n"; echo '<script language="javascript" type="text/javascript">'; echo sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s");', $this->menuobj, $this->layer, $this->images, $this->menuobj, $this->linkTarget); echo "\n"; if (isset($this->items)) { for ($i=0; $i<count($this->items); $i++) { $this->items[$i]->_printMenu($this->menuobj . ".n[$i]"); } } echo sprintf("%s.drawMenu();\n%s.resetBranches();\n</script>", $this->menuobj, $this->menuobj); } } // HTML_TreeMenu /** * HTML_TreeNode class * * This class is supplementary to the above and provides a way to * add nodes to the tree. A node can have other nodes added to it. * * @author Richard Heyes <richard@php.net> * @author Harald Radi <harald.radi@nme.at> * @access public * @version 1.0 * @package HTML_TreeMenu */ class HTML_TreeNode { /** * The text for this node. * @var string */ var $text; /** * The link for this node. * @var string */ var $link; /** * The icon for this node. * @var string */ var $icon; /** * Indexed array of subnodes * @var array */ var $items; /** * Whether this node is expanded or not * @var bool */ var $expanded; /** * Constructor * * @access public * @param string $text The description text for this node * @param string $link The link for the text * @param string $icon Optional icon to appear to the left of the text * @param bool $expanded Whether this node is expanded or not (IE only) */ function HTML_TreeNode($text, $link, $icon = null, $expanded = false) { $this->text = ($text == null) ? "" : $text; $this->link = ($link == null) ? "" : $link; $this->icon = ($icon == null) ? "" : $icon; $this->expanded = $expanded; } /** * Adds a new subnode to this node. * * @access public * @param object $node The new node */ function addItem($node) { $this->items[] = $node; } /** * Prints jabbascript for this particular node. * * @access private * @param string $prefix The jababscript object to assign this node to. */ function _printMenu($prefix) { echo sprintf("\t%s = new TreeNode('%s', %s, %s, %s);\n", $prefix, $this->text, !empty($this->icon) ? "'" . $this->icon . "'" : 'null', !empty($this->link) ? "'" . $this->link . "'" : 'null', $this->expanded ? 'true' : 'false'); if (!empty($this->items)) { for ($i=0; $i<count($this->items); $i++) { $this->items[$i]->_printMenu($prefix . ".n[$i]"); } } } } ?> Index: pear/HTML_TreeMenu/docs/example.php +++ pear/HTML_TreeMenu/docs/example.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Richard Heyes <richard@phpguru.org> | // | Harald Radi <harald.radi@nme.at> | // +----------------------------------------------------------------------+ // // $Id: example.php,v 1.1 2002/06/15 18:24:07 richard Exp $ require_once('HTML/TreeMenu.php'); $icon = 'folder.gif'; $menu = new HTML_TreeMenu("menuLayer", './images'); $node1 = new HTML_TreeNode("INBOX", "test.php", $icon); $node1->addItem(new HTML_TreeNode("deleted-items", "test.php", $icon)); $node1->addItem(new HTML_TreeNode("sent-items", "test.php", $icon)); $node1->addItem(new HTML_TreeNode("drafts", "test.php", $icon)); $menu->addItem($node1); $menu->addItem(new HTML_TreeNode("drafts", "test.php", $icon)); $menu->addItem($node1); ?> <html> <head> <script src="./TreeMenu.js" language="JavaScript" type="text/javascript"></script> </head> <body> <div id="menuLayer"></div> <?$menu->printMenu()?> </body> </html>
« previous php.pear.cvs (#4632) next »