diff options
Diffstat (limited to 'src/parent.js')
-rwxr-xr-x | src/parent.js | 188 |
1 files changed, 96 insertions, 92 deletions
diff --git a/src/parent.js b/src/parent.js index 4436d72..44514c2 100755 --- a/src/parent.js +++ b/src/parent.js @@ -1,102 +1,106 @@ -SVG.Parent = function(element) { - this.constructor.call(this, element) -} +SVG.Parent = SVG.invent({ + // Initialize node + create: function(element) { + this.constructor.call(this, element) + } -// Inherit from SVG.Element -SVG.Parent.prototype = new SVG.Element + // Inherit from +, inherit: 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) - element.parent.children().splice(element.parent.index(element), 1) - - /* add element references */ - this.children().splice(i, 0, element) - this.node.insertBefore(element.node, this.node.childNodes[i] || null) - element.parent = this + // Add class methods +, extend: { + // 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) + element.parent.children().splice(element.parent.index(element), 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) + /* reposition defs */ + if (this._defs) { + this.node.removeChild(this._defs.node) + this.node.appendChild(this._defs.node) + } + + return this } - - 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.index(element) >= 0 - } - // Gets index of given element -, index: function(element) { - return this.children().indexOf(element) - } - // 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) + // Basically does the same as `add()` but returns the added element instead + , put: function(element, i) { + this.add(element, i) + return element } - - return this - } - // Remove a child element at a position -, removeElement: function(element) { - this.children().splice(this.index(element), 1) - this.node.removeChild(element.node) - element.parent = null + // Checks if the given element is a child + , has: function(element) { + return this.index(element) >= 0 + } + // Gets index of given element + , index: function(element) { + return this.children().indexOf(element) + } + // 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 all elements in this container -, clear: function() { - /* remove children */ - for (var i = this.children().length - 1; i >= 0; i--) - this.removeElement(this.children()[i]) + return this + } + // Remove a child element at a position + , removeElement: function(element) { + this.children().splice(this.index(element), 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() + /* remove defs node */ + if (this._defs) + this._defs.clear() - return this - } - , // Get defs - defs: function() { - return this.doc().defs() + return this + } + , // Get defs + defs: function() { + return this.doc().defs() + } } -})
\ No newline at end of file + +}) |