Skip to content

Commit b1e7d34

Browse files
committed
Move out opt in rendering to themes. Appliy opt_in_widget option when rendering editors
1 parent 2ff7955 commit b1e7d34

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/editor.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class AbstractEditor {
2929

3030
this.link_watchers = []
3131
this.watchLoop = false
32+
this.optInWidget = this.options.opt_in_widget ?? this.jsoneditor.options.opt_in_widget
3233

3334
if (options.container) this.setContainer(options.container)
3435
this.registerDependencies()
@@ -216,16 +217,17 @@ export class AbstractEditor {
216217
this.container.setAttribute('data-schemapath', this.path)
217218
}
218219

219-
setOptInCheckbox (header) {
220-
/* the active/deactive checbox control. */
220+
setOptInCheckbox () {
221+
let optIn
221222

222-
this.optInLabel = this.theme.getHiddenLabel(this.formname + ' opt-in')
223-
this.optInLabel.setAttribute('for', this.formname + '-opt-in')
224-
this.optInCheckbox = document.createElement('input')
225-
this.optInCheckbox.setAttribute('type', 'checkbox')
226-
this.optInCheckbox.setAttribute('style', 'margin: 0 10px 0 0;')
227-
this.optInCheckbox.setAttribute('id', this.formname + '-opt-in')
228-
this.optInCheckbox.classList.add('json-editor-opt-in')
223+
if (this.optInWidget === 'switch') {
224+
optIn = this.theme.getOptInSwitch(this.formname)
225+
} else {
226+
optIn = this.theme.getOptInCheckbox(this.formname)
227+
}
228+
229+
this.optInCheckbox = optIn.checkbox
230+
this.optInContainer = optIn.container
229231

230232
this.optInCheckbox.addEventListener('click', () => {
231233
if (this.isActive()) {
@@ -240,12 +242,12 @@ export class AbstractEditor {
240242
const parentOptInDefined = (typeof this.parent.options.show_opt_in !== 'undefined')
241243
const parentOptInEnabled = (parentOptInDefined && this.parent.options.show_opt_in === true)
242244
const parentOptInDisabled = (parentOptInDefined && this.parent.options.show_opt_in === false)
245+
243246
if (parentOptInEnabled || (!parentOptInDisabled && globalOptIn) || (!parentOptInDefined && globalOptIn)) {
244247
/* and control to type object editors if they are not required */
245248
if (this.parent && this.parent.schema.type === 'object' && !this.isRequired() && this.header) {
246-
this.header.appendChild(this.optInLabel)
247-
this.header.appendChild(this.optInCheckbox)
248-
this.header.insertBefore(this.optInCheckbox, this.header.firstChild)
249+
this.header.insertBefore(this.optInContainer, this.header.firstChild)
250+
this.optInAppended = true
249251
}
250252
}
251253
}

0 commit comments

Comments
 (0)