Subject: [Pkg-cacti-maint] Bug#679980: patch in progress

From: Paul Gevers <[email-masked]>
Date: Thu, 22 Nov 2012 22:24:18 +0100

control: -1 tag + patch

Hi,

I gave up on upstream providing a patch any time soon, so I decided to
do some of the work. I have added my current work as a patch to the
upstream bug report.

Although clearly not up-to-par yet, I think this could be nearly usable
within Debian. (The patch should be using the embedded version of
jquery-jstree, for now I used one one-line.)

Additional to this patch, it requires a dependency on libjs-jquery and
libjs-jquery-cookie and the embedding of jquery-jstree. [1][2]

Work in progress, but I would appreciate it if somebody could review the
patch.

Paul
[1] http://www.jstree.com/
[2]
http://svn.cacti.net/viewvc/cacti/branches/0.8.9/include/js/jquery/jquery.jstree.js
diff --git a/include/top_graph_header.php b/include/top_graph_header.php
index 86bba4b..625d580 100644
--- a/include/top_graph_header.php
+++ b/include/top_graph_header.php
@@ -84,8 +84,9 @@ $page_title = api_plugin_hook_function('page_title', draw_navigation_text("title
 	<link href="<?php echo $config['url_path']; ?>include/main.css" type="text/css" rel="stylesheet">
 	<link href="<?php echo $config['url_path']; ?>images/favicon.ico" rel="shortcut icon"/>
 	<script type="text/javascript" src="<?php echo $config['url_path']; ?>include/layout.js"></script>
-	<script type="text/javascript" src="<?php echo $config['url_path']; ?>include/treeview/ua.js"></script>
-	<script type="text/javascript" src="<?php echo $config['url_path']; ?>include/treeview/ftiens4.js"></script>
+	<script type="text/javascript" src="/javascript/jquery/jquery.min.js" language="javascript"></script>
+	<script type="text/javascript" src="/javascript/jquery-cookie/jquery.cookie.min.js" language="javascript"></script>
+	<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
 	<script type="text/javascript" src="<?php echo $config['url_path']; ?>include/jscalendar/calendar.js"></script>
 	<script type="text/javascript" src="<?php echo $config['url_path']; ?>include/jscalendar/lang/calendar-en.js"></script>
 	<script type="text/javascript" src="<?php echo $config['url_path']; ?>include/jscalendar/calendar-setup.js"></script>
@@ -178,7 +179,6 @@ $page_title = api_plugin_hook_function('page_title', draw_navigation_text("title
 		<td valign="top" style="padding: 5px; border-right: #aaaaaa 1px solid;background-repeat:repeat-y;background-color:#efefef;" bgcolor='#efefef' width='<?php print htmlspecialchars(read_graph_config_option("default_dual_pane_width"));?>' class='noprint'>
 			<table border=0 cellpadding=0 cellspacing=0><tr><td><a style="font-size:7pt;text-decoration:none;color:silver" href="http://www.treemenu.net/" target=_blank></a></td></tr></table>
 			<?php grow_dhtml_trees(); ?>
-			<script type="text/javascript">initializeDocument();</script>
 
 			<?php if (isset($_GET["select_first"])) { ?>
 			<script type="text/javascript">
diff --git a/lib/html_tree.php b/lib/html_tree.php
index 861c6cc..68251ca 100644
--- a/lib/html_tree.php
+++ b/lib/html_tree.php
@@ -495,18 +495,11 @@ function grow_dhtml_trees() {
 	include_once($config["library_path"] . "/data_query.php");
 
 	?>
-	<script type="text/javascript">
-	<!--
-	USETEXTLINKS = 1
-	STARTALLOPEN = 0
-	USEFRAMES = 0
-	USEICONS = 0
-	WRAPTEXT = 1
-	PERSERVESTATE = 1
-	HIGHLIGHT = 1
+	<div id="jtree">
+					<ul>
 	<?php
 	/* get current time */
-	list($micro,$seconds) = explode(" ", microtime());
+/*	list($micro,$seconds) = explode(" ", microtime());
 	$current_time = $seconds + $micro;
 	$expand_hosts = read_graph_config_option("expand_hosts");
 
@@ -522,6 +515,8 @@ function grow_dhtml_trees() {
 			$dhtml_tree = $_SESSION['dhtml_tree'];
 		}
 	}
+*/
+	$dhtml_tree = create_dhtml_tree();
 
 	$total_tree_items = sizeof($dhtml_tree) - 1;
 
@@ -529,8 +524,29 @@ function grow_dhtml_trees() {
 		print $dhtml_tree[$i];
 	}
 	?>
-	//-->
-	</script>
+					</ul>
+				</div>
+				<script type="text/javascript">
+$(function () {
+	$("#jtree")
+		.jstree({
+        	        "themes" : {"icons" : false},
+                        "plugins" : ["ui","themes","html_data","cookies"]
+                })
+
+                // Make sure that the nodes are actually used as links
+                // We need reselect to prevent endless loops
+                // https://groups.google.com/d/topic/jstree/j6XNq9hQdeA/discussion
+                .bind("reselect.jstree", function (e, data) { 
+                      data.inst.get_container().bind("select_node.jstree", function (e, data) {
+                           // data.rstl.obj is the object that was selected.
+                           document.location.href = data.rslt.obj.children("a").attr("href");
+                       });
+                });
+
+});
+
+				</script>
 	<?php
 }
 
@@ -543,9 +559,7 @@ function create_dhtml_tree() {
 
 	$dhtml_tree[0] = $start;
 	$dhtml_tree[1] = read_graph_config_option("expand_hosts");
-	$dhtml_tree[2] = "foldersTree = gFld(\"\", \"\")\n";
-	$dhtml_tree[3] = "foldersTree.xID = \"root\"\n";
-	$i = 3;
+	$i = 1;
 
 	$tree_list = get_graph_tree_array();
 
@@ -583,19 +597,17 @@ function create_dhtml_tree() {
 				and graph_tree_items.local_graph_id = 0
 				order by graph_tree_items.order_key");
 
-			$dhtml_tree[$i] = "ou0 = insFld(foldersTree, gFld(\"" . htmlspecialchars($tree["name"]) . "\", \"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"]) . "\"))\n";
-			$i++;
-			$dhtml_tree[$i] = "ou0.xID = \"tree_" . $tree["id"] . "\"\n";
+			$dhtml_tree[$i] = "<li id=\"" . htmlspecialchars("tree_" . $tree["id"]) . "\"><a href=\"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"]) . "\">" . htmlspecialchars($tree["name"]) . "</a>" . "\n";
 
 			if (sizeof($hierarchy) > 0) {
+			        $i++;
+				$dhtml_tree[$i] = "<ul>\n";
 				foreach ($hierarchy as $leaf) {
 					$i++;
 					$tier = tree_tier($leaf["order_key"]);
 
 					if ($leaf["host_id"] > 0) {
-						$dhtml_tree[$i] = "ou" . ($tier) . " = insFld(ou" . abs(($tier-1)) . ", gFld(\"" . "Host: " . htmlspecialchars($leaf["hostname"]) . "\", \"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"]) . "\"))\n";
-						$i++;
-						$dhtml_tree[$i] = "ou" . ($tier) . ".xID = \"tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "\"\n";
+						$dhtml_tree[$i] = "<li id=\"" . htmlspecialchars("tree_" . $tree["id"] . "_leaf_" . $leaf["id"]) . "\"><a href=\"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"]) . "\">Host: " . htmlspecialchars($leaf["hostname"]) . "</a>" . "\n";
 
 						if (read_graph_config_option("expand_hosts") == "on") {
 							if ($leaf["host_grouping_type"] == HOST_GROUPING_GRAPH_TEMPLATE) {
@@ -612,9 +624,7 @@ function create_dhtml_tree() {
 								if (sizeof($graph_templates) > 0) {
 									foreach ($graph_templates as $graph_template) {
 										$i++;
-										$dhtml_tree[$i] = "ou" . ($tier+1) . " = insFld(ou" . ($tier) . ", gFld(\" " . htmlspecialchars($graph_template["name"]) . "\", \"graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"] . "&host_group_data=graph_template:" . $graph_template["id"] . "\"))\n";
-										$i++;
-										$dhtml_tree[$i] = "ou" . ($tier+1) . ".xID = \"tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "_hgd_gt_" . $graph_template["id"] . "\"\n";
+										$dhtml_tree[$i] = "<li id=\"" . htmlspecialchars("tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "_hgd_gt_" . $graph_template["id"]) . "\"><a href=\"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"] . "&host_group_data=graph_template:" . $graph_template["id"]) . "\">" . htmlspecialchars($graph_template["name"]) . "</a>" . "\n";
 									}
 								}
 							}else if ($leaf["host_grouping_type"] == HOST_GROUPING_DATA_QUERY_INDEX) {
@@ -645,16 +655,12 @@ function create_dhtml_tree() {
 										if ((($data_query["id"] == 0) && ($non_template_graphs > 0)) ||
 											(($data_query["id"] > 0) && (sizeof($sort_field_data) > 0))) {
 											$i++;
-											$dhtml_tree[$i] = "ou" . ($tier+1) . " = insFld(ou" . ($tier) . ", gFld(\" " . htmlspecialchars($data_query["name"]) . "\", \"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"] . "&host_group_data=data_query:" . $data_query["id"]) . "\"))\n";
-											$i++;
-											$dhtml_tree[$i] = "ou" . ($tier+1) . ".xID = \"tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "_hgd_dq_" . $data_query["id"] . "\"\n";
-
+											$dhtml_tree[$i] = "<li id=\"" . htmlspecialchars("tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "_hgd_dq_" . $data_query["id"]) . "\"><a href=\"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"] . "&host_group_data=data_query:" . $data_query["id"]) . "\">" . htmlspecialchars($data_query["name"]) . "</a>" . "\n";
 											if ($data_query["id"] > 0) {
 												while (list($snmp_index, $sort_field_value) = each($sort_field_data)) {
 													$i++;
-													$dhtml_tree[$i] = "ou" . ($tier+2) . " = insFld(ou" . ($tier+1) . ", gFld(\" " . htmlspecialchars($sort_field_value) . "\", \"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"] . "&host_group_data=data_query_index:" . $data_query["id"] . ":" . urlencode($snmp_index)) . "\"))\n";
-													$i++;
-													$dhtml_tree[$i] = "ou" . ($tier+2) . ".xID = \"tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "_hgd_dqi" . $data_query["id"] . "_" . urlencode($snmp_index) . "\"\n";
+													$dhtml_tree[$i] = "<li id=\"" . htmlspecialchars("tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "_hgd_dqi" . $data_query["id"]) . "_" . urlencode($snmp_index) . "\"><a href=\"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"] . "&host_group_data=graph_template:" . $graph_template["id"]) . "\">" . htmlspecialchars($graph_template["name"]) . "</a>" . "\n";
+/*													$dhtml_tree[$i] = "ou" . ($tier+2) . " = insFld(ou" . ($tier+1) . ", gFld(\" " . htmlspecialchars($sort_field_value) . "\", \"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"] . "&host_group_data=data_query_index:" . $data_query["id"] . ":" . urlencode($snmp_index)) . "\"))\n";*/
 												}
 											}
 										}
