diff options
Diffstat (limited to 'src/element.js')
-rwxr-xr-x | src/element.js | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/src/element.js b/src/element.js index 52f9ad7..9c52dd0 100755 --- a/src/element.js +++ b/src/element.js @@ -341,6 +341,49 @@ SVG.Element = SVG.invent({ , toString: function() { return this.attr('id') } + // Return array of classes on the node + , classes: function() { + var classAttr = this.node.getAttribute('class') + if (classAttr === null) { + return [] + } else { + return classAttr.trim().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) { + var classArray + 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) { + var classArray + 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) + } + return this + } // Private: find svg parent by instance , _parent: function(parent) { var element = this @@ -351,4 +394,4 @@ SVG.Element = SVG.invent({ return element } } -})
\ No newline at end of file +}) |