2018-08-01 18:18:29 +02:00
|
|
|
import attributes from "./attributes";
|
2018-07-23 11:44:34 +00:00
|
|
|
import { macroName } from "./utils";
|
|
|
|
import part from "./part";
|
2018-07-23 20:14:32 +02:00
|
|
|
import point from "./point";
|
|
|
|
import path from "./path";
|
|
|
|
import snippet from "./snippet";
|
2018-07-23 11:44:34 +00:00
|
|
|
import svg from "./svg";
|
|
|
|
import hooks from "./hooks";
|
2018-08-01 18:18:29 +02:00
|
|
|
import pack from "bin-pack";
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-23 11:44:34 +00: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
|
|
|
|
|
|
|
// Svg and hooks instance
|
|
|
|
this.svg = new svg(this);
|
|
|
|
this.hooks = new hooks();
|
|
|
|
|
|
|
|
// Data containers
|
2018-07-24 08:34:26 +02:00
|
|
|
this.settings = {};
|
2018-08-05 14:04:15 +02:00
|
|
|
this.options = {};
|
|
|
|
this.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: {},
|
|
|
|
options: {},
|
|
|
|
units: "metric"
|
|
|
|
};
|
|
|
|
this.config = { ...defaults, ...config };
|
|
|
|
if (config.options.length > 0) {
|
2018-07-23 11:12:06 +00:00
|
|
|
for (let conf of config.options) {
|
2018-08-01 14:55:54 +02:00
|
|
|
if (conf.type === "%") this.options[conf.id] = conf.val / 100;
|
2018-07-23 11:12:06 +00:00
|
|
|
else this.options[conf.id] = conf.val;
|
|
|
|
}
|
|
|
|
}
|
2018-08-05 14:04:15 +02:00
|
|
|
|
|
|
|
// Constructors
|
|
|
|
this.part = part;
|
|
|
|
this.point = point;
|
|
|
|
this.path = path;
|
|
|
|
this.snippet = snippet;
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
values: this.values
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-08-05 14:04:15 +02:00
|
|
|
this.part.prototype.context = this.context;
|
|
|
|
this.part.prototype.macros = {};
|
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
|
|
|
|
*/
|
|
|
|
pattern.prototype.draft = function() {
|
|
|
|
throw Error(
|
|
|
|
"You have to implement the draft() method in your Pattern instance."
|
|
|
|
);
|
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
pattern.prototype.render = function() {
|
|
|
|
this.hooks.attach("preRenderSvg", this.svg);
|
2018-08-01 18:18:29 +02:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
this.hooks.attach("postRenderSvg", this.svg);
|
|
|
|
//this.hooks.attach('insertText', this.svg);
|
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-07-23 20:14:32 +02:00
|
|
|
pattern.prototype.on = function(hook, method) {
|
|
|
|
if (typeof this.hooks._hooks[hook] === "undefined") {
|
|
|
|
this.hooks._hooks[hook] = [];
|
|
|
|
}
|
|
|
|
this.hooks._hooks[hook].push(method);
|
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-24 14:38:03 +00:00
|
|
|
pattern.prototype.with = function(plugin) {
|
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-07-23 20:14:32 +02:00
|
|
|
pattern.prototype.loadPluginHooks = function(plugin) {
|
|
|
|
for (let hook of this.hooks.all) {
|
|
|
|
if (typeof plugin.hooks[hook] === "function") {
|
|
|
|
this.on(hook, plugin.hooks[hook]);
|
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-07-23 20:14:32 +02:00
|
|
|
pattern.prototype.loadPluginMacros = function(plugin) {
|
|
|
|
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
|
|
|
|
|
|
|
pattern.prototype.macro = function(key, method) {
|
2018-08-05 14:04:15 +02:00
|
|
|
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 */
|
|
|
|
pattern.prototype.pack = function() {
|
|
|
|
let bins = [];
|
|
|
|
for (let key in this.parts) {
|
|
|
|
let part = this.parts[key];
|
|
|
|
if (part.render) {
|
|
|
|
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;
|
|
|
|
};
|