diff --git a/src/attributes.js b/src/attributes.js index ab8789c4bfc..f091efbf65a 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -18,6 +18,13 @@ attributes.prototype.add = function(name, value) { return this; }; +/** Sets an attribute, overwriting existing value */ +attributes.prototype.set = function(name, value) { + this.list[name] = [value]; + + return this; +}; + /** Retrieves an attribute */ attributes.prototype.get = function(name) { if (typeof this.list[name] === "undefined") return false; diff --git a/src/part.js b/src/part.js index 64bde56850f..d6d9adcae03 100644 --- a/src/part.js +++ b/src/part.js @@ -6,18 +6,16 @@ import attributes from "./attributes"; import * as hooklib from "hooks"; import { round, units } from "./utils"; -function part(id) { +function part() { this.attributes = new attributes(); this.points = {}; this.paths = {}; this.snippets = {}; - //this.id = id; this.freeId = 0; this.topLeft = false; this.bottomRight = false; this.width = false; this.height = false; - //this.render = id.substr(0, 1) === "_" ? false : true; this.render = true; this.points.origin = new point(0, 0); for (let k in hooklib) this[k] = hooklib[k]; @@ -99,45 +97,24 @@ part.prototype.stack = function() { }; /** Adds an attribute. This is here to make this call chainable in assignment */ -part.prototype.attr = function(name, value) { - this.attributes.add(name, value); +part.prototype.attr = function(name, value, overwrite = false) { + if (overwrite) this.attributes.set(name, value); + else this.attributes.add(name, value); return this; }; -/** Returns a (deep) clone of this part object */ -part.prototype.clone = function() {}; +/** Copies point/path/snippet data from part orig into this */ +part.prototype.copy = function(orig) { + for (let type of ["points", "paths", "snippets"]) { + for (let i in orig[type]) { + if (typeof this[type][i] === "undefined") { + this[type][i] = orig[type][i].clone(); + } + } + } -/** Returns a deep copy of this */ -part.prototype.clone = function(id = false) { - let clone = new part(id); - clone.freeId = 0; - clone.width = this.width; - clone.height = this.height; - clone.attributes = this.attributes.clone(); - clone.render = this.render; - - if (!id) clone.id = this.id; - if (this.topLeft) clone.topLeft = this.topLeft.clone(); - else clone.topLeft = false; - if (this.bottomRight) clone.bottomRight = this.bottomRight.clone(); - else clone.bottomRight = false; - - clone.points = {}; - clone.paths = {}; - clone.snippets = {}; - for (let i in this.points) clone.points[i] = this.points[i].clone(); - for (let i in this.paths) clone.paths[i] = this.paths[i].clone(); - for (let i in this.snippets) clone.snippets[i] = this.snippets[i].clone(); - for (let k in hooklib) clone[k] = hooklib[k]; - - clone.point = point; - clone.path = path; - clone.snippet = snippet; - clone.round = round; - - clone.context = this.context; - - return clone; + return this; }; + export default part; diff --git a/src/path.js b/src/path.js index ebf01173fb7..4e329cf79b4 100644 --- a/src/path.js +++ b/src/path.js @@ -40,8 +40,9 @@ path.prototype.close = function() { }; /** Adds an attribute. This is here to make this call chainable in assignment */ -path.prototype.attr = function(name, value) { - this.attributes.add(name, value); +path.prototype.attr = function(name, value, overwrite = false) { + if (overwrite) this.attributes.set(name, value); + else this.attributes.add(name, value); return this; }; diff --git a/src/pattern.js b/src/pattern.js index b479275531d..4dbad6644cf 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -115,7 +115,7 @@ pattern.prototype.pack = function() { if (part.render) { part.stack(); bins.push({ - id: part.id, + id: key, width: part.bottomRight.x - part.topLeft.x, height: part.bottomRight.y - part.topLeft.y }); diff --git a/src/point.js b/src/point.js index 113ae14f383..e58799790a2 100644 --- a/src/point.js +++ b/src/point.js @@ -18,8 +18,9 @@ point.prototype.deg2rad = function(degrees) { }; /** Adds an attribute. This is here to make this call chainable in assignment */ -point.prototype.attr = function(name, value) { - this.attributes.add(name, value); +point.prototype.attr = function(name, value, overwrite = false) { + if (overwrite) this.attributes.set(name, value); + else this.attributes.add(name, value); return this; }; diff --git a/src/svg.js b/src/svg.js index 81dbd6be4cd..d53762bbe81 100644 --- a/src/svg.js +++ b/src/svg.js @@ -54,7 +54,7 @@ svg.prototype.render = function(pattern) { for (let partId in pattern.parts) { let part = pattern.parts[partId]; if (part.render) { - this.svg += this.openGroup(part.id, part.attributes); + this.svg += this.openGroup(this.getUid(), part.attributes); this.svg += this.renderPart(part); this.svg += this.closeGroup(); }