};
//draw method
var drawLineRight = function (x, y, color) {
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x, y + config.unitSize / 2);
ctx.lineTo(x + config.unitSize, y + config.unitSize / 2);
ctx.stroke();
};
var drawLineUp = function (x, y, color) {
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x, y + config.unitSize / 2);
ctx.lineTo(x, y - config.unitSize / 2);
ctx.stroke();
};
var drawNode = function (x, y, color) {
ctx.strokeStyle = color;
drawLineUp(x, y, color);
ctx.beginPath();
ctx.arc(x, y, config.nodeRadius, 0, Math.PI * 2, true);
ctx.fill();
};
var drawLineIn = function (x, y, color) {
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x + config.unitSize, y + config.unitSize / 2);
ctx.lineTo(x, y - config.unitSize / 2);
ctx.stroke();
};
var drawLineOut = function (x, y, color) {
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x, y + config.unitSize / 2);
ctx.lineTo(x + config.unitSize, y - config.unitSize / 2);
ctx.stroke();
};
var draw = function (graphList) {
var colomn, colomnIndex, prevColomn, condenseIndex;
var x, y;
var color;
var nodePos, outPos;
var tempFlow;
var prevRowLength = 0;
var flowSwapPos = -1;
var lastLinePos;
var i, k, l;
var condenseCurrentLength, condensePrevLength = 0, condenseNextLength = 0;
var inlineIntersect = false;
//initiate for first row
for (i = 0, l = graphList[0].length; i < l; i++) {
if (graphList[0][i] !== "_" && graphList[0][i] !== " ") {
flows.push(genNewFlow());
}
}
y = (canvas.height / ratio) - config.unitSize * 0.5;
//iterate
for (i = 0, l = graphList.length; i < l; i++) {
x = config.unitSize * 0.5;
var currentRow = graphList[i];
var nextRow = graphList[i + 1];
var prevRow = graphList[i - 1];
flowSwapPos = -1;
condenseCurrentLength = currentRow.filter(function (val) {
return (val !== " " && val !== "_")
}).length;
if (nextRow) {
condenseNextLength = nextRow.filter(function (val) {
return (val !== " " && val !== "_")
}).length;
} else {
condenseNextLength = 0;
}
//pre process begin
//use last row for analysing
if (prevRow) {
if (!inlineIntersect) {
//intersect might happen
for (colomnIndex = 0; colomnIndex < prevRowLength; colomnIndex++) {
if (prevRow[colomnIndex + 1] &&
(prevRow[colomnIndex] === "/" && prevRow[colomnIndex + 1] === "|") ||
((prevRow[colomnIndex] === "_" && prevRow[colomnIndex + 1] === "|") &&
(prevRow[colomnIndex + 2] === "/"))) {
flowSwapPos = colomnIndex;
//swap two flow
tempFlow = {id: flows[flowSwapPos].id, color: flows[flowSwapPos].color};
flows[flowSwapPos].id = flows[flowSwapPos + 1].id;
flows[flowSwapPos].color = flows[flowSwapPos + 1].color;
flows[flowSwapPos + 1].id = tempFlow.id;
flows[flowSwapPos + 1].color = tempFlow.color;
}
}
}
if (condensePrevLength < condenseCurrentLength &&
((nodePos = findColomn("*", currentRow)) !== -1 &&
(findColomn("_", currentRow) === -1))) {
flows.splice(nodePos - 1, 0, genNewFlow());
}
if (prevRowLength > currentRow.length &&
(nodePos = findColomn("*", prevRow)) !== -1) {
if (findColomn("_", currentRow) === -1 &&
findColomn("/", currentRow) === -1 &&
findColomn("\\", currentRow) === -1) {
flows.splice(nodePos + 1, 1);
}
}
} //done with the previous row
prevRowLength = currentRow.length; //store for next round
colomnIndex = 0; //reset index
condenseIndex = 0;
condensePrevLength = 0;
while (colomnIndex < currentRow.length) {
colomn = currentRow[colomnIndex];
if (colomn !== " " && colomn !== "_") {
++condensePrevLength;
}
if (colomn === " " &&
currentRow[colomnIndex + 1] &&
currentRow[colomnIndex + 1] === "_" &&
currentRow[colomnIndex - 1] &&
currentRow[colomnIndex - 1] === "|") {
currentRow.splice(colomnIndex, 1);
currentRow[colomnIndex] = "/";
colomn = "/";
}
//create new flow only when no intersetc happened
if (flowSwapPos === -1 &&
colomn === "/" &&
currentRow[colomnIndex - 1] &&
currentRow[colomnIndex - 1] === "|") {
flows.splice(condenseIndex, 0, genNewFlow());
}
//change \ and / to | when it's in the last position of the whole row
if (colomn === "/" || colomn === "\\") {
if (!(colomn === "/" && findBranchOut(nextRow) === -1)) {
if ((lastLinePos = Math.max(findColomn("|", currentRow),
findColomn("*", currentRow))) !== -1 &&
(lastLinePos < colomnIndex - 1)) {
while (currentRow[++lastLinePos] === " ") {
}
if (lastLinePos === colomnIndex) {
currentRow[colomnIndex] = "|";
}
}
}
}
if (colomn === "*" &&
prevRow &&
prevRow[condenseIndex + 1] === "\\") {
flows.splice(condenseIndex + 1, 1);
}
if (colomn !== " ") {
++condenseIndex;
}
++colomnIndex;
}
condenseCurrentLength = currentRow.filter(function (val) {
return (val !== " " && val !== "_")
}).length;
//do some clean up
if (flows.length > condenseCurrentLength) {
flows.splice(condenseCurrentLength, flows.length - condenseCurrentLength);
}
colomnIndex = 0;
//a little inline analysis and draw process
while (colomnIndex < currentRow.length) {
colomn = currentRow[colomnIndex];
prevColomn = currentRow[colomnIndex - 1];
if (currentRow[colomnIndex] === " ") {
currentRow.splice(colomnIndex, 1);
x += config.unitSize;
continue;
}
//inline interset
if ((colomn === "_" || colomn === "/") &&
currentRow[colomnIndex - 1] === "|" &&
currentRow[colomnIndex - 2] === "_") {
inlineIntersect = true;
tempFlow = flows.splice(colomnIndex - 2, 1)[0];
flows.splice(colomnIndex - 1, 0, tempFlow);
currentRow.splice(colomnIndex - 2, 1);
colomnIndex = colomnIndex - 1;
} else {
inlineIntersect = false;
}
color = flows[colomnIndex].color;
switch (colomn) {
case "_" :
drawLineRight(x, y, color);
x += config.unitSize;
break;
case "*" :
drawNode(x, y, color);
break;
case "|" :
drawLineUp(x, y, color);
break;
case "/" :
if (prevColomn &&
(prevColomn === "/" ||
prevColomn === " ")) {
x -= config.unitSize;
}
drawLineOut(x, y, color);
x += config.unitSize;
break;
case "\\" :
drawLineIn(x, y, color);
break;
}
++colomnIndex;
}
y -= config.unitSize;
}
};
init();
draw(graphList);