Problem/Motivation

When the root item in a Space Tree uses an integer for ID (such as node ID), something goes wrong when JIT tries to determine the ID for displaying tree node info. This leads to an empty info box. (See example at http://skillcompass.org/.)

Proposed resolution

Make sure the ID is casted as a string.

Remaining tasks

Find a proper place in the JIT to cast the ID as a string.
Cast the ID as a string.
Test.
Commit, either to the JIT module or to the JIT library, whatever is more appropriate.

Comments

clemens.tolboom’s picture

Title: Problems with dispalying info for root item » Problems with displaying info for root item

Is there no callback added to the Root node or not 'discovered' by the jit?

Can you provide for a data snippet if possible :)

itangalo’s picture

Sorry – I was going to add some more comments, but got distracted. :-)

Below is the code I tested this with. It outputs the $id_string when any non-root item is clicked, but nothing is outputted when the root item is clicked. So it seems the function isn't called at all.

/**
 * Function providing JSON formatted information about a topic in a tree.
 *
 * @param $id_string
 *   A string of IDs in the tree leading up to the clicked item, on the form
 *   ID1__ID2__ID3 (where ID1 is the starting point for the tree path and ID3 is
 *   the clicked item). This is the way it's provided from The JIT.
 */
function sc_topictrees_info_callback($id_string) {
  // DEBUG START
  $item_info = array('title' => 'title', 'body' => $id_string);
  print drupal_json_encode($item_info);
  die;
  // DEBUG END

  // Get the ID we're really looking for.
  $id_array = explode('__', $id_string);
  $clicked_id = array_pop($id_array);

  // If the root object was clicked, just abort all output in a brutal way.
  if ($clicked_id == '_root') {
    // Brutal way:
    die;
  }

  // Get a custom-built view, created for providing tree item info.
  $view = views_get_view('sc_topictree_item_info');
  $view->set_arguments(array($clicked_id));
  $view->execute();

  // The JIT expects an array with the keys 'title' and 'body'. Let's make it.
  $item_info = array(
    // Note that the view is created to have a result called 'title' and
    // 'sc_description', but if you change the view you might have to change
    // the render_field.
    'title' => $view->render_field('title', 0),
    'body' => $view->render_field('sc_topic_description', 0),
  );

  // Return the info as JSON, then stop any further execution of Drupal.
  print drupal_json_encode($item_info);
  die;
}

And here's the menu hook, in case it could be a source of problem.

/**
 * Implements hook_menu().
 */
function sc_topictrees_menu() {
  $items['sc_topictrees/tree-info-callback/%'] = array(
    'page callback' => 'sc_topictrees_info_callback',
    'page arguments' => array(2),
    'title' => 'Information about tree items',
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
  );
  return $items;
}
clemens.tolboom’s picture

You could have said install sc for the code :p

But what I'm curious about is what the rendered tree has for the callback in javascript.

From http://skillcompass.org/ just below

the javascript shows the data strucutre (line ~85)
window.thejit_spacetree['spacetree'].render({"id":1,"graphID":1,"name":"Drupal","data":{},"children":[{"id":"1__2","graphID":2,"name"

So there is an ID for 'Drupal' ... which is 1. So I'm puzzled.

Maybe you could try the graphAPI modules view to fit into your callback scheme: admin/modules/graph that gives us better tests.

clemens.tolboom’s picture

FWIW

On the demo site taken from the project page and fixed now #1534958: Demonstration link broken + another to see the module in action. http://aci-dev.tacc.utexas.edu/mrhanlon/thejit/jit/spacetree/debug

Clicking on the root works. (Make sure to check the show info node)

itangalo’s picture

I've been staring at various output and JS, trying to understand something.

My best guess right now is that the info callback fails when the ID is an integer rather than a string. When using node ID as identifier in JIT, the root item (and only the root item) gets an integer ID. All the others have IDs like "1__2".

When the root item is NOT a Drupal node, the ID becomes "_root", and things work.

Maybe it is some JS function that assumes that the ID is a string, and the callback is aborted when an error is thrown. Could be.
I don't know a line of JS, but found this that looks like a candidate: id = node['id'].replace(that.thisid + '_',''); (used at some place for fetching ID for a tree node)

In the demo, linked above, the root item has ID "jitnode_1", so that fits the pattern as well.

itangalo’s picture

Title: Problems with displaying info for root item » JIT fails the node info callback when item ID is an integer (not string)

Ok, I got some more support for the hypothesis in #5. I tried changing the to/from IDs from node ID to node titles, and now the info box renders just fine.

Something goes wrong when the tree item ID is an integer.

Updating the title and the issue summary.

clemens.tolboom’s picture

IC

So this code could be the problem:

function thejit_spacetree_graph_to_tree($graph, $config = array()) {
    if (isset($config['root_label'])) {
      $root_label = $config['root_label'];
    }
    else {
      $root_label = 'Root';
    }

  if (!$graph) {
    graphapi_add_node($graph, '_root');
    graphapi_set_node_title($graph, '_root', $root_label);
    $root = '_root';
  }
  else {
    // Possible root nodes do not have incoming edges
    $roots = array_keys($graph);
    foreach ($graph as $root => $data) {
      // Removing incoming edges from our list
      $roots = array_diff($roots, array_keys($data['edges']));
    }
    // Only when having 0 or >1 we should add 'Root'
    if (!$roots) {
      // Just pick one node
      $root = array_shift(array_keys($graph));
    }
    elseif (count($roots) > 1) {
      // We add a root node to make sure it's a tree
      graphapi_add_node($graph, '_root');
      graphapi_set_node_title($graph, '_root', $root_label);
      foreach ($roots as $root) {
        graphapi_add_link($graph, '_root', $root);
      }
      $root = '_root';
    }
    elseif (count($roots) == 1) {
      $root = array_shift($roots);
    }
  }
  $tree = new stdClass;
  $tree->id = $root; <==what is it's value in both cases?
  // the ID within the graph
  $tree->graphID = $root;
itangalo’s picture

Status: Active » Needs review
StatusFileSize
new635 bytes

Awesome!
This patch solves the problem.

clemens.tolboom’s picture

Status: Needs review » Active
StatusFileSize
new1.75 KB

I checked forcedirected ... patch only affects graph API data ... so needs works.

Attached patch will be committed shortly.

clemens.tolboom’s picture

Title: JIT fails the node info callback when item ID is an integer (not string) » JIT fails the node info callback when item ID is an integer (not string) for all engines
Component: Space Tree » Code
Assigned: Unassigned » clemens.tolboom
Category: bug » task

(commit has: 7300825..8cc16c7)

We need to make sure this will always happen. For whatever engine added in the future. As there are many.

So we need a thejit_validId() construct.

@see http://drupal.org/project/data_visualization having a Vizstruct defined. Does that overlap with thejit data tree?

clemens.tolboom’s picture

Issue summary: View changes

Updated summary with info about root item having integer (not string) ID casuses an error.