Added class definitions to nodes

Removed the inline styling of nodes with classes, replaced with class
attributes and a common CSS Style section at the start of the SVG.
This commit is contained in:
Björn Weström
2014-12-14 00:32:27 +01:00
parent 71fd8e1b70
commit 3fcbc7ae56
10 changed files with 238 additions and 91 deletions

View File

@@ -25,3 +25,31 @@ module.exports.detectType = function(text,a){
return "graph";
};
/**
* Copies all relevant CSS content into the graph SVG.
* This allows the SVG to be copied as is while keeping class based styling
* @param {element} svg The root element of the SVG
* @param {string} defaultStyle Default style definitions (for elements without classes)
*/
module.exports.cloneCssStyles = function(svg, defaultStyle){
var used = "";
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
var rules = sheets[i].cssRules;
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
if (typeof(rule.style) != "undefined") {
var elems = svg.querySelectorAll(rule.selectorText);
if (elems.length > 0) {
used += rule.selectorText + " { " + rule.style.cssText + " }\n";
}
}
}
}
var s = document.createElement('style');
s.setAttribute('type', 'text/css');
s.innerHTML = "/* <![CDATA[ */\n" + defaultStyle + "\n" + used + "\n/* ]]> */";
svg.insertBefore(s, svg.firstChild);
};