🚧 Moved hooks to pattern, but they overwrite each other
This commit is contained in:
parent
530a2b11bb
commit
7cc632ff45
3 changed files with 21 additions and 18 deletions
19
lib/hooks.ts
19
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
|
||||
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]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue