Juice/static/juice.js

25 lines
665 B
JavaScript

function toggle_outlet(svg) {
let sub_dev = svg.parentElement;
let params = {
device_id: sub_dev.parentElement.parentElement.id,
sub_dev_id: sub_dev.id
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'toggle?' + query)
.then(function(response) {
if (!response.ok) { throw new Error('HTTP error, status = ' + response.status); }
return response.json();
})
.then(function(json) {
if (json[sub_dev.id]) {
svg.classList.remove('off');
svg.classList.add('on');
} else {
svg.classList.remove('on');
svg.classList.add('off');
}
});
}