2012-03-06 16:26:11 +01:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - Execution layer for legacy event code
|
2012-03-06 16:26:11 +01:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel
|
2013-10-04 16:43:56 +02:00
|
|
|
* @copyright Stylite 2011-13
|
2012-03-06 16:26:11 +01:00
|
|
|
* @version $Id: et2_core_phpExpressionCompiler.js 38256 2012-03-05 13:07:38Z igel457 $
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
et2_interfaces;
|
|
|
|
et2_core_common;
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve javascript pseudo functions in onclick or onchange:
|
|
|
|
* - egw::link('$l','$p') calls egw.link($l,$p)
|
|
|
|
* - form::name('name') returns expanded name/id taking into account the name at that point of the template hierarchy
|
|
|
|
* - egw::lang('Message ...') translate the message, calls egw.lang()
|
|
|
|
* - confirm('message') translates 'message' and adds a '?' if not present
|
|
|
|
* - window.open() replaces it with egw_openWindowCentered2()
|
|
|
|
* - xajax_doXMLHTTP('etemplate. replace ajax calls in widgets with special handler not requiring etemplate run rights
|
|
|
|
*
|
|
|
|
* @param string _val onclick, onchange, ... action
|
|
|
|
* @param string _cname name-prefix / name-space
|
2013-10-04 16:43:56 +02:00
|
|
|
* @ToDo replace xajax_doXMLHTTP with egw.json()
|
|
|
|
* @ToDo replace (common) cases of confirm with new dialog, idea: calling function supplys function to call after confirm
|
|
|
|
* @ToDo template::styles(name) inserts the styles of a named template
|
2012-03-06 16:26:11 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-03-30 00:41:54 +02:00
|
|
|
function js_pseudo_funcs(_val,widget)
|
2012-03-06 16:26:11 +01:00
|
|
|
{
|
|
|
|
if (_val.indexOf('egw::link(') != -1)
|
|
|
|
{
|
|
|
|
_val = _val.replace(/egw::link\(/g,'egw.link(');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_val.indexOf('form::name(') != -1)
|
|
|
|
{
|
2012-03-30 00:41:54 +02:00
|
|
|
// et2_form_name doesn't care about ][, just [
|
|
|
|
var _cname = widget.getPath() ? widget.getPath().join("[") : false;
|
2013-08-20 16:23:00 +02:00
|
|
|
_val = _val.replace(/form::name\(/g, "'"+widget.getRoot()._inst.uniqueId+"_'+"+(_cname ? "et2_form_name('"+_cname+"'," : '('));
|
2012-03-06 16:26:11 +01:00
|
|
|
}
|
2014-01-14 18:08:41 +01:00
|
|
|
|
2012-03-06 16:26:11 +01:00
|
|
|
if (_val.indexOf('egw::lang(') != -1)
|
|
|
|
{
|
|
|
|
_val = _val.replace(/egw::lang\(/g,'egw.lang(');
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToDo: inserts the styles of a named template
|
|
|
|
/*if (preg_match('/template::styles\(["\']{1}(.*)["\']{1}\)/U',$on,$matches))
|
|
|
|
{
|
|
|
|
$tpl = $matches[1] == $this->name ? $this : new etemplate($matches[1]);
|
|
|
|
$on = str_replace($matches[0],"'<style>".str_replace(array("\n","\r"),'',$tpl->style)."</style>'",$on);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// translate messages in confirm()
|
|
|
|
if (_val.indexOf('confirm(') != -1)
|
|
|
|
{
|
|
|
|
_val = _val.replace(/confirm\((['"])(.*?)(\?)?['"]\)/,"confirm(egw.lang($1$2$1)+'$3')"); // add ? if not there, saves extra phrase
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace window.open() with EGw's egw_openWindowCentered2()
|
|
|
|
if (_val.indexOf('window.open(') != -1)
|
|
|
|
{
|
|
|
|
_val = _val.replace(/window.open\('(.*)','(.*)','dependent=yes,width=([^,]*),height=([^,]*),scrollbars=yes,status=(.*)'\)/,
|
|
|
|
"egw_openWindowCentered2('$1', '$2', $3, $4, '$5')");
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace xajax calls to code in widgets, with the "etemplate" handler,
|
|
|
|
// this allows to call widgets with the current app, otherwise everyone would need etemplate run rights
|
|
|
|
if (_val.indexOf("xajax_doXMLHTTP('etemplate.") != -1)
|
|
|
|
{
|
|
|
|
_val = _val.replace(/^xajax_doXMLHTTP\('etemplate\.([a-z]+_widget\.[a-zA-Z0-9_]+)\'/,
|
|
|
|
"xajax_doXMLHTTP('"+egw.getAppName()+".$1.etemplate'");
|
|
|
|
}
|
2012-03-29 17:10:25 +02:00
|
|
|
|
|
|
|
if (_val.indexOf('this.form.submit()') != -1)
|
|
|
|
{
|
2013-10-04 16:43:56 +02:00
|
|
|
_val = _val.replace('this.form.submit()','widget.getInstanceManager().submit()');
|
2012-03-29 17:10:25 +02:00
|
|
|
}
|
2012-03-06 16:26:11 +01:00
|
|
|
return _val;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.et2_compileLegacyJS = function(_code, _widget, _context) {
|
|
|
|
// Replace the javascript pseudo-functions
|
2012-03-30 00:41:54 +02:00
|
|
|
_code = js_pseudo_funcs(_code,_widget);
|
2012-03-06 16:26:11 +01:00
|
|
|
|
|
|
|
// Check whether _code is simply "1" -- if yes replace it accordingly
|
|
|
|
if (_code === '1')
|
|
|
|
{
|
2014-01-14 18:08:41 +01:00
|
|
|
_code = 'widget.getInstanceManager().submit(); return false;';
|
2012-03-06 16:26:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether some pseudo-variables still reside inside of the code,
|
|
|
|
// if yes, replace them.
|
|
|
|
if (_code.indexOf("$") >= 0 || _code.indexOf("@") >= 0)
|
|
|
|
{
|
|
|
|
// Get the content array manager for the widget
|
|
|
|
var mgr = _widget.getArrayMgr("content");
|
|
|
|
if (mgr)
|
|
|
|
{
|
|
|
|
_code = mgr.expandName(_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context is the context in which the function will run. Set context to
|
|
|
|
// null as a default, so that it's possible to find bugs where "this" is
|
|
|
|
// accessed in the code, but not properly set.
|
|
|
|
var context = _context ? _context : null;
|
|
|
|
|
|
|
|
// Check whether the given widget implements the "et2_IDOMNode"
|
|
|
|
// interface
|
|
|
|
if (!context && _widget.implements(et2_IDOMNode))
|
|
|
|
{
|
|
|
|
context = _widget.getDOMNode();
|
|
|
|
}
|
|
|
|
|
2012-03-09 16:32:29 +01:00
|
|
|
// Generate the function itself, if it fails, log the error message and
|
|
|
|
// return a function which always returns false
|
|
|
|
try {
|
2013-10-01 18:52:38 +02:00
|
|
|
// Code is app.appname.function, add the arguments so it can be executed
|
|
|
|
if (typeof _code == 'string' && _code.indexOf('app') == 0 && _code.split('.').length >= 3 && _code.indexOf('(') == -1)
|
|
|
|
{
|
2013-10-09 16:35:03 +02:00
|
|
|
_code += '(ev,widget)';
|
2013-10-01 18:52:38 +02:00
|
|
|
}
|
2013-10-09 16:35:03 +02:00
|
|
|
var func = new Function('ev', 'widget', _code);
|
2012-03-09 16:32:29 +01:00
|
|
|
} catch(e) {
|
|
|
|
_widget.egw().debug('error', 'Error while compiling JS code ', _code);
|
2013-04-13 21:00:13 +02:00
|
|
|
return (function() {return false;});
|
2012-03-09 16:32:29 +01:00
|
|
|
}
|
2012-03-06 16:26:11 +01:00
|
|
|
|
|
|
|
// Execute the code and return its results, pass the egw instance and
|
|
|
|
// the widget
|
2013-10-09 16:35:03 +02:00
|
|
|
return function(ev) {
|
2012-03-06 16:26:11 +01:00
|
|
|
// Dump the executed code for debugging
|
|
|
|
egw.debug('log', 'Executing legacy JS code: ', _code);
|
|
|
|
|
|
|
|
// Return the result of the called function
|
2013-10-09 16:35:03 +02:00
|
|
|
return func.call(context, ev, _widget);
|
2013-04-13 21:00:13 +02:00
|
|
|
};
|
|
|
|
};
|
2012-03-06 16:26:11 +01:00
|
|
|
|
|
|
|
}).call(window);
|
|
|
|
|