"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 = $("