From 2dc370faed8598b216fcf4910430d3e5a42d5fcf Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Fri, 9 Oct 2020 18:16:30 -0600 Subject: [PATCH] Add ability to create new machines in app --- www/assets/js/machine.js | 89 ++++++++++++++++++++++++++++++++++-- www/pages/home.html | 6 +++ www/pages/machine.html | 2 +- www/pages/machineeditor.html | 4 +- www/routes.js | 5 ++ 5 files changed, 98 insertions(+), 8 deletions(-) diff --git a/www/assets/js/machine.js b/www/assets/js/machine.js index 31125c2..0637523 100644 --- a/www/assets/js/machine.js +++ b/www/assets/js/machine.js @@ -97,6 +97,71 @@ function machineEditorOpenAsync(routeTo, routeFrom, resolve, reject) { }); } +function newMachineEditorOpenAsync(routeTo, routeFrom, resolve, reject) { + app.dialog.preloader("Loading editor..."); + + apirequest( + "getmachineformfields", + {}, + function (resp) { + app.dialog.close(); + if (resp.status == "ERROR") { + app.dialog.alert(resp.msg, "Error"); + reject(); + } else { + var context = { + machineid: "", + info: {} + }; + var tabindex = 0; + for (var k in resp.formdata.inputtypes) { + if (resp.formdata.inputtypes.hasOwnProperty(k)) { + context.info[k] = { + name: k, + value: "", + type: (typeof resp.formdata.inputtypes[k] == "string" ? resp.formdata.inputtypes[k] : "text"), + label: (typeof resp.formdata.labels[k] == "string" ? resp.formdata.labels[k] : k), + icon: (typeof resp.formdata.icons[k] == "string" ? resp.formdata.icons[k] : "fas fa-square"), + tabindex: tabindex + }; + if (context.info[k].type == "select") { + console.log(resp.formdata.options[k]); + var options = []; + for (var m in resp.formdata.options[k]) { + if (resp.formdata.options[k].hasOwnProperty(m)) { + options.push({ + value: "val_" + m, + label: resp.formdata.options[k][m] + }); + } + } + console.log(options); + context.info[k].options = options; + } + tabindex++; + } + } + console.log(context); + + resolve({ + templateUrl: "pages/machineeditor.html", + }, { + context: context + }); + } + }, + function (xhr) { + app.dialog.close(); + var error = $.parseJSON(xhr.responseText); + if (error && typeof error.msg != 'undefined') { + app.dialog.alert(error.msg, "Error"); + } else { + app.dialog.alert("A server or network error occurred.", "Error"); + } + reject(); + }); +} + function machineInfoOpenAsync(routeTo, routeFrom, resolve, reject) { app.dialog.preloader("Loading..."); @@ -157,6 +222,9 @@ function machineInfoOpenAsync(routeTo, routeFrom, resolve, reject) { var tabindex = 0; for (var k in resp.info) { if (resp.info.hasOwnProperty(k)) { +// if (resp.info[k] == null) { +// resp.info[k] = ""; +// } switch (k) { case "type": case "clientid": @@ -164,7 +232,7 @@ function machineInfoOpenAsync(routeTo, routeFrom, resolve, reject) { break; default: var value = resp.info[k]; - if (value == false || value == 0) { + if (value == false || value == 0 || value == null) { value = false; } else { value = value.toString().replace("\n", "
"); @@ -203,9 +271,16 @@ function machineInfoOpenAsync(routeTo, routeFrom, resolve, reject) { function saveMachineEdits(machineid) { app.dialog.preloader("Saving..."); - var machinedata = { - id: machineid - }; + if (typeof machineid == "undefined" || machineid == null) { + var apirequesttype = "addmachine"; + var machinedata = {}; + } else { + var apirequesttype = "editmachine"; + var machinedata = { + id: machineid + }; + + } $("#machineform .machinefield").each(function (i, o) { if ($(this).parent().is(".smart-select")) { machinedata[$(this).attr("name")] = app.smartSelect.get('#smartselect-' + $(this).attr("name")).getValue().replace("val_", ""); @@ -215,7 +290,7 @@ function saveMachineEdits(machineid) { }); apirequest( - "editmachine", + apirequesttype, machinedata, function (resp) { app.dialog.close(); @@ -226,7 +301,11 @@ function saveMachineEdits(machineid) { text: 'Machine saved!', closeTimeout: 2000, }).open(); + addMachineToSearchHistory(resp.id); router.back(); + if (apirequesttype === "addmachine") { + router.navigate("/machine/" + resp.id); + } } }, function (xhr) { diff --git a/www/pages/home.html b/www/pages/home.html index 0bce4e7..38e4418 100644 --- a/www/pages/home.html +++ b/www/pages/home.html @@ -23,6 +23,12 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/. +
+ + add + +
+
- + floppy_disk diff --git a/www/routes.js b/www/routes.js index c3e5d7d..82cd026 100644 --- a/www/routes.js +++ b/www/routes.js @@ -25,6 +25,11 @@ var routes = [ name: "machine", async: machineInfoOpenAsync }, + { + path: "/machineeditor", + name: "machineeditor", + async: newMachineEditorOpenAsync + }, { path: "/machineeditor/:id", name: "machineeditor",