allow to autoload and/or supply initial tree via json

This commit is contained in:
Ralf Becker 2010-10-15 16:19:31 +00:00
parent f779454f4c
commit a6c1f34be7

View File

@ -1196,31 +1196,36 @@ class html
* @param string $_tree='foldertree' id of the div and name of the variable containing the tree object * @param string $_tree='foldertree' id of the div and name of the variable containing the tree object
* @param string $_divClass='' css class of the div * @param string $_divClass='' css class of the div
* @param string $_leafImage='' default image of a leaf-node, ''=default of foldertree, set it eg. 'folderClosed.gif' to show leafs as folders * @param string $_leafImage='' default image of a leaf-node, ''=default of foldertree, set it eg. 'folderClosed.gif' to show leafs as folders
* @param boolean/string $_onCheckHandler=false string with handler-name to display a checkbox for each folder, or false (default), 'null' switches checkboxes on without an handler! * @param boolean|string $_onCheckHandler=false string with handler-name to display a checkbox for each folder, or false (default), 'null' switches checkboxes on without an handler!
* @param string $delimiter='/' path-delimiter, default / * @param string $delimiter='/' path-delimiter, default /
* @param mixed $folderImageDir=null string path to the tree menu images, null uses default path * @param string $folderImageDir=null string path to the tree menu images, null uses default path
* @param string|array $autoLoading=null EGw relative path or array with get parameter, both send through egw::link
* @param string $dataMode='JSON' data type for autoloading: XML, JSON, CSV
* *
* @return string the html code, to be added into the template * @return string the html code, to be added into the template
*/ */
static function tree($_folders,$_selected,$_topFolder=false,$_onNodeSelect="null",$tree='foldertree',$_divClass='',$_leafImage='',$_onCheckHandler=false,$delimiter='/',$folderImageDir=null) static function tree($_folders,$_selected,$_topFolder=false,$_onNodeSelect="null",$tree='foldertree',$_divClass='',
$_leafImage='',$_onCheckHandler=false,$delimiter='/',$folderImageDir=null,$autoLoading=null,$dataMode='JSON')
{ {
if(is_null($folderImageDir)) if(is_null($folderImageDir))
{ {
$folderImageDir = $GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/templates/default/images/'; $folderImageDir = $GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/templates/default/images';
} }
static $tree_initialised=false; static $tree_initialised=false;
if (!$tree_initialised) if (!$tree_initialised)
{ {
$html .= '<link rel="STYLESHEET" type="text/css" href="'.$GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/js/dhtmlxtree/css/dhtmlXTree.css" />'."\n"; egw_framework::includeCSS('/phpgwapi/js/dhtmlxtree/dhtmlxTree/codebase/dhtmlxtree.css');
$html .= "<script type='text/javascript' src='{$GLOBALS['egw_info']['server']['webserver_url']}/phpgwapi/js/dhtmlxtree/js/dhtmlXCommon.js'></script>\n"; egw_framework::validate_file('dhtmlxtree','dhtmlxTree/codebase/dhtmlxcommon');
$html .= "<script type='text/javascript' src='{$GLOBALS['egw_info']['server']['webserver_url']}/phpgwapi/js/dhtmlxtree/js/dhtmlXTree.js'></script>\n"; egw_framework::validate_file('dhtmlxtree','dhtmlxTree/codebase/dhtmlxtree');
if ($autoLoading && $dataMode != 'XML') egw_framework::validate_file('dhtmlxtree','dhtmlxTree/codebase/ext/dhtmlxtree_json');
$tree_initialised = true; $tree_initialised = true;
if (!$_folders) return $html; if (!$_folders && !$autoLoading) return $html;
} }
$html = self::div("\n",'id="'.$tree.'"',$_divClass).$html; $html = self::div("\n",'id="'.$tree.'"',$_divClass).$html;
$html .= "<script type='text/javascript'>\n"; $html .= "<script type='text/javascript'>\n";
$html .= "var $tree = new dhtmlXTreeObject('$tree','100%','100%',0);\n"; $html .= "var $tree = new dhtmlXTreeObject('$tree','100%','100%',0);\n";
if (translation::charset() == 'utf-8') $html .= "if ($tree.setEscapingMode) $tree.setEscapingMode('utf8');\n";
$html .= "$tree.setImagePath('$folderImageDir/dhtmlxtree/');\n"; $html .= "$tree.setImagePath('$folderImageDir/dhtmlxtree/');\n";
if($_onCheckHandler) if($_onCheckHandler)
@ -1229,6 +1234,23 @@ class html
$html .= "$tree.setOnCheckHandler('$_onCheckHandler');\n"; $html .= "$tree.setOnCheckHandler('$_onCheckHandler');\n";
} }
if ($autoLoading)
{
$autoLoading = is_array($autoLoading) ?
egw::link('/index.php',$autoLoading) : egw::link($autoLoading);
$html .= "$tree.setXMLAutoLoading('$autoLoading');\n";
if ($dataMode != 'XML') $html .= "$tree.setDataMode('$dataMode');\n";
// if no folders given, use xml url to load root, incl. setting of selected folder
if (!$_folders)
{
if ($_selected) $autoLoading .= '&selected='.urlencode($_selected);
unset($_selected);
$html .= "$tree.loadXML('$autoLoading');\n";
return $html."</script>\n";
}
}
$top = 0; $top = 0;
if ($_topFolder) if ($_topFolder)
{ {
@ -1253,8 +1275,22 @@ class html
$html .= "$tree.setItemText('$top','".addslashes($label)."','".addslashes($_topFolder['title'])."');\n"; $html .= "$tree.setItemText('$top','".addslashes($label)."','".addslashes($_topFolder['title'])."');\n";
} }
} }
if (is_string($_folders))
{
switch($dataMode)
{
case 'JSON':
$html .= "$tree.loadJSONObject($_folders);\n"; break;
case 'XML':
$html .= "$tree.loadXMLString('$_folders');\n"; break;
}
}
else
{
// evtl. remove leading delimiter // evtl. remove leading delimiter
if ($_selected[0] == $delimiter) $_selected = substr($_selected,1); if ($_selected[0] == $delimiter) $_selected = substr($_selected,1);
$n = 0;
foreach($_folders as $path => $data) foreach($_folders as $path => $data)
{ {
if (!is_array($data)) if (!is_array($data))
@ -1262,6 +1298,20 @@ class html
$data = array('label' => $data); $data = array('label' => $data);
} }
$image1 = $image2 = $image3 = '0'; $image1 = $image2 = $image3 = '0';
// if _leafImage given, set it only for leaves, not for folders containing children
if ($_leafImage)
{
$image1 = $image2 = $image3 = "'".$_leafImage."'";
if ($next_item = array_slice($_folders, $n+1, 1, true))
{
list($next_path) = each($next_item);
if (substr($next_path,0,strlen($path)+1) == $path.'/')
{
$image1 = $image2 = $image3 = '0';
}
}
}
if (isset($data['image'])) if (isset($data['image']))
{ {
$image1 = $image2 = $image3 = "'".$data['image']."'"; $image1 = $image2 = $image3 = "'".$data['image']."'";
@ -1278,7 +1328,7 @@ class html
$parentName = implode((array)$folderParts,$delimiter); $parentName = implode((array)$folderParts,$delimiter);
if(empty($parentName)) $parentName = $top; if(empty($parentName)) $parentName = $top;
$entryOptions = 'CHILD'; $entryOptions = !isset($data['child']) || $data['child'] ? 'CHILD' : '';
if ($_onCheckHandler && $_selected) // check selected items on multi selection if ($_onCheckHandler && $_selected) // check selected items on multi selection
{ {
if (!is_array($_selected)) $_selected = explode(',',$_selected); if (!is_array($_selected)) $_selected = explode(',',$_selected);
@ -1296,6 +1346,8 @@ class html
{ {
$html .= "$tree.setItemText('".addslashes($path)."','".addslashes($label)."','".addslashes($data['title'])."');\n"; $html .= "$tree.setItemText('".addslashes($path)."','".addslashes($label)."','".addslashes($data['title'])."');\n";
} }
++$n;
}
} }
$html .= "$tree.closeAllItems(0);\n"; $html .= "$tree.closeAllItems(0);\n";
if ($_selected) if ($_selected)