2018-12-16 18:06:01 +01:00
|
|
|
import { macroName, round, sampleStyle, capitalize } from "./utils";
|
2018-08-05 18:19:48 +02:00
|
|
|
import Part from "./part";
|
|
|
|
import Point from "./point";
|
|
|
|
import Path from "./path";
|
|
|
|
import Snippet from "./snippet";
|
|
|
|
import Svg from "./svg";
|
2018-08-01 18:18:29 +02:00
|
|
|
import pack from "bin-pack";
|
2018-08-05 18:19:48 +02:00
|
|
|
import Store from "./store";
|
2018-12-09 14:17:46 +01:00
|
|
|
import hooks from "./hooks";
|
2018-12-18 15:35:07 +01:00
|
|
|
import Attributes from "./attributes";
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-12-20 14:39:24 +01:00
|
|
|
export default function Pattern(config = { options: {} }) {
|
|
|
|
this.config = config; // Pattern configuration
|
2018-12-11 18:49:00 +01:00
|
|
|
this.width = false; // Will be set after render
|
|
|
|
this.height = false; // Will be set after render
|
|
|
|
this.is = ""; // Will be set when drafting/sampling
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-12-11 18:49:00 +01:00
|
|
|
this.store = new Store(); // Store for sharing data across parts
|
|
|
|
this.parts = {}; // Parts container
|
|
|
|
this.hooks = hooks; // Hooks container
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-12-11 18:49:00 +01:00
|
|
|
this.Point = Point; // Point constructor
|
|
|
|
this.Path = Path; // Path constructor
|
|
|
|
this.Snippet = Snippet; // Snippet constructor
|
2018-12-18 15:35:07 +01:00
|
|
|
this.Attributes = Attributes; // Attributes constructor
|
2018-12-11 18:49:00 +01:00
|
|
|
|
|
|
|
// Default settings
|
2018-08-09 15:10:15 +02:00
|
|
|
this.settings = {
|
2018-09-12 17:34:53 +02:00
|
|
|
complete: true,
|
2018-08-27 16:45:03 +02:00
|
|
|
idPrefix: "fs-",
|
|
|
|
locale: "en",
|
2018-09-11 11:39:50 +02:00
|
|
|
units: "metric",
|
2018-12-11 18:49:00 +01:00
|
|
|
margin: 2,
|
2018-08-27 16:45:03 +02:00
|
|
|
options: {}
|
2018-08-05 14:04:15 +02:00
|
|
|
};
|
2018-12-09 14:17:46 +01:00
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
if (typeof this.config.inject === "undefined") this.config.inject = [];
|
|
|
|
if (typeof this.config.dependencies === "undefined")
|
|
|
|
this.config.dependencies = {};
|
|
|
|
if (typeof this.config.hide === "undefined") this.config.hide = [];
|
|
|
|
this.config.resolvedDependencies = this.resolveDependencies(
|
|
|
|
this.config.dependencies
|
|
|
|
);
|
|
|
|
this.config.draftOrder = this.draftOrder(this.config.resolvedDependencies);
|
|
|
|
|
2018-12-09 14:17:46 +01:00
|
|
|
// Convert options
|
2018-08-09 14:21:10 +02:00
|
|
|
for (let i in config.options) {
|
|
|
|
let option = config.options[i];
|
2018-08-11 15:05:40 +02:00
|
|
|
if (typeof option === "object") {
|
2018-12-11 18:49:00 +01:00
|
|
|
if (typeof option.pct !== "undefined")
|
|
|
|
this.settings.options[i] = option.pct / 100;
|
|
|
|
else if (typeof option.mm !== "undefined")
|
|
|
|
this.settings.options[i] = option.mm;
|
|
|
|
else if (typeof option.deg !== "undefined")
|
|
|
|
this.settings.options[i] = option.deg;
|
2018-09-22 10:41:51 +02:00
|
|
|
else if (typeof option.count !== "undefined")
|
2018-12-11 18:49:00 +01:00
|
|
|
this.settings.options[i] = option.count;
|
2018-12-29 11:38:46 +01:00
|
|
|
else if (typeof option.bool !== "undefined")
|
|
|
|
this.settings.options[i] = option.bool;
|
2018-09-03 12:20:51 +02:00
|
|
|
else if (typeof option.dflt !== "undefined")
|
2018-12-11 18:49:00 +01:00
|
|
|
this.settings.options[i] = option.dflt;
|
2018-12-29 12:56:09 +01:00
|
|
|
else throw new Error("Unknown option type: " + JSON.stringify(option));
|
2018-09-03 12:20:51 +02:00
|
|
|
} else {
|
2018-12-11 18:49:00 +01:00
|
|
|
this.settings.options[i] = option;
|
2018-08-11 15:05:40 +02:00
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
}
|
2018-08-05 14:04:15 +02:00
|
|
|
|
2018-12-17 11:49:08 +01:00
|
|
|
// Macros
|
|
|
|
this.macros = {};
|
|
|
|
|
|
|
|
// Context object to add to Part closure
|
|
|
|
const context = {
|
2018-07-23 11:12:06 +00:00
|
|
|
parts: this.parts,
|
2018-07-24 08:34:26 +02:00
|
|
|
config: this.config,
|
2018-07-25 14:53:10 +00:00
|
|
|
settings: this.settings,
|
2018-12-17 11:49:08 +01:00
|
|
|
store: this.store,
|
|
|
|
macros: this.macros
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-12-17 11:49:08 +01:00
|
|
|
// Part closure
|
|
|
|
this.Part = () => {
|
|
|
|
let part = new Part();
|
|
|
|
part.context = context;
|
|
|
|
for (let macro in context.macros) {
|
|
|
|
part[macroName(macro)] = context.macros[macro];
|
|
|
|
}
|
|
|
|
return part;
|
|
|
|
};
|
|
|
|
}
|
2018-09-30 16:25:03 +02:00
|
|
|
|
2018-12-11 18:49:00 +01:00
|
|
|
// Merges settings object with this.settings
|
2018-12-22 11:14:42 +01:00
|
|
|
Pattern.prototype.apply = function(settings) {
|
2018-12-22 11:17:28 +01:00
|
|
|
if (typeof settings !== "object") return this;
|
2018-12-11 18:49:00 +01:00
|
|
|
for (let key of Object.keys(settings)) {
|
2018-12-16 18:43:57 +01:00
|
|
|
if (Array.isArray(settings[key])) {
|
|
|
|
if (Array.isArray(this.settings[key])) {
|
|
|
|
for (let entry of settings[key]) this.settings[key].push(entry);
|
|
|
|
} else this.settings[key] = settings[key];
|
|
|
|
} else if (typeof settings[key] === "object") {
|
2018-12-11 18:49:00 +01:00
|
|
|
this.settings[key] = {
|
|
|
|
...this.settings[key],
|
|
|
|
...settings[key]
|
|
|
|
};
|
|
|
|
} else this.settings[key] = settings[key];
|
|
|
|
}
|
2018-12-22 11:14:42 +01:00
|
|
|
|
|
|
|
return this;
|
2018-12-11 18:49:00 +01:00
|
|
|
};
|
|
|
|
|
2018-12-09 14:17:46 +01:00
|
|
|
Pattern.prototype.runHooks = function(hookName, data = false) {
|
|
|
|
if (data === false) data = this;
|
|
|
|
let hooks = this.hooks[hookName];
|
|
|
|
if (hooks.length > 0) {
|
|
|
|
for (let hook of hooks) {
|
|
|
|
hook.method(data, hook.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-09-30 16:25:03 +02:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
/**
|
2018-12-16 18:06:01 +01:00
|
|
|
* The default draft method with pre- and postDraft hooks
|
2018-07-23 20:14:32 +02:00
|
|
|
*/
|
2018-08-05 18:19:48 +02:00
|
|
|
Pattern.prototype.draft = function() {
|
2018-12-17 14:39:50 +01:00
|
|
|
if (this.is !== "sample") this.is = "draft";
|
2018-12-09 14:17:46 +01:00
|
|
|
this.runHooks("preDraft");
|
2018-12-16 18:06:01 +01:00
|
|
|
for (let partName of this.config.draftOrder) {
|
2018-12-17 11:49:08 +01:00
|
|
|
this.parts[partName] = new this.Part();
|
2018-12-16 18:06:01 +01:00
|
|
|
if (typeof this.config.inject[partName] === "string") {
|
2018-12-17 07:41:20 +01:00
|
|
|
this.parts[partName].inject(this.parts[this.config.inject[partName]]);
|
2018-12-16 18:06:01 +01:00
|
|
|
}
|
|
|
|
if (this.needs(partName)) {
|
|
|
|
let method = "draft" + capitalize(partName);
|
|
|
|
if (typeof this[method] !== "function")
|
|
|
|
throw new Error(
|
|
|
|
'Method "' + method + '" on pattern object is not callable'
|
|
|
|
);
|
2018-12-17 07:41:20 +01:00
|
|
|
this.parts[partName] = this[method](this.parts[partName]);
|
2018-12-16 18:06:01 +01:00
|
|
|
this.parts[partName].render = this.wants(partName);
|
|
|
|
}
|
|
|
|
}
|
2018-12-09 14:17:46 +01:00
|
|
|
this.runHooks("postDraft");
|
2018-09-30 16:25:03 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-08-09 15:10:15 +02:00
|
|
|
/**
|
|
|
|
* Handles pattern sampling
|
|
|
|
*/
|
|
|
|
Pattern.prototype.sample = function() {
|
|
|
|
if (this.settings.sample.type === "option") {
|
|
|
|
return this.sampleOption(this.settings.sample.option);
|
2018-08-09 16:45:46 +02:00
|
|
|
} else if (this.settings.sample.type === "measurement") {
|
|
|
|
return this.sampleMeasurement(this.settings.sample.measurement);
|
2018-08-10 14:25:26 +02:00
|
|
|
} else if (this.settings.sample.type === "models") {
|
2018-08-24 20:16:01 +02:00
|
|
|
return this.sampleModels(
|
|
|
|
this.settings.sample.models,
|
|
|
|
this.settings.sample.focus || false
|
|
|
|
);
|
2018-08-09 15:10:15 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-10 14:25:26 +02:00
|
|
|
Pattern.prototype.sampleParts = function() {
|
|
|
|
let parts = {};
|
2018-09-22 10:41:51 +02:00
|
|
|
this.settings.complete = false;
|
2018-08-10 14:25:26 +02:00
|
|
|
this.settings.paperless = false;
|
|
|
|
this.draft();
|
|
|
|
for (let i in this.parts) {
|
2018-12-17 11:49:08 +01:00
|
|
|
parts[i] = new this.Part();
|
2018-08-10 14:25:26 +02:00
|
|
|
parts[i].render = this.parts[i].render;
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
};
|
|
|
|
|
2018-09-22 10:41:51 +02:00
|
|
|
Pattern.prototype.sampleRun = function(
|
|
|
|
parts,
|
|
|
|
anchors,
|
|
|
|
run,
|
|
|
|
runs,
|
|
|
|
extraClass = false
|
|
|
|
) {
|
2018-09-07 16:09:14 +02:00
|
|
|
this.draft();
|
|
|
|
for (let i in this.parts) {
|
|
|
|
let anchor = false;
|
|
|
|
let dx = 0;
|
|
|
|
let dy = 0;
|
|
|
|
if (this.parts[i].points.anchor) {
|
|
|
|
if (typeof anchors[i] === "undefined")
|
|
|
|
anchors[i] = this.parts[i].points.anchor;
|
|
|
|
else {
|
|
|
|
if (!anchors[i].sitsOn(this.parts[i].points.anchor)) {
|
|
|
|
dx = this.parts[i].points.anchor.dx(anchors[i]);
|
|
|
|
dy = this.parts[i].points.anchor.dy(anchors[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let j in this.parts[i].paths) {
|
2018-09-22 10:41:51 +02:00
|
|
|
parts[i].paths[j + "_" + run] = this.parts[i].paths[j]
|
2018-09-07 16:09:14 +02:00
|
|
|
.clone()
|
2018-09-22 10:41:51 +02:00
|
|
|
.attr("style", sampleStyle(run, runs));
|
2018-09-07 16:09:14 +02:00
|
|
|
if (this.parts[i].points.anchor)
|
2018-09-22 10:41:51 +02:00
|
|
|
parts[i].paths[j + "_" + run] = parts[i].paths[j + "_" + run].translate(
|
2018-09-07 16:09:14 +02:00
|
|
|
dx,
|
|
|
|
dy
|
|
|
|
);
|
|
|
|
if (extraClass !== false)
|
2018-09-22 10:41:51 +02:00
|
|
|
parts[i].paths[j + "_" + run].attributes.add("class", extraClass);
|
2018-09-07 16:09:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-09 15:10:15 +02:00
|
|
|
/**
|
|
|
|
* Handles option sampling
|
|
|
|
*/
|
2018-09-06 12:03:18 +02:00
|
|
|
Pattern.prototype.sampleOption = function(optionName) {
|
2018-12-09 14:17:46 +01:00
|
|
|
this.is = "sample";
|
|
|
|
this.runHooks("preSample");
|
2018-08-12 18:50:48 +02:00
|
|
|
let step, val;
|
2018-09-06 16:48:36 +02:00
|
|
|
let factor = 1;
|
2018-09-06 15:32:43 +02:00
|
|
|
let anchors = {};
|
2018-08-10 14:25:26 +02:00
|
|
|
let parts = this.sampleParts();
|
2018-09-06 12:03:18 +02:00
|
|
|
let option = this.config.options[optionName];
|
2018-09-11 16:22:08 +02:00
|
|
|
if (typeof option.list === "object") {
|
|
|
|
return this.sampleListOption(optionName);
|
|
|
|
}
|
2018-09-06 12:03:18 +02:00
|
|
|
if (typeof option.min === "undefined" || typeof option.max === "undefined") {
|
2018-09-07 16:25:17 +02:00
|
|
|
let min = option * 0.9;
|
|
|
|
let max = option * 1.1;
|
|
|
|
option = { min, max };
|
2018-08-12 18:50:48 +02:00
|
|
|
}
|
2018-09-06 16:48:36 +02:00
|
|
|
if (typeof option.pct !== "undefined") factor = 100;
|
|
|
|
val = option.min / factor;
|
|
|
|
step = (option.max / factor - val) / 9;
|
2018-09-22 10:41:51 +02:00
|
|
|
for (let run = 1; run < 11; run++) {
|
2018-12-11 18:49:00 +01:00
|
|
|
this.settings.options[optionName] = val;
|
2018-12-18 15:35:07 +01:00
|
|
|
this.debug({
|
|
|
|
type: "info",
|
|
|
|
label: "🏃🏿♀️ Sample run",
|
|
|
|
msg: `Sampling option ${optionName} with value ${round(val)}`
|
|
|
|
});
|
2018-09-22 10:41:51 +02:00
|
|
|
this.sampleRun(parts, anchors, run, 10);
|
2018-08-10 14:25:26 +02:00
|
|
|
val += step;
|
2018-08-09 16:45:46 +02:00
|
|
|
}
|
|
|
|
this.parts = parts;
|
2018-12-09 14:17:46 +01:00
|
|
|
this.runHooks("postSample");
|
2018-08-09 16:45:46 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-09-11 16:22:08 +02:00
|
|
|
Pattern.prototype.sampleListOption = function(optionName) {
|
|
|
|
let parts = this.sampleParts();
|
|
|
|
let option = this.config.options[optionName];
|
|
|
|
let anchors = {};
|
2018-09-22 10:41:51 +02:00
|
|
|
let run = 1;
|
2018-12-07 15:59:57 +01:00
|
|
|
let runs = option.list.length;
|
2018-09-11 16:22:08 +02:00
|
|
|
for (let val of option.list) {
|
2018-12-11 18:49:00 +01:00
|
|
|
this.settings.options[optionName] = val;
|
2018-12-18 15:35:07 +01:00
|
|
|
this.debug({
|
|
|
|
type: "info",
|
|
|
|
label: "🏃🏿♀️ Sample run",
|
|
|
|
msg: `Sampling option ${optionName} with value ${round(val)}`
|
|
|
|
});
|
2018-09-22 10:41:51 +02:00
|
|
|
this.sampleRun(parts, anchors, run, runs);
|
|
|
|
run++;
|
2018-09-11 16:22:08 +02:00
|
|
|
}
|
|
|
|
this.parts = parts;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-08-09 16:45:46 +02:00
|
|
|
/**
|
|
|
|
* Handles measurement sampling
|
|
|
|
*/
|
2018-09-06 12:03:18 +02:00
|
|
|
Pattern.prototype.sampleMeasurement = function(measurementName) {
|
2018-12-09 14:17:46 +01:00
|
|
|
this.is = "sample";
|
|
|
|
this.runHooks("preSample");
|
2018-09-07 16:09:14 +02:00
|
|
|
let anchors = {};
|
2018-08-10 14:25:26 +02:00
|
|
|
let parts = this.sampleParts();
|
2018-09-06 12:03:18 +02:00
|
|
|
let val = this.settings.measurements[measurementName];
|
2018-12-29 12:56:09 +01:00
|
|
|
if (val === undefined)
|
|
|
|
throw new Error(
|
|
|
|
"Cannot sample a measurement that is undefined: " + measurementName
|
|
|
|
);
|
2018-08-10 14:25:26 +02:00
|
|
|
let step = val / 50;
|
2018-08-09 16:45:46 +02:00
|
|
|
val = val * 0.9;
|
2018-09-22 10:41:51 +02:00
|
|
|
for (let run = 1; run < 11; run++) {
|
2018-09-06 12:03:18 +02:00
|
|
|
this.settings.measurements[measurementName] = val;
|
2018-12-18 15:35:07 +01:00
|
|
|
this.debug({
|
|
|
|
type: "info",
|
|
|
|
label: "🏃🏿♀️ Sample run",
|
|
|
|
msg: `Sampling option ${measurementName} with value ${round(val)}`
|
|
|
|
});
|
2018-09-22 10:41:51 +02:00
|
|
|
this.sampleRun(parts, anchors, run, 10);
|
2018-08-10 14:25:26 +02:00
|
|
|
val += step;
|
|
|
|
}
|
|
|
|
this.parts = parts;
|
2018-12-09 14:17:46 +01:00
|
|
|
this.runHooks("postSample");
|
2018-08-10 14:25:26 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles models sampling
|
|
|
|
*/
|
2018-08-24 20:16:01 +02:00
|
|
|
Pattern.prototype.sampleModels = function(models, focus = false) {
|
2018-12-09 14:17:46 +01:00
|
|
|
this.is = "sample";
|
|
|
|
this.runHooks("preSample");
|
2018-09-07 16:09:14 +02:00
|
|
|
let anchors = {};
|
2018-08-10 14:25:26 +02:00
|
|
|
let parts = this.sampleParts();
|
2018-09-22 10:41:51 +02:00
|
|
|
let run = 0;
|
2018-09-22 11:14:10 +02:00
|
|
|
let runs = Object.keys(models).length;
|
2018-08-10 14:25:26 +02:00
|
|
|
for (let l in models) {
|
2018-09-22 10:41:51 +02:00
|
|
|
run++;
|
2018-08-10 14:25:26 +02:00
|
|
|
this.settings.measurements = models[l];
|
2018-12-18 15:35:07 +01:00
|
|
|
this.debug({
|
|
|
|
type: "info",
|
|
|
|
label: "🏃🏿♀️ Sample run",
|
|
|
|
msg: `Sampling model ${l}`
|
|
|
|
});
|
2018-09-07 16:09:14 +02:00
|
|
|
let className = l === focus ? "sample-focus" : "";
|
2018-09-22 10:41:51 +02:00
|
|
|
this.sampleRun(parts, anchors, run, runs, className);
|
2018-08-09 15:10:15 +02:00
|
|
|
}
|
|
|
|
this.parts = parts;
|
2018-12-09 14:17:46 +01:00
|
|
|
this.runHooks("postSample");
|
2018-08-09 15:57:30 +02:00
|
|
|
|
|
|
|
return this;
|
2018-08-09 15:10:15 +02:00
|
|
|
};
|
|
|
|
|
2018-08-07 15:23:37 +02:00
|
|
|
/** Debug method, exposes debug hook */
|
2018-12-18 15:35:07 +01:00
|
|
|
Pattern.prototype.debug = function(data) {
|
2018-12-09 14:17:46 +01:00
|
|
|
this.runHooks("debug", data);
|
|
|
|
};
|
2018-08-01 18:18:29 +02:00
|
|
|
|
2018-08-07 15:23:37 +02:00
|
|
|
Pattern.prototype.render = function() {
|
|
|
|
this.svg = new Svg(this);
|
2018-12-09 14:17:46 +01:00
|
|
|
this.svg.hooks = this.hooks;
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-03 14:20:28 +02:00
|
|
|
return this.pack().svg.render(this);
|
2018-07-23 20:14:32 +02:00
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-12-09 14:17:46 +01:00
|
|
|
Pattern.prototype.on = function(hook, method, data) {
|
|
|
|
this.hooks[hook].push({ method, data });
|
2018-07-23 20:14:32 +02:00
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-12-22 10:27:05 +01:00
|
|
|
Pattern.prototype.use = function(plugin, data = false) {
|
2018-12-18 15:35:07 +01:00
|
|
|
this.debug({
|
|
|
|
type: "success",
|
|
|
|
label: "🔌 Plugin loaded",
|
|
|
|
msg: `${plugin.name} v${plugin.version}`
|
|
|
|
});
|
2018-12-09 14:17:46 +01:00
|
|
|
if (plugin.hooks) this.loadPluginHooks(plugin, data);
|
2018-07-23 20:14:32 +02:00
|
|
|
if (plugin.macros) this.loadPluginMacros(plugin);
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
return this;
|
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-12-09 14:17:46 +01:00
|
|
|
Pattern.prototype.loadPluginHooks = function(plugin, data) {
|
|
|
|
for (let hook of Object.keys(this.hooks)) {
|
2018-07-23 20:14:32 +02:00
|
|
|
if (typeof plugin.hooks[hook] === "function") {
|
2018-12-09 14:17:46 +01:00
|
|
|
this.on(hook, plugin.hooks[hook], data);
|
2018-12-17 14:39:50 +01:00
|
|
|
} else if (Array.isArray(plugin.hooks[hook])) {
|
2018-08-11 14:03:06 +02:00
|
|
|
for (let method of plugin.hooks[hook]) {
|
2018-12-09 14:17:46 +01:00
|
|
|
this.on(hook, method, data);
|
2018-08-11 14:03:06 +02:00
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
}
|
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
Pattern.prototype.loadPluginMacros = function(plugin) {
|
2018-07-23 20:14:32 +02:00
|
|
|
for (let macro in plugin.macros) {
|
|
|
|
if (typeof plugin.macros[macro] === "function") {
|
|
|
|
this.macro(macro, plugin.macros[macro]);
|
2018-07-23 11:12:06 +00:00
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
}
|
|
|
|
};
|
2018-07-24 14:38:03 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
Pattern.prototype.macro = function(key, method) {
|
2018-12-17 11:49:08 +01:00
|
|
|
this.macros[key] = method;
|
2018-07-24 14:38:03 +00:00
|
|
|
};
|
2018-08-01 18:18:29 +02:00
|
|
|
|
|
|
|
/** Packs parts in a 2D space and sets pattern size */
|
2018-08-05 18:19:48 +02:00
|
|
|
Pattern.prototype.pack = function() {
|
2018-08-01 18:18:29 +02:00
|
|
|
let bins = [];
|
|
|
|
for (let key in this.parts) {
|
|
|
|
let part = this.parts[key];
|
2018-08-12 16:19:04 +02:00
|
|
|
// Avoid multiple render calls to cause stacking of transforms
|
|
|
|
part.attributes.set("transform", "");
|
2018-08-16 12:09:57 +02:00
|
|
|
if (part.render && this.needs(key)) {
|
2018-08-01 18:18:29 +02:00
|
|
|
part.stack();
|
|
|
|
bins.push({
|
2018-08-05 15:52:37 +02:00
|
|
|
id: key,
|
2018-08-01 18:18:29 +02:00
|
|
|
width: part.bottomRight.x - part.topLeft.x,
|
|
|
|
height: part.bottomRight.y - part.topLeft.y
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let size = pack(bins, { inPlace: true });
|
|
|
|
for (let bin of bins) {
|
|
|
|
let part = this.parts[bin.id];
|
|
|
|
if (bin.x !== 0 || bin.y !== 0)
|
|
|
|
part.attr("transform", `translate (${bin.x}, ${bin.y})`);
|
|
|
|
}
|
|
|
|
this.width = size.width;
|
|
|
|
this.height = size.height;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
2018-08-15 18:54:47 +02:00
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
/** Determines the order to draft parts in, based on dependencies */
|
|
|
|
Pattern.prototype.draftOrder = function(graph = this.resolveDependencies()) {
|
|
|
|
let sorted = [];
|
|
|
|
let visited = {};
|
|
|
|
Object.keys(graph).forEach(function visit(name, ancestors) {
|
|
|
|
if (!Array.isArray(ancestors)) ancestors = [];
|
|
|
|
ancestors.push(name);
|
|
|
|
visited[name] = true;
|
|
|
|
if (typeof graph[name] !== "undefined") {
|
|
|
|
graph[name].forEach(function(dep) {
|
|
|
|
if (visited[dep]) return;
|
|
|
|
visit(dep, ancestors.slice(0));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (sorted.indexOf(name) < 0) sorted.push(name);
|
|
|
|
});
|
|
|
|
|
|
|
|
return sorted;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Recursively solves part dependencies for a part */
|
|
|
|
Pattern.prototype.resolveDependency = function(
|
|
|
|
seen,
|
|
|
|
part,
|
|
|
|
graph = this.config.dependencies,
|
|
|
|
deps = []
|
|
|
|
) {
|
|
|
|
if (typeof seen[part] === "undefined") seen[part] = true;
|
|
|
|
if (typeof graph[part] === "string") {
|
|
|
|
deps.push(graph[part]);
|
|
|
|
return this.resolveDependency(seen, graph[part], graph, deps);
|
2018-12-20 14:34:26 +01:00
|
|
|
} else if (Array.isArray(graph[part])) {
|
|
|
|
if (graph[part].length === 0) return [];
|
|
|
|
else {
|
|
|
|
if (deps.length === 0) deps = graph[part];
|
|
|
|
for (let apart of graph[part])
|
|
|
|
deps.concat(this.resolveDependency(seen, apart, graph, deps));
|
|
|
|
}
|
2018-12-16 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return deps;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Resolves part dependencies into a flat array */
|
|
|
|
Pattern.prototype.resolveDependencies = function(
|
|
|
|
graph = this.config.dependencies
|
|
|
|
) {
|
2018-12-17 14:39:50 +01:00
|
|
|
// Include parts outside the dependency graph
|
|
|
|
if (Array.isArray(this.config.parts)) {
|
|
|
|
for (let part of this.config.parts) {
|
|
|
|
if (typeof this.config.dependencies[part] === "undefined")
|
|
|
|
this.config.dependencies[part] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
let resolved = {};
|
|
|
|
let seen = {};
|
2018-12-20 14:34:26 +01:00
|
|
|
for (let part of Object.keys(graph)) {
|
2018-12-16 18:06:01 +01:00
|
|
|
resolved[part] = this.resolveDependency(seen, part, graph);
|
2018-12-20 14:34:26 +01:00
|
|
|
}
|
2018-12-16 18:06:01 +01:00
|
|
|
for (let part of Object.keys(seen))
|
|
|
|
if (typeof resolved[part] === "undefined") resolved[part] = [];
|
|
|
|
|
|
|
|
return resolved;
|
|
|
|
};
|
|
|
|
|
2018-08-16 12:09:57 +02:00
|
|
|
/** Determines whether a part is needed
|
2018-12-16 18:06:01 +01:00
|
|
|
* This depends on the 'only' setting and the
|
|
|
|
* configured dependencies.
|
2018-08-15 18:54:47 +02:00
|
|
|
*/
|
2018-12-16 18:06:01 +01:00
|
|
|
Pattern.prototype.needs = function(partName) {
|
2018-12-16 18:28:37 +01:00
|
|
|
if (typeof this.settings.only === "undefined" || this.settings.only === false)
|
|
|
|
return true;
|
2018-12-16 18:06:01 +01:00
|
|
|
else if (typeof this.settings.only === "string") {
|
|
|
|
if (this.settings.only === partName) return true;
|
|
|
|
if (Array.isArray(this.config.resolvedDependencies[this.settings.only])) {
|
|
|
|
for (let dependency of this.config.resolvedDependencies[
|
|
|
|
this.settings.only
|
|
|
|
]) {
|
|
|
|
if (dependency === partName) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(this.settings.only)) {
|
|
|
|
for (let part of this.settings.only) {
|
|
|
|
if (part === partName) return true;
|
|
|
|
for (let dependency of this.config.resolvedDependencies[part]) {
|
|
|
|
if (dependency === partName) return true;
|
|
|
|
}
|
2018-08-15 18:54:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-16 18:06:01 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Checks whether a part is hidden in the config */
|
|
|
|
Pattern.prototype.isHidden = function(partName) {
|
|
|
|
if (Array.isArray(this.config.hide)) {
|
|
|
|
if (this.config.hide.indexOf(partName) !== -1) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Determines whether a part is wanted by the user
|
|
|
|
* This depends on the 'only' setting
|
|
|
|
*/
|
|
|
|
Pattern.prototype.wants = function(partName) {
|
2018-12-16 18:28:37 +01:00
|
|
|
if (
|
|
|
|
typeof this.settings.only === "undefined" ||
|
|
|
|
this.settings.only === false
|
|
|
|
) {
|
2018-12-16 18:06:01 +01:00
|
|
|
if (this.isHidden(partName)) return false;
|
|
|
|
} else if (typeof this.settings.only === "string") {
|
|
|
|
if (this.settings.only === partName) return true;
|
|
|
|
return false;
|
|
|
|
} else if (Array.isArray(this.settings.only)) {
|
|
|
|
for (let part of this.settings.only) {
|
|
|
|
if (part === partName) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-08-15 18:54:47 +02:00
|
|
|
};
|