Skip to content

Commit

Permalink
Improve if-then-else data porting between switching
Browse files Browse the repository at this point in the history
  • Loading branch information
gbisurgi committed Nov 18, 2024
1 parent 6592494 commit 16179b7
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 80 deletions.
2 changes: 1 addition & 1 deletion dist/cjs/jedi.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cjs/jedi.cjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cjs/jedi.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cjs/jedi.js.map

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions dist/esm/jedi.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/jedi.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/umd/jedi.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/umd/jedi.js.map

Large diffs are not rendered by default.

21 changes: 0 additions & 21 deletions docs/assets/index-CB8zX2Yx.js

This file was deleted.

21 changes: 21 additions & 0 deletions docs/assets/index-SIDPvT3T.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.7/awesomplete.css"/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/quill.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.7/awesomplete.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es2018/jodit.fat.min.js"></script> <script type="module" crossorigin src="/jedi/assets/index-CB8zX2Yx.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es2018/jodit.fat.min.js"></script> <script type="module" crossorigin src="/jedi/assets/index-SIDPvT3T.js"></script>
</head>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/raty.min.js"></script>
Expand Down
8 changes: 5 additions & 3 deletions src-docs/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<div class="col-xs-12 col-sm-8">
<main>
<div class="btn-group">
<button class="btn btn-primary" id="disable-editor" @click="disableEditor()">Disable editor</button>
<button class="btn btn-primary" id="enable-editor" @click="enableEditor()">Enable editor</button>
<button class="btn btn-primary" id="disable-editor" @click="disableEditor()">Disable</button>
<button class="btn btn-primary" id="enable-editor" @click="enableEditor()">Enable</button>
<button class="btn btn-primary" id="show-validation-errors" @click="showValidationErrors()">Show validation errors</button>
<button class="btn btn-primary" id="destroy-editor" @click="destroyEditor()">Destroy editor</button>
<button class="btn btn-primary" id="destroy-editor" @click="destroyEditor()">Destroy</button>
</div>

