188 lines
5.7 KiB
Python
188 lines
5.7 KiB
Python
from abc import ABC, abstractmethod
|
|
from owrx.service import ServiceDetector
|
|
|
|
|
|
class Input(ABC):
|
|
def __init__(self, id, label, infotext=None):
|
|
self.id = id
|
|
self.label = label
|
|
self.infotext = infotext
|
|
|
|
def bootstrap_decorate(self, input):
|
|
infotext = (
|
|
"<small>{text}</small>".format(text=self.infotext) if self.infotext else ""
|
|
)
|
|
return """
|
|
<div class="form-group row">
|
|
<label class="col-form-label col-form-label-sm col-3" for="{id}">{label}</label>
|
|
<div class="col-9 p-0">
|
|
{input}
|
|
{infotext}
|
|
</div>
|
|
</div>
|
|
""".format(
|
|
id=self.id, label=self.label, input=input, infotext=infotext
|
|
)
|
|
|
|
def input_classes(self):
|
|
return " ".join(["form-control", "form-control-sm"])
|
|
|
|
@abstractmethod
|
|
def render_input(self, value):
|
|
pass
|
|
|
|
def render(self, config):
|
|
return self.bootstrap_decorate(self.render_input(config[self.id]))
|
|
|
|
def parse(self, data):
|
|
return {self.id: data[self.id][0]} if self.id in data else {}
|
|
|
|
|
|
class TextInput(Input):
|
|
def render_input(self, value):
|
|
return """
|
|
<input type="text" class="{classes}" id="{id}" name="{id}" placeholder="{label}" value="{value}">
|
|
""".format(
|
|
id=self.id, label=self.label, classes=self.input_classes(), value=value
|
|
)
|
|
|
|
|
|
class NumberInput(Input):
|
|
def render_input(self, value):
|
|
return """
|
|
<input type="number" class="{classes}" id="{id}" name="{id}" placeholder="{label}" value="{value}">
|
|
""".format(
|
|
id=self.id, label=self.label, classes=self.input_classes(), value=value
|
|
)
|
|
|
|
def convert_value(self, v):
|
|
return int(v)
|
|
|
|
def parse(self, data):
|
|
return {k: self.convert_value(v) for k, v in super().parse(data).items()}
|
|
|
|
|
|
class FloatInput(NumberInput):
|
|
def convert_value(self, v):
|
|
return float(v)
|
|
|
|
|
|
class LocationInput(Input):
|
|
def render_input(self, value):
|
|
# TODO make this work and pretty
|
|
return "Placeholder for a map widget to select receiver location"
|
|
|
|
|
|
class TextAreaInput(Input):
|
|
def render_input(self, value):
|
|
return """
|
|
<textarea class="{classes}" id="{id}" name="{id}" style="height:200px;">{value}</textarea>
|
|
""".format(
|
|
id=self.id, classes=self.input_classes(), value=value
|
|
)
|
|
|
|
|
|
class CheckboxInput(Input):
|
|
def __init__(self, id, label, checkboxText, infotext=None):
|
|
super().__init__(id, label, infotext=infotext)
|
|
self.checkboxText = checkboxText
|
|
|
|
def render_input(self, value):
|
|
return """
|
|
<div class="{classes}">
|
|
<input class="form-check-input" type="checkbox" id="{id}" name="{id}" {checked}>
|
|
<label class="form-check-label" for="{id}">
|
|
{checkboxText}
|
|
</label>
|
|
</div>
|
|
""".format(
|
|
id=self.id,
|
|
classes=self.input_classes(),
|
|
checked="checked" if value else "",
|
|
checkboxText=self.checkboxText,
|
|
)
|
|
|
|
def input_classes(self):
|
|
return " ".join(["form-check", "form-control-sm"])
|
|
|
|
def parse(self, data):
|
|
return {self.id: self.id in data and data[self.id][0] == "on"}
|
|
|
|
|
|
class Option(object):
|
|
# used for both MultiCheckboxInput and DropdownInput
|
|
def __init__(self, value, text):
|
|
self.value = value
|
|
self.text = text
|
|
|
|
|
|
class MultiCheckboxInput(Input):
|
|
def __init__(self, id, label, options, infotext=None):
|
|
super().__init__(id, label, infotext=infotext)
|
|
self.options = options
|
|
|
|
def render_input(self, value):
|
|
return "".join(self.render_checkbox(o, value) for o in self.options)
|
|
|
|
def checkbox_id(self, option):
|
|
return "{0}-{1}".format(self.id, option.value)
|
|
|
|
def render_checkbox(self, option, value):
|
|
return """
|
|
<div class="{classes}">
|
|
<input class="form-check-input" type="checkbox" id="{id}" name="{id}" {checked}>
|
|
<label class="form-check-label" for="{id}">
|
|
{checkboxText}
|
|
</label>
|
|
</div>
|
|
""".format(
|
|
id=self.checkbox_id(option),
|
|
classes=self.input_classes(),
|
|
checked="checked" if option.value in value else "",
|
|
checkboxText=option.text,
|
|
)
|
|
|
|
def parse(self, data):
|
|
def in_response(option):
|
|
boxid = self.checkbox_id(option)
|
|
return boxid in data and data[boxid][0] == "on"
|
|
|
|
return {self.id: [o.value for o in self.options if in_response(o)]}
|
|
|
|
def input_classes(self):
|
|
return " ".join(["form-check", "form-control-sm"])
|
|
|
|
|
|
class ServicesCheckboxInput(MultiCheckboxInput):
|
|
def __init__(self, id, label, infotext=None):
|
|
services = [
|
|
Option(s, s.upper()) for s in ServiceDetector.getAvailableServices()
|
|
]
|
|
super().__init__(id, label, services, infotext)
|
|
|
|
|
|
class DropdownInput(Input):
|
|
def __init__(self, id, label, options, infotext=None):
|
|
super().__init__(id, label, infotext=infotext)
|
|
self.options = options
|
|
|
|
def render_input(self, value):
|
|
return """
|
|
<select class="{classes}" id="{id}" name="{id}">{options}</select>
|
|
""".format(
|
|
classes=self.input_classes(), id=self.id, options=self.render_options(value)
|
|
)
|
|
|
|
def render_options(self, value):
|
|
options = [
|
|
"""
|
|
<option value="{value}" {selected}>{text}</option>
|
|
""".format(
|
|
text=o.text,
|
|
value=o.value,
|
|
selected="selected" if o.value == value else "",
|
|
)
|
|
for o in self.options
|
|
]
|
|
return "".join(options)
|