// build three graphs
var graph = new thinkmap.graph.Graph();
var display = new thinkmap.display.Display(document.getElementById('map'));
var tree = new thinkmap.graph.Tree();
// attach a datasource to the main graph
graph.datasource('/moviedb.tas');
// construct a history manager
var historyManager = new thinkmap.graph.history.HistoryManager(graph)
.add(display).add(tree);
// make two expands - one for each direction
var expander = new thinkmap.graph.expand.ExpandManager(graph,historyManager)
.add(display).add(tree);
function center(entity) {
// return if entity is undefined or equal to the current root or not
// a movie
if (!entity || root === entity || !entity.isType('movie')) return;
// set the root to the new entity
root = entity;
tree.root(root);
// add the root to the display
display.add(root);
// clear the expand manager so we start from scratch
expander.clear();
query(entity).then(function(results) {
display.retainOnly(results);
expander.expand(entity,results);
historyManager.pushState();
return display.transition().scrollTo(treeLayout.targetPosition(entity));
});
}
// handles expansion of ent - assumes expanded entity is in the display/tree
function expand(ent) {
graph.query(....).execute(ent)
.onresult(tree.add)
.onresult(display.add)
.oncomplete(function(results) {
expander.expand(ent,results);
historyManager.pushState();
});
}
function query(entity) {
return graph.query({$choice:[
['movie','edge', 'person'],
['person','edge', 'movie']
]})
.execute(entity)
.onresult(display.add)
.onresult(tree.add)
.oncomplete(function(results){
display.layout();
return results;
});
}
// handles when to center/expand/collapse
display.on('click',function(evt) {
var entity = evt.entity;
if (evt.shiftKey) {
center(entity);
} else if(expander.isExpanded(entity)) {
expand(entity);
} else {
expander.collapse(entity);
historyManager.pushState():
}
});
Constructs an ExpandManager with the given source graph and optional History Manager.
The ExpandManager is used to manage the expansion and collapse of entities in a predictable manner. It works by incrementally calling expand with a given node that has been expanded and the results of the expansion.
Generally these are the steps. There is a general center function in the application. When this function is called, the expand manager is first cleared. When the query completes, the expand manager calls expand using the center entity and results. Now when expanding entities, there is an expand query that is meant for expanding parts of the query. Once this completes, the ExpandManager's expand method is called with the given expand entity and the results of the query. Collapse works by rebuilding the graphs and truncating any expansions which are no longer visible.
The ExpandManager also takes an optional HistoryManager for managing history. It listens to calls for saving and restoring history, and rebuilds its internal datastructures appropriately.
the main graph used to back and store all results and entities expanded. Ideally, this is the most high level graph used by Thinkmap. It is typically the graph that handles server side queries and has one or more datasources attached to it. All other graphs in the application typically are a subset of this graph.
the optional history manager
// build three graphs
var graph = new thinkmap.graph.Graph();
var display = new thinkmap.display.Display(document.getElementById('map'));
var tree = new thinkmap.graph.Tree();
// attach a datasource to the main graph
graph.datasource('/moviedb.tas');
// construct a history manager
var historyManager = new thinkmap.graph.history.HistoryManager(graph)
.add(display).add(tree);
// make two expands - one for each direction
var expander = new thinkmap.graph.expand.ExpandManager(graph,historyManager)
.add(display).add(tree);
function center(entity) {
// return if entity is undefined or equal to the current root or not
// a movie
if (!entity || root === entity || !entity.isType('movie')) return;
// set the root to the new entity
root = entity;
tree.root(root);
// add the root to the display
display.add(root);
// clear the expand manager so we start from scratch
expander.clear();
query(entity).then(function(results) {
display.retainOnly(results);
expander.expand(entity,results);
historyManager.pushState();
return display.transition().scrollTo(treeLayout.targetPosition(entity));
});
}
// handles expansion of ent - assumes expanded entity is in the display/tree
function expand(ent) {
graph.query(....).execute(ent)
.onresult(tree.add)
.onresult(display.add)
.oncomplete(function(results) {
expander.expand(ent,results);
historyManager.pushState();
});
}
function query(entity) {
return graph.query({$choice:[
['movie','edge', 'person'],
['person','edge', 'movie']
]})
.execute(entity)
.onresult(display.add)
.onresult(tree.add)
.oncomplete(function(results){
display.layout();
return results;
});
}
// handles when to center/expand/collapse
display.on('click',function(evt) {
var entity = evt.entity;
if (evt.shiftKey) {
center(entity);
} else if(expander.isExpanded(entity)) {
expand(entity);
} else {
expander.collapse(entity);
historyManager.pushState():
}
});
Adds a graph to be managed by this ExpandManager.
Generally in Thinkmap there are multiple graphs being managed all at once (i.e. you have a display and a tree that are having the same results added to them). Much like the case for the HistoryManager, we want to have an "expand" be marked across several graphs all at once. So when starting up, we add a bunch of graphs.
a graph managed by the Expand Manager. This should not be the source graph used when constructing the ExpandManager
returns this ExpandManager
// build three graphs
var graph = new thinkmap.graph.Graph();
var display = new thinkmap.display.Display(document.getElementById('map'));
var tree = new thinkmap.graph.Tree();
// attach a datasource to the main graph
graph.datasource('/moviedb.tas');
// make two expands - one for each direction
var expandManager = new thinkmap.graph.expand.ExpandManager(graph).add(display).add(tree);
Sets or gets the center entity. Setting a new center entity with a given set. The ExpandManager currently only supports one center (root). It future, may make sense to have multiple centers
Collapses an entity if it was expanded. Collapsing an entity will result in the graph trying to rebuild any following and previous expansions which may be altered.
the entity to collapse. The center entity is not allowed to be collapsed. If the entity isn't marked as expanded, nothing will happen.
returns this Expand Manager
Expands an entity with the given results.
Generally this works by the user clicking an entity on the screen. A query is then run on that entity and the results of that query are used as the results to this method method call.
This method can only be called if there is a marked "center" for the expand manager. This is necessary so that the ExpandManager can keep track of the order of expands.
the entity that was expanded (the leaf in a tree for instance)
the results of expanding the given entity (generally the results of an executed query)
returns this Expand Manager
function expand(ent) {
graph.query(....).execute(ent)
.onresult(tree.add)
.onresult(display.add)
.oncomplete(function(results) {
expandManager.expand(ent,results);
historyManager.pushState();
});
}
Returns true if an entity has been expanded.
the entity to test if it has been expanded
returns true if the entity is expanded
Add a listener to this EventSource
the name of the event to listen for
the callback to add
this eventsource
Dispatch an event from this EventSource, supplying arguments to the listeners via an Array, rather than var-args.
the name of the event to dispatch
this EventSource
Dispatch an event from this EventSource
the name of the event to dispatch
optional arguments sent to the event listeners
this EventSource
Fire an event from this EventSource
the name of the event to dispatch
optional arguments sent to the event listeners
this EventSource
Remove a listener from this EventSource
the name of the event to remove the listener for
the callback to remove
this eventsource
Add a listener to this EventSource
the name of the event to listen for
the callback
this eventsource
Add a listener to this EventSource, which will be called at most once and will be removed the first time the given event fires.
the name of the event to listen for
the callback to add
this eventsource
Remove a listener from this EventSource
the name of the event to remove the listener for
the callback to remove
this eventsource
this event is fired when an entity is expanded. The first parameter is the entity expanded and the second parameter are the results used for the expand
this event is fired when collapse is called on an expanded entity
fired when the expand manager is rebuilding history for the graphs. This can typically be ignored by most developers as it is mainly used internally by the expand manager.