cvs: pear /HTML_TreeMenu TreeMenu.js TreeMenu.php package.xml

From: Date: Sat, 30 Nov 2002 21:53:32 +0000
Subject: cvs: pear /HTML_TreeMenu TreeMenu.js TreeMenu.php package.xml
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-7699@lists.php.net to get a copy of this message
richard Sat Nov 30 16:53:32 2002 EDT Modified files: /pear/HTML_TreeMenu TreeMenu.js TreeMenu.php package.xml Log: Updated to add event support and moved things around slightly

Index: pear/HTML_TreeMenu/TreeMenu.js diff -u pear/HTML_TreeMenu/TreeMenu.js:1.8 pear/HTML_TreeMenu/TreeMenu.js:1.9 --- pear/HTML_TreeMenu/TreeMenu.js:1.8 Sun Nov 10 13:16:19 2002 +++ pear/HTML_TreeMenu/TreeMenu.js Sat Nov 30 16:53:31 2002 @@ -32,22 +32,24 @@ // | Harald Radi <harald.radi@nme.at> | // +-----------------------------------------------------------------------+ // -// $Id: TreeMenu.js,v 1.8 2002/11/10 18:16:19 richard Exp $ +// $Id: TreeMenu.js,v 1.9 2002/11/30 21:53:31 richard Exp $ /** * TreeMenu class */ - function TreeMenu(iconpath, myname, linkTarget, defaultClass, usePersistence) + function TreeMenu(iconpath, myname, linkTarget, defaultClass, usePersistence, noTopLevelImages) { // Properties - this.iconpath = iconpath; - this.myname = myname; - this.linkTarget = linkTarget; - this.defaultClass = defaultClass; - this.usePersistence = usePersistence; - this.n = new Array(); + this.iconpath = iconpath; + this.myname = myname; + this.linkTarget = linkTarget; + this.defaultClass = defaultClass; + this.usePersistence = usePersistence; + this.noTopLevelImages = noTopLevelImages; + this.n = new Array(); + this.nodeRefs = new Array(); this.branches = new Array(); this.branchStatus = new Array(); this.layerRelations = new Array(); @@ -121,6 +123,11 @@ layerID = this.myname + '_' + 'node_' + this.implode('_', level); /** + * Store this object in the nodeRefs array + */ + this.nodeRefs[layerID] = nodes[i]; + + /** * Store the child/parent relationship */ this.childParents[layerID] = parentLayerID; @@ -139,7 +146,7 @@ /** * Single root branch is always expanded */ - if (!this.doesMenu() || (parentLayerID == null && nodes.length == 1)) { + if (!this.doesMenu() || (parentLayerID == null && (nodes.length == 1 || this.noTopLevelImages))) { expanded = true; } else if (nodes[i].expanded) { @@ -177,7 +184,14 @@ var gifname = nodes[i].n.length && this.doesMenu() && nodes[i].isDynamic ? (expanded ? 'minus' : 'plus') : 'branch'; var iconimg = nodes[i].icon ? this.stringFormat('<img src="{0}/{1}" width="20" height="20" align="top">', this.iconpath, nodes[i].icon) : ''; - + /** + * Add event handlers + */ + var eventHandlers = ""; + for (j in nodes[i].events) { + eventHandlers += this.stringFormat('{0}="{1}" ', j, nodes[i].events[j]); + } + /** * Build the html to write to the document * IMPORTANT: @@ -188,13 +202,14 @@ var imgTag = this.stringFormat('<img src="{0}/{1}{2}.gif" width="20" height="20" align="top" border="0" name="img_{3}" {4}>', this.iconpath, gifname, modifier, layerID, onMDown); var linkStart = nodes[i].link ? this.stringFormat('<a href="{0}" target="{1}">', nodes[i].link, this.linkTarget) : ''; var linkEnd = nodes[i].link ? '</a>' : ''; - - output = this.stringFormat('{0}<nobr>{1}{2}{3}{4}{5}{6}</nobr><br></div>', + + output = this.stringFormat('{0}<nobr>{1}{2}{3}{4}<span {5}>{6}</span>{7}</nobr><br></div>', layerTag, prepend, - parentLayerID == null && nodes.length == 1 ? '' : imgTag, + parentLayerID == null && (nodes.length == 1 || this.noTopLevelImages) ? '' : imgTag, iconimg, linkStart, + eventHandlers, nodes[i].title, linkEnd); @@ -215,7 +230,7 @@ * 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) { + if (parentLayerID == null && (nodes.length == 1 || this.noTopLevelImages)) { var newPrepend = ''; } else if (i < (nodes.length - 1)) { @@ -239,10 +254,11 @@ * Toggles a branches visible status. Called from resetBranches() * and also when a +/- graphic is clicked. */ - TreeMenu.prototype.toggleBranch = function (layerID, updateStatus) // OPTIONAL ARGS: noSave = false + TreeMenu.prototype.toggleBranch = function (layerID, updateStatus) // OPTIONAL ARGS: fireEvents = true { var currentDisplay = this.getLayer(layerID).style.display; - var newDisplay = (this.branchStatus[layerID] && currentDisplay == 'inline') ? 'none' : 'inline' + var newDisplay = (this.branchStatus[layerID] && currentDisplay == 'inline') ? 'none' : 'inline'; + var fireEvents = arguments[2] != null ? arguments[2] : true; for (var i=0; i<this.layerRelations[layerID].length; i++) { @@ -262,7 +278,24 @@ if (this.doesPersistence() && !arguments[2] && this.usePersistence) { this.setExpandedStatusForCookie(layerID, this.branchStatus[layerID]); } + + /** + * Fire custom events + */ + if (fireEvents) { + nodeObject = this.nodeRefs[layerID]; + if (nodeObject.ontoggle != null) { + eval(nodeObject.ontoggle); + } + + if (newDisplay == 'none' && nodeObject.oncollapse != null) { + eval(nodeObject.oncollapse); + } else if (newDisplay == 'inline' && nodeObject.onexpand != null){ + eval(nodeObject.onexpand); + } + } + // Swap image this.swapImage(layerID); } @@ -325,14 +358,6 @@ { this.cookieStatuses[layerID] = expanded; this.saveCookie(); -/* - if (is_ie5up) { - document.all(layerID).setAttribute("expandedStatus", expanded); - document.all(layerID).save(layerID); - } else { - - } -*/ } /** @@ -345,14 +370,6 @@ } return false; - -/* document.all(layerID).load(layerID); - if (val = document.all(layerID).getAttribute("expandedStatus")) { - return val; - } else { - return null; - } -*/ } /** @@ -407,7 +424,7 @@ // Only update if it's supposed to be expanded and it's not already if (status == true && this.branchStatus[this.branches[i]] != true) { if (this.checkParentVisibility(this.branches[i])) { - this.toggleBranch(this.branches[i], true, true); + this.toggleBranch(this.branches[i], true, false); } else { this.branchStatus[this.branches[i]] = true; this.swapImage(this.branches[i]); @@ -488,13 +505,18 @@ */ function TreeNode(title, icon, link, expanded, isDynamic, cssClass) { - this.title = title; - this.icon = icon; - this.link = link; - this.expanded = expanded; - this.isDynamic = isDynamic; - this.cssClass = cssClass; - this.n = new Array(); + this.title = title; + this.icon = icon; + this.link = link; + this.expanded = expanded; + this.isDynamic = isDynamic; + this.cssClass = cssClass; + this.n = new Array(); + this.events = new Array(); + this.handlers = null; + this.oncollapse = null; + this.onexpand = null; + this.ontoggle = null; } /** @@ -506,6 +528,29 @@ this.n[newIndex] = newNode; return this.n[newIndex]; + } + +/** +* Sets an event for this particular node +*/ + TreeNode.prototype.setEvent = function (eventName, eventHandler) + { + switch (eventName.toLowerCase()) { + case 'onexpand': + this.onexpand = eventHandler; + break; + + case 'oncollapse': + this.oncollapse = eventHandler; + break; + + case 'ontoggle': + this.ontoggle = eventHandler; + break; + + default: + this.events[eventName] = eventHandler; + } } /** Index: pear/HTML_TreeMenu/TreeMenu.php diff -u pear/HTML_TreeMenu/TreeMenu.php:1.9 pear/HTML_TreeMenu/TreeMenu.php:1.10 --- pear/HTML_TreeMenu/TreeMenu.php:1.9 Sun Nov 10 13:16:19 2002 +++ pear/HTML_TreeMenu/TreeMenu.php Sat Nov 30 16:53:31 2002 @@ -33,7 +33,7 @@ // | Harald Radi <harald.radi@nme.at> | // +-----------------------------------------------------------------------+ // -// $Id: TreeMenu.php,v 1.9 2002/11/10 18:16:19 richard Exp $ +// $Id: TreeMenu.php,v 1.10 2002/11/30 21:53:31 richard Exp $ /** * HTML_TreeMenu Class @@ -70,66 +70,13 @@ var $items; /** - * Path to the images - * @var string - */ - var $images; - - /** - * Target for the links generated - * @var string - */ - var $linkTarget; - - /** - * Whether to use clientside persistence or not - * @var bool - */ - var $userPersistence; - - /** - * The default CSS class for the nodes - */ - var $defaultClass; - - /** * Constructor * * @access public - * @param array $options An array of the various options you can set for the - * treemenu. These are: - * o images The path to the images folder. Defaults to "images" - * o linkTarget The target for the link. Defaults to "_self" - * o usePersistence Whether to use clientside persistence. This persistence - * is achieved using cookies. Default is true. - * o defaultClass The default CSS class to apply to a node. Default is none. - */ - function HTML_TreeMenu($options = array()) - { - $this->images = 'images'; - $this->linkTarget = '_self'; - $this->usePersistence = true; - $this->defaultClass = ''; - - foreach ($options as $option => $value) { - $this->$option = $value; - } - } - - /** - * Allows setting of various parameters after the initial - * constructor call. Possible options you can set are: - * o images - The path to the images to be used - * o linkTarget - The default target for links - * o usePersistence - Whether to use client side persistence or not - * - * @param string $option Option to set - * @param string $value Value to set the option to - * @access public */ - function setOption($option, $value) + function HTML_TreeMenu() { - $this->$option = $value; + // Not much to do here :( } /** @@ -217,6 +164,12 @@ var $parent; /** + * Javascript event handlers; + * @var array + */ + var $events; + + /** * Constructor * * @access public @@ -233,8 +186,12 @@ * o ensureVisible If true this node will be made visible despite the expanded * settings, and client side persistence. Will not affect * some presentation styles, such as Listbox. Default is false + * @param array $events An array of javascript events and the corresponding event handlers. + * Additionally to the standard javascript events you can specify handlers + * for the 'onexpand', 'oncollapse' and 'ontoggle' events which will be fired + * whenever a node is collapsed and/or expanded. */ - function HTML_TreeNode($options = array()) + function HTML_TreeNode($options = array(), $events = array()) { $this->text = ''; $this->link = ''; @@ -245,6 +202,7 @@ $this->ensureVisible = false; $this->parent = null; + $this->events = $events; foreach ($options as $option => $value) { $this->$option = $value; @@ -308,18 +266,16 @@ $this->parent->_ensureVisible(); } } -} +} // HTML_TreeNode /** -* HTML_TreeMenu_DHTML class -* -* This class is a presentation class for the tree structure -* created using the TreeMenu/TreeNode. It presents the -* traditional tree, static for browsers that can't handle -* the DHTML. +* HTML_TreeMenu_Presentation class +* +* Base class for other presentation classes to +* inherit from. */ -class HTML_TreeMenu_DHTML +class HTML_TreeMenu_Presentation { /** * The TreeMenu structure @@ -328,39 +284,115 @@ var $menu; /** - * Dynamic status of the treemenu. If true (default) this has no effect. If - * false it will override all dynamic status vars and set the menu to be - * fully expanded an non-dynamic. + * Base constructor simply sets the menu object + * + * @param object $structure The menu structure */ - var $isDynamic; + function HTML_TreeMenu_Presentation(&$structure) + { + $this->menu = &$structure; + } /** - * Constructor, takes the tree structure as - * an argument and an array of options which - * can consist of: - * o isDynamic Defines menu wide dynamic status - * - * @param object $structure The menu structure - * @param array $options Array of options + * Prints the HTML generated by the toHTML() method. + * toHTML() must therefore be defined by the derived + * class. + * + * @access public + * @param array Options to set. Any options taken by + * the presentation class can be specified + * here. */ - function HTML_TreeMenu_DHTML($structure, $options = array()) + function printMenu($options = array()) { - $this->menu = $structure; - $this->isDynamic = true; - foreach ($options as $option => $value) { $this->$option = $value; } + + echo $this->toHTML(); } +} + +/** +* HTML_TreeMenu_DHTML class +* +* This class is a presentation class for the tree structure +* created using the TreeMenu/TreeNode. It presents the +* traditional tree, static for browsers that can't handle +* the DHTML. +*/ +class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation +{ + /** + * Dynamic status of the treemenu. If true (default) this has no effect. If + * false it will override all dynamic status vars and set the menu to be + * fully expanded an non-dynamic. + */ + var $isDynamic; + + /** + * Path to the images + * @var string + */ + var $images; + + /** + * Target for the links generated + * @var string + */ + var $linkTarget; + + /** + * Whether to use clientside persistence or not + * @var bool + */ + var $userPersistence; + + /** + * The default CSS class for the nodes + */ + var $defaultClass; + + /** + * Whether to skip first level branch images + * @var bool + */ + var $noTopLevelImages; /** - * Prints the HTML generated by the above toHTML() + * Constructor, takes the tree structure as + * an argument and an array of options which + * can consist of: + * o images - The path to the images folder. Defaults to "images" + * o linkTarget - The target for the link. Defaults to "_self" + * o defaultClass - The default CSS class to apply to a node. Default is none. + * o usePersistence - Whether to use clientside persistence. This persistence + * 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. * - * @access public + * And also a boolean for whether the entire tree is dynamic or not. + * This overrides any perNode dynamic settings. + * + * @param object $structure The menu structure + * @param array $options Array of options + * @param bool $isDynamic Whether the tree is dynamic or not */ - function printMenu() + function HTML_TreeMenu_DHTML(&$structure, $options = array(), $isDynamic = true) { - echo $this->toHTML(); + $this->menu = &$structure; + $this->isDynamic = $isDynamic; + + // Defaults + $this->images = 'images'; + $this->linkTarget = '_self'; + $this->defaultClass = ''; + $this->usePersistence = true; + $this->noTopLevelImages = false; + + foreach ($options as $option => $value) { + $this->$option = $value; + } } /** @@ -377,14 +409,15 @@ $menuObj = 'objTreeMenu_' . ++$count; $html = "\n"; - $html .= '<script language="javascript" type="text/javascript">' . "\n\t"; - $html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s);', + $html .= '<script language="javascript" type="text/javascript">' . "\n\t"; + $html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s, %s);', $menuObj, - $this->menu->images, + $this->images, $menuObj, - $this->menu->linkTarget, - $this->menu->defaultClass, - $this->menu->usePersistence ? 'true' : 'false'); + $this->linkTarget, + $this->defaultClass, + $this->usePersistence ? 'true' : 'false', + $this->noTopLevelImages ? 'true' : 'false'); $html .= "\n"; @@ -398,7 +431,7 @@ } $html .= sprintf("\n\t%s.drawMenu();", $menuObj); - if ($this->menu->usePersistence && $this->isDynamic) { + if ($this->usePersistence && $this->isDynamic) { $html .= sprintf("\n\t%s.resetBranches();", $menuObj); } $html .= "\n</script>"; @@ -425,6 +458,13 @@ $isDynamic, $nodeObj->cssClass); + foreach ($nodeObj->events as $event => $handler) { + $html .= sprintf("\t %s.setEvent('%s', '%s');\n", + $return, + $event, + str_replace(array("\r", "\n", "'"), array('\r', '\n', "\'"), $handler)); + } + /** * Loop through subnodes */ @@ -444,15 +484,9 @@ * * This class presents the menu as a listbox */ -class HTML_TreeMenu_Listbox +class HTML_TreeMenu_Listbox extends HTML_TreeMenu_Presentation { /** - * The menu structure - * @var object - */ - var $menu; - - /** * The text that is displayed in the first option * @var string */ @@ -470,6 +504,12 @@ * @var integer */ var $indentNum; + + /** + * Target for the links generated + * @var string + */ + var $linkTarget; /** * Constructor @@ -483,6 +523,8 @@ * Defaults to "&nbsp;" * o indentNum How many of the indentChars to use per indentation level * Defaults to 2 + * o linkTarget Target for the links. Defaults to "_self" + * o submitText Text for the submit button. Defaults to "Go" */ function HTML_TreeMenu_Listbox($structure, $options = array()) { @@ -490,20 +532,14 @@ $this->promoText = 'Select...'; $this->indentChar = '&nbsp;'; $this->indentNum = 2; + $this->linkTarget = '_self'; + $this->submitText = 'Go'; foreach ($options as $option => $value) { $this->$option = $value; } } - - /** - * Prints the listbox - */ - function printMenu() - { - echo $this->toHTML(); - } - + /** * Returns the HTML generated */ @@ -521,12 +557,14 @@ } } - return sprintf('<form onsubmit="var link = this.%s.options[this.%s.selectedIndex].value; if (link) location.href = link; return false"><select name="%s"><option value="">%s</option>%s</select> <input type="submit" value="Go" /></form>', + return sprintf('<form target="%s" action="" onsubmit="var link = this.%s.options[this.%s.selectedIndex].value; if (link) {this.action = link; return true} else return false"><select name="%s"><option value="">%s</option>%s</select> <input type="submit" value="%s" /></form>', + $this->linkTarget, 'HTML_TreeMenu_Listbox_' . ++$count, 'HTML_TreeMenu_Listbox_' . $count, 'HTML_TreeMenu_Listbox_' . $count, $this->promoText, - $nodeHTML); + $nodeHTML, + $this->submitText); } /** Index: pear/HTML_TreeMenu/package.xml diff -u pear/HTML_TreeMenu/package.xml:1.13 pear/HTML_TreeMenu/package.xml:1.14 --- pear/HTML_TreeMenu/package.xml:1.13 Sun Nov 10 13:25:59 2002 +++ pear/HTML_TreeMenu/package.xml Sat Nov 30 16:53:31 2002 @@ -28,12 +28,14 @@ </maintainers> <release> + <license>BSD</license> - <version>1.1.0</version> - <date>2002-11-10</date> + <version>1.1.1</version> + <date>2002-11-30</date> <state>stable</state> <notes> -Many changes. See CVS for changelog +Added event support, and moved various options from the TreeMenu class to +the presentation classes. Added presentation base class. </notes> <filelist> @@ -95,6 +97,16 @@ </release> <changelog> + <release> + <license>BSD</license> + <version>1.1.0</version> + <date>2002-11-10</date> + <state>stable</state> + <notes> + Many changes. See CVS for changelog + </notes> + </release> + <release> <license>BSD</license> <version>1.0.4</version>
« previous php.pear.cvs (#7699) next »