| 1 |
define([ |
| 2 |
"dojo/aspect", |
| 3 |
"dojo/_base/declare", |
| 4 |
"./_base", |
| 5 |
"dijit/_TemplatedMixin", |
| 6 |
"dojo/dom-construct", |
| 7 |
"dojo/cache", |
| 8 |
"dojo/_base/array", |
| 9 |
"dojo/string", |
| 10 |
"dojo/parser" |
| 11 |
], function(aspect,declare,dd,TemplatedMixin, domConstruct,Cache,Array,dString,Parser){ |
| 12 |
|
| 13 |
return declare("dojox.dtl._Templated", TemplatedMixin, { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
_dijitTemplateCompat: false, |
| 18 |
|
| 19 |
buildRendering: function(){ |
| 20 |
var node; |
| 21 |
|
| 22 |
if(this.domNode && !this._template){ |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
if(!this._template){ |
| 27 |
var t = this.getCachedTemplate( |
| 28 |
this.templatePath, |
| 29 |
this.templateString, |
| 30 |
this._skipNodeCache |
| 31 |
); |
| 32 |
if(t instanceof dd.Template) { |
| 33 |
this._template = t; |
| 34 |
}else{ |
| 35 |
node = t.cloneNode(true); |
| 36 |
} |
| 37 |
} |
| 38 |
if(!node){ |
| 39 |
var context = new dd._Context(this); |
| 40 |
if(!this._created){ |
| 41 |
delete context._getter; |
| 42 |
} |
| 43 |
var nodes = domConstruct.toDom( |
| 44 |
this._template.render(context) |
| 45 |
); |
| 46 |
|
| 47 |
if(nodes.nodeType !== 1 && nodes.nodeType !== 3){ |
| 48 |
|
| 49 |
|
| 50 |
for(var i = 0, l = nodes.childNodes.length; i < l; ++i){ |
| 51 |
node = nodes.childNodes[i]; |
| 52 |
if(node.nodeType == 1){ |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
}else{ |
| 57 |
|
| 58 |
node = nodes; |
| 59 |
} |
| 60 |
} |
| 61 |
this._attachTemplateNodes(node); |
| 62 |
if(this.widgetsInTemplate){ |
| 63 |
|
| 64 |
|
| 65 |
var parser = Parser, qry, attr; |
| 66 |
if(parser._query != "[dojoType]"){ |
| 67 |
qry = parser._query; |
| 68 |
attr = parser._attrName; |
| 69 |
parser._query = "[dojoType]"; |
| 70 |
parser._attrName = "dojoType"; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
var cw = (this._startupWidgets = Parser.parse(node, { |
| 75 |
noStart: !this._earlyTemplatedStartup, |
| 76 |
inherited: {dir: this.dir, lang: this.lang} |
| 77 |
})); |
| 78 |
|
| 79 |
|
| 80 |
if(qry){ |
| 81 |
parser._query = qry; |
| 82 |
parser._attrName = attr; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
for(var i = 0; i < cw.length; i++){ |
| 87 |
this._processTemplateNode(cw[i], function(n,p){ |
| 88 |
return n[p]; |
| 89 |
}, function(widget, type, callback){ |
| 90 |
|
| 91 |
if(type in widget){ |
| 92 |
|
| 93 |
return aspect.after(widget, type, callback, true); |
| 94 |
}else{ |
| 95 |
|
| 96 |
return widget.on(type, callback, true); |
| 97 |
} |
| 98 |
}); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if(this.domNode){ |
| 103 |
domConstruct.place(node, this.domNode, "before"); |
| 104 |
this.destroyDescendants(); |
| 105 |
domConstruct.destroy(this.domNode); |
| 106 |
} |
| 107 |
this.domNode = node; |
| 108 |
|
| 109 |
this._fillContent(this.srcNodeRef); |
| 110 |
}, |
| 111 |
|
| 112 |
_processTemplateNode: function( baseNode, getAttrFunc, attachFunc){ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if(this.widgetsInTemplate && (getAttrFunc(baseNode, "dojoType") || getAttrFunc(baseNode, "data-dojo-type"))){ |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
this.inherited(arguments); |
| 121 |
}, |
| 122 |
|
| 123 |
_templateCache: {}, |
| 124 |
getCachedTemplate: function(templatePath, templateString, alwaysUseString){ |
| 125 |
|
| 126 |
|
| 127 |
var tmplts = this._templateCache; |
| 128 |
var key = templateString || templatePath; |
| 129 |
if(tmplts[key]){ |
| 130 |
return tmplts[key]; |
| 131 |
} |
| 132 |
|
| 133 |
templateString = dString.trim(templateString || Cache(templatePath, {sanitize: true})); |
| 134 |
|
| 135 |
if( this._dijitTemplateCompat && |
| 136 |
(alwaysUseString || templateString.match(/\$\{([^\}]+)\}/g)) |
| 137 |
){ |
| 138 |
templateString = this._stringRepl(templateString); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
if(alwaysUseString || !templateString.match(/\{[{%]([^\}]+)[%}]\}/g)){ |
| 143 |
return tmplts[key] = domConstruct.toDom(templateString); |
| 144 |
}else{ |
| 145 |
return tmplts[key] = new dd.Template(templateString); |
| 146 |
} |
| 147 |
}, |
| 148 |
render: function(){ |
| 149 |
|
| 150 |
|
| 151 |
this.buildRendering(); |
| 152 |
}, |
| 153 |
startup: function(){ |
| 154 |
Array.forEach(this._startupWidgets, function(w){ |
| 155 |
if(w && !w._started && w.startup){ |
| 156 |
w.startup(); |
| 157 |
} |
| 158 |
}); |
| 159 |
this.inherited(arguments); |
| 160 |
} |
| 161 |
}); |
| 162 |
}); |