Skip to content

Commit

Permalink
works on #479
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Sep 7, 2022
1 parent 8eeada5 commit 8a02e87
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
17 changes: 16 additions & 1 deletion jpcore/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ def as_javascript(self):
title=self.page_options.get_title()
debug=str(self.page_options.get_debug()).lower()
page_ready=str(self.page_options.get_page_ready()).lower()
result_ready=str(self.page_options.get_result_ready()).lower()
reload_interval_ms=self.page_options.get_reload_interval_ms()
javascript=f"""let justpy_core=new JustpyCore(
this, // window
'{title}', // title
{page_ready}, // page_ready
{result_ready}, // result_ready
{reload_interval_ms}, // reload_interval
{debug} // debug
);"""
return javascript
Expand All @@ -49,4 +53,15 @@ def get_debug(self):
return self.page_options_dict.get("debug",False)

def get_page_ready(self):
return "page_ready" in self.events
return "page_ready" in self.events

def get_result_ready(self):
return "result_ready" in self.events

def get_reload_interval_ms(self)->float:
reload_interval=self.page_options_dict.get("reload_interval",0)
if reload_interval:
ms=round(reload_interval*1000)
else:
ms=0
return ms
4 changes: 3 additions & 1 deletion justpy/templates/js/justpy_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@
class JustpyCore {

// create a JustpyCore instance
constructor(window,title,page_ready,debug) {
constructor(window,title,page_ready,result_ready,reload_interval_ms,debug) {
this.window=window
this.setTitle(title);
this.page_ready=page_ready
this.result_ready=result_ready
this.reload_interval_ms=reload_interval_ms
this.debug=debug
}

Expand Down
10 changes: 5 additions & 5 deletions justpy/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@
'request_id': msg.request_id,
'result': js_result //JSON.stringify(js_result)
};
{% if 'result_ready' in page_options.events %}
if (justpy_core.result_ready) {
if (msg.send) send_to_server(e, 'page_event', false);
{% endif %}
}
}

const jsPromise = (new Promise(function () {
Expand Down Expand Up @@ -225,7 +225,7 @@
send_to_server(e);
});
}
{%- if page_options.reload_interval %}
if (justpy_core.reload_interval_ms >0) {
setInterval(function () {
$.ajax({
type: "POST",
Expand All @@ -239,8 +239,8 @@
},
dataType: 'json'
});
}, 1000 * {{ page_options.reload_interval }});
{%- endif %}
}, justpy_core.reload_interval_ms);
}

var app1 = new Vue({
el: '#components',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def test_javascript(self):
debug=True
if debug:
print(js)
for param in ["window","title","page_ready","debug"]:
for param in ["window","title","page_ready","result_ready","reload_interval","debug"]:
self.assertTrue(f"// {param}" in js)

0 comments on commit 8a02e87

Please sign in to comment.