2018-07-10 19:29:09 +02:00
|
|
|
import { PatternConfig, PatternOption } from './types'
|
2018-07-15 11:00:48 +00:00
|
|
|
import { Point } from './point'
|
2018-07-12 12:53:49 +00:00
|
|
|
import { Part } from './part'
|
2018-07-15 08:36:19 +00:00
|
|
|
import { Svg } from './svg'
|
2018-07-17 14:16:17 +00:00
|
|
|
import { Hooks } from './hooks'
|
2018-07-12 12:53:49 +00:00
|
|
|
import { Option } from './option'
|
2018-07-18 16:57:19 +00:00
|
|
|
import { Snippet } from './snippet'
|
|
|
|
import { Path } from './path'
|
2018-07-10 12:20:53 +00:00
|
|
|
|
2018-07-12 12:53:49 +00:00
|
|
|
export class Pattern {
|
2018-07-10 12:20:53 +00:00
|
|
|
config: PatternConfig;
|
2018-07-18 11:45:46 +00:00
|
|
|
svg: Svg;
|
2018-07-14 16:04:39 +00:00
|
|
|
parts: {
|
|
|
|
[index: string]: Part;
|
|
|
|
}
|
2018-07-12 07:37:52 +00:00
|
|
|
options: {[propName: string]: number};
|
2018-07-15 11:00:48 +00:00
|
|
|
values: {[propName: string]: any} = {};
|
|
|
|
settings: {[propName: string]: any} = {mode: 'draft', units: 'metric'};
|
2018-07-17 14:16:17 +00:00
|
|
|
hooks: Hooks;
|
2018-07-18 16:57:19 +00:00
|
|
|
snippet: Snippet
|
|
|
|
path: Path
|
2018-07-19 14:02:04 +00:00
|
|
|
context: any
|
2018-07-10 12:20:53 +00:00
|
|
|
|
|
|
|
constructor(config: PatternConfig) {
|
2018-07-17 14:16:17 +00:00
|
|
|
if(!config) {
|
|
|
|
throw "Could not create pattern: You need to provide a pattern config."
|
|
|
|
}
|
|
|
|
if(typeof config.parts === 'undefined' || !config.parts || config.parts.length < 1) {
|
|
|
|
throw "Could not create pattern: You should define at least one part in your pattern config";
|
|
|
|
}
|
2018-07-10 12:20:53 +00:00
|
|
|
this.config = config;
|
2018-07-18 16:57:19 +00:00
|
|
|
this.path = Path;
|
|
|
|
this.snippet = Snippet;
|
2018-07-10 19:29:09 +02:00
|
|
|
this.parts = {};
|
2018-07-18 13:42:20 +00:00
|
|
|
this.svg = new Svg(this);
|
2018-07-17 14:16:17 +00:00
|
|
|
this.hooks = new Hooks();
|
2018-07-10 19:29:09 +02:00
|
|
|
for (let id of config.parts) {
|
|
|
|
this.parts[id] = new Part(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.options = {};
|
2018-07-17 14:16:17 +00:00
|
|
|
if(typeof config.options !== 'undefined' && config.options.length > 0) {
|
|
|
|
for (let conf of config.options) {
|
|
|
|
if(conf.type === 'percentage') this.options[conf.id] = conf.val/100;
|
|
|
|
else this.options[conf.id] = conf.val;
|
|
|
|
}
|
2018-07-10 19:29:09 +02:00
|
|
|
}
|
2018-07-19 14:02:04 +00:00
|
|
|
this.context = {
|
|
|
|
parts: this.parts,
|
|
|
|
options: this.options,
|
|
|
|
values: this.values,
|
|
|
|
config: this.config,
|
|
|
|
settings: this.settings
|
|
|
|
}
|
2018-07-10 12:20:53 +00:00
|
|
|
return this;
|
|
|
|
}
|
2018-07-10 19:29:09 +02:00
|
|
|
|
2018-07-15 11:00:48 +00:00
|
|
|
draft(): void {
|
2018-07-10 19:29:09 +02:00
|
|
|
throw Error('You have to implement the draft() method in your Pattern instance.');
|
|
|
|
}
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-15 08:36:19 +00:00
|
|
|
render(): string {
|
2018-07-18 11:45:46 +00:00
|
|
|
this.hooks.attach('preRenderSvg', this.svg);
|
|
|
|
this.hooks.attach('postRenderSvg', this.svg);
|
2018-07-15 11:00:48 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
return this.svg.render(this);
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:42:20 +00:00
|
|
|
on(hook, method): void {
|
2018-07-18 14:48:49 +00:00
|
|
|
if(typeof this.hooks._hooks[hook] === 'undefined') {
|
2018-07-18 13:42:20 +00:00
|
|
|
this.hooks._hooks[hook] = [];
|
|
|
|
}
|
|
|
|
this.hooks._hooks[hook].push(method);
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
loadPlugin(plugin: () => void): void {
|
|
|
|
for(let hook of this.hooks.all) {
|
|
|
|
if(typeof plugin[hook] === 'function') {
|
|
|
|
this.on(hook, plugin[hook]);
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 16:04:39 +00:00
|
|
|
}
|
2018-07-10 12:20:53 +00:00
|
|
|
}
|