aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorWout <wout@impinc.co.uk>2014-06-10 13:39:15 +0200
committerWout <wout@impinc.co.uk>2014-06-10 13:39:15 +0200
commit7e6b5edfdd08dd28120442a2c3528c3d94c346c3 (patch)
tree6693bb631dab6a126fdc214d537856c4ae9ac8f7 /src
parent8e44d115af4907276cb23567dc2d2a10d0efdf2f (diff)
parentc13717a8d8d85e5d1d408e27a6db881fe773cc04 (diff)
downloadsvg.js-1.0.0-rc.7.tar.gz
svg.js-1.0.0-rc.7.zip
Merge pull request #200 from pklingem/master1.0.0-rc.7
add class manipulation functions to Element
Diffstat (limited to 'src')
-rwxr-xr-xsrc/element.js45
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
+})