1
0
Fork 0

🚧 Fighting with hooks

This commit is contained in:
joostdecock 2018-07-17 14:16:17 +00:00
parent 7b4514acd8
commit 5d81c6d50a
16 changed files with 379 additions and 661 deletions

View file

@ -2,35 +2,43 @@ import { PatternConfig, PatternOption } from './types'
import { Point } from './point'
import { Part } from './part'
import { Svg } from './svg'
import { Hooks } from './hooks'
import { Option } from './option'
import themes from './themes'
import { Theme } from './themes/theme'
export class Pattern {
config: PatternConfig;
svg: Svg = new Svg();
themes: {[index:string]: Theme} = themes;
parts: {
[index: string]: Part;
}
options: {[propName: string]: number};
values: {[propName: string]: any} = {};
settings: {[propName: string]: any} = {mode: 'draft', units: 'metric'};
hooks: Hooks;
on: function;
constructor(config: PatternConfig) {
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";
}
this.config = config;
this.parts = {};
this.hooks = new Hooks();
this.on = this.hooks.on;
for (let id of config.parts) {
this.parts[id] = new Part(id);
}
this.options = {};
for (let conf of config.options) {
if(conf.type === 'percentage') this.options[conf.id] = conf.val/100;
else this.options[conf.id] = conf.val;
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;
}
}
return this;
}
@ -39,9 +47,13 @@ export class Pattern {
}
render(): string {
let svg = new Svg();
let theme = this.themes[this.settings.mode];
theme.preRender(this, svg);
let svg = new Svg(this);
this.hooks.attach('loadStyle', svg);
//svg.pre('preRenderSvg', function(next) {
// console.log('manual attach');
// this.style += "path {stroke: #000; fill: none;}";
// next();
//});
return svg.render(this);
}