1
0
Fork 0

Added support for and options

This commit is contained in:
Joost De Cock 2018-08-15 18:54:47 +02:00
parent 13e06162bc
commit ce3f77f623
3 changed files with 28 additions and 6 deletions

View file

@ -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;
};

View file

@ -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();

View file

@ -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);