Fixes to JsonForm and json
* Update model when JsonForm.editable set false * Bugfixes in jsonPatch
This commit is contained in:
55
jsonform.js
55
jsonform.js
@@ -1,6 +1,6 @@
|
||||
|
||||
import { dom, el } from './dom.js';
|
||||
import { jsonQuery } from './json.js';
|
||||
import { jsonQuery, jsonPatch } from './json.js';
|
||||
|
||||
/**
|
||||
* This form encapsulates a form with a model and a layout.
|
||||
@@ -50,6 +50,7 @@ class JsonForm {
|
||||
|
||||
refreshModel() {
|
||||
// TODO go over the fields and update the model with any modified data
|
||||
this.#model = this.#layout.updateModel(this.#model);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -77,6 +78,11 @@ class FormLayout {
|
||||
this.#rows.forEach(row => row.modelUpdated(model));
|
||||
}
|
||||
|
||||
updateModel(model) {
|
||||
this.#rows.forEach(row => model = row.updateModel(model));
|
||||
return model;
|
||||
}
|
||||
|
||||
setEditable(state) {
|
||||
this.#rows.forEach(row => row.setEditable(state));
|
||||
}
|
||||
@@ -110,6 +116,14 @@ class FormRow {
|
||||
});
|
||||
}
|
||||
|
||||
updateModel(model) {
|
||||
this.#fields.forEach(field => {
|
||||
if (!field.options.path) return;
|
||||
model = jsonPatch(model, field.options.path, field.value);
|
||||
});
|
||||
return model;
|
||||
}
|
||||
|
||||
setEditable(state) {
|
||||
this.#fields.forEach(field => {
|
||||
if (field.options.locked === true) return;
|
||||
@@ -163,6 +177,15 @@ class FormField {
|
||||
return this.#el;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the value without triggering any effects
|
||||
*
|
||||
* @param {*} value The new value
|
||||
*/
|
||||
updateValue(value) {
|
||||
this.#value = value;
|
||||
}
|
||||
|
||||
buildField() {
|
||||
}
|
||||
|
||||
@@ -192,6 +215,35 @@ class TextField extends FormField {
|
||||
if (state) {
|
||||
dom.apply(this.#input, { 'contenteditable':'plaintext-only' });
|
||||
} else {
|
||||
this.updateValue(this.#input.innerText);
|
||||
dom.apply(this.#input, { 'contenteditable':null });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class NumericField extends FormField {
|
||||
#input = null;
|
||||
buildField() {
|
||||
if (!this.#input) {
|
||||
this.#input = el.div({ class: '-field -number-field' })
|
||||
this.el.appendChild(this.#input);
|
||||
}
|
||||
this.#input.innerText = this.value;
|
||||
}
|
||||
valueUpdated() {
|
||||
this.#input.innerText = this.value;
|
||||
}
|
||||
getValue() {
|
||||
if (this.options.float) {
|
||||
return parseFloat(this.#input.innerText);
|
||||
}
|
||||
return parseInt(this.#input.innerText);
|
||||
}
|
||||
setEditable(state) {
|
||||
if (state) {
|
||||
dom.apply(this.#input, { 'contenteditable':'plaintext-only' });
|
||||
} else {
|
||||
this.updateValue(this.#input.innerText);
|
||||
dom.apply(this.#input, { 'contenteditable':null });
|
||||
}
|
||||
}
|
||||
@@ -222,6 +274,7 @@ export {
|
||||
FormRow,
|
||||
FormField,
|
||||
TextField,
|
||||
NumericField,
|
||||
DateField,
|
||||
SelectField,
|
||||
CheckField,
|
||||
|
||||
Reference in New Issue
Block a user