Skip to content

Commit

Permalink
popmenu z-index and right click
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Jan 28, 2023
1 parent a6e017b commit 8d21365
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 50 deletions.
2 changes: 1 addition & 1 deletion htbulma/fileselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _selectFile(self,o):
path = os.path.realpath(os.path.join(self.path,o.path))
assert path.startswith(self._root)
self._selected = o.path
self(f"""FileSelect_select('{id(o)}')""")
self.call(f"""FileSelect_select('{id(o)}')""")
self.onselect(path)

if __name__=="__main__":
Expand Down
2 changes: 1 addition & 1 deletion htbulma/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Form(Tag.form):
def __init__(self,onsubmit=None,**a):
Tag.__init__(self,**a)
# rewrite the form.submit() (bicoz this method doesn't call the onsubmit ;-( )
self.js = """tag.submit=function() {%s}""" % self.bind._onsubmit(b"JSON.stringify(Object.fromEntries(new FormData(this)))")
self.js = """self.submit=function() {%s}""" % self.bind._onsubmit(b"JSON.stringify(Object.fromEntries(new FormData(this)))")

self["onsubmit"]="event.preventDefault();this.submit()"
self._callback = onsubmit
Expand Down
2 changes: 1 addition & 1 deletion htbulma/inputs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _redraw(self):

def _set(self,value):
self._value = self.fix_value_if_options(value)
self( self["onchange"] )
self.call( self["onchange"] )
self._redraw()

class TabsHeader(SelectButtons):
Expand Down
4 changes: 2 additions & 2 deletions htbulma/nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def update(self): # DYNAMIC RENDERING HERE !
self <= divMenu

def evtSelectEntry(self, name):
self("document.querySelector('.navbar-menu').classList.remove('is-active')")
self("document.querySelector('.navbar-burger').classList.remove('is-active')")
self.call("document.querySelector('.navbar-menu').classList.remove('is-active')")
self.call("document.querySelector('.navbar-burger').classList.remove('is-active')")
entries = {**self._entries_first,**self._entries_end}
callback = entries[name]
callback()
Expand Down
2 changes: 1 addition & 1 deletion htbulma/navside.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ def init(self,title,sidecontent,width:str="200px",width_small:str="80%",class_co

def hide(self):
""" hide menu if on mobile """
self("""hideMenu()""")
self.call("""hideMenu()""")

14 changes: 7 additions & 7 deletions htbulma/rte.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ def init(self,value:"str or delta", onsave:"cb(self)"=None, opts:list=None, edit
]

self.js="""
tag.ed = new Quill(tag, {
self.ed = new Quill(self, {
modules: {
toolbar: %s,
},
readOnly: %s,
theme: %s,
});
tag.getValue = function(asDelta) {
self.getValue = function(asDelta) {
if(asDelta)
return tag.ed.getContents();
return self.ed.getContents();
else
return tag.ed.root.innerHTML;
return self.ed.root.innerHTML;
}
""" % (
Expand All @@ -91,13 +91,13 @@ def setValue(self,value:"str or delta",init:bool=False):
if init:
self.set( value )
else:
self( f"tag.ed.setContents(tag.ed.clipboard.convert(`{value}`) ,'silent');" )
self.call( f"self.ed.setContents(self.ed.clipboard.convert(`{value}`) ,'silent');" )
else:
cmd=f"tag.ed.setContents( {json.dumps(value)}, 'silent')"
cmd=f"self.ed.setContents( {json.dumps(value)}, 'silent')"
if init:
self.js += cmd
else:
self( cmd )
self.call( cmd )

def eventSave(self,asJson=False) -> "js call":
tag=f"document.getElementById('{id(self)}')".encode()
Expand Down
31 changes: 16 additions & 15 deletions htbulma/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ def init(self,entries,x,y):
self += Tag.div(
_class="modal-background",
_onclick=self.close,
_style="background-color:inherit;z-index:9"
_oncontextmenu = self.bind( self.close ) + "return false",
_style="background-color:inherit;z-index:1000000"
)

js="""(function(tag,x,y) {
tag.style="position:fixed;z-index:10;padding:2px;left:"+x+"px;top:"+y+"px";
tag.style="position:fixed;z-index:1000001;padding:2px;left:"+x+"px;top:"+y+"px";
let bw=document.body.clientWidth;
let bh=document.body.clientHeight;
let w=tag.clientWidth;
let h=tag.clientHeight;
if(x+w > bw) x=bw-w;
if(y+h > bh) y=bh-h;
tag.style="position:fixed;z-index:10;padding:2px;left:"+x+"px;top:"+y+"px";
})(tag,%s,%s)"""
tag.style="position:fixed;z-index:1000001;padding:2px;left:"+x+"px;top:"+y+"px";
})(self,%s,%s)"""

self += Tag.div(
Tag.aside( omenu,_class="menu"),
_class="card",
Expand All @@ -61,7 +62,7 @@ def init(self,entries,x,y):

def close(self,o=None):
self.remove()

class Modal(Tag.div):
def init(self, content, canClose=True, full=False):
self["class"] = "modal is-active"
Expand All @@ -81,7 +82,7 @@ def init(self, content, canClose=True, full=False):
Tag.div(
content,
_tabindex=0,
js="tag.focus()",
js="self.focus()",
_style="outline: none" + ("height:100%;overflow-y:auto" if full else ""),
_class="box",
),
Expand Down Expand Up @@ -128,13 +129,13 @@ def clipboard(self,txt):
self._reroot()

assert "`" not in txt # ;-)
self("""
self.call("""
let ta = document.createElement('textarea');
ta.value = `%s`;
tag.appendChild(ta);
self.appendChild(ta);
ta.select();
document.execCommand('copy');
tag.removeChild(ta);
self.removeChild(ta);
""" % txt)


