Juice/static/juice.js

25 lines
665 B
JavaScript
Raw Normal View History

2019-06-06 13:16:59 -04:00
function toggle_outlet(svg) {
let sub_dev = svg.parentElement;
2019-06-06 13:16:59 -04:00
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); }
2019-06-06 13:16:59 -04:00
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');
2019-06-06 13:16:59 -04:00
}
});
2019-06-06 13:16:59 -04:00
}