From 58b1a0b92ba78e0ce2047e41caa02f65d16cd557 Mon Sep 17 00:00:00 2001 From: wout Date: Thu, 26 Jun 2014 08:16:14 +0200 Subject: Complete new clone() method --- src/element.js | 31 +------------------------------ src/helpers.js | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/element.js b/src/element.js index 753ac33..069c487 100755 --- a/src/element.js +++ b/src/element.js @@ -67,36 +67,7 @@ SVG.Element = SVG.invent({ } // Clone element , clone: function() { - var clone , attr - , type = this.type - - /* invoke shape method with shape-specific arguments */ - clone = type == 'rect' || type == 'ellipse' ? - this.parent()[type](0,0) : - type == 'line' ? - this.parent()[type](0,0,0,0) : - type == 'image' ? - this.parent()[type](this.src) : - type == 'text' ? - this.parent()[type](this.content) : - type == 'path' ? - this.parent()[type](this.attr('d')) : - type == 'polyline' || type == 'polygon' ? - this.parent()[type](this.attr('points')) : - type == 'g' ? - this.parent().group() : - this.parent()[type]() - - /* apply attributes attributes */ - attr = this.attr() - delete attr.id - clone.attr(attr) - - /* copy transformations */ - clone.trans = this.trans - - /* apply attributes and translations */ - return clone.transform({}) + return assignNewId(this.node.cloneNode(true)) } // Remove element , remove: function() { diff --git a/src/helpers.js b/src/helpers.js index 492039c..ad73fde 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -91,6 +91,19 @@ function arrayToString(a) { return s + ' ' } +// Deep new id assignment +function assignNewId(node) { + // Adopt element and assign new id + var element = SVG.adopt(node).id(SVG.eid(node.nodeName)) + + // Do the same for SVG child nodes as well + for (var i = node.childNodes.length - 1; i >= 0; i--) + if (node.childNodes[i] instanceof SVGElement) + assignNewId(node.childNodes[i]) + + return element +} + // Add more bounding box properties function boxProperties(b) { b.x2 = b.x + b.width @@ -102,10 +115,10 @@ function boxProperties(b) { // Parse a matrix string function parseMatrix(o) { if (o.matrix) { - /* split matrix string */ + // Split matrix string var m = o.matrix.replace(/\s/g, '').split(',') - /* pasrse values */ + // Pasrse values if (m.length == 6) { o.a = parseFloat(m[0]) o.b = parseFloat(m[1]) -- cgit v1.2.3