diff --git a/lib/hooks.ts b/lib/hooks.ts index cf51472d3ef..0195f65b57f 100644 --- a/lib/hooks.ts +++ b/lib/hooks.ts @@ -7,13 +7,16 @@ export class Hooks { this.all = ['preRenderSvg', 'postRenderSvg']; } - on(hook, method): void { - if(typeof this.hooks._hooks[method] === 'undefined') { - this.hooks._hooks[hook] = []; - } - this.hooks._hooks[hook].push(method); - console.log('Hooks::on ', hook, method); - } + ///** Add hook */ + //on(hook, method): void { + // // This gets called from the pattern context + // // so 'this' is not actually this class + // let self = this.hooks; + // if(typeof self._hooks[method] === 'undefined') { + // self._hooks[hook] = []; + // } + // self._hooks[hook].push(method); + //} list(hook): function[] { if(typeof this._hooks[hook] === 'undefined') { @@ -31,10 +34,8 @@ 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('Hooks::attach', hook, func); obj.pre(hook, func); } } diff --git a/lib/pattern.ts b/lib/pattern.ts index 180f199019f..dbe76f7a2f7 100644 --- a/lib/pattern.ts +++ b/lib/pattern.ts @@ -15,7 +15,6 @@ export class Pattern { values: {[propName: string]: any} = {}; settings: {[propName: string]: any} = {mode: 'draft', units: 'metric'}; hooks: Hooks; - on: () => void; constructor(config: PatternConfig) { if(!config) { @@ -26,9 +25,8 @@ export class Pattern { } this.config = config; this.parts = {}; - this.svg = new Svg(); + this.svg = new Svg(this); this.hooks = new Hooks(); - this.on = this.hooks.on; for (let id of config.parts) { this.parts[id] = new Part(id); } @@ -54,11 +52,17 @@ export class Pattern { return this.svg.render(this); } + /** Add hook */ + on(hook, method): void { + if(typeof this.hooks._hooks[method] === 'undefined') { + this.hooks._hooks[hook] = []; + } + this.hooks._hooks[hook].push(method); + } + 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 da642f002e5..6b8b387b980 100644 --- a/lib/svg.ts +++ b/lib/svg.ts @@ -23,14 +23,12 @@ export class Svg { hooks: string[]; pattern: Pattern; - constructor() { + constructor(pattern: Pattern) { + this.pattern = pattern; // Needed to expose pattern to hooks this.prefix = ''; - this.attributes.add this.attributes.add("xmlns", "http://www.w3.org/2000/svg"); this.attributes.add("xmlns:svg", "http://www.w3.org/2000/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.hooks = ['preRenderSvg', 'postRenderSvg']; for(let k in hooklib) this[k] = hooklib[k]; for(let k in this.hooks) this.hook(k, this[k]);