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-07-23 11:12:06 +00:00
|
|
|
|
2018-07-23 11:44:34 +00:00
|
|
|
export default function pattern(config = false) {
|
2018-07-23 11:12:06 +00:00
|
|
|
// Allow no-config patterns
|
2018-07-23 11:44:34 +00:00
|
|
|
if (!config) {
|
2018-07-23 11:12:06 +00:00
|
|
|
this.config = {
|
2018-07-23 11:44:34 +00:00
|
|
|
parts: ["part"],
|
2018-07-23 11:12:06 +00:00
|
|
|
measurements: {},
|
|
|
|
options: {}
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
} else {
|
|
|
|
this.config = config;
|
|
|
|
}
|
2018-07-23 11:44:34 +00:00
|
|
|
if (
|
|
|
|
typeof config.parts === "undefined" ||
|
|
|
|
!config.parts ||
|
|
|
|
config.parts.length < 1
|
|
|
|
) {
|
2018-07-23 11:12:06 +00:00
|
|
|
throw "Could not create pattern: You should define at least one part in your pattern config";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
this.point = point;
|
|
|
|
this.path = path;
|
|
|
|
this.snippet = snippet;
|
|
|
|
|
|
|
|
// 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-07-23 11:12:06 +00:00
|
|
|
this.values = {};
|
|
|
|
this.parts = {};
|
|
|
|
for (let id of config.parts) {
|
|
|
|
this.parts[id] = new part(id);
|
|
|
|
}
|
|
|
|
this.options = {};
|
2018-07-23 11:44:34 +00:00
|
|
|
if (typeof config.options !== "undefined" && config.options.length > 0) {
|
2018-07-23 11:12:06 +00:00
|
|
|
for (let conf of config.options) {
|
2018-07-23 11:44:34 +00:00
|
|
|
if (conf.type === "percentage") this.options[conf.id] = conf.val / 100;
|
2018-07-23 11:12:06 +00:00
|
|
|
else this.options[conf.id] = conf.val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context object to pass around
|
|
|
|
this.context = {
|
|
|
|
parts: this.parts,
|
|
|
|
options: this.options,
|
|
|
|
values: this.values,
|
2018-07-24 08:34:26 +02:00
|
|
|
config: this.config,
|
|
|
|
settings: this.settings
|
2018-07-23 11:44:34 +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
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
this.hooks.attach("postRenderSvg", this.svg);
|
|
|
|
//this.hooks.attach('insertText', this.svg);
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
return this.svg.render(this);
|
|
|
|
};
|
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) {
|
2018-07-24 14:38:03 +00:00
|
|
|
console.log("loading ", macro);
|
2018-07-23 20:14:32 +02:00
|
|
|
if (typeof plugin.macros[macro] === "function") {
|
2018-07-24 14:38:03 +00:00
|
|
|
console.log(macro, "is a function");
|
2018-07-23 20:14:32 +02:00
|
|
|
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) {
|
|
|
|
let name = macroName(key);
|
|
|
|
this.on(name, method);
|
|
|
|
for (let partId in this.parts) {
|
|
|
|
console.log(`Attaching macro ${name} to part ${partId}`);
|
|
|
|
let part = this.parts[partId];
|
|
|
|
part[name] = () => null;
|
|
|
|
this.hooks.attach(name, part);
|
|
|
|
}
|
|
|
|
};
|