Skip to content

Commit

Permalink
Allow adding custom attributes, changed JS method calls to only pass …
Browse files Browse the repository at this point in the history
…event, everything else is in attributes
  • Loading branch information
washad committed Jul 12, 2019
1 parent 76f2ee6 commit 95fa81a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 18 deletions.
69 changes: 55 additions & 14 deletions pyrediseasyio/html/html_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pyrediseasyio.io.boolean_io import BooleanIO
from pyrediseasyio.io.float_io import FloatIO
from dominate.tags import div, tr, td, button
from typing import List, Tuple

_ = gettext.gettext

Expand Down Expand Up @@ -55,15 +56,15 @@ def _build(self, row_tag: dominate.tags, cell_tag: dominate.tags, **kwargs):

if show_set:
with cell_tag(cls='easyio_btn_cell easyio_set_btn_cell'):
self.set_button(button, set_text)
self.set_button(button, set_text, **kwargs)

if show_reset:
with cell_tag(cls='easyio_btn_cell easyio_rst_btn_cell'):
self.reset_button(button, reset_text)
self.reset_button(button, reset_text, **kwargs)

if show_change:
with cell_tag(cls='easyio_btn_cell easyio_change_btn_cell'):
self.change_button(button, change_text)
self.change_button(button, change_text, **kwargs)



Expand All @@ -81,6 +82,9 @@ def as_divs(self, **kwargs) -> dominate.tags:
show_units: bool: optional, set to true so thow the units column.
set_text: str: The text to place on the 'set' button
reset_text: str: The text to place on the 'reset' button)
set_button_attributes: List[Tuple]: Allows additional attributes to be give to the set button
reset_button_attributes: List[Tuple]: Allows additional attributes to be give to the reset button
change_button_attributes: List[Tuple]: Allows additional attributes to be give to the change button
:return: dominate tag object that can be rendered to html.
"""
return self._build(div, div, **kwargs)
Expand All @@ -97,40 +101,74 @@ def as_table_row(self, **kwargs) -> dominate.tags:
show_units: bool: optional, set to true so thow the units column.
set_text: str: The text to place on the 'set' button
reset_text: str: The text to place on the 'reset' button)
set_button_attributes: List[Tuple]: Allows additional attributes to be give to the set button
reset_button_attributes: List[Tuple]: Allows additional attributes to be give to the reset button
change_button_attributes: List[Tuple]: Allows additional attributes to be give to the change button
:return: dominate tag object that can be rendered to html.
"""
return self._build(tr, td, **kwargs)


def change_button(self, tag: dominate.tags = button, txt: str = _("Change")):
def change_button(self, tag: dominate.tags = button, txt: str = _("Change"),
change_button_attributes: List[Tuple] = None, **kwargs) -> dominate.tags:
io = self.io
change_button_attributes = [] if change_button_attributes is None else change_button_attributes
name, addr, val, units, ns = io.name, io.addr, io.value, io.units, self.namespace
ns = "na" if not ns else ns
_max = io.max if hasattr(io, "max") else None
_min = io.min if hasattr(io, "min") else None
allow_decimal = isinstance(io, FloatIO)
allow_negative = _min is None or _min < 0
allow_negative = str(allow_negative).lower()
allow_decimal = str(allow_decimal).lower()
return tag(txt, cls='easyio_change',
onclick=f"EasyIOChange('{io.name}', '{io.key}','{io.units}', '{_min}', "
f"'{_max}', {allow_decimal}, {allow_negative})")

change_button_attributes.append(('data-allow-negative', str(allow_negative).lower()))
change_button_attributes.append(('data-allow-decimal', str(allow_decimal).lower()))
change_button_attributes.append(('data-units', str(io.units)))
change_button_attributes.append(('data-max', _max))
change_button_attributes.append(('data-min', _min))
change_button_attributes.append(('data-value-id', io.key))
change_button_attributes.append(('data-name', name))

def set_button(self, tag: dominate.tags = button, txt: str = "On"):
t = tag(txt, cls='easyio_change', onclick="EasyIOChange(event)")

for a in change_button_attributes:
t.attributes[a[0]] = a[1]
return t


def set_button(self, tag: dominate.tags = button, txt: str = "On", set_button_attributes: List[Tuple] = None,
**kwargs) -> dominate.tags:
io = self.io
name, addr, val, units, ns = io.name, io.addr, io.value, io.units, self.namespace
ns = "na" if not ns else ns
set_button_attributes = [] if set_button_attributes is None else set_button_attributes
value = self.default_set_value
return tag(txt, cls='easyio_set', onclick=f"EasyIOSet('{ns}','{addr}','{self.value_id}','{value}')")
ns = "na" if not ns else ns

