From 8d21365cf6b31acc5705750d2893efe3108783b7 Mon Sep 17 00:00:00 2001 From: manatlan Date: Sat, 28 Jan 2023 15:58:07 +0100 Subject: [PATCH] popmenu z-index and right click --- htbulma/fileselect.py | 2 +- htbulma/form.py | 2 +- htbulma/inputs2.py | 2 +- htbulma/nav.py | 4 ++-- htbulma/navside.py | 2 +- htbulma/rte.py | 14 +++++++------- htbulma/service.py | 31 ++++++++++++++++--------------- htbulma/services/clipboard.py | 6 +++--- htbulma/services/mbox.py | 4 ++-- htbulma/services/toaster.py | 2 +- manual_tests_service.py | 33 ++++++++++++++++++--------------- pyproject.toml | 2 +- 12 files changed, 54 insertions(+), 50 deletions(-) diff --git a/htbulma/fileselect.py b/htbulma/fileselect.py index 3da9b04..e374e7d 100644 --- a/htbulma/fileselect.py +++ b/htbulma/fileselect.py @@ -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__": diff --git a/htbulma/form.py b/htbulma/form.py index cae0f32..63b684c 100644 --- a/htbulma/form.py +++ b/htbulma/form.py @@ -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 diff --git a/htbulma/inputs2.py b/htbulma/inputs2.py index d28c2ac..426371d 100644 --- a/htbulma/inputs2.py +++ b/htbulma/inputs2.py @@ -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): diff --git a/htbulma/nav.py b/htbulma/nav.py index 0f323a0..7232190 100644 --- a/htbulma/nav.py +++ b/htbulma/nav.py @@ -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() diff --git a/htbulma/navside.py b/htbulma/navside.py index 9d6b664..9ba56ac 100644 --- a/htbulma/navside.py +++ b/htbulma/navside.py @@ -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()""") diff --git a/htbulma/rte.py b/htbulma/rte.py index fa22362..4a070c3 100644 --- a/htbulma/rte.py +++ b/htbulma/rte.py @@ -54,7 +54,7 @@ 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, }, @@ -62,11 +62,11 @@ def init(self,value:"str or delta", onsave:"cb(self)"=None, opts:list=None, edit 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; } """ % ( @@ -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() diff --git a/htbulma/service.py b/htbulma/service.py index f1268b7..a37b861 100644 --- a/htbulma/service.py +++ b/htbulma/service.py @@ -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", @@ -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" @@ -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", ), @@ -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) @@ -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): @@ -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%;") diff --git a/htbulma/services/clipboard.py b/htbulma/services/clipboard.py index 108829e..0d73682 100644 --- a/htbulma/services/clipboard.py +++ b/htbulma/services/clipboard.py @@ -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__": diff --git a/htbulma/services/mbox.py b/htbulma/services/mbox.py index a31e093..829c164 100644 --- a/htbulma/services/mbox.py +++ b/htbulma/services/mbox.py @@ -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) @@ -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 diff --git a/htbulma/services/toaster.py b/htbulma/services/toaster.py index 6c46699..7757f4c 100644 --- a/htbulma/services/toaster.py +++ b/htbulma/services/toaster.py @@ -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 diff --git a/manual_tests_service.py b/manual_tests_service.py index 3a03ff1..de6d18e 100644 --- a/manual_tests_service.py +++ b/manual_tests_service.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 3a2c6cd..60375f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"