This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): configUpdater, new API
configUpdater: for downward compatibility new API(think to be fixed @4) BREAKING CHANGES: new API. fix #29, re #19
- Loading branch information
Showing
2 changed files
with
362 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,309 @@ | ||
import configStorage from './configStorage'; | ||
|
||
|
||
/** | ||
* Default config according to user's config and default config. | ||
* @param {Config} userConfig User's custom config. | ||
* @return {Config} Config that has been defaulted. | ||
* To update a Config from previous version. | ||
* @param {Config} oldConfig Any previous version of Config. | ||
* @return {Config} Updated Config. | ||
*/ | ||
function configDefaulter (userConfig) { | ||
function configUpdater (oldConfig) { | ||
|
||
const newConfig = {}; | ||
// For [email protected] to [email protected] | ||
if ('model' in oldConfig) { | ||
|
||
if ('jsonPath' in oldConfig.model) { | ||
|
||
newConfig.modelJsonPath = oldConfig.model.jsonPath; | ||
|
||
} | ||
if ('scale' in oldConfig.model) { | ||
|
||
console.error('model.scale: deprecated.'); | ||
|
||
} | ||
if ('hHeadPos' in oldConfig.model) { | ||
|
||
newConfig.modelHeadPosH = oldConfig.model.hHeadPos; | ||
|
||
} | ||
if ('vHeadPos' in oldConfig.model) { | ||
|
||
newConfig.modelHeadPosV = oldConfig.model.vHeadPos; | ||
|
||
} | ||
if ('myDefine' in oldConfig.model) { | ||
|
||
console.error('model.myDefine: deprecated.'); | ||
|
||
} | ||
|
||
} | ||
if ('display' in oldConfig) { | ||
|
||
if ('superSample' in oldConfig.display) { | ||
|
||
newConfig.displaySampleLevel = oldConfig.display.superSample; | ||
|
||
} | ||
if ('width' in oldConfig.display) { | ||
|
||
newConfig.displayWidth = oldConfig.display.width; | ||
|
||
} | ||
if ('height' in oldConfig.display) { | ||
|
||
newConfig.displayHeight = oldConfig.display.height; | ||
|
||
} | ||
|
||
if ('position' in oldConfig.display) { | ||
|
||
newConfig.displaySide = oldConfig.display.position; | ||
|
||
} | ||
|
||
if ('hOffset' in oldConfig.display) { | ||
|
||
newConfig.displayOffsetH = oldConfig.display.hOffset; | ||
|
||
} | ||
|
||
if ('vOffset' in oldConfig.display) { | ||
|
||
newConfig.displayOffsetV = oldConfig.display.vOffset; | ||
|
||
if (typeof userConfig.displayHeight === 'number') { | ||
} | ||
|
||
} | ||
if ('mobile' in oldConfig) { | ||
|
||
if ('show' in oldConfig.mobile) { | ||
|
||
console.error('mobile.show: please use displayFunc manually.'); | ||
|
||
} | ||
if ('scale' in oldConfig.mobile) { | ||
|
||
console.error('mobile.scale: please use displayFunc manually.'); | ||
|
||
} | ||
if ('motion' in oldConfig.mobile) { | ||
|
||
newConfig.displayDeviceMotion = oldConfig.mobile.motion; | ||
|
||
userConfig.displayHeight = `${userConfig.displayHeight}px`; | ||
} | ||
|
||
} | ||
|
||
if (typeof userConfig.displayWidth === 'number') { | ||
if ('name' in oldConfig) { | ||
|
||
if ('canvas' in oldConfig.name) { | ||
|
||
console.error('name.canvas: deprecated.'); | ||
|
||
userConfig.displayWidth = `${userConfig.displayWidth}px`; | ||
} | ||
if ('div' in oldConfig.name) { | ||
|
||
console.error('name.div: deprecated'); | ||
|
||
} | ||
|
||
} | ||
|
||
return Object.assign({}, userConfig, configStorage.defaultConfig); | ||
if ('react' in oldConfig) { | ||
|
||
if ('opacityDefault' in oldConfig.react) { | ||
|
||
newConfig.displayOpacityDefault = oldConfig.react.opacityDefault; | ||
|
||
} | ||
if ('opacityOnHover' in oldConfig.react) { | ||
|
||
newConfig.displayOpacityOnHover = oldConfig.react.opacityOnHover; | ||
|
||
} | ||
if ('myFunc' in oldConfig.react) { | ||
|
||
console.error('react.myFunc: deprecated'); | ||
|
||
} | ||
|
||
} | ||
if ('dev' in oldConfig) { | ||
|
||
if ('log' in oldConfig.dev) { | ||
|
||
newConfig.devLog = oldConfig.dev.log; | ||
|
||
} | ||
if ('border' in oldConfig.dev) { | ||
|
||
newConfig.devBorder = oldConfig.dev.border; | ||
|
||
} | ||
if ('mouseLog' in oldConfig.dev) { | ||
|
||
newConfig.devMouseLog = oldConfig.dev.mouseLog; | ||
|
||
} | ||
if ('mouseFunc' in oldConfig.dev) { | ||
|
||
console.error('dev.mouseFunc: deprecated.'); | ||
|
||
} | ||
|
||
} | ||
|
||
// For [email protected] to [email protected] (config only) | ||
if ('model' in oldConfig) { | ||
|
||
console.error('model: use modelJsonPath instead.'); | ||
|
||
} | ||
if ('width' in oldConfig) { | ||
|
||
newConfig.displayWidth = oldConfig.width; | ||
|
||
} | ||
if ('height' in oldConfig) { | ||
|
||
newConfig.displayHeight = oldConfig.height; | ||
|
||
} | ||
if ('scaling' in oldConfig) { | ||
|
||
newConfig.displaySampleLevel = oldConfig.scaling; | ||
|
||
} | ||
if ('opacityDefault' in oldConfig) { | ||
|
||
newConfig.displayOpacityDefault = oldConfig.opacityDefault; | ||
|
||
} | ||
if ('opacityHover' in oldConfig) { | ||
|
||
newConfig.displayOpacityOnHover = oldConfig.opacityHover; | ||
|
||
} | ||
if ('mobileShow' in oldConfig) { | ||
|
||
console.error('mobileShow: please use displayFunc manually.'); | ||
|
||
} | ||
if ('mobileScaling' in oldConfig) { | ||
|
||
console.error('mobileScaling: please use displayFunc manually.'); | ||
|
||
} | ||
if ('position' in oldConfig) { | ||
|
||
newConfig.displaySide = oldConfig.position; | ||
|
||
} | ||
if ('horizontalOffset' in oldConfig) { | ||
|
||
newConfig.displayOffsetH = oldConfig.horizontalOffset; | ||
|
||
} | ||
if ('verticalOffset' in oldConfig) { | ||
|
||
newConfig.displayOffsetV = oldConfig.verticalOffset; | ||
|
||
} | ||
if ('className' in oldConfig) { | ||
|
||
console.error('className: deprecated.'); | ||
|
||
} | ||
if ('id' in oldConfig) { | ||
|
||
console.error('id: deprecated.'); | ||
|
||
} | ||
if ('bottom' in oldConfig) { | ||
|
||
newConfig.displayOffsetV = oldConfig.bottom; | ||
|
||
} | ||
|
||
// Convert type | ||
|
||
if ('displayHeight' in newConfig) { | ||
|
||
if (typeof newConfig.displayHeight !== 'string') { | ||
|
||
newConfig.displayHeight = `${newConfig.displayHeight}px`; | ||
|
||
} | ||
|
||
} | ||
if ('displayWidth' in newConfig) { | ||
|
||
if (typeof newConfig.displayWidth !== 'string') { | ||
|
||
newConfig.displayWidth = `${newConfig.displayWidth}px`; | ||
|
||
} | ||
|
||
} | ||
if ('displayOffsetH' in newConfig) { | ||
|
||
if (typeof newConfig.displayOffsetH !== 'string') { | ||
|
||
newConfig.displayOffsetH = `${newConfig.displayOffsetH}px`; | ||
|
||
} | ||
|
||
} | ||
if ('displayOffsetV' in newConfig) { | ||
|
||
if (typeof newConfig.displayOffsetV !== 'string') { | ||
|
||
newConfig.displayOffsetV = `${newConfig.displayOffsetV}px`; | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
return newConfig; | ||
|
||
} | ||
|
||
/** | ||
* Default config based on user's config and default config.<br> | ||
* Will update the Config first. | ||
* @param {Config} userConfig User's custom config. | ||
* @return {Config} Config that has been defaulted. | ||
*/ | ||
function configDefaulter (userConfig) { | ||
|
||
const newConfig = configUpdater(userConfig); | ||
|
||
return Object.assign({}, newConfig, configStorage.defaultConfig); | ||
|
||
} | ||
|
||
|
||
|
||
/** | ||
* To validate whether user config is valid. | ||
* @param {Config} userConfig User's custom config. | ||
* @return {Boolean} Whether the config is valid. | ||
* To validate whether user config is valid.<br> | ||
* Console an error if there's something wrong. | ||
* @param {Config} userConfig User's custom config. | ||
* @return {Boolean} Whether the config is valid. | ||
*/ | ||
function configValidater (userConfig) { | ||
|
||
let validFlag = true; | ||
|
||
for (const key in configStorage.configType) { | ||
|
||
if ({}.hasOwnProperty.call(configStorage.configType, key)) { | ||
|
||
if (typeof userConfig[key] !== configStorage.configType[key]) { | ||
if (key in userConfig && typeof userConfig[key] !== configStorage.configType[key]) { | ||
|
||
throw new Error(`config.${key} is expected a ${configStorage.configType[key]}, but got a ${typeof userConfig[key]}`); | ||
validFlag = false; | ||
console.error(`config.${key} is expected a ${configStorage.configType[key]}, but got a ${typeof userConfig[key]}`); | ||
|
||
} | ||
|
||
|
@@ -48,11 +312,12 @@ function configValidater (userConfig) { | |
|
||
} | ||
|
||
return true; | ||
return validFlag; | ||
|
||
} | ||
|
||
export { | ||
configDefaulter, | ||
configValidater, | ||
configUpdater, | ||
}; |
Oops, something went wrong.