<hr>
Expand Down Expand Up @@ -154,6 +154,7 @@ import ifThenElseAllOf from './json/validator/if-then-else-allOf.json'
import ifThenElseNested from './json/validator/if-then-else-nested.json'
import ifThenElseNestedSimple from './json/validator/if-then-else-nested-simple.json'
import ifThenElseNestedComplex from './json/validator/if-then-else-nested-complex.json'
import ifThenElseDeeplyNestedAndUnselected from './json/validator/if-then-else-deeply-nested-and-unselected.json'
import items from './json/validator/items.json'
import maxContains from './json/validator/maxContains.json'
import maximum from './json/validator/maximum.json'
Expand Down Expand Up @@ -227,6 +228,7 @@ export default {
'validator/if-then-else-nested': ifThenElseNested,
'validator/if-then-else-nested-simple': ifThenElseNestedSimple,
'validator/if-then-else-nested-complex': ifThenElseNestedComplex,
'validator/if-then-else-deeply-nested-and-unselected': ifThenElseDeeplyNestedAndUnselected,
'validator/items': items,
'validator/maxContains': maxContains,
'validator/maximum': maximum,
Expand Down
128 changes: 128 additions & 0 deletions src-docs/json/validator/if-then-else-deeply-nested-and-unselected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"title": "Red Deer",
"type": "object",
"required": [
"occurrence"
],
"properties": {
"occurrence": {
"title": "Did the species occur in your hunting area during the hunting year 2020/2021?",
"type": "string",
"enum": [
"Resident game",
"Transient game",
"Individual occurrence",
"No occurrence",
"No information"
],
"x-options": {
"enforceEnumDefault": false,
"format": "radio"
}
}
},
"if": {
"properties": {
"occurrence": {
"enum": [
"Resident game",
"Transient game",
"Individual occurrence"
]
}
}
},
"then": {
"properties": {
"huntingBag": {
"$ref": "#/$defs/huntingBag",
"required": [
"harvest",
"accidentGame",
"foundDead",
"unknown"
],
"properties": {
"harvest": {
"$ref": "#/$defs/harvest",
"minimum": 0
},
"accidentGame": {
"$ref": "#/$defs/accidentGame",
"minimum": 0
},
"foundDead": {
"$ref": "#/$defs/foundDead",
"minimum": 0
},
"unknown": {
"$ref": "#/$defs/unknown",
"minimum": 0
}
}
},
"total": {
"$ref": "#/$defs/total",
"minimum": 1
}
}
},
"else": {
"properties": {
"huntingBag": {
"readOnly": true,
"$ref": "#/$defs/huntingBag",
"properties": {
"harvest": {
"$ref": "#/$defs/harvest",
"default": 0,
"const": 0
},
"accidentGame": {
"$ref": "#/$defs/accidentGame",
"default": 0,
"const": 0
},
"foundDead": {
"$ref": "#/$defs/foundDead",
"default": 0,
"const": 0
}
}
},
"total": {
"$ref": "#/$defs/total",
"default": 0,
"const": 0,
"x-options": {}
}
}
},
"$defs": {
"huntingBag": {
"title": "Hunting Bag 2020/21",
"type": "object"
},
"harvest": {
"title": "Harvest Count",
"type": "integer"
},
"accidentGame": {
"title": "Accident Game Count",
"type": "integer"
},
"foundDead": {
"title": "Other Found Dead Count",
"type": "integer"
},
"unknown": {
"title": "Unknown Count",
"type": "integer"
},
"total": {
"title": "Total Hunting Bag 2020/21:",
"type": "integer",
"readOnly": true
}
}
}
2 changes: 1 addition & 1 deletion src/editors/object.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Editor from './editor.js'
import {
equal,
hasOwn, isNumber,
hasOwn,
isObject,
isSet,
pathToAttribute
Expand Down
17 changes: 11 additions & 6 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,22 @@ export function mergeDeep (target, ...sources) {
}

/**
* Merges objects but only the properties that exists in both objects
* if they are the same type of value
* Merges objects but only the properties that exist in both objects
* if they are the same type of value.
* Handles nested objects recursively.
* @param {object} obj1 - The target object
* @param {object[]} obj2 - Objects whose properties are the overrides
* @param {object} obj2 - Object whose properties are the overrides
* @return {object} The overwritten object
*/
export function overwriteExistingProperties (obj1, obj2) {
Object.keys(obj2).forEach(function (key) {
export const overwriteExistingProperties = (obj1, obj2) => {
Object.keys(obj2).forEach((key) => {
if (key in obj1) {
if (typeof obj1[key] === typeof obj2[key]) {
obj1[key] = obj2[key]
if (isObject(obj1[key]) && isObject(obj2[key])) {
overwriteExistingProperties(obj1[key], obj2[key])
} else {
obj1[key] = obj2[key]
}
}
}
})
Expand Down
26 changes: 11 additions & 15 deletions src/instances/if-then-else.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '../helpers/utils.js'

import {
getSchemaConst,
getSchemaElse,
getSchemaIf,
getSchemaThen
Expand All @@ -29,6 +28,7 @@ class InstanceIfThenElse extends Instance {
prepare () {
this.instances = []
this.instanceStartingValues = []
this.instanceWithoutIf = null
this.activeInstance = null
this.index = 0
this.schemas = []
Expand Down Expand Up @@ -67,7 +67,7 @@ class InstanceIfThenElse extends Instance {
delete schemaClone.then
delete schemaClone.else

const instanceWithoutIf = this.jedi.createInstance({
this.instanceWithoutIf = this.jedi.createInstance({
jedi: this.jedi,
schema: schemaClone,
path: this.path,
Expand Down Expand Up @@ -101,20 +101,17 @@ class InstanceIfThenElse extends Instance {
})

this.on('set-value', (newValue) => {
this.instances.forEach((instance) => {
const currentValue = instance.getValue()
let ifValue = this.instanceWithoutIf.getValue()

if (isObject(currentValue) && isObject(newValue)) {
newValue = overwriteExistingProperties(currentValue, newValue)
if (isObject(ifValue) && isObject(newValue)) {
ifValue = overwriteExistingProperties(ifValue, newValue)
}

// restore any const value here
instance.children.forEach((child) => {
const schemaConst = getSchemaConst(child.schema)
this.instances.forEach((instance, index) => {
const startingValue = this.instanceStartingValues[index]

if (isSet(schemaConst)) {
newValue[child.getKey()] = schemaConst
}
})
if (isObject(startingValue) && isObject(newValue)) {
newValue = overwriteExistingProperties(startingValue, ifValue)
}

instance.setValue(newValue, false)
Expand All @@ -129,8 +126,7 @@ class InstanceIfThenElse extends Instance {
})

// initial value and active instance
this.value = instanceWithoutIf.getValue()
instanceWithoutIf.destroy()
this.value = this.instanceWithoutIf.getValue()
const fittestIndex = this.getFittestIndex(this.value)
this.switchInstance(fittestIndex)
}
Expand Down
Loading

0 comments on commit 16179b7

Please sign in to comment.