frontend load/save

This commit is contained in:
ale 2019-04-07 20:48:48 +02:00
parent bc4cb79f11
commit 42ffa73bba
3 changed files with 113 additions and 61 deletions

View File

@ -8,31 +8,33 @@ const http = require('http'),
router = express.Router(),
server = http.createServer(app).listen(8080, () => {
client.indices.get({ index: '_all' }).then(indices => {
if (indices.forms) {
client.indices.delete({
index: 'forms'
})
}
client.indices.create({
index: 'forms',
body: {
mappings: {
form: {
properties: {
schema: {
type: 'object'
},
options: {
type: 'object'
},
data: {
type: 'object'
// client.indices.delete({
// index: 'forms'
// })
if (!indices.forms) {
client.indices.create({
index: 'forms',
body: {
mappings: {
form: {
dynamic: true,
numeric_detection: true,
properties: {
schema: {
type: 'object'
},
options: {
type: 'object'
},
data: {
type: 'object'
}
}
}
}
}
}
})
})
}
})
console.log(`Serving on ${server.address().address}:${server.address().port}`)
})
@ -65,7 +67,7 @@ router.get('/load/:id', (req, res, next) => {
}
}).then(form => {
if (form.hits.hits.length > 0) {
res.json(form.hits.hits[0])
res.json(form.hits.hits[0]._source)
} else res.sendStatus(404)
})
.catch(error => {
@ -96,8 +98,8 @@ router.get('/all', (req, res, next) => {
type: 'form'
}).then(forms => {
if (forms.hits.hits.length > 0) {
res.json(forms.hits.hits[0])
} else res.sendStatus(404)
res.json(forms.hits.hits)
} else res.json([])
})
.catch(next)
})

View File

@ -206,7 +206,10 @@
<br/>
<button class="btn btn-default save-button">Save Form</button>
<br/>
<br/>
<br/>
<input id="form" type="text" />
<select class="select-form"></select>
</div>
</div>
</div>

View File

@ -1024,52 +1024,71 @@ var setup = function()
// load button
$(".load-button").off().click(function() {
if (!localStorage)
{
alert("Your browser must support HTML5 local storage in order to use this feature");
return;
}
// if (!localStorage)
// {
// alert("Your browser must support HTML5 local storage in order to use this feature");
// return;
// }
var configString = localStorage.getItem("alpacaDesignerConfig");
if (!configString)
{
return;
}
// var configString = localStorage.getItem("alpacaDesignerConfig");
// if (!configString)
// {
// return;
// }
try
{
var config = JSON.parse(configString);
if (!config.schema) {
config.schema = {};
// try
// {
// var config = JSON.parse(configString);
// if (!config.schema) {
// config.schema = {};
// }
// if (!config.options) {
// config.options = {};
// }
// if (!config.data) {
// config.data = {};
// }
var config = {};
if (schema)
{
config.schema = schema;
}
if (!config.options) {
config.options = {};
if (options)
{
config.options = options;
}
if (!config.data) {
config.data = {};
if (data)
{
config.data = data;
}
editor1.setValue(JSON.stringify(config.schema, null, " "));
editor2.setValue(JSON.stringify(config.options, null, " "));
editor3.setValue(JSON.stringify(config.data, null, " "));
$.ajax({
url: '/api/load/' + $('.select-form option:selected').text(),
success: function(config) {
editor1.setValue(JSON.stringify(config.schema, null, " "));
editor2.setValue(JSON.stringify(config.options, null, " "));
editor3.setValue(JSON.stringify(config.data, null, " "));
alert("Formulario " + $('.select-form option:selected').text() + " cargado correctamente.")
},
error: function() {
alert("Hubo un error al cargar el formulario")
}})
//alert("Your form was loaded from HTML5 local storage");
}
catch (e)
{
// bad value
}
// }
// catch (e)
// {
// // bad value
// }
});
// save button
$(".save-button").off().click(function() {
if (!localStorage)
{
alert("Your browser must support HTML5 local storage in order to use this feature");
return;
}
// if (!localStorage)
// {
// alert("Your browser must support HTML5 local storage in order to use this feature");
// return;
// }
var config = {};
if (schema)
@ -1084,9 +1103,21 @@ var setup = function()
{
config.data = data;
}
var configString = JSON.stringify(config);
//var configString = JSON.stringify(config);
localStorage.setItem("alpacaDesignerConfig", configString);
$.ajax({
url: '/api/save/'+ $('#form').val(),
data : JSON.stringify(config),
contentType : 'application/json',
type : 'POST',
success: function() {
$('.select-form').append($('<option>', {value:$('#form').val(), text: $('#form').val()}))
alert("Formulario " + $('#form').val() + " almacenado.")
},
error: function() {
alert("Hubo un error al almacenar el formulario")
}})
//localStorage.setItem("alpacaDesignerConfig", configString);
//alert("Your form was saved in HTML5 local storage");
});
@ -1097,5 +1128,21 @@ $(document).ready(function() {
// wait a bit to allow ACE to load
setTimeout(function() {
setup();
$.ajax({
url: '/api/all',
contentType : 'application/json',
type : 'GET',
success: function(forms) {
$('.select-form').empty()
for(var i = 0; i < forms.length; i++) {
$('.select-form').append($('<option>', {value:forms[i]._id, text: forms[i]._id}))
}
$('.select-form').change(function(e) {
$('#form').val($('.select-form option:selected').text())
})
alert("Formularios cargados correctamente.")
}, error: function() {
alert("Formularios cargados correctamente.")
}})
}, 200);
});