From ce3f77f6236b92cd6d8bb6aa00fe7297bf1a1c4b Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Wed, 15 Aug 2018 18:54:47 +0200 Subject: [PATCH] :sparkles: Added support for and options --- src/pattern.js | 23 ++++++++++++++++++++++- src/svg.js | 5 +++-- tests/pattern.test.js | 6 +++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/pattern.js b/src/pattern.js index 2457c30432d..02756f8a37d 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -249,7 +249,7 @@ Pattern.prototype.pack = function() { let part = this.parts[key]; // Avoid multiple render calls to cause stacking of transforms part.attributes.set("transform", ""); - if (part.render) { + if (part.render && this.inScope(key)) { part.stack(); bins.push({ id: key, @@ -269,3 +269,24 @@ Pattern.prototype.pack = function() { return this; }; + +/** Determines whether a part is in scope + * Scope depends on the 'only' setting people can pass + * with the name of a part, or an array of parts + * The absence of only means all parts + * + * If partName is an array of names, any name being in + * scope will cause this to return true + */ +Pattern.prototype.inScope = function(partName) { + if (typeof partName !== "string") { + for (let part of partName) { + if (this.inScope(part)) return true; + } + return false; + } + if (typeof this.settings.only === "undefined") return true; + else if (this.settings.only === partName) return true; + else if (this.settings.only.indexOf(partName) !== -1) return true; + else return false; +}; diff --git a/src/svg.js b/src/svg.js index 79c084b187d..3af63cd82f4 100644 --- a/src/svg.js +++ b/src/svg.js @@ -50,7 +50,8 @@ Svg.prototype.render = function(pattern) { this.preRender(); this.attributes.add("width", pattern.width + "mm"); this.attributes.add("height", pattern.height + "mm"); - this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`); + if (!pattern.settings.embed) + this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`); this.svg = this.prefix; this.svg += this.renderComments(this.header); this.svg += this.renderSvgTag(pattern); @@ -60,7 +61,7 @@ Svg.prototype.render = function(pattern) { this.svg += this.openGroup("draftContainer"); for (let partId in pattern.parts) { let part = pattern.parts[partId]; - if (part.render) { + if (part.render && pattern.inScope(partId)) { this.svg += this.openGroup(this.getUid(), part.attributes); this.svg += this.renderPart(part); this.svg += this.closeGroup(); diff --git a/tests/pattern.test.js b/tests/pattern.test.js index c7dafb6df1e..3183a36a7e3 100644 --- a/tests/pattern.test.js +++ b/tests/pattern.test.js @@ -132,21 +132,21 @@ it("Should register a hook from a plugin", () => { it("Should check whether a part is in scope", () => { let pattern = new freesewing.Pattern(); - pattern.settings.scope = "test"; + pattern.settings.only = "test"; expect(pattern.inScope("test")).to.equal(true); expect(pattern.inScope("mist")).to.equal(false); }); it("Should check whether an array of parts is in scope", () => { let pattern = new freesewing.Pattern(); - pattern.settings.scope = "test"; + pattern.settings.only = "test"; expect(pattern.inScope(["foo", "bar", "test"])).to.equal(true); expect(pattern.inScope(["foo", "bar", "mist"])).to.equal(false); }); it("Should check whether a parts is in a scope array", () => { let pattern = new freesewing.Pattern(); - pattern.settings.scope = ["test", "foo", "bar"]; + pattern.settings.only = ["test", "foo", "bar"]; expect(pattern.inScope("foo")).to.equal(true); expect(pattern.inScope(["bar"])).to.equal(true); expect(pattern.inScope(["mest", "foo"])).to.equal(true);