diff --git a/juice.py b/juice.py index b84d52d..d1f0183 100644 --- a/juice.py +++ b/juice.py @@ -277,6 +277,25 @@ def lock_device(): return jsonify(device_id=device.id, locked=device.locked) +@app_views.route('/delete') +def delete(): + """ + Deletes a device. + """ + device_id = request.args.get('device_id') + + for device in network: + if device.id == device_id: + break + else: + return make_error(404, "device_id not found") + + network.remove(device) + save_network() + + return jsonify(True) + + def make_error(code, message): """ Returns a JSON error. diff --git a/static/fontawesome-webfont.woff2 b/static/fontawesome-webfont.woff2 index 2d18833..2153264 100644 Binary files a/static/fontawesome-webfont.woff2 and b/static/fontawesome-webfont.woff2 differ diff --git a/static/juice.css b/static/juice.css index 3c446d1..f6104c3 100644 --- a/static/juice.css +++ b/static/juice.css @@ -77,13 +77,13 @@ nav span:hover { src: url("/static/fontawesome-webfont.woff2"); } -.edit, .save, .lock, .unlock { +.edit, .save, .lock, .unlock, .delete { font-size: 0.8em; color: dimgrey; margin-left: 0.4em; } -.edit:hover, .save:hover, .lock:hover, .unlock:hover { +.edit:hover, .save:hover, .lock:hover, .unlock:hover, .delete:hover { color: red; cursor: pointer; } diff --git a/static/juice.js b/static/juice.js index d9241de..2a90d6d 100644 --- a/static/juice.js +++ b/static/juice.js @@ -114,7 +114,7 @@ function new_device() { .then(function(json) { let template = document.querySelector('#RelayDevice_template'); let node = document.importNode(template.content, true); - node.querySelector('.id').textContent = json.device_id; + node.querySelector('.id').querySelector('.field_value').textContent = json.device_id; document.querySelector('#devices').appendChild(node); let children = document.querySelector('#devices').children; children[children.length - 1].id = json.device_id; @@ -143,6 +143,7 @@ function lock_device(device) { field.querySelector('.edit').remove(); }); device.querySelector('.id').querySelector('.lock').remove(); + device.querySelector('.id').querySelector('.delete').remove(); let unlock = document.createElement('span'); unlock.innerHTML = ''; unlock.className = 'unlock font-awesome'; @@ -176,6 +177,13 @@ function unlock_device(device) { field.appendChild(edit); }); device.querySelector('.id').querySelector('.unlock').remove(); + + let delete_elem = document.createElement('span'); + delete_elem.innerHTML = ''; + delete_elem.className = 'delete font-awesome'; + delete_elem.setAttribute('onclick', 'delete_device(this.parentElement.parentElement)'); + device.querySelector('.id').appendChild(delete_elem); + let lock = document.createElement('span'); lock.innerHTML = ''; lock.className = 'lock font-awesome'; @@ -183,3 +191,22 @@ function unlock_device(device) { device.querySelector('.id').appendChild(lock); }); } + +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() + }); +} diff --git a/templates/index.html b/templates/index.html index d2ce55f..6e7ba0d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -18,7 +18,7 @@
{% for device in network %}
-
{{ device.id }}{% if not device.locked %}{% else %}{% endif %}
+
{{ device.id }}{% if not device.locked %}{% else %}{% endif %}
{{ device.description }}{% if not device.locked %}{% endif %}
{{ device.location }}{% if not device.locked %}{% endif %}
{{ device.ip_address }}{% if not device.locked %}{% endif %}
@@ -37,20 +37,20 @@