eGroupWare PHP Cross Reference Groupware Applications

Source: /etemplate/js/et2_core_legacyJSFunctions.js - 156 lines - 5445 bytes - Summary - Text - Print

Description: EGroupware eTemplate2 - Execution layer for legacy event code

   1  /**
   2   * EGroupware eTemplate2 - Execution layer for legacy event code
   3   *
   4   * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
   5   * @package etemplate
   6   * @subpackage api
   7   * @link http://www.egroupware.org
   8   * @author Andreas Stöckel
   9   * @copyright Stylite 2011-13
  10   * @version $Id: et2_core_phpExpressionCompiler.js 38256 2012-03-05 13:07:38Z igel457 $
  11   */
  12  
  13  "use strict";
  14  
  15  /*egw:uses
  16      et2_interfaces;
  17      et2_core_common;
  18  */
  19  
  20  (function() {
  21  
  22      /**
  23      * Resolve javascript pseudo functions in onclick or onchange:
  24      * - egw::link('$l','$p') calls egw.link($l,$p)
  25      * - form::name('name') returns expanded name/id taking into account the name at that point of the template hierarchy
  26      * - egw::lang('Message ...') translate the message, calls egw.lang()
  27      * - confirm('message') translates 'message' and adds a '?' if not present
  28      * - window.open() replaces it with egw_openWindowCentered2()
  29      * - xajax_doXMLHTTP('etemplate. replace ajax calls in widgets with special handler not requiring etemplate run rights
  30      *
  31      * @param string _val onclick, onchange, ... action
  32      * @param string _cname name-prefix / name-space
  33      * @ToDo replace xajax_doXMLHTTP with egw.json()
  34      * @ToDo replace (common) cases of confirm with new dialog, idea: calling function supplys function to call after confirm
  35      * @ToDo template::styles(name) inserts the styles of a named template
  36      * @return string
  37      */
  38  	function js_pseudo_funcs(_val,widget)
  39      {
  40          if (_val.indexOf('egw::link(') != -1)
  41          {
  42              _val = _val.replace(/egw::link\(/g,'egw.link(');
  43          }
  44  
  45          if (_val.indexOf('form::name(') != -1)
  46          {
  47              // et2_form_name doesn't care about ][, just [
  48              var _cname = widget.getPath() ? widget.getPath().join("[") : false;
  49              _val = _val.replace(/form::name\(/g, "'"+widget.getRoot()._inst.uniqueId+"_'+"+(_cname ? "et2_form_name('"+_cname+"'," : '('));
  50          }
  51  
  52          if (_val.indexOf('egw::lang(') != -1)
  53          {
  54              _val = _val.replace(/egw::lang\(/g,'egw.lang(');
  55          }
  56  
  57          // ToDo: inserts the styles of a named template
  58          /*if (preg_match('/template::styles\(["\']{1}(.*)["\']{1}\)/U',$on,$matches))
  59          {
  60              $tpl = $matches[1] == $this->name ? $this : new etemplate($matches[1]);
  61              $on = str_replace($matches[0],"'<style>".str_replace(array("\n","\r"),'',$tpl->style)."</style>'",$on);
  62          }*/
  63  
  64          // translate messages in confirm()
  65          if (_val.indexOf('confirm(') != -1)
  66          {
  67              _val = _val.replace(/confirm\((['"])(.*?)(\?)?['"]\)/,"confirm(egw.lang($1$2$1)+'$3')"); // add ? if not there, saves extra phrase
  68          }
  69  
  70          // replace window.open() with EGw's egw_openWindowCentered2()
  71          if (_val.indexOf('window.open(') != -1)
  72          {
  73              _val = _val.replace(/window.open\('(.*)','(.*)','dependent=yes,width=([^,]*),height=([^,]*),scrollbars=yes,status=(.*)'\)/,
  74                  "egw_openWindowCentered2('$1', '$2', $3, $4, '$5')");
  75          }
  76  
  77          // replace xajax calls to code in widgets, with the "etemplate" handler,
  78          // this allows to call widgets with the current app, otherwise everyone would need etemplate run rights
  79          if (_val.indexOf("xajax_doXMLHTTP('etemplate.") != -1)
  80          {
  81              _val = _val.replace(/^xajax_doXMLHTTP\('etemplate\.([a-z]+_widget\.[a-zA-Z0-9_]+)\'/,
  82                  "xajax_doXMLHTTP('"+egw.getAppName()+".$1.etemplate'");
  83          }
  84  
  85          if (_val.indexOf('this.form.submit()') != -1)
  86          {
  87              _val = _val.replace('this.form.submit()','widget.getInstanceManager().submit()');
  88          }
  89          return _val;
  90      }
  91  
  92      this.et2_compileLegacyJS = function(_code, _widget, _context) {
  93          // Replace the javascript pseudo-functions
  94          _code = js_pseudo_funcs(_code,_widget);
  95  
  96          // Check whether _code is simply "1" -- if yes replace it accordingly
  97          if (_code === '1')
  98          {
  99              _code = 'widget.getInstanceManager().submit(); return false;';
 100          }
 101  
 102          // Check whether some pseudo-variables still reside inside of the code,
 103          // if yes, replace them.
 104          if (_code.indexOf("$") >= 0 || _code.indexOf("@") >= 0)
 105          {
 106              // Get the content array manager for the widget
 107              var mgr = _widget.getArrayMgr("content");
 108              if (mgr)
 109              {
 110                  _code = mgr.expandName(_code);
 111              }
 112          }
 113  
 114          // Context is the context in which the function will run. Set context to
 115          // null as a default, so that it's possible to find bugs where "this" is
 116          // accessed in the code, but not properly set.
 117          var context = _context ? _context : null;
 118  
 119          // Check whether the given widget implements the "et2_IDOMNode"
 120          // interface
 121          if (!context && _widget.implements(et2_IDOMNode))
 122          {
 123              context = _widget.getDOMNode();
 124          }
 125  
 126          // Generate the function itself, if it fails, log the error message and
 127          // return a function which always returns false
 128          try {
 129              // Code is app.appname.function, add the arguments so it can be executed
 130              if (typeof _code == 'string' && _code.indexOf('app') == 0 && _code.split('.').length >= 3 && _code.indexOf('(') == -1)
 131              {
 132                  _code += '(ev,widget)';
 133              }
 134              var func = new Function('ev', 'widget', _code);
 135          } catch(e) {
 136              _widget.egw().debug('error', 'Error while compiling JS code ', _code);
 137              return (function() {return false;});
 138          }
 139  
 140          // Execute the code and return its results, pass the egw instance and
 141          // the widget
 142          return function(ev) {
 143              // Dump the executed code for debugging
 144              egw.debug('log', 'Executing legacy JS code: ', _code);
 145  
 146              if(arguments && arguments.length > 2)
 147              {
 148                  egw.debug('warn', 'Legacy JS code only supports 2 arguments (event and widget)', _code, arguments);
 149              }
 150              // Return the result of the called function
 151              return func.call(context, ev, _widget);
 152          };
 153      };
 154  
 155  }).call(window);
 156  

title

Description

title

Description

title

Description

title

title

Body