class CGraphDisplay extends CGraph {
var area;
var masked;
function CGraphDisplay() {
area._width = _width;
area._height = _height;
_xscale = _yscale = 100;
if (urlToGraph != undefined) loadGraph(urlToGraph);
}
var drawAssetKind;
function onDataReady() {
createEmptyMovieClip('masked', 1);
masked.setMask(area);
masked.createEmptyMovieClip('links', 1);
masked.createEmptyMovieClip('nodes', 2);
//
if (drawAssetKind == undefined) drawAssetKind = 'UGraphDisplay';
onDataCreateShorts();
drawLinks();
drawNodes();
//
super.onDataReady();
}
function onDataCreateShorts() {
var iNode;
//
iNode = data.firstChild.firstChild
while (iNode && iNode.nodeName != 'assets') iNode = iNode.nextSibling;
if (iNode) {
data.icons = new XMLQuery('icon', iNode);
data.edges = new XMLQuery('edge', iNode);
}
//
iNode = data.firstChild.firstChild
while (iNode && iNode.nodeName != 'nodes') iNode = iNode.nextSibling;
data.nodes = iNode;
//
iNode = data.firstChild.firstChild
while (iNode && iNode.nodeName != 'links') iNode = iNode.nextSibling;
data.links = iNode;
}
function getAbsoluteX(wX) {
// trace(wX + ' > ' + Math.round(area._width * (wX / 100)));
return Math.round(area._width * (wX / 100));
}
function getAbsoluteY(wY) {
return Math.round(area._height * (wY / 100));
}
function getRelativeX(wX) {
return Math.round(100 * (wX / area._width));
}
function getRelativeY(wY) {
return Math.round(100 * (wY / area._height));
}
/*+ Icons */
function getIcon(iIconID) {
var iIcon;
if (iIconID == undefined) iIconID = data.nodes.attributes.defaulticon;
iIcon = data.icons.firstSibling;
while (iIcon && iIcon.attributes.id != iIconID) iIcon = iIcon.nextSibling;
return iIcon;
}
/*+ Nodes */
function drawNodes(oneNode) {
var iIcon;
var iNode = data.nodes;
if (oneNode instanceof XMLNode) iNode = oneNode;
else { iNode = iNode.firstChild; oneNode = true; }
//
while (iNode && oneNode) {
// resolving Icon link
iIcon = getIcon(iNode.attributes.icon);
// attaching Node
masked.nodes.attachMovie(drawAssetKind + 'Node', 'Node'+iNode.attributes.id,
masked.nodes.getNextHighestDepth(), { owner: this, node: iNode, icon: iIcon });
//
iNode = iNode.nextSibling;
oneNode = oneNode == true;
}
}
function createNewNode(nNodeAttributes) {
if (this['editOptions'].maxNodes != undefined && this['editOptions'].maxNodes <= data.nodes.childNodes.length) return;
if (nNodeAttributes == undefined) nNodeAttributes = new Object();
if (nNodeAttributes.x == undefined) nNodeAttributes.x = getRelativeX(_xmouse);
if (nNodeAttributes.y == undefined) nNodeAttributes.y = getRelativeY(_ymouse);
//
var nNode = data.createElement('node');
for (var prop in nNodeAttributes) {
nNode.attributes[prop] = nNodeAttributes[prop];
}
nNode.attributes.id = getNodeID();
nNode.attributes.name = getNodeName();
data.nodes.appendChild(nNode);
//
drawNodes(nNode);
trigger('onDataChange');
return masked.nodes['Node'+nNode.attributes.id];
}
function removeNode(wNode) {
var iLink = data.links.firstChild;
var rLink;
while (iLink) {
if (iLink.attributes.nodex == wNode.attributes.id || iLink.attributes.nodey == wNode.attributes.id) {
masked.links['Link_'+iLink.attributes.nodex+'_'+iLink.attributes.nodey].removeMovieClip();
rLink = iLink;
iLink = iLink.nextSibling;
rLink.removeNode();
} else {
iLink = iLink.nextSibling;
}
}
wNode.removeNode();
masked.nodes['Node'+wNode.attributes.id].removeMovieClip();
trigger('onDataChange');
}
function getNodeID() {
var used = [ ], min = Infinity, iNode, i;
for (iNode = data.nodes.firstChild; iNode; iNode = iNode.nextSibling) {
used[Number(iNode.attributes.id)] = true;
min = Math.min(min, Number(iNode.attributes.id));
}
if (min > 1) return 1;
if (used.length == data.nodes.childNodes.length + 1) return used.length;
for (i = 1; i < used.length; i++) {
if (used[i] !== true) return i;
}
}
function getNodeName(wName) {
var used = [ ], min = Infinity, iNode, i;
for (iNode = data.nodes.firstChild; iNode; iNode = iNode.nextSibling) {
used[Number(iNode.attributes.name)] = true;
min = Math.min(min, Number(iNode.attributes.name));
}
if (wName != undefined && used[wName] !== true) return wName;
if (min > 1) return 1;
if (used.length == data.nodes.childNodes.length + 1) return used.length;
for (i = 1; i < used.length; i++) {
if (used[i] !== true) return i;
}
}
/*+ Links */
function drawLinks(oneLink) {
var iEdge, iEdgeID;
var iNodeX, iNodeXID;
var iNodeY, iNodeYID;
var iLink = data.links;
if (oneLink instanceof XMLNode) iLink = oneLink;
else { iLink = iLink.firstChild; oneLink = true; }
//
while (iLink && oneLink) {
// resolving Edge link
iEdgeID = iLink.attributes.edge;
if (iEdgeID == undefined) iEdgeID = iLink.parentNode.attributes.defaultedge;
iEdge = data.edges.firstSibling;
while (iEdge && iEdge.attributes.id != iEdgeID) iEdge = iEdge.nextSibling;
// resolving Nodes link
iNodeXID = iLink.attributes.nodex;
iNodeX = data.nodes.firstChild;
while (iNodeX && iNodeX.attributes.id != iNodeXID) iNodeX = iNodeX.nextSibling;
iNodeYID = iLink.attributes.nodey;
iNodeY = data.nodes.firstChild;
while (iNodeY && iNodeY.attributes.id != iNodeYID) iNodeY = iNodeY.nextSibling;
// attaching Link
masked.links.attachMovie(drawAssetKind + 'Link', 'Link_'+iLink.attributes.nodex+'_'+iLink.attributes.nodey,
masked.links.getNextHighestDepth(), { owner: this, link: iLink, nodex: iNodeX, nodey: iNodeY, edge: iEdge });
//
//! trace(iLink + '\n' + iNodeX + '\n' + iNodeY + '\n---')
iLink = iLink.nextSibling;
oneLink = oneLink == true;
}
}
var pLinkAttributes;
function createNewLink(nLinkAttributes) {
if (nLinkAttributes != undefined) pLinkAttributes = nLinkAttributes;
if (pLinkAttributes.nodex == undefined) return;
if (pLinkAttributes.nodey == undefined) return;
if (pLinkAttributes.nodex == pLinkAttributes.nodey) return;
if (masked.links['Link_'+pLinkAttributes.nodex+'_'+pLinkAttributes.nodey] != undefined) return false;
if (masked.links['Link_'+pLinkAttributes.nodey+'_'+pLinkAttributes.nodex] != undefined) return false;
if (pLinkAttributes.cost == undefined) pLinkAttributes.cost = 1;
if (this['editOptions'].maxLinks != undefined && this['editOptions'].maxLinks <= data.links.childNodes.length) return;
//
var nLink = data.createElement('link');
for (var prop in pLinkAttributes) {
nLink.attributes[prop] = pLinkAttributes[prop];
}
data.links.appendChild(nLink);
//
drawLinks(nLink);
trigger('onDataChange');
return masked.links['Link_'+nLink.attributes.nodex+'_'+nLink.attributes.nodey];
}
function removeLink(wLink) {
if (wLink == undefined) {
wLink = data.links.firstChild;
while (wLink) {
if (wLink.attributes.nodex == pLinkAttributes.nodex && wLink.attributes.nodey == pLinkAttributes.nodey ||
wLink.attributes.nodex == pLinkAttributes.nodey && wLink.attributes.nodey == pLinkAttributes.nodex) {
break;
}
wLink = wLink.nextSibling;
}
if (wLink == null) return false;
}
wLink.removeNode();
masked.links['Link_'+wLink.attributes.nodex+'_'+wLink.attributes.nodey].removeMovieClip();
trigger('onDataChange');
}
function getLink(wLinkAttributes) {
if (wLinkAttributes instanceof XMLNode) {
pLinkAttributes = {
nodex: wLinkAttributes.attributes.nodex,
nodey: wLinkAttributes.attributes.nodey
};
} else if (wLinkAttributes != undefined) pLinkAttributes = wLinkAttributes;
if (pLinkAttributes.nodex == undefined) return;
if (pLinkAttributes.nodey == undefined) return;
if (pLinkAttributes.nodex == pLinkAttributes.nodey) return;
if (masked.links['Link_'+pLinkAttributes.nodex+'_'+pLinkAttributes.nodey] != undefined)
return masked.links['Link_'+pLinkAttributes.nodex+'_'+pLinkAttributes.nodey];
if (masked.links['Link_'+pLinkAttributes.nodey+'_'+pLinkAttributes.nodex] != undefined)
return masked.links['Link_'+pLinkAttributes.nodey+'_'+pLinkAttributes.nodex];
return null;
}
/*- generate */
function generate(wMinNodes, wMaxNodes, wXOffset, wYOffset, wXMLString) {
var result = true;
var tries = 0, overlaps = false;
do {
super.generate(wMinNodes, wMaxNodes, wXOffset, wYOffset, wXMLString);
overlaps = overlapping();
tries++;
} while (overlaps && (tries < 100));
if (_global.ADVANCED) {
result = 'Informaţii avansate:';
result += '\n » Graf generat în ' + tries + ' încercări.';
}
return (tries < 100) ? result : false;
}
function overlapping() {
var gphXML = data;
var nodes = gphXML.firstChild.firstChild.nextSibling, nNodes, iNode, usedNode = [ ];
var links = nodes.nextSibling, nLinks, iLink, usedLink = [ ];
var i;
// check for overlapping nodes/links
var overlaps = false;
var iNodeMC, jNode, jNodeMC;
var iLinkMC, iRotation, jLink, jLinkMC, jRotation;
for (iNode = nodes.firstChild; iNode; iNode = iNode.nextSibling) {
iNodeMC = masked.nodes['Node'+iNode.attributes.id];
for (jNode = nodes.firstChild; jNode && !overlaps; jNode = jNode.nextSibling) {
if (iNode == jNode) continue;
jNodeMC = masked.nodes['Node'+jNode.attributes.id];
if (iNodeMC.hitTest(jNodeMC)) overlaps = iNode + '\n' + jNode;
}
if (overlaps) break;
for (jLink = links.firstChild; jLink && !overlaps; jLink = jLink.nextSibling) {
jLinkMC = masked.links['Link_'+jLink.attributes.nodex+'_'+jLink.attributes.nodey].graphic.num;
jRotation = jLinkMC._rotation; jLinkMC._rotation = 0;
if (iNodeMC.hitTest(jLinkMC)) overlaps = iNode + '\n' + jLink;
jLinkMC._rotation = jRotation;
}
if (overlaps) break;
}
for (iLink = links.firstChild; iLink && !overlaps; iLink = iLink.nextSibling) {
iLinkMC = masked.links['Link_'+iLink.attributes.nodex+'_'+iLink.attributes.nodey].graphic.num;
iRotation = iLinkMC._rotation; iLinkMC._rotation = 0;
for (jNode = nodes.firstChild; jNode && !overlaps; jNode = jNode.nextSibling) {
jNodeMC = masked.nodes['Node'+jNode.attributes.id];
if (iLinkMC.hitTest(jNodeMC)) overlaps = iLink + '\n' + jNode;
}
if (overlaps) break;
for (jLink = links.firstChild; jLink && !overlaps; jLink = jLink.nextSibling) {
if (iLink == jLink) continue;
jLinkMC = masked.links['Link_'+jLink.attributes.nodex+'_'+jLink.attributes.nodey].graphic.num;
jRotation = jLinkMC._rotation; jLinkMC._rotation = 0;
if (iLinkMC.hitTest(jLinkMC)) overlaps = iLink + '\n' + jLink;
jLinkMC._rotation = jRotation;
}
if (overlaps) break;
iLinkMC._rotation = iRotation;
}
return overlaps;
}
//+ working with SubGraphs
function highlight(wColor) {
for (var i in masked.links) if (masked.links[i] instanceof MovieClip) {
masked.links[i].highlight(wColor);
}
}
function getHighlighted() {
var aSubGraph = new CSubGraph(this);
for (var i in masked.links) if (masked.links[i] instanceof MovieClip) {
if (masked.links[i].highlighted !== false) {
aSubGraph.addLink(masked.links[i].link);
}
}
return aSubGraph;
}
}