Skip to content

Commit

Permalink
Merge pull request #127 from mhsmith/children-rename
Browse files Browse the repository at this point in the history
Rename App.content to "pages" and Component.content to "children"
  • Loading branch information
ntoll authored Oct 25, 2024
2 parents 6316a4b + 29bbaa0 commit 792c908
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 106 deletions.
6 changes: 3 additions & 3 deletions examples/calculator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ def calculate(data) -> None:

invent.ui.App(
name="Calculator",
content=[
pages=[
invent.ui.Page(
content=[
children=[
invent.ui.Grid(
columns=4,
content=[
children=[
invent.ui.TextInput(
layout=dict(column_span=4),
value=invent.ui.from_datastore("numbers"),
Expand Down
6 changes: 3 additions & 3 deletions examples/catfacts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def ready(value):

app = App(
name="CatFacts!",
content=[
pages=[
Page(
name="Facts",
content=[
children=[
Column(
content=[
children=[
Image(
image=invent.media.images.puff.svg,
visible=from_datastore("working"),
Expand Down
26 changes: 13 additions & 13 deletions examples/farmyard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ def make_pigs(number_of_oinks):

app = App(
name="Farmyard!",
content=[
pages=[
Page(
name="Lucy",
content=[
children=[
Column(
content=[
children=[
Image(
image=invent.media.images.goose.png,
channel="honk",
layout=dict(align_self="center"),
),
Row(
layout=dict(align_self="center"),
content=[
children=[
Button(
name="button honk",
label="HONK!",
Expand All @@ -87,7 +87,7 @@ def make_pigs(number_of_oinks):
],
),
Row(
content=[
children=[
TextBox(
name="number_of_honks",
text=from_datastore("number_of_honks"),
Expand All @@ -104,7 +104,7 @@ def make_pigs(number_of_oinks):
Row(
id="geese",
justify_content="center",
content=from_datastore(
children=from_datastore(
"number_of_honks", with_function=make_geese
),
),
Expand All @@ -119,17 +119,17 @@ def make_pigs(number_of_oinks):
),
Page(
name="Percy",
content=[
children=[
Column(
content=[
children=[
Image(
image=invent.media.images.pig.png,
channel="oink",
layout=dict(align_self="center"),
),
Row(
layout=dict(align_self="center"),
content=[
children=[
Button(
name="button oink",
label="OINK!!",
Expand All @@ -150,7 +150,7 @@ def make_pigs(number_of_oinks):
Row(
id="pigs",
justify_content="center",
content=from_datastore(
children=from_datastore(
"number_of_oinks", with_function=make_pigs
),
),
Expand All @@ -170,12 +170,12 @@ def make_pigs(number_of_oinks):
# Add a page that shows the code! ######################################################


app.content.append(
app.pages.append(
Page(
name="Code",
content=[
children=[
Row(
content=[
children=[
Button(
name="to_lucy",
label="Visit Lucy",
Expand Down
20 changes: 10 additions & 10 deletions src/invent/ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class App:
"""
An instance of App is the root object for an Invent application. General
app related metadata hangs off an object of this type. E.g. name, author,
icon, description, license and other such things. In addition, the content
of Pages defines the UI tree.
icon, description, license and other such things. In addition, the list
of Pages defines the UI of the app.
"""

def __init__(
Expand All @@ -49,7 +49,7 @@ def __init__(
description=None,
author=None,
license=None,
content=None,
pages=None,
):
global __app__
if not __app__:
Expand All @@ -60,7 +60,7 @@ def __init__(
self.description = description
self.author = author
self.license = license
self.content = content or []
self.pages = pages or []
self._current_page = None

invent.set_media_root(media_root)
Expand All @@ -76,7 +76,7 @@ def as_dict(self):
description=self.description,
author=self.author,
license=self.license,
content=[item.as_dict() for item in self.content],
pages=[item.as_dict() for item in self.pages],
)

def as_json(self):
Expand All @@ -102,7 +102,7 @@ def get_page_by_id(self, page_id):
Return the page with the specified id or None if no such page exists.
"""

for page in self.content:
for page in self.pages:
if page.id == page_id:
break

Expand All @@ -116,7 +116,7 @@ def get_page_by_name(self, page_name):
Return the page with the specified name or None if no such page exists.
"""

for page in self.content:
for page in self.pages:
if page.name == page_name:
break

Expand All @@ -141,10 +141,10 @@ def go(self):
# Load the i18n stuff.
load_translations()
# Render all the pages to the DOM.
if self.content:
for page in self.content:
if self.pages:
for page in self.pages:
document.body.appendChild(page.element)
# Show the first page.
self.show_page(self.content[0].name)
self.show_page(self.pages[0].name)
else:
raise ValueError(_("No pages in the app!"))
30 changes: 15 additions & 15 deletions src/invent/ui/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ def as_dict(self):

# If the component is a Container, we format its content recursively.
if isinstance(self, Container):
if not self.get_from_datastore("content"):
properties["content"] = [
item.as_dict() for item in self.content
if not self.get_from_datastore("children"):
properties["children"] = [
item.as_dict() for item in self.children
]

return {
Expand Down Expand Up @@ -452,7 +452,7 @@ class Container(Component):
insert the children into the container in the correct manner.
"""

content = ListProperty(
children = ListProperty(
"The contents of the container",
default_value=None,
)
Expand Down Expand Up @@ -480,12 +480,12 @@ class Container(Component):

def __init__(self, **kwargs):
super().__init__(**kwargs)
for item in self.content:
for item in self.children:
item.parent = self

def on_content_changed(self):
def on_children_changed(self):
self.element.innerHTML = ""
for child in self.content:
for child in self.children:
self.element.appendChild(child.element)
self.update_children()

Expand Down Expand Up @@ -546,7 +546,7 @@ def append(self, item):
"""
# Update the object model.
item.parent = self
self.content.append(item)
self.children.append(item)

# Update the DOM.
self.element.appendChild(item.element)
Expand All @@ -560,10 +560,10 @@ def insert(self, index, item):
"""
# Update the object model.
item.parent = self
self.content.insert(index, item)
self.children.insert(index, item)

# Update the DOM.
if item is self.content[-1]:
if item is self.children[-1]:
self.element.appendChild(item.element)
else:
self.element.insertBefore(
Expand All @@ -579,7 +579,7 @@ def remove(self, item):
"""
# Update the object model.
item.parent = None
self.content.remove(item)
self.children.remove(item)

# Update the DOM.
item.element.remove()
Expand All @@ -591,13 +591,13 @@ def __getitem__(self, index):
"""
Index items like a list.
"""
return self.content[index]
return self.children[index]

def __iter__(self):
"""
Iterate like a list.
"""
return iter(self.content)
return iter(self.children)

def __delitem__(self, item):
"""
Expand All @@ -610,7 +610,7 @@ def contains(self, component):
This is recursive, so this really means "is a descendant of".
"""
for item in self.content:
for item in self.children:
if item is component:
return True

Expand All @@ -636,7 +636,7 @@ def update_children(self):
"""
Update the container's children.
"""
for counter, child in enumerate(self.content, start=1):
for counter, child in enumerate(self.children, start=1):
self.update_child(child, counter)

def update_child(self, child, index):
Expand Down
Loading

0 comments on commit 792c908

Please sign in to comment.