From a95b90002e766a2b596ac3036a9f569225f90a42 Mon Sep 17 00:00:00 2001 From: iou1name Date: Wed, 19 Jun 2019 10:15:08 -0400 Subject: [PATCH] svgs color correctly on page load --- juice.py | 8 +++++++- static/juice.js | 14 ++++++++++++++ templates/index.html | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/juice.py b/juice.py index 24f6709..f175db6 100644 --- a/juice.py +++ b/juice.py @@ -140,7 +140,13 @@ def index(): """ The index page. """ - return render_template('index.html', network=network) + init_state = {} + for device in network: + device_state = {} + for sub_dev in device.sub_devices: + device_state[sub_dev.id] = sub_dev.state + init_state[device.id] = device_state + return render_template('index.html', network=network,init_state=init_state) @app_views.route('/toggle') diff --git a/static/juice.js b/static/juice.js index e713584..edd17cb 100644 --- a/static/juice.js +++ b/static/juice.js @@ -1,3 +1,17 @@ +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]) => { + let sub_dev = device.querySelector('#' + sub_dev_id); + let svg = sub_dev.querySelector('object').getSVGDocument().firstElementChild; + if (state) { + svg.classList.remove('off'); + svg.classList.add('on'); + } + }); + }); +} + function get_object_from_svg(svg) { var all_objects = document.getElementsByTagName("object"); for (var i=0; i < all_objects.length; i++) { diff --git a/templates/index.html b/templates/index.html index 912069b..8e035d7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,6 +4,8 @@ Juice + +
@@ -27,5 +29,25 @@ {% endfor %}
+