Expand All @@ -157,10 +158,10 @@ def toast(self,content,delay=2000):

x=Toast(content)
self += x
# prefer the self() way, to send the js for this case
# prefer the self.call() way, to send the js for this case
# because with x.js, js is re-executed at each redraw
# and can cause dead objects (when event reach server)
self("""setTimeout(function(){%s},%s);""" % (x.bind.close(None),delay))
self.call("""setTimeout(function(){%s},%s);""" % (x.bind.close(None),delay))
return x

def alert(self, content, canClose=True, full=False):
Expand Down Expand Up @@ -202,7 +203,7 @@ def prompt(self, title, defaultValue, ok, ko=None,txtok="OK",txtko="Cancel"): #
""" "same" signature as js window.prompt() """
self._reroot()

input = Tag.input(_value=defaultValue, js="tag.focus();tag.setSelectionRange(0, tag.value.length)", _class="input")
input = Tag.input(_value=defaultValue, js="self.focus();self.setSelectionRange(0, self.value.length)", _class="input")

bko=Button(txtko, _class="is-light", _style="flex: 1 0 25%;")
bok=Button(txtok, _style="flex: 1 0 25%;")
Expand Down
6 changes: 3 additions & 3 deletions htbulma/services/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def __init__(self,parent):

def copy(self,txt):
assert "`" not in txt # ;-)
self("""
self.call("""
let ta = document.createElement('textarea');
ta.value = `%s`;
tag.appendChild(ta);
self.appendChild(ta);
ta.select();
document.execCommand('copy');
tag.removeChild(ta);
self.removeChild(ta);
""" % txt)

if __name__=="__main__":
Expand Down
4 changes: 2 additions & 2 deletions htbulma/services/mbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def confirm(self, content, ok, ko=None,txtok="OK",txtko="Cancel"):
Tag.div(_style="flex: 1 0 25%;"),
Tag.div(_style="flex: 1 0 25%;"),
Button(txtko, _onclick=self.bind._confirm(0), _class="is-light",_style="flex: 1 0 25%;"),
Button(txtok, _onclick=self.bind._confirm(1), js="tag.focus()",_onkeyup = js , _style="flex: 1 0 25%;"),
Button(txtok, _onclick=self.bind._confirm(1), js="self.focus()",_onkeyup = js , _style="flex: 1 0 25%;"),
)
self.show(main, canClose=True)

Expand All @@ -70,7 +70,7 @@ def prompt(self, title, defaultValue, ok, ko=None,txtok="OK",txtko="Cancel"):

js = """if (event.keyCode === 13) {event.preventDefault();%s;}""" % self.bind._prompt(b"this.value")
js += """if (event.keyCode === 27) {event.preventDefault();%s;}""" % self.bind._prompt()
input = Tag.input(_value=defaultValue, js="tag.focus();tag.setSelectionRange(0, tag.value.length)", _class="input", _onkeyup = js)
input = Tag.input(_value=defaultValue, js="self.focus();self.setSelectionRange(0, self.value.length)", _class="input", _onkeyup = js)

main = Content( Tag.h3(title) )
main <= input
Expand Down
2 changes: 1 addition & 1 deletion htbulma/services/toaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def show(self,content,delay=2000): #TODO: can't be called immediatly ;-( (coz __
o.add( Tag.button(_class="delete", _onclick=jsclose) )
o.add( content )

self("""setTimeout(function() {%s;},%s);""" % (jsclose,delay))
self.call("""setTimeout(function() {%s;},%s);""" % (jsclose,delay))

self <= o

Expand Down
33 changes: 18 additions & 15 deletions manual_tests_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@


class App(Tag.body):
statics="html,body {width:100%;height:100%;border:1px solid red}"

def init(self):
self._s = b.Service(self)
self.redraw()

def redraw(self,o=None):

def pmenu(o):
entries=[
Tag.a("menu1", v="1",_onclick=self.entry),
Tag.A("menu2", v="2",_onclick=self.entry),
Tag.A("alert", _onclick=self.alert),
Tag.A("confirm()", _onclick=self.confirm),
Tag.A("prompt()", _onclick=self.prompt),
Tag.A("toast()", _onclick=self.toast),
Tag.A("clipboard()", _onclick=self.cc),
Tag.hr(_style="padding:0px;margin:0px"),
"nimp:",
Tag.button("menu3", v="3",_onclick=pmenu),
]
self._s.popmenu(entries,o)

self.clear()
self += Tag.button( "Popmenu", _onclick=self.pmenu)
self += Tag.button( "Popmenu", _onclick=pmenu)
self += Tag.button( "Toast", _onclick=self.toast)
self += Tag.button( "clear/redraw", _onclick=self.redraw)

def pmenu(self,o):
entries=[
Tag.a("menu1", v="1",_onclick=self.entry),
Tag.A("menu2", v="2",_onclick=self.entry),
Tag.A("alert", _onclick=self.alert),
Tag.A("confirm()", _onclick=self.confirm),
Tag.A("prompt()", _onclick=self.prompt),
Tag.A("toast()", _onclick=self.toast),
Tag.A("clipboard()", _onclick=self.cc),
Tag.hr(_style="padding:0px;margin:0px"),
"nimp:",
Tag.button("menu3", v="3",_onclick=self.pmenu),
]
self._s.popmenu(entries,o)

def cc(self,o):
import time
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
[tool.poetry.dependencies]
# https://python-poetry.org/docs/dependency-specification/
python = "^3.7"
htag = "^0.9"
htag = ">= 0.9.20"

[tool.poetry.dev-dependencies]
pytest = "^3.0"
Expand Down

0 comments on commit 8d21365

Please sign in to comment.