2019-08-03 15:03:33 +02:00
|
|
|
import Pattern from './pattern'
|
2019-02-16 07:28:56 +01:00
|
|
|
|
2022-08-07 17:29:33 +02:00
|
|
|
/*
|
|
|
|
* The Design constructor. Returns a Pattern constructor
|
|
|
|
* So it's sort of a super-constructor
|
|
|
|
*/
|
2020-09-12 19:01:12 +02:00
|
|
|
export default function Design(config, plugins = false, conditionalPlugins = false) {
|
2022-08-07 17:29:33 +02:00
|
|
|
|
|
|
|
// Ensure all options have a hide() method
|
|
|
|
config.options = optionsWithHide(config.options)
|
|
|
|
|
|
|
|
// A place to store deprecation and other warnings before we even have a pattern instantiated
|
|
|
|
config.warnings = []
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The newer way to initalize a design is to pass one single parameter
|
|
|
|
* The old way passed multiple parameters.
|
|
|
|
* So let's figure out which is which and be backwards compatible
|
|
|
|
*
|
|
|
|
* This mitigation should be removed in v3 when we drop support for the legacy way
|
|
|
|
*/
|
|
|
|
config = migrateConfig(config, plugins, conditionalPlugins)
|
2022-02-05 07:11:09 +01:00
|
|
|
|
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
|
|
|
|
2020-09-12 19:01:12 +02:00
|
|
|
// Load plugins
|
2022-08-07 17:29:33 +02:00
|
|
|
if (Array.isArray(config.plugins)) for (const plugin of config.plugins) this.use(plugin)
|
|
|
|
else if (config.plugins) this.use(config.plugins)
|
|
|
|
|
2020-09-12 19:01:12 +02:00
|
|
|
// Load conditional plugins
|
2022-08-07 17:29:33 +02:00
|
|
|
if (Array.isArray(config.conditionalPlugins))
|
|
|
|
for (const plugin of config.conditionalPlugins) this.useIf(plugin, settings)
|
|
|
|
else if (config.conditionalPlugins.plugin && config.conditionalPlugins.condition)
|
|
|
|
this.useIf(config.conditionalPlugins, settings)
|
2020-09-12 19:01:12 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
this.apply(settings)
|
2019-02-16 07:28:56 +01:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper method to handle the legacy way of passing configuration
|
|
|
|
* to the design constructor
|
|
|
|
*/
|
|
|
|
const migrateConfig = (config, plugins, conditionalPlugins) => {
|
|
|
|
|
|
|
|
// Migrate plugins
|
|
|
|
if (plugins && config.plugins) config.warnings.push(
|
|
|
|
'Passing plugins to the Design constructor both as a second parameter and in the config is unsupported',
|
|
|
|
'Ignoring plugins passed as parameter. Only config.plugins will be used.'
|
|
|
|
)
|
|
|
|
else if (plugins && !config.plugins) {
|
|
|
|
config.plugins = plugins
|
|
|
|
config.warnings.push(
|
|
|
|
'Passing a plugins parameter to the Design constructure is deprecated',
|
|
|
|
'Please store them in the `plugins` key of the config object that is the first parameter'
|
|
|
|
)
|
|
|
|
} else if (!config.plugins) config.plugins = []
|
|
|
|
|
|
|
|
// Migrate conditional plugins
|
|
|
|
if (conditionalPlugins && config.conditionalPlugins) config.warnings.push(
|
|
|
|
'Passing conditionalPlugins to the Design constructor both as a third parameter and in the config is unsupported.',
|
|
|
|
'Ignoring conditionalPlugins passes as parameter. Only config.conditionalPlugins will be used.',
|
|
|
|
)
|
|
|
|
else if (conditionalPlugins && !config.conditionalPlugins) {
|
|
|
|
config.conditionalPlugins = conditionalPlugins
|
|
|
|
config.warnings.push(
|
|
|
|
'Passing a conditionalPlugins parameter to the Design constructure is deprecated.',
|
|
|
|
'Please store them in the `conditionalPlugins` key of the config object that is the first parameter'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else if (!config.conditionalPlugins) config.conditionalPlugins = []
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A default hide() method for options that lack it
|
|
|
|
* Since this will always return false, the option will never be hidden
|
|
|
|
*/
|
|
|
|
const hide = () => false // The default hide() method
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper method to add the default hide() method to options who lack one
|
|
|
|
*/
|
|
|
|
const optionsWithHide = options => {
|
|
|
|
if (options) {
|
|
|
|
for (const option in options) {
|
|
|
|
if (typeof options[option] === 'object') options[option] = { hide, ...options[option] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|