Skip to content

Commit 6c7cd72

Browse files
committed
first form element, button and builder to create button to toggle all details .open property. see changelog
1 parent 8d9f70f commit 6c7cd72

File tree

10 files changed

+97
-13
lines changed

10 files changed

+97
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,6 @@ dmypy.json
145145
# I use rp.py to keep stuff I want to import in the repl
146146
# this keeps me from typing the same stuff when I have to restart the repl
147147
rp.py
148+
149+
# a directory for experimental stuff
150+
sampleCode

docs/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## v0.1.15 (2025-01-13)
4+
5+
- this version is motivated by a desire to toggle visibility of all details elements
6+
- and now, forms... starting with the button
7+
- this is a very basic ability to create a button element
8+
- any attributes (e.g., type) will be done manually or via a builders class
9+
- this is all TBD and might change in subsequent releases
10+
- a new builder to add a toggle button to a page to expand/collapse the content of a details element
11+
- this is also the introduction of JavaScript into domible (sorry, it was inevitable)
12+
- the JS is hard coded into the builder, which might be the long term solution, but I doubt it
13+
- focus is on functionality, no CSS has been added
14+
- added toggle details button into efo.py in tests
15+
- added a toggle details button in open_object_in_browser() in domible tools
16+
- fixed bug in Script element where contents was not being passed to super().__init__
17+
318
## v0.1.14 (2024-12-19) -- BREAKING BACKWARD COMPATIBILITY
419

520
- removed typer from dicli, starts a little faster

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "domible"
3-
version = "0.1.14"
3+
version = "0.1.15"
44
description = "python classes to create accessible HTML elements and documents "
55
authors = ["Joel Dodson <joeldodson@gmail.com>"]
66
maintainers = ["BlindGumption <blindgumption@gmail.com>"]

src/domible/builders/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from domible.builders.tableBuilder import RowBuilder, TableBuilder, build_table_from_dicts
44
from domible.builders.navBuilder import NavBuilder
55
from domible.builders.elementFromObject import element_from_object
6+
from domible.builders.formBuilder import ToggleDetailsButton, default_toggle_details_button
67

78
# end of file
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
""" domible/src/domible/builders/formBuilder.py
2+
"""
3+
4+
from typing import Any
5+
6+
from domible.elements import Button, Div, Script
7+
8+
9+
js_code = """
10+
function toggleDetails(expand) {
11+
const detailsElements = document.querySelectorAll('details');
12+
detailsElements.forEach(details => {
13+
details.open = expand;
14+
});
15+
}
16+
17+
// Function to toggle between expanding and collapsing
18+
function toggleAllDetails() {
19+
const detailsElements = document.querySelectorAll('details');
20+
const areAllOpen = Array.from(detailsElements).every(details => details.open);
21+
22+
// Decide whether to expand or collapse based on current state
23+
toggleDetails(!areAllOpen);
24+
}
25+
"""
26+
27+
28+
class ToggleDetailsButton:
29+
def __init__(self, contents: Any = None, **attributes):
30+
"""
31+
generates a single purpose button to set the details 'opn' property to True/False
32+
attributes passed in are intended for the button element
33+
"""
34+
contents = "Toggle All Details" if not contents else contents
35+
self.toggle_button = Button(
36+
contents, onclick="toggleAllDetails()", ** attributes
37+
)
38+
39+
def get_button(self):
40+
return Div([Script(js_code), self.toggle_button])
41+
42+
def __call__(self):
43+
return self.get_button()
44+
45+
##
46+
# create this default to be easily used where the default label is acceptable
47+
default_toggle_details_button = ToggleDetailsButton()
48+
49+
## end of file

src/domible/elements/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
from domible.elements.scripting import Canvas, NoScript, Script, Template
1515
from domible.elements.sectioning import Article, Body, Footer, Header, Heading, HorizontalRule, Main, Nav, Section
1616
from domible.elements.tables import TableData, TableHeader, TableColumnHeader, TableRowHeader, TableRow, TableHead, TableBody, TableFoot, Caption, Table
17+
from domible.elements.forms import Button
1718

18-
# end of file
19+
## end of file

