diff options
author | wout <wout@impinc.co.uk> | 2013-08-05 14:08:36 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2013-08-05 14:08:36 +0100 |
commit | 945c1a0a423cf8a0b80fc8288651e04dec8b6610 (patch) | |
tree | d17c0f9ea6c4755d8d7169c2131f22990238a946 /src | |
parent | 2942e015a826e126d4c6bd4a5ae4a8f95673d0b0 (diff) | |
download | svg.js-945c1a0a423cf8a0b80fc8288651e04dec8b6610.tar.gz svg.js-945c1a0a423cf8a0b80fc8288651e04dec8b6610.zip |
Distinguished SVG.Container from SVG.parent, bumped to v0.32
SVG.Use inherits from SVG.Shape
Diffstat (limited to 'src')
-rw-r--r-- | src/container.js | 98 | ||||
-rw-r--r-- | src/image.js | 2 | ||||
-rw-r--r-- | src/parent.js | 102 | ||||
-rw-r--r-- | src/sugar.js | 2 | ||||
-rw-r--r-- | src/use.js | 2 |
5 files changed, 107 insertions, 99 deletions
diff --git a/src/container.js b/src/container.js index e42001f..60f62c4 100644 --- a/src/container.js +++ b/src/container.js @@ -2,93 +2,13 @@ SVG.Container = function(element) { this.constructor.call(this, element) } -// Inherit from SVG.Element -SVG.Container.prototype = new SVG.Element +// Inherit from SVG.Parent +SVG.Container.prototype = new SVG.Parent // SVG.extend(SVG.Container, { - // Returns all child elements - children: function() { - return this._children || (this._children = []) - } - // Add given element at a position -, add: function(element, i) { - if (!this.has(element)) { - /* define insertion index if none given */ - i = i == null ? this.children().length : i - - /* remove references from previous parent */ - if (element.parent) { - var index = element.parent.children().indexOf(element) - element.parent.children().splice(index, 1) - } - - /* add element references */ - this.children().splice(i, 0, element) - this.node.insertBefore(element.node, this.node.childNodes[i] || null) - element.parent = this - } - - /* reposition defs */ - if (this._defs) { - this.node.removeChild(this._defs.node) - this.node.appendChild(this._defs.node) - } - - return this - } - // Basically does the same as `add()` but returns the added element instead -, put: function(element, i) { - this.add(element, i) - return element - } - // Checks if the given element is a child -, has: function(element) { - return this.children().indexOf(element) >= 0 - } - // Get a element at the given index -, get: function(i) { - return this.children()[i] - } - // Iterates over all children and invokes a given block -, each: function(block, deep) { - var i, il - , children = this.children() - - for (i = 0, il = children.length; i < il; i++) { - if (children[i] instanceof SVG.Element) - block.apply(children[i], [i, children]) - - if (deep && (children[i] instanceof SVG.Container)) - children[i].each(block, deep) - } - - return this - } - // Remove a child element at a position -, removeElement: function(element) { - var i = this.children().indexOf(element) - - this.children().splice(i, 1) - this.node.removeChild(element.node) - element.parent = null - - return this - } - // Get defs -, defs: function() { - return this.doc().defs() - } - // Get first child, skipping the defs node -, first: function() { - return this.children()[0] instanceof SVG.Defs ? this.children()[1] : this.children()[0] - } - // Get the last child -, last: function() { - return this.children()[this.children().length - 1] - } // Get the viewBox and calculate the zoom value -, viewbox: function(v) { + viewbox: function(v) { if (arguments.length == 0) /* act as a getter if there are no arguments */ return new SVG.ViewBox(this) @@ -100,17 +20,5 @@ SVG.extend(SVG.Container, { return this.attr('viewBox', v) } - // Remove all elements in this container -, clear: function() { - /* remove children */ - for (var i = this.children().length - 1; i >= 0; i--) - this.removeElement(this.children()[i]) - - /* remove defs node */ - if (this._defs) - this._defs.clear() - - return this - } })
\ No newline at end of file diff --git a/src/image.js b/src/image.js index 24806ff..d0a1b40 100644 --- a/src/image.js +++ b/src/image.js @@ -7,12 +7,10 @@ SVG.Image.prototype = new SVG.Shape // SVG.extend(SVG.Image, { - // (re)load image load: function(url) { return (url ? this.attr('href', (this.src = url), SVG.xlink) : this) } - }) // diff --git a/src/parent.js b/src/parent.js new file mode 100644 index 0000000..8b11f5f --- /dev/null +++ b/src/parent.js @@ -0,0 +1,102 @@ +SVG.Parent = function(element) { + this.constructor.call(this, element) +} + +// Inherit from SVG.Element +SVG.Parent.prototype = new SVG.Element + +// +SVG.extend(SVG.Parent, { + // Returns all child elements + children: function() { + return this._children || (this._children = []) + } + // Add given element at a position +, add: function(element, i) { + if (!this.has(element)) { + /* define insertion index if none given */ + i = i == null ? this.children().length : i + + /* remove references from previous parent */ + if (element.parent) { + var index = element.parent.children().indexOf(element) + element.parent.children().splice(index, 1) + } + + /* add element references */ + this.children().splice(i, 0, element) + this.node.insertBefore(element.node, this.node.childNodes[i] || null) + element.parent = this + } + + /* reposition defs */ + if (this._defs) { + this.node.removeChild(this._defs.node) + this.node.appendChild(this._defs.node) + } + + return this + } + // Basically does the same as `add()` but returns the added element instead +, put: function(element, i) { + this.add(element, i) + return element + } + // Checks if the given element is a child +, has: function(element) { + return this.children().indexOf(element) >= 0 + } + // Get a element at the given index +, get: function(i) { + return this.children()[i] + } + // Get first child, skipping the defs node +, first: function() { + return this.children()[0] + } + // Get the last child +, last: function() { + return this.children()[this.children().length - 1] + } + // Iterates over all children and invokes a given block +, each: function(block, deep) { + var i, il + , children = this.children() + + for (i = 0, il = children.length; i < il; i++) { + if (children[i] instanceof SVG.Element) + block.apply(children[i], [i, children]) + + if (deep && (children[i] instanceof SVG.Container)) + children[i].each(block, deep) + } + + return this + } + // Remove a child element at a position +, removeElement: function(element) { + var i = this.children().indexOf(element) + + this.children().splice(i, 1) + this.node.removeChild(element.node) + element.parent = null + + return this + } + // Remove all elements in this container +, clear: function() { + /* remove children */ + for (var i = this.children().length - 1; i >= 0; i--) + this.removeElement(this.children()[i]) + + /* remove defs node */ + if (this._defs) + this._defs.clear() + + return this + } + , // Get defs + defs: function() { + return this.doc().defs() + } +})
\ No newline at end of file diff --git a/src/sugar.js b/src/sugar.js index 57682e8..9577d94 100644 --- a/src/sugar.js +++ b/src/sugar.js @@ -27,7 +27,7 @@ var _colorPrefix = function(type, attr) { return this } - SVG.extend(SVG.Shape, SVG.FX, extension) + SVG.extend(SVG.Element, SVG.FX, extension) }) @@ -3,7 +3,7 @@ SVG.Use = function() { } // Inherit from SVG.Shape -SVG.Use.prototype = new SVG.Element +SVG.Use.prototype = new SVG.Shape // SVG.extend(SVG.Use, { |