Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hidden input support #749

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = function(grunt) {
'src/editor.js',
'src/editors/null.js',
'src/editors/string.js',
'src/editors/hidden.js',
'src/editors/number.js',
'src/editors/integer.js',
'src/editors/object.js',
Expand Down
120 changes: 119 additions & 1 deletion dist/jsoneditor.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/jsoneditor.js.map

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions dist/jsoneditor.min.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/jsoneditor.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ JSONEditor.defaults.resolvers.unshift(function(schema) {
// Use the table editor for arrays with the format set to `table`
JSONEditor.defaults.resolvers.unshift(function(schema) {
// Type `array` with format set to `table`
if(schema.type == "array" && schema.format == "table") {
if(schema.type === "array" && schema.format === "table") {
return "table";
}
});
Expand Down
118 changes: 118 additions & 0 deletions src/editors/hidden.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Created by Mehmet Baker on 12.04.2017
*/
JSONEditor.defaults.editors.hidden = JSONEditor.AbstractEditor.extend({
register: function () {
this._super();
if (!this.input) return;
this.input.setAttribute('name', this.formname);
},
unregister: function () {
this._super();
if (!this.input) return;
this.input.removeAttribute('name');
},
setValue: function (value, initial, from_template) {
var self = this;

if(this.template && !from_template) {
return;
}

if(value === null || typeof value === 'undefined') value = "";
else if(typeof value === "object") value = JSON.stringify(value);
else if(typeof value !== "string") value = ""+value;

if(value === this.serialized) return;

// Sanitize value before setting it
var sanitized = this.sanitize(value);

if(this.input.value === sanitized) {
return;
}

this.input.value = sanitized;

var changed = from_template || this.getValue() !== value;

this.refreshValue();

if(initial) this.is_dirty = false;
else if(this.jsoneditor.options.show_errors === "change") this.is_dirty = true;

if(this.adjust_height) this.adjust_height(this.input);

// Bubble this setValue to parents if the value changed
this.onChange(changed);
},
getNumColumns: function () {
return 2;
},
enable: function () {
this._super();
},
disable: function () {
this._super();
},
refreshValue: function () {
this.value = this.input.value;
if (typeof this.value !== "string") this.value = '';
this.serialized = this.value;
},
destroy: function () {
this.template = null;
if (this.input && this.input.parentNode) this.input.parentNode.removeChild(this.input);
if (this.label && this.label.parentNode) this.label.parentNode.removeChild(this.label);
if (this.description && this.description.parentNode) this.description.parentNode.removeChild(this.description);

this._super();
},
/**
* This is overridden in derivative editors
*/
sanitize: function (value) {
return value;
},
/**
* Re-calculates the value if needed
*/
onWatchedFieldChange: function () {
var self = this, vars, j;

// If this editor needs to be rendered by a macro template
if (this.template) {
vars = this.getWatchedFieldValues();
this.setValue(this.template(vars), false, true);
}

this._super();
},
build: function () {
var self = this;

this.format = this.schema.format;
if (!this.format && this.options.default_format) {
this.format = this.options.default_format;
}
if (this.options.format) {
this.format = this.options.format;
}

this.input_type = 'hidden';
this.input = this.theme.getFormInputField(this.input_type);

if (this.format) this.input.setAttribute('data-schemaformat', this.format);

this.container.appendChild(this.input);

// Compile and store the template
if (this.schema.template) {
this.template = this.jsoneditor.compileTemplate(this.schema.template, this.template_engine);
this.refreshValue();
}
else {
this.refreshValue();
}
}
});