2011-08-05 16:53:54 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Template base class
|
2011-08-05 16:53:54 +02: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
|
|
|
|
* @copyright Stylite 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
2011-08-24 12:18:07 +02:00
|
|
|
et2_core_xml;
|
|
|
|
et2_core_DOMWidget;
|
2011-08-05 16:53:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which implements the "template" XET-Tag. When the id parameter is set,
|
|
|
|
* the template class checks whether another template with this id already
|
|
|
|
* exists. If yes, this template is removed from the DOM tree, copied and
|
|
|
|
* inserted in place of this template.
|
2014-02-25 14:04:28 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_DOMWidget
|
2014-02-25 14:04:28 +01:00
|
|
|
*/
|
2016-02-29 21:40:43 +01:00
|
|
|
var et2_template = (function(){ "use strict"; return et2_DOMWidget.extend(
|
2013-04-13 21:00:13 +02:00
|
|
|
{
|
2011-08-10 16:36:31 +02:00
|
|
|
attributes: {
|
|
|
|
"template": {
|
2011-09-28 01:44:07 +02:00
|
|
|
"name": "Template",
|
|
|
|
"type": "string",
|
2014-03-12 18:42:29 +01:00
|
|
|
"description": "Name / ID of template with optional cache-buster ('?'+filemtime of template on server)",
|
2013-05-06 19:36:37 +02:00
|
|
|
"default": et2_no_init
|
2011-08-10 16:36:31 +02:00
|
|
|
},
|
|
|
|
"group": {
|
2011-09-28 01:44:07 +02:00
|
|
|
// TODO: Not implemented
|
|
|
|
"name": "Group",
|
|
|
|
"description":"Not implemented",
|
2013-04-09 09:22:35 +02:00
|
|
|
//"default": 0
|
|
|
|
"default": et2_no_init
|
2011-08-10 16:36:31 +02:00
|
|
|
},
|
|
|
|
"version": {
|
|
|
|
"name": "Version",
|
|
|
|
"type": "string",
|
|
|
|
"description": "Version of the template"
|
|
|
|
},
|
|
|
|
"lang": {
|
|
|
|
"name": "Language",
|
|
|
|
"type": "string",
|
|
|
|
"description": "Language the template is written in"
|
2012-05-24 23:36:03 +02:00
|
|
|
},
|
|
|
|
"content": {
|
|
|
|
"name": "Content index",
|
2013-05-29 21:08:00 +02:00
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Used for passing in specific content to the template other than what it would get by ID."
|
2016-02-03 16:20:50 +01:00
|
|
|
},
|
|
|
|
url: {
|
|
|
|
name: "URL of template",
|
|
|
|
type: "string",
|
|
|
|
description: "full URL to load template incl. cache-buster"
|
2014-02-25 14:04:28 +01:00
|
|
|
}
|
2011-08-10 16:36:31 +02:00
|
|
|
},
|
|
|
|
|
2013-03-19 23:53:51 +01:00
|
|
|
createNamespace: true,
|
2011-08-19 18:00:44 +02:00
|
|
|
|
2011-08-05 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* Initializes this template widget as a simple container.
|
2014-02-25 14:04:28 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @memberOf et2_template
|
2014-02-25 14:04:28 +01:00
|
|
|
* @param {et2_widget} _parent
|
|
|
|
* @param {object} _attrs
|
2011-08-05 16:53:54 +02:00
|
|
|
*/
|
2012-05-24 23:36:03 +02:00
|
|
|
init: function(_parent, _attrs) {
|
|
|
|
// Set this early, so it's available for creating namespace
|
|
|
|
if(_attrs.content)
|
|
|
|
{
|
|
|
|
this.content = _attrs.content;
|
|
|
|
}
|
2011-08-19 18:00:44 +02:00
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
2011-08-05 16:53:54 +02:00
|
|
|
this.div = document.createElement("div");
|
2014-02-25 14:04:28 +01:00
|
|
|
|
2013-10-16 22:48:05 +02:00
|
|
|
// Deferred object so we can load via AJAX
|
|
|
|
this.loading = jQuery.Deferred();
|
2011-08-05 16:53:54 +02:00
|
|
|
|
2013-05-06 19:36:37 +02:00
|
|
|
if (this.id != "" || this.options.template)
|
2011-08-19 18:00:44 +02:00
|
|
|
{
|
2014-03-12 18:42:29 +01:00
|
|
|
var parts = (this.options.template || this.id).split('?');
|
|
|
|
var cache_buster = parts.length > 1 ? parts.pop() : null;
|
|
|
|
var template_name = parts.pop();
|
2013-05-06 19:36:37 +02:00
|
|
|
|
2015-08-20 16:57:18 +02:00
|
|
|
// Check to see if XML is known
|
|
|
|
var xml = null;
|
|
|
|
var templates = etemplate2.prototype.templates; // use global eTemplate cache
|
|
|
|
if(!(xml = templates[template_name]))
|
2011-08-05 16:53:54 +02:00
|
|
|
{
|
2014-02-25 14:04:28 +01:00
|
|
|
// Check to see if ID is short form --> prepend parent/top-level name
|
2014-02-25 17:17:41 +01:00
|
|
|
if(template_name.indexOf('.') < 0)
|
|
|
|
{
|
|
|
|
var root = _parent ? _parent.getRoot() : null;
|
|
|
|
var top_name = root && root._inst ? root._inst.name : null;
|
|
|
|
if (top_name && template_name.indexOf('.') < 0) template_name = top_name+'.'+template_name;
|
|
|
|
}
|
2015-08-20 16:57:18 +02:00
|
|
|
xml = templates[template_name];
|
|
|
|
if(!xml)
|
2012-07-09 23:29:02 +02:00
|
|
|
{
|
|
|
|
// Ask server
|
2016-02-03 16:20:50 +01:00
|
|
|
var url = this.options.url;
|
|
|
|
if (!this.options.url)
|
|
|
|
{
|
|
|
|
var splitted = template_name.split('.');
|
|
|
|
// use template base url from initial template, to continue using webdav, if that was loaded via webdav
|
|
|
|
url = this.getRoot()._inst.template_base_url + splitted.shift() + "/templates/default/" +
|
|
|
|
splitted.join('.')+ ".xet" + (cache_buster ? '?download='+cache_buster : '');
|
|
|
|
}
|
|
|
|
// if server did not give a cache-buster, fall back to current time
|
|
|
|
if (url.indexOf('?') == -1) url += '?download='+(new Date).valueOf();
|
2012-07-09 23:29:02 +02:00
|
|
|
|
2016-02-03 16:20:50 +01:00
|
|
|
if(this.options.url || splitted.length)
|
2012-07-24 01:51:47 +02:00
|
|
|
{
|
2016-02-03 16:20:50 +01:00
|
|
|
et2_loadXMLFromURL(url, function(_xmldoc) {
|
2015-08-20 16:57:18 +02:00
|
|
|
// Scan for templates and store them
|
|
|
|
for(var i = 0; i < _xmldoc.childNodes.length; i++) {
|
|
|
|
var template = _xmldoc.childNodes[i];
|
|
|
|
if(template.nodeName.toLowerCase() != "template") continue;
|
|
|
|
templates[template.getAttribute("id")] = template;
|
2012-07-24 01:51:47 +02:00
|
|
|
}
|
2015-08-20 16:57:18 +02:00
|
|
|
|
|
|
|
// Read the XML structure of the requested template
|
|
|
|
if (typeof templates[template_name] != 'undefined') this.loadFromXML(templates[template_name]);
|
|
|
|
|
|
|
|
// Update flag
|
|
|
|
this.loading.resolve();
|
|
|
|
|
|
|
|
}, this);
|
2012-07-24 01:51:47 +02:00
|
|
|
}
|
2012-07-09 23:29:02 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-05 16:53:54 +02:00
|
|
|
}
|
2015-08-20 16:57:18 +02:00
|
|
|
if(xml !== null && typeof xml !== "undefined")
|
2012-03-14 22:27:23 +01:00
|
|
|
{
|
2015-08-20 16:57:18 +02:00
|
|
|
this.egw().debug("log", "Loading template from XML: ", template_name);
|
|
|
|
this.loadFromXML(xml);
|
2013-10-16 22:48:05 +02:00
|
|
|
// Don't call this here - done by caller, or on whole widget tree
|
2012-03-14 22:27:23 +01:00
|
|
|
//this.loadingFinished();
|
2014-02-25 14:04:28 +01:00
|
|
|
|
2013-10-16 22:48:05 +02:00
|
|
|
// But resolve the promise
|
|
|
|
this.loading.resolve();
|
2012-03-14 22:27:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-20 16:57:18 +02:00
|
|
|
this.egw().debug("warn", "Unable to find XML for ", template_name);
|
2013-10-16 22:48:05 +02:00
|
|
|
this.loading.reject();
|
2012-03-14 22:27:23 +01:00
|
|
|
}
|
2011-08-05 16:53:54 +02:00
|
|
|
}
|
2013-10-17 17:23:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// No actual template
|
|
|
|
this.loading.resolve();
|
|
|
|
}
|
2011-08-05 16:53:54 +02:00
|
|
|
},
|
|
|
|
|
2012-05-24 23:36:03 +02:00
|
|
|
/**
|
|
|
|
* Override parent to support content attribute
|
2013-03-20 17:16:12 +01:00
|
|
|
* Templates always have ID set, but seldom do we want them to
|
|
|
|
* create a namespace based on their ID.
|
2012-05-24 23:36:03 +02:00
|
|
|
*/
|
|
|
|
checkCreateNamespace: function() {
|
|
|
|
if(this.content)
|
|
|
|
{
|
|
|
|
var old_id = this.id;
|
|
|
|
this.id = this.content;
|
|
|
|
this._super.apply(this, arguments);
|
2013-06-04 01:05:02 +02:00
|
|
|
this.id = old_id;
|
2012-05-24 23:36:03 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-08-19 18:00:44 +02:00
|
|
|
getDOMNode: function() {
|
2011-08-05 16:53:54 +02:00
|
|
|
return this.div;
|
2013-08-28 16:24:01 +02:00
|
|
|
},
|
2014-02-25 14:04:28 +01:00
|
|
|
|
2013-09-19 21:31:56 +02:00
|
|
|
/**
|
2013-10-16 22:48:05 +02:00
|
|
|
* Override to return the promise for deferred loading
|
2013-09-19 21:31:56 +02:00
|
|
|
*/
|
2013-10-09 16:10:33 +02:00
|
|
|
doLoadingFinished: function() {
|
2013-10-16 22:48:05 +02:00
|
|
|
// Apply parent now, which actually puts into the DOM
|
2013-10-09 16:10:33 +02:00
|
|
|
this._super.apply(this, arguments);
|
2014-02-25 14:04:28 +01:00
|
|
|
|
2013-10-16 22:48:05 +02:00
|
|
|
// Fire load event when done loading
|
|
|
|
this.loading.done(jQuery.proxy(function() {$j(this).trigger("load");},this.div));
|
2014-02-25 14:04:28 +01:00
|
|
|
|
2013-10-16 22:48:05 +02:00
|
|
|
// Not done yet, but widget will let you know
|
|
|
|
return this.loading.promise();
|
2013-10-09 16:10:33 +02:00
|
|
|
}
|
2016-02-29 21:40:43 +01:00
|
|
|
});}).call(this);
|
2011-08-05 16:53:54 +02:00
|
|
|
et2_register_widget(et2_template, ["template"]);
|
|
|
|
|