web2py_component timeout, issue 576, thanks Alan

This commit is contained in:
Massimo DiPierro
2012-02-28 15:09:12 -06:00
parent 3e26f75bd0
commit ced95854a9
5 changed files with 171 additions and 13 deletions
+1 -1
View File
@@ -1 +1 @@
Version 1.99.4 (2012-02-28 14:45:29) stable Version 1.99.4 (2012-02-28 15:09:09) stable
+45 -3
View File
@@ -102,9 +102,51 @@ function web2py_ajax_page(method, action, data, target) {
}); });
} }
function web2py_component(action,target) { function web2py_component(action, target, timeout, times){
jQuery(function(){web2py_ajax_page('get',action,null,target);}); jQuery(function(){
} var element = $("#" + target).get(0);
var statement = "$('#" + target + "').get(0).reload();";
element.reload = function (){
// Continue if times is Infinity or
// the times limit is not reached
if (this.reload_check()){
web2py_ajax_page('get', action, null, target);} }; // reload
// Method to check timing limit
element.reload_check = function (){
if (this.reload_counter == Infinity){return true;}
else {
if (!isNaN(this.reload_counter)){
this.reload_counter -= 1;
if (this.reload_counter < 0){
if (!this.run_once){
clearInterval(this.timing);
return false;
}
}
else{return true;}
} }
return false;}; // reload check
if (!isNaN(timeout)){
element.timeout = timeout;
element.reload_counter = times;
if (times > 1){
// Multiple or infinite reload
// Run first iteration
web2py_ajax_page('get', action, null, target);
element.run_once = false;
element.timing = setInterval(statement, timeout);
element.reload_counter -= 1;
}
else if (times == 1) {
// Run once with timeout
element.run_once = true;
element.setTimeout = setTimeout;
element.timing = setTimeout(statement, timeout);
}
} else {
// run once (no timeout specified)
web2py_ajax_page('get', action, null, target);
} }); }
function web2py_comet(url,onmessage,onopen,onclose) { function web2py_comet(url,onmessage,onopen,onclose) {
if ("WebSocket" in window) { if ("WebSocket" in window) {
+45 -3
View File
@@ -102,9 +102,51 @@ function web2py_ajax_page(method, action, data, target) {
}); });
} }
function web2py_component(action,target) { function web2py_component(action, target, timeout, times){
jQuery(function(){web2py_ajax_page('get',action,null,target);}); jQuery(function(){
} var element = $("#" + target).get(0);
var statement = "$('#" + target + "').get(0).reload();";
element.reload = function (){
// Continue if times is Infinity or
// the times limit is not reached
if (this.reload_check()){
web2py_ajax_page('get', action, null, target);} }; // reload
// Method to check timing limit
element.reload_check = function (){
if (this.reload_counter == Infinity){return true;}
else {
if (!isNaN(this.reload_counter)){
this.reload_counter -= 1;
if (this.reload_counter < 0){
if (!this.run_once){
clearInterval(this.timing);
return false;
}
}
else{return true;}
} }
return false;}; // reload check
if (!isNaN(timeout)){
element.timeout = timeout;
element.reload_counter = times;
if (times > 1){
// Multiple or infinite reload
// Run first iteration
web2py_ajax_page('get', action, null, target);
element.run_once = false;
element.timing = setInterval(statement, timeout);
element.reload_counter -= 1;
}
else if (times == 1) {
// Run once with timeout
element.run_once = true;
element.setTimeout = setTimeout;
element.timing = setTimeout(statement, timeout);
}
} else {
// run once (no timeout specified)
web2py_ajax_page('get', action, null, target);
} }); }
function web2py_comet(url,onmessage,onopen,onclose) { function web2py_comet(url,onmessage,onopen,onclose) {
if ("WebSocket" in window) { if ("WebSocket" in window) {
+45 -3
View File
@@ -102,9 +102,51 @@ function web2py_ajax_page(method, action, data, target) {
}); });
} }
function web2py_component(action,target) { function web2py_component(action, target, timeout, times){
jQuery(function(){web2py_ajax_page('get',action,null,target);}); jQuery(function(){
} var element = $("#" + target).get(0);
var statement = "$('#" + target + "').get(0).reload();";
element.reload = function (){
// Continue if times is Infinity or
// the times limit is not reached
if (this.reload_check()){
web2py_ajax_page('get', action, null, target);} }; // reload
// Method to check timing limit
element.reload_check = function (){
if (this.reload_counter == Infinity){return true;}
else {
if (!isNaN(this.reload_counter)){
this.reload_counter -= 1;
if (this.reload_counter < 0){
if (!this.run_once){
clearInterval(this.timing);
return false;
}
}
else{return true;}
} }
return false;}; // reload check
if (!isNaN(timeout)){
element.timeout = timeout;
element.reload_counter = times;
if (times > 1){
// Multiple or infinite reload
// Run first iteration
web2py_ajax_page('get', action, null, target);
element.run_once = false;
element.timing = setInterval(statement, timeout);
element.reload_counter -= 1;
}
else if (times == 1) {
// Run once with timeout
element.run_once = true;
element.setTimeout = setTimeout;
element.timing = setTimeout(statement, timeout);
}
} else {
// run once (no timeout specified)
web2py_ajax_page('get', action, null, target);
} }); }
function web2py_comet(url,onmessage,onopen,onclose) { function web2py_comet(url,onmessage,onopen,onclose) {
if ("WebSocket" in window) { if ("WebSocket" in window) {
+35 -3
View File
@@ -108,7 +108,19 @@ class mybuiltin(object):
def LOAD(c=None, f='index', args=None, vars=None, def LOAD(c=None, f='index', args=None, vars=None,
extension=None, target=None,ajax=False,ajax_trap=False, extension=None, target=None,ajax=False,ajax_trap=False,
url=None,user_signature=False, content='loading...',**attr): url=None,user_signature=False, timeout=None, times=1,
content='loading...',**attr):
""" LOAD a component into the action's document
Timing options:
-times: An integer or string ("infinity"/"continuous")
specifies how many times the component is requested
-timeout (milliseconds): specifies the time to wait before
starting the request or the frequency if times is greater than
1 or "infinity".
Timing options default to the normal behavior. The component
is added on page loading without delay.
"""
from html import TAG, DIV, URL, SCRIPT, XML from html import TAG, DIV, URL, SCRIPT, XML
if args is None: args = [] if args is None: args = []
vars = Storage(vars or {}) vars = Storage(vars or {})
@@ -121,9 +133,29 @@ def LOAD(c=None, f='index', args=None, vars=None,
url = url or URL(request.application, c, f, r=request, url = url or URL(request.application, c, f, r=request,
args=args, vars=vars, extension=extension, args=args, vars=vars, extension=extension,
user_signature=user_signature) user_signature=user_signature)
script = SCRIPT('web2py_component("%s","%s")' % (url, target), # timing options
_type="text/javascript") if isinstance(times, basestring):
if times.upper() in ("INFINITY", "CONTINUOUS"):
times = "Infinity"
else:
raise TypeError("Unsupported times argument %s" % times)
elif isinstance(times, int):
if times <= 0:
raise ValueError("Times argument must be greater than zero, 'Infinity' or None")
else:
raise TypeError("Unsupported times argument type %s" % type(times))
if timeout is not None:
if not isinstance(timeout, (int, long)):
raise ValueError("Timeout argument must be an integer or None")
elif timeout <= 0:
raise ValueError("Timeout argument must be greater than zero or None")
statement = "web2py_component('%s','%s', %s, %s);" \
% (url, target, timeout, times)
else:
statement = "web2py_component('%s','%s');" % (url, target)
script = SCRIPT(statement, _type="text/javascript")
return TAG[''](script, DIV(content,**attr)) return TAG[''](script, DIV(content,**attr))
else: else:
if not isinstance(args,(list,tuple)): if not isinstance(args,(list,tuple)):
args = [args] args = [args]