Skip to content

Commit

Permalink
Feature: collapse for array and object editors
Browse files Browse the repository at this point in the history
  • Loading branch information
gbisurgi committed Jun 17, 2024
1 parent 4803498 commit 868b510
Show file tree
Hide file tree
Showing 19 changed files with 659 additions and 88 deletions.
221 changes: 197 additions & 24 deletions dist/jedi.js

Large diffs are not rendered by default.

221 changes: 197 additions & 24 deletions docs/js/jedi.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions docs/js/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ window.addEventListener('DOMContentLoaded', () => {
'change',
'always'
],
editableProperties: true,
enablePropertiesToggle: true,
enableCollapseToggle: true,
startCollapsed: false,
schema: {},
editor: null,
theme: 'barebones',
Expand Down Expand Up @@ -179,7 +181,9 @@ window.addEventListener('DOMContentLoaded', () => {

const options = {
container: document.querySelector('#jedi-container'),
editableProperties: this.editableProperties,
enablePropertiesToggle: this.enablePropertiesToggle,
enableCollapseToggle: this.enableCollapseToggle,
startCollapsed: this.startCollapsed,
iconLib: this.iconLib,
showErrors: this.showErrors,
validateFormat: this.validateFormat,
Expand All @@ -189,6 +193,7 @@ window.addEventListener('DOMContentLoaded', () => {
}

this.editor = new Jedi.Jedi(options)
window.editor = this.editor
this.editorChangeHandler()
this.editor.on('change', this.editorChangeHandler)
},
Expand Down
2 changes: 1 addition & 1 deletion docs/json/examples/login.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"csfr"
],
"options": {
"editableProperties": false
"enablePropertiesToggle": false
},
"properties": {
"email": {
Expand Down
27 changes: 23 additions & 4 deletions docs/json/validator/prefixItems.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
{
"type": "array",
"prefixItems": [
{ "type": "number" },
{ "type": "string" },
{ "enum": ["Street", "Avenue", "Boulevard"] },
{ "enum": ["NW", "NE", "SW", "SE"] }
{
"type": "number"
},
{
"type": "string"
},
{
"type": "string",
"enum": [
"Street",
"Avenue",
"Boulevard"
]
},
{
"type": "string",
"enum": [
"NW",
"NE",
"SW",
"SE"
]
}
]
}
14 changes: 12 additions & 2 deletions docs/views/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ <h1>Playground</h1>
</div>

<div class="form-group">
<input type="checkbox" id="editableProperties" v-model="editableProperties" @change="initEditor()">
<label for="editableProperties">editableProperties</label>
<input type="checkbox" id="enablePropertiesToggle" v-model="enablePropertiesToggle" @change="initEditor()">
<label for="enablePropertiesToggle">enablePropertiesToggle</label>
</div>

<div class="form-group">
<input type="checkbox" id="enableCollapseToggle" v-model="enableCollapseToggle" @change="initEditor()">
<label for="enableCollapseToggle">enableCollapseToggle</label>
</div>

<div class="form-group">
<input type="checkbox" id="startCollapsed" v-model="startCollapsed" @change="initEditor()">
<label for="startCollapsed">startCollapsed</label>
</div>

<div class="form-group">
Expand Down
4 changes: 3 additions & 1 deletion src/editors/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class EditorArray extends Editor {
title: getSchemaTitle(this.instance.schema) || this.instance.getKey(),
titleHidden: getSchemaOption(this.instance.schema, 'titleHidden'),
id: pathToAttribute(this.instance.path),
description: getSchemaDescription(this.instance.schema)
description: getSchemaDescription(this.instance.schema),
enableCollapseToggle: this.instance.jedi.options.enableCollapseToggle || getSchemaOption(this.instance.schema, 'enableCollapseToggle'),
startCollapsed: this.instance.jedi.options.startCollapsed || getSchemaOption(this.instance.schema, 'startCollapsed')
})
}

Expand Down
20 changes: 11 additions & 9 deletions src/editors/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ class EditorObject extends Editor {
addProperty = false
}

let editableProperties = false
let enablePropertiesToggle = false

if (isSet(this.instance.jedi.options.editableProperties)) {
editableProperties = this.instance.jedi.options.editableProperties
if (isSet(this.instance.jedi.options.enablePropertiesToggle)) {
enablePropertiesToggle = this.instance.jedi.options.enablePropertiesToggle
}

if (isSet(schemaOptions.editableProperties)) {
editableProperties = schemaOptions.editableProperties
if (isSet(schemaOptions.enablePropertiesToggle)) {
enablePropertiesToggle = schemaOptions.enablePropertiesToggle
}

this.control = this.theme.getObjectControl({
title: getSchemaTitle(this.instance.schema) || this.instance.getKey(),
titleHidden: getSchemaOption(this.instance.schema, 'titleHidden'),
id: pathToAttribute(this.instance.path),
description: getSchemaDescription(this.instance.schema),
editableProperties: editableProperties,
addProperty: addProperty
enablePropertiesToggle: enablePropertiesToggle,
addProperty: addProperty,
enableCollapseToggle: this.instance.jedi.options.enableCollapseToggle || getSchemaOption(this.instance.schema, 'enableCollapseToggle'),
startCollapsed: this.instance.jedi.options.startCollapsed || getSchemaOption(this.instance.schema, 'startCollapsed')
})
}

Expand Down Expand Up @@ -105,9 +107,9 @@ class EditorObject extends Editor {
}

refreshPropertiesSlot () {
const schemaOptionEditableProperties = getSchemaOption(this.instance.schema, 'editableProperties')
const schemaOptionEnablePropertiesToggle = getSchemaOption(this.instance.schema, 'enablePropertiesToggle')

if (equal(this.instance.jedi.options.editableProperties, true) || equal(schemaOptionEditableProperties, true)) {
if (equal(this.instance.jedi.options.enablePropertiesToggle, true) || equal(schemaOptionEnablePropertiesToggle, true)) {
Object.keys(this.instance.properties).forEach((property) => {
// refactor with isNotRequired
const isRequired = this.instance.isRequired(property)
Expand Down
7 changes: 6 additions & 1 deletion src/instances/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ class InstanceObject extends Instance {

let musstCreateChild = true

const optionsDeactivateNonRequired = this.jedi.options.deactivateNonRequired
const deactivateNonRequired = getSchemaOption(this.schema, 'deactivateNonRequired')
const schemaDeactivateNonRequired = getSchemaOption(schema, 'deactivateNonRequired')

if (this.isNotRequired(key) && isSet(optionsDeactivateNonRequired) && optionsDeactivateNonRequired === true) {
musstCreateChild = false
}

if (this.isNotRequired(key) && isSet(deactivateNonRequired) && deactivateNonRequired === true) {
musstCreateChild = false
}
Expand Down Expand Up @@ -112,7 +117,7 @@ class InstanceObject extends Instance {
this.children.push(instance)
this.value[key] = instance.getValue()

const deactivateNonRequired = getSchemaOption(this.schema, 'deactivateNonRequired')
const deactivateNonRequired = this.jedi.options.deactivateNonRequired || getSchemaOption(this.schema, 'deactivateNonRequired')

if (this.isNotRequired(key) && isSet(deactivateNonRequired) && deactivateNonRequired === true) {
instance.deactivate()
Expand Down
6 changes: 5 additions & 1 deletion src/jedi.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class Jedi extends EventEmitter {
this.options = Object.assign({
container: null,
refParser: null,
editableProperties: false,
enablePropertiesToggle: false,
enableCollapseToggle: false,
startCollapsed: false,
deactivateNonRequired: false,
schema: {},
showErrors: 'change',
data: undefined,
Expand Down Expand Up @@ -190,6 +193,7 @@ class Jedi extends EventEmitter {
if (this.refParser) {
config.schema = this.refParser.expand(config.schema, config.path)
}

const schemaType = getSchemaType(config.schema)
const schemaOneOf = getSchemaOneOf(config.schema)
const schemaAnyOf = getSchemaAnyOf(config.schema)
Expand Down
18 changes: 18 additions & 0 deletions src/themes/bootstrap3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ class ThemeBootstrap3 extends Theme {
this.useToggleEvents = false
}

getCollapseToggle (config) {
const toggle = super.getCollapseToggle(config)
toggle.setAttribute('href', '#' + config.collapseId)
toggle.setAttribute('data-toggle', 'collapse')
return toggle
}

getCollapse (config) {
const collapse = super.getCollapse(config)
collapse.classList.add('collapse')

if (!config.startCollapsed) {
collapse.classList.add('in')
}

return collapse
}

getFieldset () {
const fieldset = document.createElement('fieldset')
fieldset.classList.add('panel')
Expand Down
18 changes: 18 additions & 0 deletions src/themes/bootstrap4.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ class ThemeBootstrap4 extends Theme {
this.useToggleEvents = false
}

getCollapseToggle (config) {
const toggle = super.getCollapseToggle(config)
toggle.setAttribute('href', '#' + config.collapseId)
toggle.setAttribute('data-toggle', 'collapse')
return toggle
}

getCollapse (config) {
const collapse = super.getCollapse(config)
collapse.classList.add('collapse')

if (!config.startCollapsed) {
collapse.classList.add('show')
}

return collapse
}

getFieldset () {
const fieldset = document.createElement('fieldset')
fieldset.classList.add('card')
Expand Down
18 changes: 18 additions & 0 deletions src/themes/bootstrap5.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ class ThemeBootstrap5 extends Theme {
this.useToggleEvents = false
}

getCollapseToggle (config) {
const toggle = super.getCollapseToggle(config)
toggle.setAttribute('href', '#' + config.collapseId)
toggle.setAttribute('data-bs-toggle', 'collapse')
return toggle
}

getCollapse (config) {
const collapse = super.getCollapse(config)
collapse.classList.add('collapse')

if (!config.startCollapsed) {
collapse.classList.add('show')
}

return collapse
}

getFieldset () {
const fieldset = document.createElement('fieldset')
fieldset.classList.add('card')
Expand Down
24 changes: 18 additions & 6 deletions src/themes/icons/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const glyphicons = {
delete: 'glyphicon glyphicon-trash',
add: 'glyphicon glyphicon-plus',
moveUp: 'glyphicon glyphicon-arrow-up',
moveDown: 'glyphicon glyphicon-arrow-down'
moveDown: 'glyphicon glyphicon-arrow-down',
expand: 'glyphicon glyphicon-chevron-right',
collapse: 'glyphicon glyphicon-chevron-down'
}

/**
Expand All @@ -24,7 +26,9 @@ export const bootstrapIcons = {
delete: 'bi bi-trash2',
add: 'bi bi-plus',
moveUp: 'bi bi-arrow-up',
moveDown: 'bi bi-arrow-down'
moveDown: 'bi bi-arrow-down',
expand: 'bi bi-chevron-right',
collapse: 'bi bi-chevron-down'
}

/**
Expand All @@ -36,7 +40,9 @@ export const fontAwesome3 = {
delete: 'icon-trash',
add: 'icon-plus',
moveUp: 'icon-arrow-up',
moveDown: 'icon-arrow-down'
moveDown: 'icon-arrow-down',
expand: 'icon-chevron-right',
collapse: 'icon-chevron-down'
}

/**
Expand All @@ -48,7 +54,9 @@ export const fontAwesome4 = {
delete: 'fa fa-trash-o',
add: 'fa fa-plus',
moveUp: 'fa fa-arrow-up',
moveDown: 'fa fa-arrow-down'
moveDown: 'fa fa-arrow-down',
expand: 'fa fa-chevron-right',
collapse: 'fa fa-chevron-down'
}

/**
Expand All @@ -60,7 +68,9 @@ export const fontAwesome5 = {
delete: 'fas fa-trash',
add: 'fas fa-plus',
moveUp: 'fas fa-arrow-up',
moveDown: 'fas fa-arrow-down'
moveDown: 'fas fa-arrow-down',
expand: 'fas fa-chevron-right',
collapse: 'fas fa-chevron-down'
}

/**
Expand All @@ -72,5 +82,7 @@ export const fontAwesome6 = {
delete: 'fa-solid fa-trash',
add: 'fa-solid fa-plus',
moveUp: 'fa-solid fa-arrow-up',
moveDown: 'fa-solid fa-arrow-down'
moveDown: 'fa-solid fa-arrow-down',
expand: 'fa-solid fa-chevron-right',
collapse: 'fa-solid fa-chevron-down'
}
Loading

0 comments on commit 868b510

Please sign in to comment.