From 42ffa73bbaa06f744610acafaad86416bd4d4464 Mon Sep 17 00:00:00 2001 From: ale Date: Sun, 7 Apr 2019 20:48:48 +0200 Subject: [PATCH] frontend load/save --- web/index.js | 50 +++++++------- web/public/index.html | 5 +- web/public/js/form-builder.js | 119 ++++++++++++++++++++++++---------- 3 files changed, 113 insertions(+), 61 deletions(-) diff --git a/web/index.js b/web/index.js index b5706d5..82692d9 100644 --- a/web/index.js +++ b/web/index.js @@ -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) }) \ No newline at end of file diff --git a/web/public/index.html b/web/public/index.html index ece256b..2e328f0 100644 --- a/web/public/index.html +++ b/web/public/index.html @@ -206,7 +206,10 @@

- +
+
+ + diff --git a/web/public/js/form-builder.js b/web/public/js/form-builder.js index d67a95e..2a73d98 100644 --- a/web/public/js/form-builder.js +++ b/web/public/js/form-builder.js @@ -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($('