src/domible/elements/forms.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
""" domible/src/domible/elements/forms.py
2+
Eventually will include all the HTML form elements.
3+
Anythin fancy will probably be done in formsBuilder.
4+
"""
5+
6+
from typing import Any
7+
8+
from domible.elements.baseElements import BaseElement
9+
10+
11+
class Button(BaseElement):
12+
def __init__(self, contents: Any = None, **kwArgs):
13+
super().__init__(tag="button", contents=contents, **kwArgs)
14+
15+
16+
## end of file

src/domible/elements/scripting.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ class Canvas(BaseElement):
1212
All I know of the <canvas> element is it's a problem for accessibility.
1313
As I learn more, I might build out this class to enforce as much accessibility as possible in a <canvas>.
1414
"""
15-
1615
def __init__(self, contents: Any = None, **kwArgs):
17-
super().__init__(tag="canvas", **kwArgs)
16+
super().__init__(tag="canvas", contents=contents, **kwArgs)
1817

1918

2019
class NoScript(BaseElement):
@@ -23,9 +22,8 @@ class NoScript(BaseElement):
2322
I guess this is like a <div> that can contain general HTML
2423
to be rendered if the rowser doesn't support the desired scripting.
2524
"""
26-
2725
def __init__(self, contents: Any = None, **kwArgs):
28-
super().__init__(tag="noscript", **kwArgs)
26+
super().__init__(tag="noscript", contents=contents, **kwArgs)
2927

3028

3129
class Script(BaseElement):
@@ -35,9 +33,8 @@ class Script(BaseElement):
3533
This class exists for the domible user to add scripts to their HTML.
3634
The content should be a string of valid text (probably JS) given the attributes of the element.
3735
"""
38-
3936
def __init__(self, contents: str, **kwArgs):
40-
super().__init__(tag="script", **kwArgs)
37+
super().__init__(tag="script", contents=contents, **kwArgs)
4138

4239

4340
class Template(BaseElement):
@@ -46,9 +43,8 @@ class Template(BaseElement):
4643
I guess this is like a <div> that can contain general HTML
4744
to be rendered later using JavaScript
4845
"""
49-
5046
def __init__(self, contents: Any = None, **kwArgs):
51-
super().__init__(tag="template", **kwArgs)
47+
super().__init__(tag="template", contents=contents, **kwArgs)
5248

5349

5450
## end of file

src/domible/tools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import webbrowser as wb
1414

1515
from domible.elements import BaseElement
16-
from domible.elements import Html, Body
17-
from domible.builders import element_from_object
16+
from domible.elements import Html, Body, Heading
17+
from domible.builders import element_from_object, default_toggle_details_button
1818
from domible.starterDocuments import basic_head_empty_body
1919

2020

@@ -67,6 +67,7 @@ def open_object_in_browser(obj: object, depth:int = 42, title: str = "opening an
6767
obj_html = element_from_object(obj, depth)
6868
html_doc: Html = basic_head_empty_body(title)
6969
body: Body = html_doc.get_body_element()
70+
body.add_content([Heading(1, f"showing object of type {type(obj).__name__}"), default_toggle_details_button()])
7071
body.add_content(obj_html)
7172
open_html_in_browser(html_doc, save_file, force)
7273

tests/efo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from sys import argv
1616

17-
from domible.builders import element_from_object
17+
from domible.builders import element_from_object, ToggleDetailsButton, default_toggle_details_button
1818
from domible import open_html_in_browser, open_object_in_browser
1919
from domible.starterDocuments import basic_head_empty_body
2020
from domible.elements import Html, Body, BaseElement, Heading
@@ -79,6 +79,8 @@
7979
elif arg == 'cases':
8080
html = basic_head_empty_body(f"EFO: running all cases, depth {depth}")
8181
body = html.get_body_element()
82+
body.add_content([Heading(1, "Show All Objects"),ToggleDetailsButton("Custom Togle Details Button Label").get_button()])
83+
body.add_content([Heading(1, "Show All Objects"),default_toggle_details_button()])
8284
for name, obj in cases.items():
8385
elem = element_from_object(obj, depth)
8486
body.add_content([Heading(2, f"Object {name}"), elem])

0 commit comments

Comments
 (0)