1
0
Fork 0
freesewing/packages/core/src/design.mjs

66 lines
1.7 KiB
JavaScript
Raw Normal View History

import { Pattern } from './pattern.mjs'
2022-08-26 18:51:02 +02:00
import { addPartConfig } from './utils.mjs'
2022-08-27 09:28:36 +02:00
/*
* The Design constructor. Returns a Pattern constructor
* So it's sort of a super-constructor
*/
export function Design(config) {
2022-08-27 09:28:36 +02:00
// Initialize config with defaults
2022-08-27 09:28:36 +02:00
config = {
measurements: [],
optionalMeasurements: [],
options: {},
parts: [],
2022-08-27 09:28:36 +02:00
plugins: [],
2022-09-04 18:22:02 +02:00
// A place to store deprecation and other warnings before we even have a pattern instantiated
events: {
debug: [],
error: [],
info: [],
suggestion: [],
warning: [],
},
2022-08-27 09:28:36 +02:00
...config
}
2022-09-04 18:22:02 +02:00
const raiseEvent = function (data, type) {
config.events[type].push(data)
}
// Polyfill for pattern raise methods
const raise = {
debug: data => raiseEvent(`[early] `+data, 'debug'),
error: data => raiseEvent(`[early] `+data, 'error'),
info: data => raiseEvent(`[early] `+data, 'info'),
suggestion: data => raiseEvent(`[early] `+data, 'suggestion'),
warning: data => raiseEvent(`[early] `+data, 'warning'),
}
2022-08-27 09:28:36 +02:00
const parts = {}
for (const part of config.parts) {
if (typeof part === 'object') {
parts[part.name] = part
2022-09-04 18:22:02 +02:00
config = addPartConfig(parts[part.name], config, raise )
}
2022-08-27 09:28:36 +02:00
else throw("Invalid part configuration. Part is not an object")
}
2022-08-27 09:28:36 +02:00
// Replace config.parts with the resolved config
config.parts = parts
const pattern = function (settings) {
2019-08-03 15:03:33 +02:00
Pattern.call(this, config)
return this.init().apply(settings)
2019-08-03 15:03:33 +02:00
}
2019-02-16 07:28:56 +01:00
// Set up inheritance
2019-08-03 15:03:33 +02:00
pattern.prototype = Object.create(Pattern.prototype)
pattern.prototype.constructor = pattern
2019-02-16 07:28:56 +01:00
// Make config available without need to instantiate pattern
pattern.config = config
2019-08-03 15:03:33 +02:00
return pattern
2019-02-16 07:28:56 +01:00
}