@@ -664,9 +670,9 @@ function create_dhtml_tree() {
 						}
 					}else{
 						$dhtml_tree[$i] = "ou" . ($tier) . " = insFld(ou" . abs(($tier-1)) . ", gFld(\"" . htmlspecialchars($leaf["title"]) . "\", \"" . htmlspecialchars("graph_view.php?action=tree&tree_id=" . $tree["id"] . "&leaf_id=" . $leaf["id"]) . "\"))\n";
-						$i++;
-						$dhtml_tree[$i] = "ou" . ($tier) . ".xID = \"tree_" . $tree["id"] . "_leaf_" . $leaf["id"] . "\"\n";
 					}
+				$i++;
+				$dhtml_tree[$i] = "</li>\n</ul>\n";
 				}
 			}
 		}
@@ -758,14 +764,6 @@ function grow_right_pane_tree($tree_id, $leaf_id, $host_group_data) {
 		}
 	}
 
-	print "<script type=\"text/javascript\">\n";
-	print "<!--\n";
-	print "myNode = findObj(\"$nodeid\")\n";
-	print "myNode.forceOpeningOfAncestorFolders();\n";
-	print "highlightObjLink(myNode)\n";
-	print "//-->\n";
-	print "</script>";
-
 	/* ================= input validation ================= */
 	input_validate_input_number(get_request_var_post("graphs"));
 	input_validate_input_number(get_request_var_post("page"));

---

From: Paul Gevers <[email-masked]>
Date: Sat, 24 Nov 2012 22:39:34 +0100

The previous patch was not good. But I am working on it. I think I have
a good patch now for upstream (see upstream bts).

Will try to get this properly done for Debian, and see if the
release-team can except my changes and adding of jstree (apart for
dsfg-ing the source tar ball).

Paul