1
0
Fork 0
freesewing/lib/pattern.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-07-10 19:29:09 +02:00
import { PatternConfig, PatternOption } from './types'
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-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-15 08:36:19 +00:00
svg: Svg = new 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};
values: {[propName: string]: any} = {};
settings: {[propName: string]: any} = {mode: 'draft', units: 'metric'};
2018-07-17 14:16:17 +00:00
hooks: Hooks;
on: function;
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-10 19:29:09 +02:00
this.parts = {};
2018-07-17 14:16:17 +00:00
this.hooks = new Hooks();
this.on = this.hooks.on;
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-10 12:20:53 +00:00
return this;
}
2018-07-10 19:29:09 +02: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-17 14:16:17 +00:00
let svg = new Svg(this);
this.hooks.attach('preSvgRender', svg);
this.hooks.attach('postSvgRender', svg);
2018-07-17 14:16:17 +00:00
//svg.pre('preRenderSvg', function(next) {
// console.log('manual attach');
// this.style += "path {stroke: #000; fill: none;}";
// next();
//});
2018-07-15 08:36:19 +00:00
return svg.render(this);
2018-07-14 16:04:39 +00:00
}
2018-07-10 12:20:53 +00:00
}