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 = [];
|
2015-01-07 21:02:58 +01:00
|
|
|
var subGraphs = [];
|
2015-05-15 12:11:36 +02:00
|
|
|
var subCount=0;
|
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-12-04 17:58:05 +01:00
|
|
|
|
|
|
|
if(typeof id === 'undefined'){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(id.trim().length === 0){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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') {
|
2015-05-09 19:05:47 +02:00
|
|
|
vertices[id].text = text.trim();
|
2014-11-20 20:46:51 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-12-04 17:58:05 +01:00
|
|
|
|
2014-11-20 20:46:51 +01:00
|
|
|
/**
|
|
|
|
* 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) {
|
2014-12-04 17:58:05 +01:00
|
|
|
//console.log('Got edge', start, end);
|
2014-11-20 20:46:51 +01:00
|
|
|
var edge = {start: start, end: end, type: undefined, text: ''};
|
2014-12-09 22:26:42 -08:00
|
|
|
linktext = type.text;
|
|
|
|
|
2014-11-20 20:46:51 +01:00
|
|
|
if (typeof linktext !== 'undefined') {
|
2015-05-09 19:05:47 +02:00
|
|
|
edge.text = linktext.trim();
|
2014-11-20 20:46:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof type !== 'undefined') {
|
|
|
|
edge.type = type.type;
|
2015-01-10 14:33:41 +01:00
|
|
|
edge.stroke = type.stroke;
|
2014-11-20 20:46:51 +01:00
|
|
|
}
|
|
|
|
edges.push(edge);
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Updates a link with a style
|
|
|
|
* @param pos
|
|
|
|
* @param style
|
|
|
|
*/
|
|
|
|
exports.updateLink = function (pos, style) {
|
|
|
|
var position = pos.substr(1);
|
2015-02-28 23:50:23 +01:00
|
|
|
|
|
|
|
if(pos === 'default'){
|
|
|
|
edges.defaultStyle = style;
|
|
|
|
}else{
|
|
|
|
edges[pos].style = style;
|
|
|
|
}
|
2014-11-20 20:46:51 +01:00
|
|
|
};
|
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 () {
|
2014-12-13 20:58:53 +01:00
|
|
|
eval(functionName + '(\'' + id2 + '\')'); // jshint ignore:line
|
2014-11-24 22:03:32 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}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);
|
2014-12-13 20:58:53 +01:00
|
|
|
elem.onclick = function(){eval(functionName+'(\'' + id + '\')');}; // jshint ignore:line
|
2014-11-24 22:03:32 +01:00
|
|
|
}
|
|
|
|
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 = [];
|
2015-01-07 21:02:58 +01:00
|
|
|
subGraphs = [];
|
2015-05-15 12:11:36 +02:00
|
|
|
subCount = 0;
|
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;";
|
|
|
|
};
|
2015-01-07 21:02:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the internal graph db so that a new graph can be parsed.
|
|
|
|
*/
|
2015-01-10 19:23:57 +01:00
|
|
|
exports.addSubGraph = function (list, title) {
|
2015-01-07 21:02:58 +01:00
|
|
|
function uniq(a) {
|
|
|
|
var prims = {"boolean":{}, "number":{}, "string":{}}, objs = [];
|
|
|
|
|
|
|
|
return a.filter(function(item) {
|
|
|
|
var type = typeof item;
|
2015-03-22 18:36:17 +01:00
|
|
|
if(item===' '){
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-07 21:02:58 +01:00
|
|
|
if(type in prims)
|
|
|
|
return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);
|
|
|
|
else
|
|
|
|
return objs.indexOf(item) >= 0 ? false : objs.push(item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-15 12:11:36 +02:00
|
|
|
var nodeList = [];
|
2015-01-07 21:02:58 +01:00
|
|
|
|
2015-05-15 12:11:36 +02:00
|
|
|
nodeList = uniq(nodeList.concat.apply(nodeList,list));
|
2015-01-07 21:02:58 +01:00
|
|
|
|
2015-05-15 12:11:36 +02:00
|
|
|
|
|
|
|
var subGraph = {id:'subGraph'+subCount, nodes:nodeList,title:title};
|
2015-05-26 20:41:53 +02:00
|
|
|
//console.log('subGraph:' + subGraph.title + subGraph.id);
|
|
|
|
//console.log(subGraph.nodes);
|
2015-05-15 12:11:36 +02:00
|
|
|
subGraphs.push(subGraph);
|
|
|
|
subCount = subCount + 1;
|
|
|
|
return subGraph.id;
|
2015-01-07 21:02:58 +01:00
|
|
|
};
|
2015-05-26 20:41:53 +02:00
|
|
|
|
|
|
|
var getPosForId = function(id){
|
|
|
|
var i;
|
|
|
|
for(i=0;i<subGraphs.length;i++){
|
|
|
|
if(subGraphs[i].id===id){
|
|
|
|
//console.log('Found pos for ',id,' ',i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//console.log('No pos found for ',id,' ',i);
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
var secCount = -1;
|
|
|
|
var posCrossRef = [];
|
|
|
|
var indexNodes = function (id, pos) {
|
|
|
|
var nodes = subGraphs[pos].nodes;
|
|
|
|
secCount = secCount + 1;
|
|
|
|
if(secCount>2000){
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
//var nPos = getPosForId(subGraphs[pos].id);
|
|
|
|
posCrossRef[secCount]=pos;
|
|
|
|
console.log('Setting ',' ',secCount,' to ',pos);
|
|
|
|
// Check if match
|
|
|
|
if(subGraphs[pos].id === id){
|
|
|
|
return {
|
|
|
|
result:true,
|
|
|
|
count:0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
var posCount = 1;
|
|
|
|
while(count<nodes.length){
|
|
|
|
var childPos = getPosForId(nodes[count]);
|
|
|
|
// Ignore regular nodes (pos will be -1)
|
|
|
|
if(childPos>=0){
|
|
|
|
var res = indexNodes(id,childPos);
|
|
|
|
if(res.result){
|
|
|
|
return {
|
|
|
|
result:true,
|
|
|
|
count:posCount+res.count
|
|
|
|
};
|
|
|
|
}else{
|
|
|
|
posCount = posCount + res.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = count +1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
result:false,
|
|
|
|
count:posCount
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getDepthFirstPos = function (pos) {
|
|
|
|
return posCrossRef[pos];
|
|
|
|
};
|
|
|
|
exports.indexNodes = function (id) {
|
|
|
|
secCount = -1;
|
|
|
|
if(subGraphs.length>0){
|
|
|
|
indexNodes('none',subGraphs.length-1,0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-07 21:02:58 +01:00
|
|
|
exports.getSubGraphs = function (list) {
|
|
|
|
return subGraphs;
|
|
|
|
};
|
2015-01-24 19:33:10 +01:00
|
|
|
|
|
|
|
exports.parseError = function(err,hash){
|
|
|
|
mermaid.parseError(err,hash);
|
|
|
|
};
|