"use strict"; const DEBUG = true; const MASONJSON = "application/vnd.mason+json"; const PLAINJSON = "application/json"; function renderError(jqxhr) { let msg = jqxhr.responseJSON["@error"]["@message"]; $("div.notification").html("

" + msg + "

"); } function renderMsg(msg) { $("div.notification").html("

" + msg + "

"); } function getResource(href, renderer) { $.ajax({ url: href, success: renderer, error: renderError }); } function sendData(href, method, item, postProcessor) { $.ajax({ url: href, type: method, data: JSON.stringify(item), contentType: PLAINJSON, processData: false, success: postProcessor, error: renderError }); } function sensorRow(item) { let link = "show"; return "" + item.name + "" + item.model + "" + item.location + "" + link + ""; } function appendSensorRow(body) { $(".resulttable tbody").append(sensorRow(body)); } function getSubmittedSensor(data, status, jqxhr) { renderMsg("Successful"); let href = jqxhr.getResponseHeader("Location"); if (href) { getResource(href, appendSensorRow); } } function followLink(event, a, renderer) { event.preventDefault(); getResource($(a).attr("href"), renderer); } function submitSensor(event) { event.preventDefault(); let data = {}; let form = $("div.form form"); data.name = $("input[name='name']").val(); data.model = $("input[name='model']").val(); sendData(form.attr("action"), form.attr("method"), data, getSubmittedSensor); } function renderSensorForm(ctrl) { let form = $("
"); let name = ctrl.schema.properties.name; let model = ctrl.schema.properties.model; form.attr("action", ctrl.href); form.attr("method", ctrl.method); form.submit(submitSensor); form.append(""); form.append(""); form.append(""); form.append(""); ctrl.schema.required.forEach(function (property) { $("input[name='" + property + "']").attr("required", true); }); form.append(""); $("div.form").html(form); } function renderSensor(body) { $("div.navigation").html( "collection" ); $(".resulttable thead").empty(); $(".resulttable tbody").empty(); renderSensorForm(body["@controls"].edit); $("input[name='name']").val(body.name); $("input[name='model']").val(body.model); $("form input[type='submit']").before( "" + "" ); } function renderSensors(body) { $("div.navigation").empty(); $("div.tablecontrols").empty(); $(".resulttable thead").html( "NameModelLocationActions" ); let tbody = $(".resulttable tbody"); tbody.empty(); body.items.forEach(function (item) { tbody.append(sensorRow(item)); }); renderSensorForm(body["@controls"]["senhub:add-sensor"]); } $(document).ready(function () { getResource("http://localhost:5000/api/sensors/", renderSensors); });