set_button_attributes.append(('data-value-id', self.value_id))
set_button_attributes.append(('data-value', str(value)))
set_button_attributes.append(('data-namespace', ns))

def reset_button(self, tag: dominate.tags = button, txt: str = "Off") -> dominate.tags:
t = tag(txt, cls='easyio_set', onclick=f"EasyIOSet(event)")
for a in set_button_attributes:
t.attributes[a[0]] = a[1]
return t


def reset_button(self, tag: dominate.tags = button, txt: str = "Off", reset_button_attributes: List[Tuple] = None,
**kwargs)-> dominate.tags:
io = self.io
name, addr, val, units, ns = io.name, io.addr, io.value, io.units, self.namespace
ns = "na" if not ns else ns
value = self.default_reset_value
return tag(txt, cls='easyio_reset', onclick=f"EasyIOSet('{ns}','{addr}','{self.value_id}','{value}')")
reset_button_attributes = [] if reset_button_attributes is None else reset_button_attributes

reset_button_attributes.append(('data-value-id', self.value_id))
reset_button_attributes.append(('data-value', str(value)))
reset_button_attributes.append(('data-namespace', ns))

t = tag(txt, cls='easyio_reset', onclick=f"EasyIOSet(event)")
for a in reset_button_attributes:
t.attributes[a[0]] = a[1]
return t


def with_custom_tags(self, outer: dominate.tags, inner: dominate.tags, **kwargs):
Expand All @@ -147,6 +185,9 @@ def with_custom_tags(self, outer: dominate.tags, inner: dominate.tags, **kwargs)
show_units: bool: optional, set to true so thow the units column.
set_text: str: The text to place on the 'set' button
reset_text: str: The text to place on the 'reset' button)
set_button_attributes: List[Tuple]: Allows additional attributes to be give to the set button
reset_button_attributes: List[Tuple]: Allows additional attributes to be give to the reset button
change_button_attributes: List[Tuple]: Allows additional attributes to be give to the change button
:return: dominate tag object that can be rendered to html.
"""
return self._build(tr, td, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pyrediseasyio",
version="0.0.28",
version="0.0.31",
author="Steve Jackson",
author_email="washad@gmail.com",
description="A set of tools for simplifying reading and writing of single values to/from Redis.",
Expand Down
6 changes: 4 additions & 2 deletions tests/hmi/static/easyio.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@


function EasyIOSet(ns, addr, id, value){
function EasyIOSet(event){
let target = event.currentTarget;
let id = target.getAttribute('data-value-id');
let value = target.getAttribute('data-value');
let xhttp = new XMLHttpRequest();
let element = document.getElementById(id);
url = `/api/io/${id}/${value}`;
xhttp.open("POST", url, true);
xhttp.send();
Expand Down
10 changes: 9 additions & 1 deletion tests/hmi/static/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ class Keyboard{
let easyio_num_keyboard = null;


function EasyIOChange(name, key, units, min, max, allow_decimal, allow_negative){
function EasyIOChange(event){
let target = event.currentTarget;
let name = target.getAttribute('data-name');
let key = target.getAttribute('data-value-id');
let units = target.getAttribute('data-units');
let min = target.getAttribute('data-min');
let max = target.getAttribute('data-max');
let allow_decimal=target.getAttribute('data-allow-decimal');
let allow_negative = target.getAttribute('data-allow-negative');

let value_element = document.getElementById(key);
let value = Number(value_element.innerText);
Expand Down
35 changes: 35 additions & 0 deletions tests/test_html_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
from pyrediseasyio.io.io_group import IOGroup
from pyrediseasyio.html.html_io import HTMLIO
from pyrediseasyio import BooleanIO, SingleIO, FloatIO, StringIO
from assertpy import assert_that
from htmldiffer import utils as diff_utils
import json


class TestGroup(IOGroup):
Bool1 = BooleanIO("Boolean 1", units="On/Off")
Float1 = FloatIO("Float 1", default=1.23, units="furlongs")


test_group = TestGroup(namespace='Pin1')


def html_equals(actual, expected):
lines1 = [line.strip() for line in diff_utils.html2list(actual)]
lines1 = [line for line in lines1 if len(line) > 0]
lines2 = [line.strip() for line in diff_utils.html2list(expected)]
lines2 = [line for line in lines2 if len(line) > 0]
diffs = [(line[0], line[1]) for line in zip(lines1, lines2) if line[0] != line[1]]
return lines1 == lines2


class TestHTMLIO(unittest.TestCase):

def test_can_add_set_button_attributes(self):
attrs = [
('data-test', 'Test'),
('data-me', 'Me')
]
h = HTMLIO(test_group.Bool1).as_divs(show_set = True, set_button_attributes=attrs)
print(h)

0 comments on commit 95fa81a

Please sign in to comment.