diff --git a/lib/hooks.ts b/lib/hooks.ts index 64273ccf3f3..cf51472d3ef 100644 --- a/lib/hooks.ts +++ b/lib/hooks.ts @@ -1,8 +1,10 @@ export class Hooks { - _hooks: object; + hooks: object; + all: string[]; constructor(app) { this._hooks = {}; + this.all = ['preRenderSvg', 'postRenderSvg']; } on(hook, method): void { @@ -10,7 +12,7 @@ export class Hooks { this.hooks._hooks[hook] = []; } this.hooks._hooks[hook].push(method); - console.log('in on method', hook, method); + console.log('Hooks::on ', hook, method); } list(hook): function[] { @@ -29,11 +31,11 @@ export class Hooks { } attach (hook: string, obj: object): void { + console.log('Hooks::attach', hook, obj, this._hooks); if(typeof this._hooks[hook] === 'undefined') return; for(let func of this._hooks[hook]) { - console.log('in attach', hook, func); + console.log('Hooks::attach', hook, func); obj.pre(hook, func); } } - } diff --git a/lib/pattern.ts b/lib/pattern.ts index 861881557de..180f199019f 100644 --- a/lib/pattern.ts +++ b/lib/pattern.ts @@ -7,7 +7,7 @@ import { Option } from './option' export class Pattern { config: PatternConfig; - svg: Svg = new Svg(); + svg: Svg; parts: { [index: string]: Part; } @@ -15,7 +15,7 @@ export class Pattern { values: {[propName: string]: any} = {}; settings: {[propName: string]: any} = {mode: 'draft', units: 'metric'}; hooks: Hooks; - on: function; + on: () => void; constructor(config: PatternConfig) { if(!config) { @@ -26,6 +26,7 @@ export class Pattern { } this.config = config; this.parts = {}; + this.svg = new Svg(); this.hooks = new Hooks(); this.on = this.hooks.on; for (let id of config.parts) { @@ -47,15 +48,19 @@ export class Pattern { } render(): string { - let svg = new Svg(this); - this.hooks.attach('preSvgRender', svg); - this.hooks.attach('postSvgRender', svg); - //svg.pre('preRenderSvg', function(next) { - // console.log('manual attach'); - // this.style += "path {stroke: #000; fill: none;}"; - // next(); - //}); + this.hooks.attach('preRenderSvg', this.svg); + this.hooks.attach('postRenderSvg', this.svg); - return svg.render(this); + return this.svg.render(this); + } + + loadPlugin(plugin: () => void): void { + console.log('Pattern::loadPlugin', plugin); + for(let hook of this.hooks.all) { + if(typeof plugin[hook] === 'function') { + console.log('Pattern::loadPlugin - hook', plugin[hook]); + this.on(hook, plugin[hook]); + } + } } } diff --git a/lib/svg.ts b/lib/svg.ts index a743e4a9a0e..da642f002e5 100644 --- a/lib/svg.ts +++ b/lib/svg.ts @@ -23,7 +23,7 @@ export class Svg { hooks: string[]; pattern: Pattern; - constructor(pattern) { + constructor() { this.prefix = ''; this.attributes.add this.attributes.add("xmlns", "http://www.w3.org/2000/svg"); @@ -31,23 +31,22 @@ export class Svg { this.attributes.add("xmlns:xlink", "http://www.w3.org/1999/xlink"); this.attributes.add("xmlns:freesewing", "http://freesewing.org/namespaces/freesewing"); this.attributes.add("freesewing:foo", "bar"); - this.pattern = pattern; - this.hooks = ['preSvgRender', 'postSvgRender']; + this.hooks = ['preRenderSvg', 'postRenderSvg']; for(let k in hooklib) this[k] = hooklib[k]; for(let k in this.hooks) this.hook(k, this[k]); return this; } - /** Method to attach preSvgRender hooks on */ - preSvgRender(): void {} + /** Method to attach preRenderSvg hooks on */ + preRenderSvg(): void {} - /** Method to attach postSvgRender hooks on */ - postSvgRender(): void {} + /** Method to attach postRenderSvg hooks on */ + postRenderSvg(): void {} /** Renders a draft object as SVG */ render(pattern: Pattern): string { - this.preSvgRender(); + this.preRenderSvg(); this.svg = this.prefix; this.svg += this.renderComments(this.header); this.svg += this.renderSvgTag(pattern); @@ -66,7 +65,7 @@ export class Svg { this.svg += this.closeGroup(); this.svg += this.nl()+''; this.svg += this.renderComments(this.footer); - this.postSvgRender(); + this.postRenderSvg(); return this.svg; }