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

View File

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

View File

@ -1024,52 +1024,71 @@ var setup = function()
// load button // load button
$(".load-button").off().click(function() { $(".load-button").off().click(function() {
if (!localStorage) // if (!localStorage)
{ // {
alert("Your browser must support HTML5 local storage in order to use this feature"); // alert("Your browser must support HTML5 local storage in order to use this feature");
return; // return;
} // }
var configString = localStorage.getItem("alpacaDesignerConfig"); // var configString = localStorage.getItem("alpacaDesignerConfig");
if (!configString) // if (!configString)
{ // {
return; // return;
} // }
try // try
{ // {
var config = JSON.parse(configString); // var config = JSON.parse(configString);
if (!config.schema) { // if (!config.schema) {
config.schema = {}; // config.schema = {};
// }
// if (!config.options) {
// config.options = {};
// }
// if (!config.data) {
// config.data = {};
// }
var config = {};
if (schema)
{
config.schema = schema;
} }
if (!config.options) { if (options)
config.options = {}; {
config.options = options;
} }
if (!config.data) { if (data)
config.data = {}; {
config.data = data;
} }
$.ajax({
editor1.setValue(JSON.stringify(config.schema, null, " ")); url: '/api/load/' + $('.select-form option:selected').text(),
editor2.setValue(JSON.stringify(config.options, null, " ")); success: function(config) {
editor3.setValue(JSON.stringify(config.data, null, " ")); 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"); //alert("Your form was loaded from HTML5 local storage");
} // }
catch (e) // catch (e)
{ // {
// bad value // // bad value
} // }
}); });
// save button // save button
$(".save-button").off().click(function() { $(".save-button").off().click(function() {
if (!localStorage) // if (!localStorage)
{ // {
alert("Your browser must support HTML5 local storage in order to use this feature"); // alert("Your browser must support HTML5 local storage in order to use this feature");
return; // return;
} // }
var config = {}; var config = {};
if (schema) if (schema)
@ -1084,9 +1103,21 @@ var setup = function()
{ {
config.data = data; 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"); //alert("Your form was saved in HTML5 local storage");
}); });
@ -1097,5 +1128,21 @@ $(document).ready(function() {
// wait a bit to allow ACE to load // wait a bit to allow ACE to load
setTimeout(function() { setTimeout(function() {
setup(); 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); }, 200);
}); });