<?php  

require_once(dirname(__FILE__) . '/tree_model_base.php');

class JsTreeModel extends TreeModelBase
{
	public function HtmlUnorderedList()
	{
		$data = $this->DisplayNode(0);
		$i = 0;
		
		return empty($data) ? '' : '<ul>' . $this->outputLi($data, $i, 0) . '</ul>';
	}
	
	private function outputLi($nodes, &$i)
	{
		$node = $nodes[$i];
		
		$currentDepth = $node['depth'];
		$nextDepth = isset($nodes[$i+1]) ? $nodes[$i+1]['depth'] : 0;
		
		// Set up output for LI
		$class = 'jstree-' . ($node['leaf'] ? 'leaf' : 'open');
		$id = $node['id'];						
		$output = '<li id="file-tree-node-'.$id.'" class="'.$class.'"><a href="#">' . $node['name'] . '</a>';

		// If next node is below, start a UL node
		if($nextDepth > $currentDepth)
			$output .= '<ul>' . $this->outputLi($nodes, ++$i);
		
		// If next node is above, close current LI and the multiple UL/LI above
		else if($nextDepth < $currentDepth)
			return $output . '</li>' . str_repeat('</ul></li>', $currentDepth-$nextDepth);
			
		// If same level, just close current LI.
		else
			$output .= '</li>';
		
		if(isset($nodes[$i+1]))
			return $output . $this->outputLi($nodes, ++$i);
		else
			return $output;
	}
}