Juice/static/juice.js

257 lines
7.5 KiB
JavaScript
Raw Normal View History

2019-06-19 10:15:08 -04:00
function load() {
Object.entries(init_state).forEach(([device_id, sub_devs]) => {
let device = document.querySelector('#' + device_id);
Object.entries(sub_devs).forEach(([sub_dev_id, state]) => {
2019-06-24 09:06:41 -04:00
let sub_dev = device.querySelector('.' + sub_dev_id);
2019-06-19 10:15:08 -04:00
let svg = sub_dev.querySelector('object').getSVGDocument().firstElementChild;
if (state) {
svg.classList.remove('off');
svg.classList.add('on');
}
});
});
2019-07-02 13:09:31 -04:00
document.querySelectorAll('input[type=color]').forEach((led_input) => {
led_input.onchange = function(event) {
event.target.parentElement.style.backgroundColor = event.target.value;
}
});
2019-06-19 10:15:08 -04:00
}
2019-06-18 12:49:01 -04:00
function get_object_from_svg(svg) {
var all_objects = document.getElementsByTagName("object");
for (var i=0; i < all_objects.length; i++) {
if (svg === all_objects[i].getSVGDocument().firstElementChild) {
return all_objects[i];
}
}
return null;
}
2019-06-06 13:16:59 -04:00
function toggle_outlet(svg) {
2019-06-18 12:49:01 -04:00
let sub_dev = get_object_from_svg(svg).parentElement;
2019-06-06 13:16:59 -04:00
let params = {
device_id: sub_dev.parentElement.parentElement.id,
2019-06-24 09:06:41 -04:00
sub_dev_id: sub_dev.querySelector('.id').innerText,
2019-06-06 13:16:59 -04:00
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'toggle?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
2019-06-13 10:46:59 -04:00
if (!json.ok) { throw new Error('HTTP error, status = ' + json.status + ', message = ' + json.message); }
2019-06-24 09:06:41 -04:00
if (json[sub_dev.querySelector('.id').innerText]) {
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
}
2019-06-13 10:46:59 -04:00
function edit_field(field) {
let value = field.firstElementChild.innerText;
let input = document.createElement('input');
input.value = value;
field.firstElementChild.replaceWith(input);
let save = document.createElement('span');
save.innerHTML = '&#xe801;';
2019-06-20 07:48:11 -04:00
save.className = 'save font-awesome';
2019-06-13 10:46:59 -04:00
save.setAttribute('onclick', 'save_field(this.parentElement)');
field.children[1].replaceWith(save);
}
function save_field(field) {
let value = field.firstElementChild.value;
let device_id = field.parentElement.id;
let sub_dev_id;
if (field.parentElement.className.includes('sub_device')) {
sub_dev_id = device_id;
device_id = field.parentElement.parentElement.parentElement.id;
} else {
sub_dev_id = '';
}
let params = {
device_id: device_id,
sub_dev_id: sub_dev_id,
2019-06-20 11:28:26 -04:00
field: field.classList[0],
2019-06-13 10:46:59 -04:00
value: value
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'edit?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
if (!json.ok) { throw new Error('HTTP error, status = ' + json.status + ', message = ' + json.message); }
let span = document.createElement('span');
span.innerText = json['value'];
span.className = 'field_value';
field.firstElementChild.replaceWith(span);
let edit = document.createElement('span');
edit.innerHTML = '&#xe800;';
2019-06-20 07:48:11 -04:00
edit.className = 'edit font-awesome';
2019-06-13 10:46:59 -04:00
edit.setAttribute('onclick', 'edit_field(this.parentElement)');
field.children[1].replaceWith(edit);
});
}
2019-06-20 07:49:29 -04:00
function new_device() {
let params = {
device_type: 'RelayDevice',
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'new_device?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
2019-06-20 08:38:56 -04:00
let template = document.querySelector('#RelayDevice_template');
let node = document.importNode(template.content, true);
2019-06-20 14:51:00 -04:00
node.querySelector('.id').querySelector('.field_value').textContent = json.device_id;
2019-06-20 08:38:56 -04:00
document.querySelector('#devices').appendChild(node);
let children = document.querySelector('#devices').children;
children[children.length - 1].id = json.device_id;
2019-06-20 07:49:29 -04:00
});
}
2019-06-20 11:19:31 -04:00
function lock_device(device) {
if (device.querySelector('.save')) { return; }
2019-06-20 12:36:19 -04:00
let params = {
device_id: device.id,
locked: true,
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'lock_device?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
if (!json.locked) { return; }
let fields = device.querySelectorAll('.editable');
fields.forEach(function(field) {
field.querySelector('.edit').remove();
});
device.querySelector('.id').querySelector('.lock').remove();
2019-06-20 14:51:00 -04:00
device.querySelector('.id').querySelector('.delete').remove();
2019-06-20 12:36:19 -04:00
let unlock = document.createElement('span');
unlock.innerHTML = '&#xf13e;';
unlock.className = 'unlock font-awesome';
unlock.setAttribute('onclick', 'unlock_device(this.parentElement.parentElement)');
device.querySelector('.id').appendChild(unlock);
2019-06-20 11:19:31 -04:00
});
}
function unlock_device(device) {
2019-06-20 12:36:19 -04:00
let params = {
device_id: device.id,
locked: false,
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'lock_device?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
if (json.locked) { return; }
let fields = device.querySelectorAll('.editable');
fields.forEach(function(field) {
let edit = document.createElement('span');
edit.innerHTML = '&#xe800;';
edit.className = 'edit font-awesome';
edit.setAttribute('onclick', 'edit_field(this.parentElement)');
field.appendChild(edit);
});
device.querySelector('.id').querySelector('.unlock').remove();
2019-06-20 14:51:00 -04:00
let delete_elem = document.createElement('span');
delete_elem.innerHTML = '&#xe804;';
delete_elem.className = 'delete font-awesome';
delete_elem.setAttribute('onclick', 'delete_device(this.parentElement.parentElement)');
device.querySelector('.id').appendChild(delete_elem);
2019-06-20 12:36:19 -04:00
let lock = document.createElement('span');
lock.innerHTML = '&#xe803;';
lock.className = 'lock font-awesome';
lock.setAttribute('onclick', 'lock_device(this.parentElement.parentElement)');
device.querySelector('.id').appendChild(lock);
2019-06-20 11:28:26 -04:00
});
2019-06-20 11:19:31 -04:00
}
2019-06-20 14:51:00 -04:00
function delete_device(device) {
if (!window.confirm("Are you sure you want to delete this device?")) { return; }
let params = {
device_id: device.id,
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'delete?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
if (!json) { return; }
device.remove()
});
}
2019-06-23 14:01:03 -04:00
function delete_key(key_id) {
if (!window.confirm("Are you sure you want to delete this key? This action may leave you unable to access your account.")) { return; }
let params = {
key_id: key_id,
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(url_prefix + '/delete_key?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
if (!json.ok) { return; }
document.querySelector('#key_' + key_id).remove();
});
}
function delete_token(token_id) {
if (!window.confirm("Are you sure you want to delete this token?")) { return; }
let params = {
token_id: token_id,
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(url_prefix + '/delete_token?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
if (!json.ok) { return; }
document.querySelector('#token_' + token_id).remove();
});
}