[3526] | 1 | <?php
|
---|
| 2 | // 2002-11-02 cchapin:
|
---|
| 3 | // This version of the TreeMenu includes my HTML_TreeNodeStyle subclass extension,
|
---|
| 4 | // as well as a few other changes. It is based on release version v1.0.4.
|
---|
| 5 |
|
---|
| 6 | // +-----------------------------------------------------------------------+
|
---|
| 7 | // | Copyright (c) 2002, Richard Heyes, Harald Radi |
|
---|
| 8 | // | All rights reserved. |
|
---|
| 9 | // | |
|
---|
| 10 | // | Redistribution and use in source and binary forms, with or without |
|
---|
| 11 | // | modification, are permitted provided that the following conditions |
|
---|
| 12 | // | are met: |
|
---|
| 13 | // | |
|
---|
| 14 | // | o Redistributions of source code must retain the above copyright |
|
---|
| 15 | // | notice, this list of conditions and the following disclaimer. |
|
---|
| 16 | // | o Redistributions in binary form must reproduce the above copyright |
|
---|
| 17 | // | notice, this list of conditions and the following disclaimer in the |
|
---|
| 18 | // | documentation and/or other materials provided with the distribution.|
|
---|
| 19 | // | o The names of the authors may not be used to endorse or promote |
|
---|
| 20 | // | products derived from this software without specific prior written |
|
---|
| 21 | // | permission. |
|
---|
| 22 | // | |
|
---|
| 23 | // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
---|
| 24 | // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
---|
| 25 | // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
---|
| 26 | // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
---|
| 27 | // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
---|
| 28 | // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
---|
| 29 | // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
---|
| 30 | // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
---|
| 31 | // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
---|
| 32 | // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
---|
| 33 | // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
---|
| 34 | // | |
|
---|
| 35 | // +-----------------------------------------------------------------------+
|
---|
| 36 | // | Author: Richard Heyes <richard@phpguru.org> |
|
---|
| 37 | // | Harald Radi <harald.radi@nme.at> |
|
---|
| 38 | // +-----------------------------------------------------------------------+
|
---|
| 39 | //
|
---|
| 40 | // $Id: TreeMenu.php,v 1.7 2002/07/27 12:43:15 richard Exp $
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * HTML_TreeMenu Class
|
---|
| 44 | *
|
---|
| 45 | * A simple couple of PHP classes and some not so simple
|
---|
| 46 | * Jabbascript which produces a tree menu. In IE this menu
|
---|
| 47 | * is dynamic, with branches being collapsable. In IE5+ the
|
---|
| 48 | * status of the collapsed/open branches persists across page
|
---|
| 49 | * refreshes.In any other browser the tree is static. Code is
|
---|
| 50 | * based on work of Harald Radi.
|
---|
| 51 | *
|
---|
| 52 | * Usage.
|
---|
| 53 | *
|
---|
| 54 | * After installing the package, copy the example php script to
|
---|
| 55 | * your servers document root. Also place the TreeMenu.js and the
|
---|
| 56 | * images folder in the same place. Running the script should
|
---|
| 57 | * then produce the tree.
|
---|
| 58 | *
|
---|
| 59 | * @author Richard Heyes <richard@php.net>
|
---|
| 60 | * @author Harald Radi <harald.radi@nme.at>
|
---|
| 61 | * @access public
|
---|
| 62 | * @package HTML_TreeMenu
|
---|
| 63 | */
|
---|
| 64 |
|
---|
| 65 | class HTML_TreeMenu
|
---|
| 66 | {
|
---|
| 67 | /**
|
---|
| 68 | * Indexed array of subnodes
|
---|
| 69 | * @var array
|
---|
| 70 | */
|
---|
| 71 | var $items;
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * The layer ID
|
---|
| 75 | * @var string
|
---|
| 76 | */
|
---|
| 77 | var $layer;
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Path to the images
|
---|
| 81 | * @var string
|
---|
| 82 | */
|
---|
| 83 | var $images;
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Name of the object
|
---|
| 87 | * This should not be changed without changing
|
---|
| 88 | * the javascript. (no longer true)
|
---|
| 89 | * @var string
|
---|
| 90 | */
|
---|
| 91 | var $menuobj;
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * Constructor
|
---|
| 95 | *
|
---|
| 96 | * @access public
|
---|
| 97 | * @param string $layer The name of the layer to add the HTML to.
|
---|
| 98 | * In browsers that do not support document.all
|
---|
| 99 | * or document.getElementById(), document.write()
|
---|
| 100 | * is used, and thus this layer name has no effect.
|
---|
| 101 | * @param string $images The path to the images folder.
|
---|
| 102 | * @param string $linkTarget The target for the link. Defaults to "_self"
|
---|
| 103 | * @param string $usePersistence Whether to use clientside persistence. This option
|
---|
| 104 | * only affects ie5+.
|
---|
| 105 | */
|
---|
| 106 | function HTML_TreeMenu($layer=null, $images=null, $linkTarget = '_self', $usePersistence = true)
|
---|
| 107 | {
|
---|
| 108 | //$this->menuobj = 'objTreeMenu'; // No longer used here. cc 2002-10-31
|
---|
| 109 | $this->layer = $layer;
|
---|
| 110 | $this->images = $images;
|
---|
| 111 | $this->linkTarget = $linkTarget;
|
---|
| 112 | $this->usePersistence = $usePersistence;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * This function adds an item to the the tree.
|
---|
| 117 | *
|
---|
| 118 | * @access public
|
---|
| 119 | * @param object $menu The node to add. This object should be
|
---|
| 120 | * a HTML_TreeNode object.
|
---|
| 121 | * @return object Returns a reference to the new node inside
|
---|
| 122 | * the tree.
|
---|
| 123 | */
|
---|
| 124 | function &addItem(&$menu)
|
---|
| 125 | {
|
---|
| 126 | $this->items[] = &$menu;
|
---|
| 127 | return $this->items[count($this->items) - 1];
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * This function prints the menu Jabbascript code. Should
|
---|
| 132 | * be called *AFTER* your layer tag has been printed. In the
|
---|
| 133 | * case of older browsers, eg Navigator 4, The menu HTML will
|
---|
| 134 | * appear where this function is called.
|
---|
| 135 | *
|
---|
| 136 | * @access public
|
---|
| 137 | * @param string $layer The name of the layer to add the HTML to.
|
---|
| 138 | * In browsers that do not support document.all
|
---|
| 139 | * or document.getElementById(), document.write()
|
---|
| 140 | * is used, and thus this layer name has no effect.
|
---|
| 141 | * @param string $images The path to the images folder.
|
---|
| 142 | */
|
---|
| 143 | function printMenu($images=null, $layer=null)
|
---|
| 144 | {
|
---|
| 145 | // Setting menuobj here, rather than in constructor, ensures that
|
---|
| 146 | // multiple copies of the menu have different JavaScript variable names.
|
---|
| 147 | static $_menuobjcnt=0;
|
---|
| 148 | $this->menuobj = 'objTM'.$_menuobjcnt++;
|
---|
| 149 |
|
---|
| 150 | // $images and $layer could have been set in the constructor,
|
---|
| 151 | // so don't assign to them unless a value has been supplied here.
|
---|
| 152 | if (!empty($images)) $this->images = $images;
|
---|
| 153 | if (!empty($layer)) $this->layer = $layer;
|
---|
| 154 |
|
---|
| 155 | echo "\n";
|
---|
| 156 |
|
---|
| 157 | echo '<script language="javascript" type="text/javascript">' . "\n\t";
|
---|
| 158 | echo sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s");',
|
---|
| 159 | $this->menuobj,
|
---|
| 160 | empty($this->layer) ? $this->menuobj : $this->layer,
|
---|
| 161 | $this->images,
|
---|
| 162 | $this->menuobj,
|
---|
| 163 | $this->linkTarget);
|
---|
| 164 |
|
---|
| 165 | echo "\n";
|
---|
| 166 |
|
---|
| 167 | if (isset($this->items)) {
|
---|
| 168 | for ($i=0; $i<count($this->items); $i++) {
|
---|
| 169 | $this->items[$i]->_printMenu($this->menuobj . ".n[$i]");
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | echo sprintf("\n\t%s.drawMenu();", $this->menuobj);
|
---|
| 174 | if ($this->usePersistence) {
|
---|
| 175 | echo sprintf("\n\t%s.resetBranches();", $this->menuobj);
|
---|
| 176 | }
|
---|
| 177 | echo "\n</script>\n"; // cc 2002-10-29 add trailing \n
|
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | function printMenu2($txt)
|
---|
| 181 | {
|
---|
| 182 | // Setting menuobj here, rather than in constructor, ensures that
|
---|
| 183 | // multiple copies of the menu have different JavaScript variable names.
|
---|
| 184 | echo $txt;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | } // HTML_TreeMenu
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * HTML_TreeNode class
|
---|
| 191 | *
|
---|
| 192 | * This class is supplementary to the above and provides a way to
|
---|
| 193 | * add nodes to the tree. A node can have other nodes added to it.
|
---|
| 194 | *
|
---|
| 195 | * @author Richard Heyes <richard@php.net>
|
---|
| 196 | * @author Harald Radi <harald.radi@nme.at>
|
---|
| 197 | * @access public
|
---|
| 198 | * @package HTML_TreeMenu
|
---|
| 199 | */
|
---|
| 200 | class HTML_TreeNode
|
---|
| 201 | {
|
---|
| 202 | /**
|
---|
| 203 | * The text for this node.
|
---|
| 204 | * @var string
|
---|
| 205 | */
|
---|
| 206 | var $text;
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * The link for this node.
|
---|
| 210 | * @var string
|
---|
| 211 | */
|
---|
| 212 | var $link;
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * The icon for this node.
|
---|
| 216 | * @var string
|
---|
| 217 | */
|
---|
| 218 | var $icon;
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
| 221 | * Indexed array of subnodes
|
---|
| 222 | * @var array
|
---|
| 223 | */
|
---|
| 224 | var $items;
|
---|
| 225 |
|
---|
| 226 | /**
|
---|
| 227 | * Whether this node is expanded or not
|
---|
| 228 | * @var bool
|
---|
| 229 | */
|
---|
| 230 | var $expanded;
|
---|
| 231 |
|
---|
| 232 | /**
|
---|
| 233 | * Constructor
|
---|
| 234 | *
|
---|
| 235 | * @access public
|
---|
| 236 | * @param string $text The description text for this node
|
---|
| 237 | * @param string $link The link for the text
|
---|
| 238 | * @param string $icon Optional icon to appear to the left of the text
|
---|
| 239 | * @param bool $expanded Whether this node is expanded or not (IE only)
|
---|
| 240 | * @param bool $isDynamic Whether this node is dynamic or not (no affect on non-supportive browsers)
|
---|
| 241 | */
|
---|
| 242 | function HTML_TreeNode($text = null, $link = null, $icon = null, $expanded = false, $isDynamic = true)
|
---|
| 243 | {
|
---|
| 244 | $this->text = (string)$text;
|
---|
| 245 | $this->link = (string)$link;
|
---|
| 246 | $this->icon = (string)$icon;
|
---|
| 247 | $this->expanded = $expanded;
|
---|
| 248 | $this->isDynamic = $isDynamic;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /**
|
---|
| 252 | * Adds a new subnode to this node.
|
---|
| 253 | *
|
---|
| 254 | * @access public
|
---|
| 255 | * @param object $node The new node
|
---|
| 256 | */
|
---|
| 257 | function &addItem(&$node)
|
---|
| 258 | {
|
---|
| 259 | $this->items[] = &$node;
|
---|
| 260 | return $this->items[count($this->items) - 1];
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
| 264 | * Prints jabbascript for this particular node.
|
---|
| 265 | *
|
---|
| 266 | * @access private
|
---|
| 267 | * @param string $prefix The jabbascript object to assign this node to.
|
---|
| 268 | */
|
---|
| 269 | function _printMenu($prefix)
|
---|
| 270 | {
|
---|
| 271 | echo sprintf("\t%s = new TreeNode('%s', %s, %s, %s, %s);\n",
|
---|
| 272 | $prefix,
|
---|
| 273 | addslashes($this->text), // cc 2002-11-01. Text shouldn't
|
---|
| 274 | // be slashed by user since static menus whill show the slashes.
|
---|
| 275 | !empty($this->icon) ? "'" . $this->icon . "'" : 'null',
|
---|
| 276 | !empty($this->link) ? "'" . $this->link . "'" : 'null',
|
---|
| 277 | $this->expanded ? 'true' : 'false',
|
---|
| 278 | $this->isDynamic ? 'true' : 'false');
|
---|
| 279 |
|
---|
| 280 | if (!empty($this->items)) {
|
---|
| 281 | for ($i=0; $i<count($this->items); $i++) {
|
---|
| 282 | $this->items[$i]->_printMenu($prefix . ".n[$i]");
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 |
|
---|
| 290 | // default style classes for use in tree text.
|
---|
| 291 | static $_autostyles = array('tmenu0text', 'tmenu1text', 'tmenu2text', 'tmenu3text');
|
---|
| 292 |
|
---|
| 293 | class HTML_TreeNodeStyle extends HTML_TreeNode
|
---|
| 294 | {
|
---|
| 295 | var $style = false;
|
---|
| 296 |
|
---|
| 297 | // Constructor
|
---|
| 298 | function HTML_TreeNodeStyle($text = null, $link = null, $icon = null, $expanded = false, $isDynamic = true, $style = false)
|
---|
| 299 | {
|
---|
| 300 | HTML_TreeNode::HTML_TreeNode($text, $link, $icon, $expanded, $isDynamic);
|
---|
| 301 | if ($style) $this->style = $style;
|
---|
| 302 | else {
|
---|
| 303 | // We want to use default style based on level in the tree
|
---|
| 304 | // Unfortunately, we cannot know our level yet so defer...
|
---|
| 305 | $this->style = 'auto';
|
---|
| 306 | }
|
---|
| 307 | } // HTML_TreeNodeStyle
|
---|
| 308 |
|
---|
| 309 | function style( $st )
|
---|
| 310 | {
|
---|
| 311 | $this->style = $st;
|
---|
| 312 | } // style
|
---|
| 313 |
|
---|
| 314 | function _printMenu($prefix)
|
---|
| 315 | {
|
---|
| 316 | HTML_TreeNode::_printMenu($prefix);
|
---|
| 317 | if ($this->style) echo "\t$prefix.style = '" . $this->style . "';\n";
|
---|
| 318 | else echo "\t$prefix.style = false;\n";
|
---|
| 319 | } // _printMenu
|
---|
| 320 | }
|
---|
| 321 | ?>
|
---|