X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=Roo%2FDomTemplate.js;h=3ffe06071494d9b7b4b7d78fc4e2adccc61c2b14;hb=refs%2Fheads%2Fwip_leon_T7094_logo_image_upload_in_boilerplate;hp=7fb5ae7701ac24f43d1edb53f49befada1cd9661;hpb=af33482aa3f3fd92db3a555fe82e5f065662f532;p=roojs1 diff --git a/Roo/DomTemplate.js b/Roo/DomTemplate.js index 7fb5ae7701..3ffe060714 100644 --- a/Roo/DomTemplate.js +++ b/Roo/DomTemplate.js @@ -27,29 +27,41 @@ * The tpl tag:
- <div roo-for="a_variable or condition.."></tpl>
- <tpl roo-if="a_variable or condition"></tpl>
- <tpl roo-exec="some javascript"></tpl>
- <tpl roo-name="named_template"></tpl>
+ <div roo-for="a_variable or condition.."></div>
+ <div roo-if="a_variable or condition"></div>
+ <div roo-exec="some javascript"></div>
+ <div roo-name="named_template"></div>
*
*/
-Roo.XTemplate = function()
+Roo.DomTemplate = function()
{
- Roo.XTemplate.superclass.constructor.apply(this, arguments);
- if (this.html) {
+ Roo.DomTemplate.superclass.constructor.apply(this, arguments);
+ if (this.html) {
this.compile();
- }
+ }
};
-Roo.extend(Roo.XTemplate, Roo.Template, {
-
+Roo.extend(Roo.DomTemplate, Roo.Template, {
+ /**
+ * id counter for sub templates.
+ */
+ id : 0,
+ /**
+ * flag to indicate if dom parser is inside a pre,
+ * it will strip whitespace if not.
+ */
+ inPre : false,
+
/**
* The various sub templates
*/
tpls : false,
+
+
+
/**
*
* basic tag replacing syntax
@@ -59,15 +71,23 @@ Roo.extend(Roo.XTemplate, Roo.Template, {
* x.t:(test,tesT)
*
*/
- re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
-
+ re : /(\{|\%7B)([\w-\.]+)(?:\:([\w\.]*)(?:\(([^)]*?)?\))?)?(\}|\%7D)/g,
+ //re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/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 the template
*
@@ -79,199 +99,207 @@ Roo.extend(Roo.XTemplate, Roo.Template, {
var s = this.html;
// covert the html into DOM...
+ var doc = false;
+ var div =false;
+ try {
+ doc = document.implementation.createHTMLDocument("");
+ doc.documentElement.innerHTML = this.html ;
+ div = doc.documentElement;
+ } catch (e) {
+ // old IE... - nasty -- it causes all sorts of issues.. with
+ // images getting pulled from server..
+ div = document.createElement('div');
+ div.innerHTML = this.html;
+ }
+ //doc.documentElement.innerHTML = htmlBody
+
- var div = document.createElement('div');
- div.innerHTML = this.html;
- this.iterChild(div, this.compileNode)
+ this.tpls = [];
+ var _t = this;
+ this.iterChild(div, function(n) {_t.compileNode(n, true); });
+
+ var tpls = this.tpls;
+
+ // create a top level template from the snippet..
+
+ //Roo.log(div.innerHTML);
+
+ 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);
+
+
+ // compile them...
+ 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) {
+ compileNode : function(node, istop) {
// test for
+ //Roo.log(node);
-
- switch(true) {
- case (node.hasAttribute('roo-for')) :
-
- case (node.hasAttribute('roo-if')) :
- var cond = node.getAttribute('roo-if');
- node.removeAttribute('roo-if');
- var id = this.id++;
- var placeholder = document.createTextNode('{domtmp' + id + '}');
- node.parentNode.replaceChild(placeholder, node);
-
+ // skip anything not a tag..
+ if (node.nodeType != 1) {
+ if (node.nodeType == 3 && !this.inPre) {
+ // reduce white space..
+ 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
- case (node.hasAttribute('roo-name')) :
-
- case (node.hasAttribute('roo-exec')) :
+ };
+
+
+ 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;
+ // no default..
+ }
+
+
+ if (!tpl.attr) {
+ // just itterate children..
+ 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);
}
+ // parent now sees '{domtplXXXX}
+ this.iterChild(node,this.compileNode);
+ // we should now have node body...
+ var div = document.createElement('div');
+ div.appendChild(node);
+ tpl.dom = node;
+ // this has the unfortunate side effect of converting tagged attributes
+ // eg. href="{...}" into %7C...%7D
+ // this has been fixed by searching for those combo's although it's a bit hacky..
- }
-
-
- s = ['