From 3a14e96d1ab2471d4784e382adcbbe9804147997 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Sat, 20 Apr 2013 19:21:42 +0000 Subject: [PATCH] allow to use hierarchical function names, eg. app.filemanager.upload via new et2_call function --- etemplate/js/et2_core_common.js | 32 ++++++++++++++++++++++++++++++++ etemplate/js/et2_widget_file.js | 9 ++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/etemplate/js/et2_core_common.js b/etemplate/js/et2_core_common.js index 2582675975..f67710ab64 100644 --- a/etemplate/js/et2_core_common.js +++ b/etemplate/js/et2_core_common.js @@ -733,3 +733,35 @@ function et2_rangeSubstract(_ar1, _ar2) return res; } +/** + * Call a function specified by it's name (possibly dot separated, eg. "app.myapp.myfunc") + * + * @param string func dot-separated function name + * @param arguments variable number of arguments + * @returns {Boolean} + */ +function et2_call(_func) +{ + var args = [].slice.call(arguments); // convert arguments to array + var func = args.shift(); + var parent = window; + + if (typeof _func == 'string') + { + var parts = _func.split('.'); + func = parts.pop(); + for(var i=0; i < parts.length && typeof parent[parts[i]] != 'undefined'; ++i) + { + parent = parent[parts[i]]; + } + if (typeof parent[func] == 'function') + { + func = parent[func]; + } + } + if (typeof func != 'function') + { + throw _func+" is not a function!"; + } + return func.apply(parent, args); +} diff --git a/etemplate/js/et2_widget_file.js b/etemplate/js/et2_widget_file.js index 8ca0d34e06..7a844e4a0a 100644 --- a/etemplate/js/et2_widget_file.js +++ b/etemplate/js/et2_widget_file.js @@ -258,7 +258,7 @@ var et2_file = et2_inputWidget.extend( this.disabled_buttons = $j("input[type='submit'], button").not("[disabled]").attr("disabled", true); event.data = this; - if(this.options.onStart && typeof this.options.onStart == 'function') return this.options.onStart(event,file_count); + if(this.options.onStart) return et2_call(this.options.onStart, event, file_count); return true; }, @@ -268,7 +268,7 @@ var et2_file = et2_inputWidget.extend( onFinish: function(event, file_count) { this.disabled_buttons.attr("disabled", false); event.data = this; - if(this.options.onFinish && typeof this.options.onFinish == 'function') return this.options.onFinish(event,file_count); + if(this.options.onFinish) return et2_call(this.options.onFinish, event, file_count); }, @@ -326,7 +326,6 @@ console.warn(event,name,error); if(typeof response.response[0].data[key] == "string") { // Message from server - probably error - $j("[file='"+name+"']",this.progress) .addClass("error") .css("display", "block") @@ -349,9 +348,9 @@ console.warn(event,name,error); .css("display", "block") .text(this.egw().lang("Server error")); } - if(this.options.onFinishOne && typeof this.options.onFinishOne == 'function') + if(this.options.onFinishOne) { - return this.options.onFinishOne(event,response,name,number,total); + return et2_call(this.options.onFinishOne,event,response,name,number,total); } return true; },