2018-07-23 11:44:34 +00:00
|
|
|
import { macroName } 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";
|
|
|
|
import Hooks from "./hooks";
|
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-08-10 15:36:39 +02:00
|
|
|
import * as hooklib from "hooks";
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
export default function Pattern(config = false) {
|
2018-08-05 14:04:15 +02:00
|
|
|
// width and height properties
|
2018-08-01 18:18:29 +02:00
|
|
|
this.width = false;
|
|
|
|
this.height = false;
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
// Hooks and Svg instance
|
2018-08-10 15:36:39 +02:00
|
|
|
for (let k in hooklib) this[k] = hooklib[k];
|
2018-08-05 18:19:48 +02:00
|
|
|
this.hooks = new Hooks();
|
2018-08-07 15:23:37 +02:00
|
|
|
Svg.prototype.hooks = this.hooks;
|
2018-07-23 11:12:06 +00:00
|
|
|
|
|
|
|
// Data containers
|
2018-08-09 15:10:15 +02:00
|
|
|
this.settings = {
|
2018-08-16 15:44:01 +02:00
|
|
|
mode: "draft",
|
2018-08-27 16:45:03 +02:00
|
|
|
idPrefix: "fs-",
|
|
|
|
locale: "en",
|
|
|
|
units: "metric"
|
2018-08-09 15:10:15 +02:00
|
|
|
};
|
2018-08-05 14:04:15 +02:00
|
|
|
this.options = {};
|
2018-08-05 18:19:48 +02:00
|
|
|
this.store = new Store();
|
2018-07-23 11:12:06 +00:00
|
|
|
this.parts = {};
|
2018-07-25 14:53:10 +00:00
|
|
|
|
2018-08-05 14:04:15 +02:00
|
|
|
// Merge config with defaults
|
|
|
|
let defaults = {
|
|
|
|
measurements: {},
|
2018-08-27 16:45:03 +02:00
|
|
|
options: {}
|
2018-08-05 14:04:15 +02:00
|
|
|
};
|
|
|
|
this.config = { ...defaults, ...config };
|
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-08-16 11:57:00 +02:00
|
|
|
this.options[i] = option.val / 100;
|
2018-08-11 15:05:40 +02:00
|
|
|
} else if (typeof option === "number") {
|
|
|
|
this.options[i] = option;
|
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
}
|
2018-08-05 14:04:15 +02:00
|
|
|
|
|
|
|
// Constructors
|
2018-08-05 18:19:48 +02:00
|
|
|
this.Part = Part;
|
|
|
|
this.Point = Point;
|
|
|
|
this.Path = Path;
|
|
|
|
this.Snippet = Snippet;
|
2018-08-05 14:04:15 +02:00
|
|
|
|
|
|
|
// Context object to inject in part prototype
|
2018-07-23 11:12:06 +00:00
|
|
|
this.context = {
|
|
|
|
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,
|
|
|
|
options: this.options,
|
2018-08-05 16:32:38 +02:00
|
|
|
store: this.store
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-08-05 18:19:48 +02:00
|
|
|
this.Part.prototype.context = this.context;
|
|
|
|
this.Part.prototype.macros = {};
|
2018-08-07 13:46:38 +02:00
|
|
|
this.Part.prototype.hooks = this.hooks;
|
2018-07-23 20:14:32 +02:00
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
/**
|
|
|
|
* @throws Will throw an error when called
|
|
|
|
*/
|
2018-08-05 18:19:48 +02:00
|
|
|
Pattern.prototype.draft = function() {
|
2018-07-23 20:14:32 +02:00
|
|
|
throw Error(
|
|
|
|
"You have to implement the draft() method in your Pattern instance."
|
|
|
|
);
|
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
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 = {};
|
|
|
|
this.settings.mode = "sample";
|
|
|
|
this.settings.paperless = false;
|
|
|
|
this.draft();
|
|
|
|
for (let i in this.parts) {
|
|
|
|
parts[i] = new Part();
|
|
|
|
parts[i].render = this.parts[i].render;
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
};
|
|
|
|
|
2018-08-09 15:10:15 +02:00
|
|
|
/**
|
|
|
|
* Handles option sampling
|
|
|
|
*/
|
|
|
|
Pattern.prototype.sampleOption = function(option) {
|
2018-08-12 18:50:48 +02:00
|
|
|
let step, val;
|
2018-08-10 14:25:26 +02:00
|
|
|
let parts = this.sampleParts();
|
2018-08-12 18:50:48 +02:00
|
|
|
if (
|
|
|
|
typeof this.config.options[option].min === "undefined" ||
|
|
|
|
typeof this.config.options[option].max === "undefined"
|
|
|
|
) {
|
|
|
|
throw "Cannot sample an option without min and max values";
|
|
|
|
}
|
|
|
|
let factor = 100;
|
2018-08-09 15:57:30 +02:00
|
|
|
val = this.config.options[option].min / factor;
|
|
|
|
step = (this.config.options[option].max / factor - val) / 9;
|
2018-08-10 14:25:26 +02:00
|
|
|
for (let l = 1; l < 11; l++) {
|
2018-08-09 15:10:15 +02:00
|
|
|
this.options[option] = val;
|
|
|
|
this.debug(`Sampling option ${option} with value ${val}`);
|
|
|
|
this.draft();
|
|
|
|
for (let i in this.parts) {
|
|
|
|
for (let j in this.parts[i].paths) {
|
2018-08-09 16:45:46 +02:00
|
|
|
parts[i].paths[j + "_" + l] = this.parts[i].paths[j]
|
|
|
|
.clone()
|
|
|
|
.attr("class", "sample-" + l, true);
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 14:25:26 +02:00
|
|
|
val += step;
|
2018-08-09 16:45:46 +02:00
|
|
|
}
|
|
|
|
this.parts = parts;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles measurement sampling
|
|
|
|
*/
|
|
|
|
Pattern.prototype.sampleMeasurement = function(measurement) {
|
2018-08-10 14:25:26 +02:00
|
|
|
let parts = this.sampleParts();
|
|
|
|
let val = this.settings.measurements[measurement];
|
2018-08-12 18:50:48 +02:00
|
|
|
if (val === undefined) throw "Cannot sample a measurement that is undefined";
|
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-08-10 14:25:26 +02:00
|
|
|
for (let l = 1; l < 11; l++) {
|
2018-08-09 16:45:46 +02:00
|
|
|
this.settings.measurements[measurement] = val;
|
|
|
|
this.debug(`Sampling measurement ${measurement} with value ${val}`);
|
|
|
|
this.draft();
|
|
|
|
for (let i in this.parts) {
|
|
|
|
for (let j in this.parts[i].paths) {
|
2018-08-09 15:57:30 +02:00
|
|
|
parts[i].paths[j + "_" + l] = this.parts[i].paths[j]
|
|
|
|
.clone()
|
|
|
|
.attr("class", "sample-" + l, true);
|
2018-08-09 15:10:15 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 14:25:26 +02:00
|
|
|
val += step;
|
|
|
|
}
|
|
|
|
this.parts = parts;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles models sampling
|
|
|
|
*/
|
2018-08-24 20:16:01 +02:00
|
|
|
Pattern.prototype.sampleModels = function(models, focus = false) {
|
2018-08-10 14:25:26 +02:00
|
|
|
let parts = this.sampleParts();
|
|
|
|
let count = 0;
|
|
|
|
for (let l in models) {
|
|
|
|
count++;
|
|
|
|
this.settings.measurements = models[l];
|
|
|
|
this.debug(`Sampling model ${l}`);
|
|
|
|
this.draft();
|
|
|
|
for (let i in this.parts) {
|
|
|
|
for (let j in this.parts[i].paths) {
|
|
|
|
parts[i].paths[j + "_" + count] = this.parts[i].paths[j]
|
|
|
|
.clone()
|
2018-08-24 20:16:01 +02:00
|
|
|
.attr("class", "sample sample-" + count, true);
|
|
|
|
if (l === focus)
|
|
|
|
parts[i].paths[j + "_" + count].attr("class", "sample-focus");
|
2018-08-10 14:25:26 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-09 15:10:15 +02:00
|
|
|
}
|
|
|
|
this.parts = parts;
|
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-08-10 15:36:39 +02:00
|
|
|
Pattern.prototype.debug = function(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-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-08-05 18:19:48 +02:00
|
|
|
Pattern.prototype.on = function(hook, method) {
|
2018-07-23 20:14:32 +02:00
|
|
|
if (typeof this.hooks._hooks[hook] === "undefined") {
|
|
|
|
this.hooks._hooks[hook] = [];
|
|
|
|
}
|
|
|
|
this.hooks._hooks[hook].push(method);
|
2018-08-10 15:36:39 +02:00
|
|
|
|
|
|
|
// Pattern object hooks need to be attached on load
|
|
|
|
let localHooks = [
|
|
|
|
"preDraft",
|
|
|
|
"postDraft",
|
|
|
|
"preSample",
|
|
|
|
"postSample",
|
|
|
|
"debug"
|
|
|
|
];
|
|
|
|
if (localHooks.includes(hook)) {
|
|
|
|
let self = this;
|
|
|
|
this.hooks.attach(hook, self);
|
|
|
|
}
|
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.with = function(plugin) {
|
2018-08-07 16:32:16 +02:00
|
|
|
this.debug(`Plugin: ${plugin.name} v${plugin.version}`);
|
2018-07-23 20:14:32 +02:00
|
|
|
if (plugin.hooks) this.loadPluginHooks(plugin);
|
|
|
|
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-08-05 18:19:48 +02:00
|
|
|
Pattern.prototype.loadPluginHooks = function(plugin) {
|
2018-07-23 20:14:32 +02:00
|
|
|
for (let hook of this.hooks.all) {
|
|
|
|
if (typeof plugin.hooks[hook] === "function") {
|
|
|
|
this.on(hook, plugin.hooks[hook]);
|
2018-08-14 09:20:47 +02:00
|
|
|
} else if (typeof plugin.hooks[hook] === "object") {
|
2018-08-11 14:03:06 +02:00
|
|
|
for (let method of plugin.hooks[hook]) {
|
|
|
|
this.on(hook, method);
|
|
|
|
}
|
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) {
|
|
|
|
this.Part.prototype[macroName(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-08-16 12:09:57 +02:00
|
|
|
/** Determines whether a part is needed
|
|
|
|
* This depends on the 'only' setting. People can pass
|
|
|
|
* the name of a part, or an array of parts
|
|
|
|
* The absence of only means all parts are needed.
|
2018-08-15 18:54:47 +02:00
|
|
|
*
|
2018-08-16 12:09:57 +02:00
|
|
|
* If partName is an array of names, any name needed
|
|
|
|
* will cause this to return true
|
2018-08-15 18:54:47 +02:00
|
|
|
*/
|
2018-08-20 12:16:13 +02:00
|
|
|
Pattern.prototype.needs = function(partName, strict = false) {
|
2018-08-15 18:54:47 +02:00
|
|
|
if (typeof partName !== "string") {
|
|
|
|
for (let part of partName) {
|
2018-08-20 12:16:13 +02:00
|
|
|
if (this.needs(part, strict)) return true;
|
2018-08-15 18:54:47 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-20 12:16:13 +02:00
|
|
|
if (typeof this.settings.only === "undefined") {
|
|
|
|
if (strict) return false;
|
|
|
|
else return true;
|
|
|
|
} else if (this.settings.only === partName) return true;
|
2018-08-16 16:09:47 +02:00
|
|
|
else if (
|
|
|
|
typeof this.settings.only === "object" &&
|
|
|
|
this.settings.only.indexOf(partName) !== -1
|
2018-08-20 12:16:13 +02:00
|
|
|
) {
|
2018-08-16 16:09:47 +02:00
|
|
|
return true;
|
2018-08-20 12:16:13 +02:00
|
|
|
} else return false;
|
2018-08-15 18:54:47 +02:00
|
|
|
};
|