diff options
author | Patrick Klingemann <patrick.klingemann@gmail.com> | 2014-06-02 19:21:58 -0500 |
---|---|---|
committer | Patrick Klingemann <patrick.klingemann@gmail.com> | 2014-06-02 19:21:58 -0500 |
commit | 5fd4a57bfd339f01221d2b51abb03465012e7828 (patch) | |
tree | 1ff64b6245565579be227c93df7a12d5ba064065 /src | |
parent | 8e44d115af4907276cb23567dc2d2a10d0efdf2f (diff) | |
download | svg.js-5fd4a57bfd339f01221d2b51abb03465012e7828.tar.gz svg.js-5fd4a57bfd339f01221d2b51abb03465012e7828.zip |
add classes, hasClass, addClass, removeClass, toggleClass functions to Element
Diffstat (limited to 'src')
-rwxr-xr-x | src/element.js | 42 |
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 +}) |