Skip to content

Commit 1e74287

Browse files
author
Riccardo Di Benedetto
committed
option for not using default value & fix select default value
1 parent b53c265 commit 1e74287

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

src/core.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ export class JSONEditor {
1515
if (!(element instanceof Element)) throw new Error('element should be an instance of Element')
1616

1717
this.element = element
18-
console.log('Before first extend')
19-
try {
20-
this.options = extend({}, JSONEditor.defaults.options, options)
21-
} catch (err) {
22-
console.log('Error first extend ', err)
23-
}
24-
console.log('After first extend - aligned')
18+
this.options = extend({}, JSONEditor.defaults.options, options)
2519
this.ready = false
2620
this.copyClipboard = null
2721
this.schema = this.options.schema
@@ -32,13 +26,15 @@ export class JSONEditor {
3226
const themeName = this.options.theme || JSONEditor.defaults.theme
3327
const themeClass = JSONEditor.defaults.themes[themeName]
3428

29+
this.MAX_RECURSIONS = this.options.maxRecurions
30+
31+
console.log('Current options ', this.options)
3532
/* Load editors and selected theme style rules */
3633
if (!themeClass) throw new Error(`Unknown theme ${themeName}`)
3734
this.element.setAttribute('data-theme', themeName)
3835
// eslint-disable-next-line new-cap
3936
this.theme = new themeClass(this)
4037
const rules = extend(themeClass.rules, this.getEditorsRules())
41-
console.log('After rules extend')
4238
if (!this.theme.options.disable_theme_rules) {
4339
/* Attempt to locate a shadowRoot parent (i.e. in Web Components) */
4440
const shadowRoot = getShadowParent(this.element)
@@ -77,9 +73,7 @@ export class JSONEditor {
7773
required: true,
7874
container: this.root_container
7975
})
80-
console.log('After create editor')
8176
this.root.preBuild()
82-
console.log('After root postBuild')
8377
this.root.build()
8478
this.root.postBuild()
8579
/* Starting data */
@@ -98,7 +92,6 @@ export class JSONEditor {
9892
this.trigger('change')
9993
})
10094
}, fetchUrl, location)
101-
console.log('End of constructor')
10295
}
10396

10497
getValue () {

src/defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ function translate (key, variables) {
283283
/* Default options when initializing JSON Editor */
284284
const options = {
285285
upload,
286-
prompt_before_delete: true
286+
prompt_before_delete: true,
287+
useDefault: true,
288+
maxRecurions: 99999
287289
}
288290

289291
/* This assignment was previously in index.js but makes more sense here */

src/editor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ export class AbstractEditor {
524524
return this.schema.default
525525
}
526526

527+
if (!this.jsoneditor.options.useDefault) {
528+
return null
529+
}
530+
527531
if (typeof this.schema.enum !== 'undefined') {
528532
return this.schema.enum[0]
529533
}

src/editors/object.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { AbstractEditor } from '../editor.js'
22
import { extend, trigger, hasOwnProperty } from '../utilities.js'
33

4-
const MAX_RECURSION = 0
5-
64
export class ObjectEditor extends AbstractEditor {
75
constructor (a, b, recursion, stopRecursionCallback = () => {}) {
86
super(a, b)
97
this._recursion = recursion
108
this._stopRecursionCallback = stopRecursionCallback
119
this.collapsed = null
1210
this.collapseOnStopRecursion = this.collapseOnStopRecursion.bind(this)
11+
console.log('jsoneditor MAX_RECURSIONS', this.jsoneditor.MAX_RECURSIONS)
1312
}
1413

1514
getDefault () {
@@ -1003,7 +1002,7 @@ export class ObjectEditor extends AbstractEditor {
10031002

10041003
this.editors[name] = this.jsoneditor.createEditor(editor, {
10051004
jsoneditor: this.jsoneditor,
1006-
schema: this._recursion >= MAX_RECURSION ? { type: schema.type || null } : schema,
1005+
schema: this._recursion >= this.jsoneditor.MAX_RECURSIONS ? { type: schema.type || null } : schema,
10071006
path: `${this.path}.${name}`,
10081007
parent: this
10091008
}, this._recursion + 1, this.collapseOnStopRecursion)
@@ -1021,7 +1020,7 @@ export class ObjectEditor extends AbstractEditor {
10211020

10221021
this.cached_editors[name] = this.editors[name]
10231022

1024-
if (this._recursion >= MAX_RECURSION) {
1023+
if (this._recursion >= this.jsoneditor.MAX_RECURSIONS) {
10251024
console.info('Stop recursion')
10261025
this._stopRecursionCallback()
10271026
}

src/editors/select.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { extend } from '../utilities.js'
33

44
export class SelectEditor extends AbstractEditor {
55
setValue (value, initial) {
6+
const correctValue = initial && value === null && this.value === undefined ? 'undefined' : value
67
/* Sanitize value before setting it */
7-
let sanitized = this.typecast(value || '')
8+
let sanitized = this.typecast(correctValue || '')
9+
console.log('SETVALUE ', 'CURRENT ', this.value, 'SET ', value, 'INITIAL ', initial, 'SANITIZED', sanitized)
810

911
if (!this.enum_values.includes(sanitized)) sanitized = this.enum_values[0]
1012

@@ -52,7 +54,9 @@ export class SelectEditor extends AbstractEditor {
5254
if (!this.dependenciesFulfilled) {
5355
return undefined
5456
}
55-
return this.typecast(this.value)
57+
const value = this.typecast(this.value)
58+
console.log('GET VALUE ', value, this.value)
59+
return value
5660
}
5761

5862
preBuild () {
@@ -170,6 +174,7 @@ export class SelectEditor extends AbstractEditor {
170174
this.setInputAttributes([])
171175

172176
this.input.addEventListener('change', (e) => {
177+
console.log('SELECT CHANGE ')
173178
e.preventDefault()
174179
e.stopPropagation()
175180
this.onInputChange()
@@ -178,8 +183,9 @@ export class SelectEditor extends AbstractEditor {
178183
this.control = this.theme.getFormControl(this.label, this.input, this.description, this.infoButton)
179184
this.container.appendChild(this.control)
180185

186+
console.log('ENUM VALUES ', this.enum_values)
181187
this.value = this.enum_values[0]
182-
188+
console.log('SET ', this.value)
183189
/* Any special formatting that needs to happen after the input is added to the dom */
184190
window.requestAnimationFrame(() => {
185191
if (this.input.parentNode) this.afterInputReady()

0 commit comments

Comments
 (0)