2014-11-20 20:46:51 +01:00
|
|
|
/**
|
|
|
|
* Created by knut on 14-11-03.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var vertices = {};
|
|
|
|
var edges = [];
|
2014-11-22 15:34:21 +01:00
|
|
|
var classes = [];
|
2014-11-20 20:46:51 +01:00
|
|
|
var direction;
|
2014-11-24 22:03:32 +01:00
|
|
|
// Functions to be run after graph rendering
|
|
|
|
var funs = [];
|
2014-11-20 20:46:51 +01:00
|
|
|
/**
|
|
|
|
* Function called by parser when a node definition has been found
|
|
|
|
* @param id
|
|
|
|
* @param text
|
|
|
|
* @param type
|
|
|
|
* @param style
|
|
|
|
*/
|
|
|
|
exports.addVertex = function (id, text, type, style) {
|
2014-11-22 15:34:21 +01:00
|
|
|
//console.log('Got node ' + id + ' ' + type + ' ' + text + ' styles: ' + JSON.stringify(style));
|
2014-11-20 20:46:51 +01:00
|
|
|
if (typeof vertices[id] === 'undefined') {
|
2014-11-22 15:34:21 +01:00
|
|
|
vertices[id] = {id: id, styles: [], classes:[]};
|
2014-11-20 20:46:51 +01:00
|
|
|
}
|
|
|
|
if (typeof text !== 'undefined') {
|
|
|
|
vertices[id].text = text;
|
|
|
|
}
|
|
|
|
if (typeof type !== 'undefined') {
|
|
|
|
vertices[id].type = type;
|
|
|
|
}
|
2014-11-22 15:34:21 +01:00
|
|
|
if (typeof type !== 'undefined') {
|
|
|
|
vertices[id].type = type;
|
|
|
|
}
|
2014-11-20 20:46:51 +01:00
|
|
|
if (typeof style !== 'undefined') {
|
|
|
|
if (style !== null) {
|
|
|
|
style.forEach(function (s) {
|
|
|
|
vertices[id].styles.push(s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Function called by parser when a link/edge definition has been found
|
|
|
|
* @param start
|
|
|
|
* @param end
|
|
|
|
* @param type
|
|
|
|
* @param linktext
|
|
|
|
*/
|
|
|
|
exports.addLink = function (start, end, type, linktext) {
|
|
|
|
var edge = {start: start, end: end, type: undefined, text: ''};
|
|
|
|
var linktext = type.text;
|
|
|
|
if (typeof linktext !== 'undefined') {
|
|
|
|
edge.text = linktext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof type !== 'undefined') {
|
|
|
|
edge.type = type.type;
|
|
|
|
}
|
|
|
|
edges.push(edge);
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Updates a link with a style
|
|
|
|
* @param pos
|
|
|
|
* @param style
|
|
|
|
*/
|
|
|
|
exports.updateLink = function (pos, style) {
|
|
|
|
var position = pos.substr(1);
|
|
|
|
edges[position].style = style;
|
|
|
|
};
|
2014-11-22 15:34:21 +01:00
|
|
|
|
|
|
|
exports.addClass = function (id, style) {
|
|
|
|
if (typeof classes[id] === 'undefined') {
|
|
|
|
classes[id] = {id: id, styles: []};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof style !== 'undefined') {
|
|
|
|
if (style !== null) {
|
|
|
|
style.forEach(function (s) {
|
|
|
|
classes[id].styles.push(s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-20 20:46:51 +01:00
|
|
|
/**
|
|
|
|
* Called by parser when a graph definition is found, stores the direction of the chart.
|
|
|
|
* @param dir
|
|
|
|
*/
|
|
|
|
exports.setDirection = function (dir) {
|
|
|
|
direction = dir;
|
|
|
|
};
|
2014-11-22 15:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by parser when a graph definition is found, stores the direction of the chart.
|
|
|
|
* @param dir
|
|
|
|
*/
|
|
|
|
exports.setClass = function (id,className) {
|
|
|
|
if(id.indexOf(',')>0){
|
|
|
|
id.split(',').forEach(function(id2){
|
|
|
|
if(typeof vertices[id2] !== 'undefined'){
|
|
|
|
vertices[id2].classes.push(className);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
if(typeof vertices[id] !== 'undefined'){
|
|
|
|
vertices[id].classes.push(className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-11-24 22:03:32 +01:00
|
|
|
/**
|
|
|
|
* Called by parser when a graph definition is found, stores the direction of the chart.
|
|
|
|
* @param dir
|
|
|
|
*/
|
|
|
|
exports.setClickEvent = function (id,functionName) {
|
2014-11-22 15:34:21 +01:00
|
|
|
|
2014-11-24 22:03:32 +01:00
|
|
|
|
|
|
|
if(id.indexOf(',')>0){
|
|
|
|
id.split(',').forEach(function(id2) {
|
|
|
|
if (typeof vertices[id2] !== 'undefined') {
|
|
|
|
funs.push(function () {
|
|
|
|
var elem = document.getElementById(id2);
|
|
|
|
if (elem !== null) {
|
|
|
|
elem.onclick = function () {
|
|
|
|
eval(functionName + '(\'' + id2 + '\')');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
//console.log('Checking now for ::'+id);
|
|
|
|
if(typeof vertices[id] !== 'undefined'){
|
|
|
|
funs.push(function(){
|
|
|
|
var elem = document.getElementById(id);
|
|
|
|
if(elem !== null){
|
|
|
|
//console.log('id was NOT null: '+id);
|
|
|
|
elem.onclick = function(){eval(functionName+'(\'' + id + '\')');};
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
//console.log('id was null: '+id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.bindFunctions = function(){
|
|
|
|
//setTimeout(function(){
|
|
|
|
funs.forEach(function(fun){
|
|
|
|
fun();
|
|
|
|
});
|
|
|
|
//},1000);
|
|
|
|
|
|
|
|
};
|
2014-11-20 20:46:51 +01:00
|
|
|
exports.getDirection = function () {
|
|
|
|
return direction;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Retrieval function for fetching the found nodes after parsing has completed.
|
|
|
|
* @returns {{}|*|vertices}
|
|
|
|
*/
|
|
|
|
exports.getVertices = function () {
|
|
|
|
return vertices;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieval function for fetching the found links after parsing has completed.
|
|
|
|
* @returns {{}|*|edges}
|
|
|
|
*/
|
|
|
|
exports.getEdges = function () {
|
|
|
|
return edges;
|
|
|
|
};
|
|
|
|
|
2014-11-22 15:34:21 +01:00
|
|
|
/**
|
|
|
|
* Retrieval function for fetching the found class definitions after parsing has completed.
|
|
|
|
* @returns {{}|*|classes}
|
|
|
|
*/
|
|
|
|
exports.getClasses = function () {
|
|
|
|
return classes;
|
|
|
|
};
|
|
|
|
|
2014-11-20 20:46:51 +01:00
|
|
|
/**
|
|
|
|
* Clears the internal graph db so that a new graph can be parsed.
|
|
|
|
*/
|
|
|
|
exports.clear = function () {
|
|
|
|
vertices = {};
|
2014-11-22 15:34:21 +01:00
|
|
|
classes = {};
|
2014-11-20 20:46:51 +01:00
|
|
|
edges = [];
|
2014-11-24 22:03:32 +01:00
|
|
|
funs = [];
|
2014-11-20 20:46:51 +01:00
|
|
|
};
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
exports.defaultStyle = function () {
|
|
|
|
return "fill:#ffa;stroke: #f66; stroke-width: 3px; stroke-dasharray: 5, 5;fill:#ffa;stroke: #666;";
|
|
|
|
};
|
|
|
|
|