From 6b362950f7a7e9a66ea36be7921ba1bc36a61b3e Mon Sep 17 00:00:00 2001 From: Ross Mountjoy Date: Sat, 30 May 2020 11:47:56 -0400 Subject: [PATCH] dark mode toggle on sidenav works now. it changes the user's theme setting. --- dashmachine/main/modify_config.py | 5 ++-- dashmachine/main/routes.py | 26 +++++++++++++++++++++ dashmachine/static/js/main/config-editor.js | 4 ++-- dashmachine/static/js/main/dashmachine.js | 21 +++++++++++++++++ dashmachine/templates/main/layout.html | 18 +++++++++++++- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/dashmachine/main/modify_config.py b/dashmachine/main/modify_config.py index 3634f7f..1e207a2 100644 --- a/dashmachine/main/modify_config.py +++ b/dashmachine/main/modify_config.py @@ -7,8 +7,9 @@ from dashmachine.main.utils import make_dict_list_string, convert_form_boolean from dashmachine.main.models import DataSources -def modify_config(form): - form = dict(form) +def modify_config(form, convert_form=True): + if convert_form: + form = dict(form) config = ConfigParser(interpolation=None) try: config.read(os.path.join(user_data_folder, "config.ini")) diff --git a/dashmachine/main/routes.py b/dashmachine/main/routes.py index 0e6f70b..287ad19 100755 --- a/dashmachine/main/routes.py +++ b/dashmachine/main/routes.py @@ -411,6 +411,32 @@ def save_ini_form_to_config(): return modify_config(request.form) +@main.route("/toggle_theme", methods=["GET"]) +def toggle_theme(): + user = User.query.filter_by(id=request.args.get("id")).first() + if request.args.get("current_status") == "toggle_off": + theme = "dark" + elif request.args.get("current_status") == "toggle_on": + theme = "light" + + form = row2dict(user) + form["ini_section"] = "Users" + form["ini_id"] = "" + form["prev_name"] = user.username + form["password"] = "" + form["confirm_password"] = "" + form["theme"] = theme + del form["id"] + del_keys = [] + for k, v in form.items(): + if v == "None": + del_keys.append(k) + for k in del_keys: + del form[k] + + return modify_config(form=form) + + # ------------------------------------------------------------------------------ # TCDROP routes # ------------------------------------------------------------------------------ diff --git a/dashmachine/static/js/main/config-editor.js b/dashmachine/static/js/main/config-editor.js index ae3b431..a2a5205 100644 --- a/dashmachine/static/js/main/config-editor.js +++ b/dashmachine/static/js/main/config-editor.js @@ -25,7 +25,7 @@ sleep(500).then(() => { var ctrlDown = false; var saved = false; - $( document ).keydown(function( e ) { + $("#config-editor-container").keydown(function( e ) { if (e.key === 'Control') { ctrlDown = true; } @@ -35,7 +35,7 @@ sleep(500).then(() => { $("#save-config-btn").trigger("click") } }); - $( document ).keyup(function( e ) { + $("#config-editor-container").keyup(function( e ) { if (e.key === 'Control') { ctrlDown = false; } diff --git a/dashmachine/static/js/main/dashmachine.js b/dashmachine/static/js/main/dashmachine.js index c22a617..082e3f0 100644 --- a/dashmachine/static/js/main/dashmachine.js +++ b/dashmachine/static/js/main/dashmachine.js @@ -455,6 +455,27 @@ $(document).ready(function () { $("#card-editor-add-btn").dropdown('recalculateDimensions'); }); + $("#toggle-user-theme-btn").on('click', function(e) { + var icon_btn = $(this).find('.icon-btn'); + $.ajax({ + url: $(this).attr('data-url'), + type: 'GET', + data: {id: $(this).attr("data-user_id"), current_status: icon_btn.text()}, + success: function(data){ + fetch_settings(); + if (icon_btn.text() == "toggle_on"){ + icon_btn.text('toggle_off'); + icon_btn.removeClass('theme-primary-text'); + icon_btn.addClass('theme-secondary-text'); + } else { + icon_btn.text('toggle_on'); + icon_btn.removeClass('theme-secondary-text'); + icon_btn.addClass('theme-primary-text'); + } + } + }); + }); + // ACTION BARS var action_providers = {} diff --git a/dashmachine/templates/main/layout.html b/dashmachine/templates/main/layout.html index 5048692..974ee47 100644 --- a/dashmachine/templates/main/layout.html +++ b/dashmachine/templates/main/layout.html @@ -57,7 +57,23 @@
- toggle_off + {% if current_user %} + + {% if current_user.theme %} + {% if current_user.theme == "light" %} + toggle_off + {% elif current_user.theme == "dark" %} + toggle_on + {% endif %} + {% else %} + {% if settings.theme == "light" %} + toggle_off + {% elif settings.theme == "dark" %} + toggle_on + {% endif %} + {% endif %} + + {% endif %} {% if page != "home" %}