1 if(typeof Raphael !== "undefined") { 2 // Inspiration (and code copied): http://strongriley.github.com/d3/ex/calendar.html (-ewebb 120430) 3 4 // From: http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript#quickIDX1 5 function d3_raphael_getCSSRule(ruleName, deleteFlag) { // Return requested style obejct 6 ruleName = ruleName.toLowerCase(); // Convert test string to lower case. 7 if (document.styleSheets) { // If browser can play with stylesheets 8 for (var i = 0; i < document.styleSheets.length; i++) { // For each stylesheet 9 var styleSheet = document.styleSheets[i]; // Get the current Stylesheet 10 var ii = 0; // Initialize subCounter. 11 var cssRule = false; // Initialize cssRule. 12 do { // For each rule in stylesheet 13 if (styleSheet.cssRules) { // Browser uses cssRules? 14 cssRule = styleSheet.cssRules[ii]; // Yes --Mozilla Style 15 } else { // Browser usses rules? 16 cssRule = styleSheet.rules[ii]; // Yes IE style. 17 } // End IE check. 18 if (cssRule) { // If we found a rule... 19 if (cssRule.selectorText.toLowerCase() == ruleName) { // match ruleName? 20 if (deleteFlag == 'delete') { // Yes. Are we deleteing? 21 if (styleSheet.cssRules) { // Yes, deleting... 22 styleSheet.deleteRule(ii); // Delete rule, Moz Style 23 } else { // Still deleting. 24 styleSheet.removeRule(ii); // Delete rule IE style. 25 } // End IE check. 26 return true; // return true, class deleted. 27 } else { // found and not deleting. 28 return cssRule; // return the style object. 29 } // End delete Check 30 } // End found rule name 31 } // end found cssRule 32 ii++; // Increment sub-counter 33 } while (cssRule) // end While loop 34 } // end For loop 35 } // end styleSheet ability check 36 return false; // we found NOTHING! 37 } // end getCSSRule 38 39 function d3_raphael_getCSSAttributes(selector) { 40 var rules = d3_raphael_getCSSRule(selector), 41 attributes = {}; 42 if (!rules) return false; 43 rules = rules.style.cssText.split(';'); 44 for (var i = 0; i < rules.length; i++) { 45 var rule = rules[i].split(':'); 46 if (rule[0] !== undefined && rule[1] !== undefined) 47 var key = rule[0].replace(' ',''), 48 value = rule[1].replace(' ',''); 49 attributes[key] = value; 50 } 51 return attributes; 52 } 53 54 55 Raphael.st.addClass = function(addClass, parentSelector) { 56 //Simple set Attribute class if SVG 57 if (Raphael.svg) { 58 for (var i = 0; i < this.length; i++) { 59 this[i].addClass(addClass) 60 }; 61 } 62 //For IE 63 else { 64 var sel = '.' + addClass; 65 sel = parentSelector ? parentSelector + ' ' + sel : sel; 66 var attributes = d3_raphael_getCSSAttributes(sel); 67 for (var i = 0; i < this.length; i++) { 68 this[i].attr(attributes); 69 } 70 } 71 } 72 73 Raphael.el.addClass = function(addClass, parentSelector) { 74 //easily add class 75 if (Raphael.svg) { 76 var cssClass = this.node.getAttribute('class') !== null ? this.node.getAttribute('class') + ' ' + addClass : addClass; 77 this.node.setAttribute('class', cssClass); 78 } 79 //must extract CSS requirements 80 else { 81 var sel = '.' + addClass; 82 sel = parentSelector ? parentSelector + ' ' + sel : sel; 83 84 var attributes = d3_raphael_getCSSAttributes(sel); 85 this.attr(attributes); 86 } 87 } 88 }