|
|
|
@ -30,3 +30,99 @@ layui.use(['form'], function(){
|
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
|
<script> |
|
|
|
|
/*global $, ace, console*/ |
|
|
|
|
$('document').ready(function () { |
|
|
|
|
var formObject = { |
|
|
|
|
schema: { |
|
|
|
|
greatform: { |
|
|
|
|
title: 'JSON Form object to render', |
|
|
|
|
type: 'string' |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
form: [ |
|
|
|
|
{ |
|
|
|
|
key: 'greatform', |
|
|
|
|
type: 'ace', |
|
|
|
|
aceMode: 'json', |
|
|
|
|
width: '100%', |
|
|
|
|
height: '' + (window.innerHeight - 140) + 'px', |
|
|
|
|
notitle: true, |
|
|
|
|
onChange: function () { |
|
|
|
|
generateForm(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
] |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Displays the form entered by the user |
|
|
|
|
* (this function runs whenever once per second whenever the user |
|
|
|
|
* changes the contents of the ACE input field) |
|
|
|
|
*/ |
|
|
|
|
var generateForm = function () { |
|
|
|
|
var values = $('#form').jsonFormValue(); |
|
|
|
|
|
|
|
|
|
// Reset result pane |
|
|
|
|
$('#result').html(''); |
|
|
|
|
|
|
|
|
|
// Parse entered content as JavaScript |
|
|
|
|
// (mostly JSON but functions are possible) |
|
|
|
|
var createdForm = null; |
|
|
|
|
try { |
|
|
|
|
// Most examples should be written in pure JSON, |
|
|
|
|
// but playground is helpful to check behaviors too! |
|
|
|
|
eval('createdForm=' + values.greatform); |
|
|
|
|
} |
|
|
|
|
catch (e) { |
|
|
|
|
$('#result').html('<pre>Entered content is not yet a valid' + |
|
|
|
|
' JSON Form object.\n\nJavaScript parser returned:\n' + |
|
|
|
|
e + '</pre>'); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Render the resulting form, binding to onSubmitValid |
|
|
|
|
try { |
|
|
|
|
createdForm.onSubmitValid = function (values) { |
|
|
|
|
if (console && console.log) { |
|
|
|
|
console.log('Values extracted from submitted form', values); |
|
|
|
|
} |
|
|
|
|
window.alert('Form submitted. Values object:\n' + |
|
|
|
|
JSON.stringify(values, null, 2)); |
|
|
|
|
}; |
|
|
|
|
createdForm.onSubmit = function (errors, values) { |
|
|
|
|
if (errors) { |
|
|
|
|
console.log('Validation errors', errors); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
}; |
|
|
|
|
$('#result').html('<form id="result-form" class="form-vertical"></form>'); |
|
|
|
|
$('#result-form').jsonForm(createdForm); |
|
|
|
|
} |
|
|
|
|
catch (e) { |
|
|
|
|
$('#result').html('<pre>Entered content is not yet a valid' + |
|
|
|
|
' JSON Form object.\n\nThe JSON Form library returned:\n' + |
|
|
|
|
e + '</pre>'); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Render the form |
|
|
|
|
$('#form').jsonForm(formObject); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait until ACE is loaded |
|
|
|
|
var itv = window.setInterval(function() { |
|
|
|
|
if (window.ace) { |
|
|
|
|
window.clearInterval(itv); |
|
|
|
|
var aceId = $('#form .ace_editor').attr('id'); |
|
|
|
|
var editor = ace.edit(aceId); |
|
|
|
|
editor.getSession().setValue({{.Format}}); |
|
|
|
|
} |
|
|
|
|
}, 2000); |
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
|