2022-08-28 02:14:39 +02:00
|
|
|
import { Pattern } from './pattern.mjs'
|
2022-08-26 18:51:02 +02:00
|
|
|
import { addPartConfig } from './utils.mjs'
|
2022-08-13 14:27:39 +02:00
|
|
|
|
2022-08-27 09:28:36 +02:00
|
|
|
|
2022-08-07 17:29:33 +02:00
|
|
|
/*
|
|
|
|
* The Design constructor. Returns a Pattern constructor
|
|
|
|
* So it's sort of a super-constructor
|
|
|
|
*/
|
2022-08-28 02:14:39 +02:00
|
|
|
export function Design(config) {
|
2022-08-27 09:28:36 +02:00
|
|
|
|
2022-08-31 17:52:39 +02:00
|
|
|
// Initialize config with defaults
|
2022-08-27 09:28:36 +02:00
|
|
|
config = {
|
|
|
|
measurements: [],
|
|
|
|
optionalMeasurements: [],
|
2022-08-31 17:52:39 +02:00
|
|
|
options: {},
|
|
|
|
parts: [],
|
2022-08-27 09:28:36 +02:00
|
|
|
plugins: [],
|
|
|
|
...config
|
|
|
|
}
|
|
|
|
const parts = {}
|
|
|
|
for (const part of config.parts) {
|
|
|
|
if (typeof part === 'object') {
|
|
|
|
parts[part.name] = part
|
|
|
|
config = addPartConfig(parts[part.name], config)
|
2022-08-14 18:16:15 +02:00
|
|
|
}
|
2022-08-27 09:28:36 +02:00
|
|
|
else throw("Invalid part configuration. Part is not an object")
|
2022-08-09 20:17:35 +02:00
|
|
|
}
|
2022-08-27 09:28:36 +02:00
|
|
|
// Replace config.parts with the resolved config
|
|
|
|
config.parts = parts
|
2022-08-13 14:27:39 +02:00
|
|
|
|
2022-08-27 09:28:36 +02:00
|
|
|
// Ensure all options have a hide() method and menu property
|
|
|
|
config.options = completeOptions(config.options)
|
2022-08-07 17:29:33 +02:00
|
|
|
|
|
|
|
// A place to store deprecation and other warnings before we even have a pattern instantiated
|
|
|
|
config.warnings = []
|
|
|
|
|
2020-09-12 19:01:12 +02:00
|
|
|
const pattern = function (settings) {
|
2019-08-03 15:03:33 +02:00
|
|
|
Pattern.call(this, config)
|
2022-08-07 17:29:33 +02:00
|
|
|
|
2022-08-31 17:52:39 +02:00
|
|
|
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
|
|
|
|
2019-09-21 19:42:53 +02: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
|
|
|
}
|
2022-08-07 17:29:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A default hide() method for options that lack it
|
2022-08-27 09:28:36 +02:00
|
|
|
* As this always return false, the option will never be hidden
|
2022-08-07 17:29:33 +02:00
|
|
|
*/
|
2022-08-27 09:28:36 +02:00
|
|
|
const hide = () => false
|
2022-08-07 17:29:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper method to add the default hide() method to options who lack one
|
2022-08-27 09:28:36 +02:00
|
|
|
* as well as set the `menu` property to false (if it's missing)
|
2022-08-07 17:29:33 +02:00
|
|
|
*/
|
2022-08-27 09:28:36 +02:00
|
|
|
const completeOptions = options => {
|
2022-08-07 17:29:33 +02:00
|
|
|
if (options) {
|
|
|
|
for (const option in options) {
|
2022-08-27 09:28:36 +02:00
|
|
|
if (typeof options[option] === 'object') {
|
|
|
|
options[option] = { hide, menu: false, ...options[option] }
|
|
|
|
}
|
2022-08-07 17:29:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|