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$
|
|
|
|
*/
|
|
|
|
|
2011-08-07 15:43:46 +02:00
|
|
|
"use strict";
|
|
|
|
|
2011-08-05 16:53:54 +02:00
|
|
|
/*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
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_template = et2_DOMWidget.extend(
|
|
|
|
{
|
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."
|
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-18 19:47:40 +02:00
|
|
|
// Check to see if the template is known
|
|
|
|
var template = null;
|
2015-01-23 15:32:47 +01:00
|
|
|
var templates = etemplate2.prototype.templates; // use global eTemplate cache
|
2015-08-18 19:47:40 +02:00
|
|
|
if(!(template = 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-18 19:47:40 +02:00
|
|
|
template = templates[template_name];
|
|
|
|
if(!template)
|
2012-07-09 23:29:02 +02:00
|
|
|
{
|
|
|
|
// Ask server
|
2013-10-07 19:00:03 +02:00
|
|
|
var splitted = template_name.split('.');
|
2014-11-05 14:07:13 +01:00
|
|
|
// use template base url from initial template, to continue using webdav, if that was loaded via webdav
|
2015-08-18 19:47:40 +02:00
|
|
|
var path = this.getRoot()._inst.template_base_url +
|
|
|
|
splitted.join('.') + (cache_buster ? '&download='+cache_buster :
|
2015-01-23 15:32:47 +01:00
|
|
|
// if server did not give a cache-buster, fall back to current time
|
2015-08-18 19:47:40 +02:00
|
|
|
'&download='+(new Date).valueOf());
|
2012-07-09 23:29:02 +02:00
|
|
|
|
2012-07-24 01:51:47 +02:00
|
|
|
if(splitted.length)
|
|
|
|
{
|
2015-08-18 19:47:40 +02:00
|
|
|
jQuery.ajax({
|
|
|
|
url: path,
|
|
|
|
context: this,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(_data, _status, _xmlhttp){
|
|
|
|
for(var i = 0; i < _data.children.length; i++)
|
|
|
|
{
|
|
|
|
var template = _data.children[i];
|
|
|
|
if(template.tag !== "template") continue;
|
|
|
|
templates[template.attributes.id] = template;
|
|
|
|
}// Read the structure of the requested template
|
|
|
|
if (typeof templates[template_name] != 'undefined') this.loadFromJSON(templates[template_name]);
|
|
|
|
|
|
|
|
// Update flag
|
|
|
|
this.loading.resolve();
|
|
|
|
},
|
|
|
|
error: function(_xmlhttp, _err) {
|
|
|
|
egw().debug('error', 'Loading eTemplate from '+_url+' failed! '+_xmlhttp.status+' '+_xmlhttp.statusText);
|
2012-07-24 01:51:47 +02:00
|
|
|
}
|
2015-08-18 19:47:40 +02:00
|
|
|
});
|
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-18 19:47:40 +02:00
|
|
|
if(template !== null && typeof template !== "undefined")
|
2012-03-14 22:27:23 +01:00
|
|
|
{
|
2015-08-18 19:47:40 +02:00
|
|
|
this.egw().debug("log", "Loading template: ", template_name);
|
|
|
|
if(template.tag)
|
|
|
|
{
|
|
|
|
this.loadFromJSON(template);
|
|
|
|
}
|
|
|
|
|
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-18 19:47:40 +02:00
|
|
|
this.egw().debug("warn", "Unable to find ", 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
|
|
|
}
|
2011-08-05 16:53:54 +02:00
|
|
|
});
|
|
|
|
et2_register_widget(et2_template, ["template"]);
|
|
|
|
|