Fix set_radio_group_value and make it re-useable

This commit is contained in:
Kovid Goyal 2019-12-30 10:43:23 +05:30
parent 9b982d3f18
commit 3305fe4a14
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 14 additions and 7 deletions

View File

@ -133,3 +133,12 @@ def add_extra_css(func):
def get_widget_css():
return extra_css.join('\n')
def set_radio_group_value(parent, name, val):
changed = False
for inp in parent.querySelectorAll(f'input[name={name}]'):
inp.checked = inp.value is val
changed = True
if not changed:
raise KeyError(f'No radio group with name={name} found')

View File

@ -6,7 +6,10 @@ from elementmaker import E
from gettext import gettext as _
from book_list.globals import get_session_data
from dom import add_extra_css, build_rule, clear, set_css, svgicon, unique_id
from dom import (
add_extra_css, build_rule, clear, set_css, set_radio_group_value, svgicon,
unique_id
)
from modals import error_dialog
from read_book.globals import default_color_schemes, ui_operations
from read_book.prefs.utils import create_button_box
@ -39,11 +42,6 @@ def get_container():
return document.getElementById(CONTAINER)
def set_radio_group_value(parent, name, val):
for inp in parent.querySelector(f'input[name={name}]'):
inp.checked = inp.value is val
def resolve_color_scheme(current_color_scheme):
sd = get_session_data()
cs = current_color_scheme or sd.get('current_color_scheme') or defaults.current_color_scheme
@ -93,7 +91,7 @@ def edit_color_scheme(ev):
for which in MARGINS:
attr = f'margin_{which}'
val = scheme[attr]
set_radio_group_value(container, f'input[name={attr}_color_type]', 'custom' if val else 'default')
set_radio_group_value(container, f'{attr}_color_type', 'custom' if val else 'default')
if val:
bg, fg = val.split(':')
container.querySelector(f'input[name={attr}_bg]').value = bg