From 4a37e31102bd2abb77936678cebfd12deb994473 Mon Sep 17 00:00:00 2001 From: nathangray Date: Mon, 25 Feb 2019 10:49:33 -0700 Subject: [PATCH] Etemplate - allow customisable onload for templates --- api/js/etemplate/et2_widget_template.js | 33 +++++++++++++++++++++++++ api/js/etemplate/etemplate2.js | 10 ++++++++ 2 files changed, 43 insertions(+) diff --git a/api/js/etemplate/et2_widget_template.js b/api/js/etemplate/et2_widget_template.js index 93c5d58416..2de6cc2de3 100644 --- a/api/js/etemplate/et2_widget_template.js +++ b/api/js/etemplate/et2_widget_template.js @@ -58,6 +58,12 @@ var et2_template = (function(){ "use strict"; return et2_DOMWidget.extend( name: "URL of template", type: "string", description: "full URL to load template incl. cache-buster" + }, + "onload": { + "name": "onload", + "type": "js", + "default": et2_no_init, + "description": "JS code which is executed after the template is loaded." } }, @@ -195,6 +201,33 @@ var et2_template = (function(){ "use strict"; return et2_DOMWidget.extend( return this.div; }, + attachToDOM: function() { + if (this.div) + { + jQuery(this.div) + .off('.et2_template') + .bind("load.et2_template", this, function(e) { + e.data.load.call(e.data, this); + }); + } + + this._super.apply(this,arguments); + }, + + /** + * Called after the template is fully loaded to handle any onload handlers + */ + load: function() { + if(typeof this.options.onload == 'function') + { + // Make sure function gets a reference to the widget + var args = Array.prototype.slice.call(arguments); + if(args.indexOf(this) == -1) args.push(this); + + return this.options.onload.apply(this, args); + } + }, + /** * Override to return the promise for deferred loading */ diff --git a/api/js/etemplate/etemplate2.js b/api/js/etemplate/etemplate2.js index d6809e231d..5088dcfe18 100644 --- a/api/js/etemplate/etemplate2.js +++ b/api/js/etemplate/etemplate2.js @@ -543,6 +543,16 @@ etemplate2.prototype.load = function(_name, _url, _data, _callback, _app, _no_et jQuery(this.DOMContainer).trigger('load', this); + if(this.templates[this.name].attributes.onload) + { + var onload = et2_checkType(this.templates[this.name].attributes.onload.value,'js','onload'); + if (typeof onload === 'string') + { + onload = et2_compileLegacyJS(onload, this, this.widgetContainer, this.widgetContainer); + } + onload.call(this.widgetContainer); + } + // Profiling if(egw.debug_level() >= 4) {