aboutsummaryrefslogtreecommitdiffstats
path: root/src/element.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/element.js')
-rwxr-xr-xsrc/element.js42
1 files changed, 41 insertions, 1 deletions
diff --git a/src/element.js b/src/element.js
index 52f9ad7..bd7e55a 100755
--- a/src/element.js
+++ b/src/element.js
@@ -341,6 +341,46 @@ SVG.Element = SVG.invent({
, toString: function() {
return this.attr('id')
}
+ // Return array of classes on the node
+ , classes: function() {
+ classAttr = this.node.getAttribute('class')
+ if (classAttr === null) {
+ return []
+ } else {
+ return classAttr.split(/\s+/)
+ }
+ }
+ // Return true if class exists on the node, false otherwise
+ , hasClass: function(className) {
+ return this.classes().indexOf(className) != -1
+ }
+ // Add class to the node
+ , addClass: function(className) {
+ if (!(this.hasClass(className))) {
+ classArray = this.classes()
+ classArray.push(className)
+ this.node.setAttribute('class', classArray.join(' '))
+ }
+ return this
+ }
+ // Remove class from the node
+ , removeClass: function(className) {
+ if (this.hasClass(className)) {
+ classArray = this.classes().filter(function(c) {
+ return c != className
+ })
+ this.node.setAttribute('class', classArray.join(' '))
+ }
+ return this
+ }
+ // Toggle the presence of a class on the node
+ , toggleClass: function(className) {
+ if (this.hasClass(className)) {
+ this.removeClass(className)
+ } else {
+ this.addClass(className)
+ }
+ }
// Private: find svg parent by instance
, _parent: function(parent) {
var element = this
@@ -351,4 +391,4 @@ SVG.Element = SVG.invent({
return element
}
}
-}) \ No newline at end of file
+})