Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/inc/CSTreeView.class.php
<?php

//
// Helper class to build JavaScript code for new TreeView instance
//

class CSTreeNode
{
	private $id;
	public function getId() { return $this->id; }

	private $parentId;
	public function getParentId() { return $this->parentId; }

	private $args;

	public function __construct($parentId, $newId, Array $args)
	{
		if (empty($parentId) || !is_integer($newId))
			throw new Exception('CSTreeNode with invalid parameters');

		$this->id = 'n'.$newId;
		$this->parentId = $parentId;
		$this->args = $args;
	}
	public function __destruct() { }

	public function __get($p)
	{
		if (array_key_exists($p, $this->args))
			return $this->args[$p];
		return null;
	}
	public function __set($p, $v)
	{
		if (is_null($v))
			unset($this->args[$p]);
		else
			$this->args[$p] = $v;
		return true;
	}

	public function __toString()
	{
		$labelCode = '';

		foreach ($this->args as $p => $v)
		{
			// If the value is empty, skip it
			if (empty($v))
				continue;

			// Only allow our JavaScript parameters out..
			if ($p == "label" || $p == "href" || $p == "target" || $p == "icon")
			{
				if (strlen($labelCode) > 0)
					$labelCode .= ', ';
				$labelCode .= $p.':"'.$v.'"';
			}
		}

		$nodeExpand = ($this->expand == TRUE) ? "true" : "false";

		$nodeCode  = "var ".$this->id."lbl = { ".$labelCode." };\n";
		$nodeCode .= "var ".$this->id." = new YAHOO.widget.TextNode(".$this->id."lbl, ".$this->parentId.", ".$nodeExpand.");\n";

		return $nodeCode;
	}
}

class CSTreeView
{
	private $id;
	public function getId() { return $this->id; }

	public function getInitName() { return $this->id.'_init'; }
	public function getRootName() { return $this->id.'_root'; }
	public function getElemName() { return $this->id.'_elem'; }

	private $nodes;
	public function &getNodes() { return $this->nodes; }

	private $lastNodeId;
	public function getLastNodeId() { return $this->lastNodeId; }

	public function __construct($treeName)
	{
		$this->id = $treeName;
		$this->nodes = array();
		$this->lastNodeId = -1;
	}
	public function __destruct() { }

	public function AddNode($parentId, $args)
	{
		$this->lastNodeId++;
		if (array_key_exists($this->lastNodeId, $this->nodes))
			throw new Exception('CSTreeView::AddNode() next slot already in use, tree is corrupt!');

		$this->nodes[$this->lastNodeId] = new CSTreeNode($parentId, $this->lastNodeId, $args);
		return $this->nodes[$this->lastNodeId]->getId();
	}

	public function InsertClientScript()
	{
		$buf = array();

		$buf[] = '<script type="text/javascript">';
		$buf[] = 'var '.$this->id.'; // Our tree context';
		$buf[] = 'function '.$this->getInitName().'() {';
		$buf[] = $this->id.' = new YAHOO.widget.TreeView("'.$this->getElemName().'");';
		$buf[] = 'var '.$this->getRootName().' = '.$this->id.'.getRoot();';
		$buf[] = '';
		foreach($this->nodes as $node)
			$buf[] = $node;
		$buf[] = '';
		$buf[] = $this->id.'.draw(); }';
		$buf[] = '</script>';

		foreach($buf as $line)
			echo "$line\n";
	}

	public function InsertTreeElement()
	{
		echo '<form id="TreeViewForm" action="javascript:;"><div id="'.$this->getElemName().'"></div></form>';
	}	

	public static function GetHeaderArray()
	{
		global $CS_URL_PREPEND; // Should be defined in CS_Includes.inc.php
		return array(
			'<script src="'.$CS_URL_PREPEND.'assets/yahoo.js" type="text/javascript"></script>',
			'<script src="'.$CS_URL_PREPEND.'assets/treeview.js" type="text/javascript"></script>',
			'<link rel="stylesheet" type="text/css" href="'.$CS_URL_PREPEND.'assets/treeview.css" />'
		);
	}
}

?>
Return current item: Network Configuration Store