Roo.DomTemplate = function()
{
Roo.DomTemplate.superclass.constructor.apply(this, arguments);
if (this.html) {
this.compile();
}
};
Roo.extend(Roo.DomTemplate, Roo.Template, {
id : 0,
inPre : false,
tpls : false,
re : /(\{|\%7B)([\w-\.]+)(?:\:([\w\.]*)(?:\(([^)]*?)?\))?)?(\}|\%7D)/g,
iterChild : function (node, method) {
var oldPre = this.inPre;
if (node.tagName == 'PRE') {
this.inPre = true;
}
for( var i = 0; i < node.childNodes.length; i++) {
method.call(this, node.childNodes[i]);
}
this.inPre = oldPre;
},
compile: function()
{
var s = this.html;
var doc = false;
var div =false;
try {
doc = document.implementation.createHTMLDocument("");
doc.documentElement.innerHTML = this.html ;
div = doc.documentElement;
} catch (e) {
div = document.createElement('div');
div.innerHTML = this.html;
}
this.tpls = [];
var _t = this;
this.iterChild(div, function(n) {_t.compileNode(n, true); });
var tpls = this.tpls;
var tpl = {
uid : 'master',
id : this.id++,
attr : false,
value : false,
body : div.innerHTML,
forCall : false,
execCall : false,
dom : div,
isTop : true
};
tpls.unshift(tpl);
this.tpls = [];
Roo.each(tpls, function(tp){
this.compileTpl(tp);
this.tpls[tp.id] = tp;
}, this);
this.master = tpls[0];
return this;
},
compileNode : function(node, istop) {
if (node.nodeType != 1) {
if (node.nodeType == 3 && !this.inPre) {
node.nodeValue = node.nodeValue.replace(/\s+/g, ' ');
}
return;
}
var tpl = {
uid : false,
id : false,
attr : false,
value : false,
body : '',
forCall : false,
execCall : false,
dom : false,
isTop : istop
};
switch(true) {
case (node.hasAttribute('roo-for')): tpl.attr = 'for'; break;
case (node.hasAttribute('roo-if')): tpl.attr = 'if'; break;
case (node.hasAttribute('roo-name')): tpl.attr = 'name'; break;
case (node.hasAttribute('roo-exec')): tpl.attr = 'exec'; break;
}
if (!tpl.attr) {
this.iterChild(node,this.compileNode);
return;
}
tpl.uid = this.id++;
tpl.value = node.getAttribute('roo-' + tpl.attr);
node.removeAttribute('roo-'+ tpl.attr);
if (tpl.attr != 'name') {
var placeholder = document.createTextNode('{domtpl' + tpl.uid + '}');
node.parentNode.replaceChild(placeholder, node);
} else {
var placeholder = document.createElement('span');
placeholder.className = 'roo-tpl-' + tpl.value;
node.parentNode.replaceChild(placeholder, node);
}
this.iterChild(node,this.compileNode);
var div = document.createElement('div');
div.appendChild(node);
tpl.dom = node;
tpl.body = div.innerHTML;
tpl.id = tpl.uid;
switch(tpl.attr) {
case 'for' :
switch (tpl.value) {
case '.': tpl.forCall = new Function('values', 'parent', 'with(values){ return values; }'); break;
case '..': tpl.forCall= new Function('values', 'parent', 'with(values){ return parent; }'); break;
default: tpl.forCall= new Function('values', 'parent', 'with(values){ return '+tpl.value+'; }');
}
break;
case 'exec':
tpl.execCall = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
break;
case 'if':
tpl.ifCall = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
break;
case 'name':
tpl.id = tpl.value; break;
}
this.tpls.push(tpl);
},
compileTpl : function(tpl)
{
var fm = Roo.util.Format;
var useF = this.disableFormats !== true;
var sep = Roo.isGecko ? "+\n" : ",\n";
var undef = function(str) {
Roo.debug && Roo.log("Property not found :" + str);
return '';
};
var fn = function(m, lbrace, name, format, args)
{
args = args ? args.replace(/\\'/g,"'") : args;
if (typeof(format) == 'undefined') {
format = 'htmlEncode';
}
if (format == 'raw' ) {
format = false;
}
if(name.substr(0, 6) == 'domtpl'){
return "'"+ sep +'this.applySubTemplate('+name.substr(6)+', values, parent)'+sep+"'";
}
var udef_ar = [];
var lookfor = '';
Roo.each(name.split('.'), function(st) {
lookfor += (lookfor.length ? '.': '') + st;
udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
});
var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; if(format && useF){
args = args ? ',' + args : "";
if(format.substr(0, 5) != "this."){
format = "fm." + format + '(';
}else{
format = 'this.call("'+ format.substr(5) + '", ';
args = ", values";
}
return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
}
if (args && args.length) {
return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
}
return "'"+ sep + udef_st + name + ")"+sep+"'";
};
var body;
if(Roo.isGecko){
body = "tpl.compiled = function(values, parent){ with(values) { return '" +
tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
"';};};";
}else{
body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
body.push(tpl.body.replace(/(\r\n|\n)/g,
'\\n').replace(/'/g, "\\'").replace(this.re, fn));
body.push("'].join('');};};");
body = body.join('');
}
Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
eval(body);
return this;
},
applySubTemplate : function(id, values, parent)
{
var t = this.tpls[id];
try {
if(t.ifCall && !t.ifCall.call(this, values, parent)){
Roo.debug && Roo.log('if call on ' + t.value + ' return false');
return '';
}
} catch(e) {
Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on roo-if="' + t.value + '" - ' + e.toString());
Roo.log(values);
return '';
}
try {
if(t.execCall && t.execCall.call(this, values, parent)){
return '';
}
} catch(e) {
Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on roo-for="' + t.value + '" - ' + e.toString());
Roo.log(values);
return '';
}
try {
var vs = t.forCall ? t.forCall.call(this, values, parent) : values;
parent = t.target ? values : parent;
if(t.forCall && vs instanceof Array){
var buf = [];
for(var i = 0, len = vs.length; i < len; i++){
try {
buf[buf.length] = t.compiled.call(this, vs[i], parent);
} catch (e) {
Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on body="' + t.value + '" - ' + e.toString());
Roo.log(e.body);
Roo.log(vs[i]);
}
}
return buf.join('');
}
} catch (e) {
Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on roo-for="' + t.value + '" - ' + e.toString());
Roo.log(values);
return '';
}
try {
return t.compiled.call(this, vs, parent);
} catch (e) {
Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on body="' + t.value + '" - ' + e.toString());
Roo.log(e.body);
Roo.log(values);
return '';
}
},
applyTemplate : function(values){
return this.master.compiled.call(this, values, {});
},
apply : function(){
return this.applyTemplate.apply(this, arguments);
}
});
Roo.DomTemplate.from = function(el){
el = Roo.getDom(el);
return new Roo.Domtemplate(el.value || el.innerHTML);
};