From 4578d849f5880ed0a4ea0abd62f60a3ad19410f9 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Fri, 23 Jan 2015 19:32:32 +0000 Subject: [PATCH] new script widget to allow to include javascript in (customizied) templates, also adding an id to style widget --- etemplate/js/et2_widget_script.js | 69 +++++++++++++++++++++++++++++++ etemplate/js/et2_widget_styles.js | 30 ++++++++++++-- etemplate/js/etemplate2.js | 1 + 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 etemplate/js/et2_widget_script.js diff --git a/etemplate/js/et2_widget_script.js b/etemplate/js/et2_widget_script.js new file mode 100644 index 0000000000..53682bba79 --- /dev/null +++ b/etemplate/js/et2_widget_script.js @@ -0,0 +1,69 @@ +/** + * EGroupware eTemplate2 - JS widget class containing javascript + * + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @package etemplate + * @subpackage api + * @link http://www.egroupware.org + * @author Ralf Becker + * @copyright Stylite 2015 + * @version $Id$ + */ + +"use strict"; + +/*egw:uses + et2_core_widget; +*/ + +/** + * Function which executes the encapsulated script data. + * + * This should only be used for customization and NOT for regular EGroupware code! + * + * We can NOT create a script tag containing the content, as this violoates our CSP policy! + * + * We use new Function(_content) instead. Therefore you have to use window to address global context: + * + * window.some_func = function() {...} + * + * instead of not working + * + * function some_funct() {...} + * + * @augments et2_widget + */ +var et2_script = et2_widget.extend( +{ + /** + * Constructor + * + * @memberOf et2_script + */ + init: function() + { + this._super.apply(this, arguments); + + // Allow no child widgets + this.supportedWidgetClasses = []; + }, + + /** + * We can NOT create a script tag containing the content, as this violoates our CSP policy! + * + * @param {string} _content + */ + loadContent: function(_content) + { + try + { + var func = new Function(_content); + func.call(window); + } + catch (e) + { + this.egw.debug('error', 'Error while executing script: ',_content,e); + } + } +}); +et2_register_widget(et2_script, ["script"]); diff --git a/etemplate/js/et2_widget_styles.js b/etemplate/js/et2_widget_styles.js index 34d1fbc5f2..9a1e68ef25 100644 --- a/etemplate/js/et2_widget_styles.js +++ b/etemplate/js/et2_widget_styles.js @@ -23,14 +23,14 @@ * TODO: The style data could be parsed for rules and appended using the JS * stylesheet interface, allowing the style only to modifiy nodes of the current * template. - * + * * @augments et2_widget */ var et2_styles = et2_widget.extend( { /** * Constructor - * + * * @memberOf et2_styles */ init: function() { @@ -64,8 +64,32 @@ var et2_styles = et2_widget.extend( { this.styleNode.appendChild(document.createTextNode(_content)); } - } + }, + /** + * Sets the id of the DOM-Node. + * + * DOM id's have dots "." replaced with dashes "-" + * + * @param {string} _value id to set + */ + set_id: function(_value) { + + this.id = _value; + this.dom_id = _value ? this.getInstanceManager().uniqueId+'_'+_value.replace(/\./g, '-') : _value; + + if (this.styleNode) + { + if (_value != "") + { + this.styleNode.setAttribute("id", this.dom_id); + } + else + { + this.styleNode.removeAttribute("id"); + } + } + } }); et2_register_widget(et2_styles, ["styles"]); diff --git a/etemplate/js/etemplate2.js b/etemplate/js/etemplate2.js index 6d7b991b6c..c4ebecab89 100644 --- a/etemplate/js/etemplate2.js +++ b/etemplate/js/etemplate2.js @@ -54,6 +54,7 @@ et2_widget_vfs; et2_widget_video; et2_widget_itempicker; + et2_widget_script; et2_extension_nextmatch; et2_extension_customfields;