Skip to content

Commit c32cd69

Browse files
committed
add View Object
1 parent ffad277 commit c32cd69

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,29 @@ self <= ui.Swiper(ll, default=0)
352352
```
353353

354354

355+
## Object View
356+
357+
A view component which handle the browser navigation mechanism (go back)
358+
359+
**IMPORTANT**:
360+
361+
- this component is here for tests only (it may disappear if real problems)
362+
- FOR POWER USERS : may be used as first class, and you need to understand how htag works, and how the browser history works ! (if the object is deleted -> nothing work)
363+
364+
TODO: make a better good example !
365+
366+
```python
367+
368+
default_view=Tag.div("Default view")
369+
370+
v = ui.View( default_view, _style="border:1px solid red;width:100%;height:400px" )
371+
372+
self <= ui.Button("p1", _onclick = lambda ev: v.go( Tag.div("p1") ))
373+
self <= ui.Button("p2", _onclick = lambda ev: v.go( Tag.div("p2") ))
374+
self <= v
375+
```
376+
377+
355378
## utilities methods
356379

357380
### hflex & vflex

htagui/all.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .splitters import HSplit, VSplit
1717
from .ifields import IText,ITextarea,IRange,IBool,ISelect,IRadios
1818
from .fileupload import FileUpload
19-
from .containers import VScroll,VScrollPager
19+
from .containers import VScroll,VScrollPager, View
2020
from .sortables import Sortable
2121
from .swipers import Swiper
2222
class App(Tag.body):
@@ -33,5 +33,5 @@ def ui(self):
3333

3434
# Swiper not inluded by default !!!!!
3535

36-
ALL=[App,Form,Tabs,Dialog,HSplit,VSplit,IText,ITextarea,IRange,IBool,ISelect,IRadios,FileUpload,Sortable,VScroll,VScrollPager]
36+
ALL=[App,Form,Tabs,Dialog,HSplit,VSplit,IText,ITextarea,IRange,IBool,ISelect,IRadios,FileUpload,Sortable,VScroll,VScrollPager,View]
3737
FULL=ALL+[Swiper] # Swiper not inluded by default !!!!! (coz +170ko)

htagui/containers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,24 @@ def _more(self):
7575
self.first=False
7676

7777

78+
class View(Tag.div):
79+
def __init__(self,tag=None,**a): # use complex constructor to do complex things ;-)
80+
super().__init__(tag,**a)
81+
self.default = tag
82+
self._refs={}
83+
self.js = """
84+
if(!window._hashchange_listener) {
85+
window.addEventListener('hashchange',() => {self._hashchange(document.location.hash);});
86+
window._hashchange_listener=true;
87+
}
88+
"""
89+
@expose
90+
def _hashchange(self,hash):
91+
self.clear( self._refs.get(hash, self.default) )
92+
93+
def go(self,tag,anchor=None):
94+
""" Set object 'tag' in the View, and navigate to it """
95+
anchor=anchor or str(id(tag))
96+
self._refs[f'#{anchor}'] = tag
97+
self.call( f"document.location=`#{anchor}`")
98+

manual_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,22 @@ def cc(self,ev):
280280
self<= ui.Swiper([lambda i=i: Item(i) for i in range(50)])
281281

282282

283+
284+
285+
286+
class TestView(Tag.div):
287+
def init(self,root):
288+
self.output=root.output
289+
290+
default_view=Tag.div("Default view")
291+
292+
v = ui.View( default_view, _style="border:1px solid red;width:100%;height:400px" )
293+
294+
self <= v
295+
self <= ui.Button("p1", _onclick = lambda ev: v.go( Tag.div("p1") ))
296+
self <= ui.Button("p2", _onclick = lambda ev: v.go( Tag.div("p2") ))
297+
298+
283299
class App(ui.App):
284300
statics="""
285301
my {cursor:pointer;padding:4px;margin:4px;display:inline-block;text-decoration:underline}
@@ -316,6 +332,7 @@ def setter(o,testobject):
316332
menu <= Tag.my("Sortable",_onclick=lambda ev: setter(ev.target,TestSortable(self)) )
317333
menu <= Tag.my("VScroll",_onclick=lambda ev: setter(ev.target,TestVscroll(self)) )
318334
menu <= Tag.my("Swiper",_onclick=lambda ev: setter(ev.target,TestSwiper(self)) )
335+
menu <= Tag.my("View",_onclick=lambda ev: setter(ev.target,TestView(self)) )
319336
self <= menu
320337
self <= Tag.hr()
321338
self <= self.omain

0 commit comments

Comments
 (0)