From: wout Date: Mon, 31 Dec 2012 13:43:26 +0000 (+0100) Subject: Added position() X-Git-Tag: 0.2~39 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8dbe3599dd9b80738c4124f7e71bc12e763c50dc;p=svg.js.git Added position() --- diff --git a/README.md b/README.md index 857bf8d..554303e 100644 --- a/README.md +++ b/README.md @@ -356,6 +356,14 @@ rect.forward(); rect.backward(); ``` +The arrange.js module brings some additional methods: + +```javascript +// returns the position (a number) of the element between its siblings +rect.position(); +``` + + _This functionality requires the arrange.js module which is included in the default distribution._ diff --git a/src/arrange.js b/src/arrange.js index ac53e43..ff255be 100644 --- a/src/arrange.js +++ b/src/arrange.js @@ -7,6 +7,11 @@ SVG.extend(SVG.Element, { return this.parent.children(); }, + // get the curent position siblings + position: function() { + return this.siblings().indexOf(this); + }, + // send given element one step forwards forward: function() { var i = this.siblings().indexOf(this); @@ -18,7 +23,7 @@ SVG.extend(SVG.Element, { backward: function() { var i, p = this.parent.level(); - i = this.siblings().indexOf(this); + i = this.position(); if (i > 1) p.remove(this).add(this, i - 1); @@ -33,11 +38,9 @@ SVG.extend(SVG.Element, { // send given element all the way to the back back: function() { - var i, p = this.parent.level(); + var p = this.parent.level(); - i = this.siblings().indexOf(this); - - if (i > 1) + if (this.position() > 1) p.remove(this).add(this, 0); return this; diff --git a/src/element.js b/src/element.js index 3f1afa3..a9cd7cd 100644 --- a/src/element.js +++ b/src/element.js @@ -43,6 +43,7 @@ SVG.extend(SVG.Element, { var c, n = this.node.nodeName; + // invoke shape method with shape-specific arguments c = n == 'rect' ? this.parent[n](this.attrs.width, this.attrs.height) : n == 'ellipse' ? @@ -53,7 +54,11 @@ SVG.extend(SVG.Element, { this.parent[n](this.content) : this.parent[n](); - return c.attr(this.attrs); + // copy translations + c.trans = this.trans; + + // apply attributes and translations + return c.attr(this.attrs).transform({}); }, // remove element