1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "./_base", |
---|
4 | "dijit/_TemplatedMixin", |
---|
5 | "dojo/dom-construct", |
---|
6 | "dojo/cache", |
---|
7 | "dojo/_base/array", |
---|
8 | "dojo/string", |
---|
9 | "dojo/parser", |
---|
10 | "dijit/_base/manager" |
---|
11 | ], function(declare,dd,TemplatedMixin, domConstruct,Cache,Array,dString,Parser,dijitMgr){ |
---|
12 | /*===== |
---|
13 | Cache = dojo.cache; |
---|
14 | dString = dojo.string; |
---|
15 | Parser = dojo.parser; |
---|
16 | TemplatedMixin = dijit._TemplatedMixin; |
---|
17 | dd = dojox.dtl; |
---|
18 | =====*/ |
---|
19 | return declare("dojox.dtl._Templated", TemplatedMixin, { |
---|
20 | // summary: The base-class for DTL-templated widgets. |
---|
21 | |
---|
22 | _dijitTemplateCompat: false, |
---|
23 | buildRendering: function(){ |
---|
24 | // summary: The method overrides dijit._TemplatedMixin.startup. |
---|
25 | var node; |
---|
26 | |
---|
27 | if(this.domNode && !this._template){ |
---|
28 | return; |
---|
29 | } |
---|
30 | |
---|
31 | if(!this._template){ |
---|
32 | var t = this.getCachedTemplate( |
---|
33 | this.templatePath, |
---|
34 | this.templateString, |
---|
35 | this._skipNodeCache |
---|
36 | ); |
---|
37 | if(t instanceof dd.Template) { |
---|
38 | this._template = t; |
---|
39 | }else{ |
---|
40 | node = t; |
---|
41 | } |
---|
42 | } |
---|
43 | if(!node){ |
---|
44 | var context = new dd._Context(this); |
---|
45 | if(!this._created){ |
---|
46 | delete context._getter; |
---|
47 | } |
---|
48 | var nodes = domConstruct.toDom( |
---|
49 | this._template.render(context) |
---|
50 | ); |
---|
51 | // TODO: is it really necessary to look for the first node? |
---|
52 | if(nodes.nodeType !== 1 && nodes.nodeType !== 3){ |
---|
53 | // nodes.nodeType === 11 |
---|
54 | // the node is a document fragment |
---|
55 | for(var i = 0, l = nodes.childNodes.length; i < l; ++i){ |
---|
56 | node = nodes.childNodes[i]; |
---|
57 | if(node.nodeType == 1){ |
---|
58 | break; |
---|
59 | } |
---|
60 | } |
---|
61 | }else{ |
---|
62 | // the node is an element or a text |
---|
63 | node = nodes; |
---|
64 | } |
---|
65 | } |
---|
66 | this._attachTemplateNodes(node, function(n,p){ |
---|
67 | return n.getAttribute(p); |
---|
68 | }); |
---|
69 | if(this.widgetsInTemplate){ |
---|
70 | //Make sure dojoType is used for parsing widgets in template. |
---|
71 | //The Parser.query could be changed from multiversion support. |
---|
72 | var parser = Parser, qry, attr; |
---|
73 | if(parser._query != "[dojoType]"){ |
---|
74 | qry = parser._query; |
---|
75 | attr = parser._attrName; |
---|
76 | parser._query = "[dojoType]"; |
---|
77 | parser._attrName = "dojoType"; |
---|
78 | } |
---|
79 | |
---|
80 | //Store widgets that we need to start at a later point in time |
---|
81 | var cw = (this._startupWidgets = Parser.parse(node, { |
---|
82 | noStart: !this._earlyTemplatedStartup, |
---|
83 | inherited: {dir: this.dir, lang: this.lang} |
---|
84 | })); |
---|
85 | |
---|
86 | //Restore the query. |
---|
87 | if(qry){ |
---|
88 | parser._query = qry; |
---|
89 | parser._attrName = attr; |
---|
90 | } |
---|
91 | |
---|
92 | this._supportingWidgets = dijitMgr.findWidgets(node); |
---|
93 | |
---|
94 | this._attachTemplateNodes(cw, function(n,p){ |
---|
95 | return n[p]; |
---|
96 | }); |
---|
97 | } |
---|
98 | |
---|
99 | if(this.domNode){ |
---|
100 | domConstruct.place(node, this.domNode, "before"); |
---|
101 | this.destroyDescendants(); |
---|
102 | domConstruct.destroy(this.domNode); |
---|
103 | } |
---|
104 | this.domNode = node; |
---|
105 | |
---|
106 | this._fillContent(this.srcNodeRef); |
---|
107 | }, |
---|
108 | _templateCache: {}, |
---|
109 | getCachedTemplate: function(templatePath, templateString, alwaysUseString){ |
---|
110 | // summary: |
---|
111 | // Layer for dijit._Templated.getCachedTemplate |
---|
112 | var tmplts = this._templateCache; |
---|
113 | var key = templateString || templatePath; |
---|
114 | if(tmplts[key]){ |
---|
115 | return tmplts[key]; |
---|
116 | } |
---|
117 | |
---|
118 | templateString = dString.trim(templateString || Cache(templatePath, {sanitize: true})); |
---|
119 | |
---|
120 | if( this._dijitTemplateCompat && |
---|
121 | (alwaysUseString || templateString.match(/\$\{([^\}]+)\}/g)) |
---|
122 | ){ |
---|
123 | templateString = this._stringRepl(templateString); |
---|
124 | } |
---|
125 | |
---|
126 | // If we always use a string, or find no variables, just store it as a node |
---|
127 | if(alwaysUseString || !templateString.match(/\{[{%]([^\}]+)[%}]\}/g)){ |
---|
128 | return tmplts[key] = domConstruct.toDom(templateString); |
---|
129 | }else{ |
---|
130 | return tmplts[key] = new dd.Template(templateString); |
---|
131 | } |
---|
132 | }, |
---|
133 | render: function(){ |
---|
134 | // summary: Renders the widget. |
---|
135 | this.buildRendering(); |
---|
136 | }, |
---|
137 | startup: function(){ |
---|
138 | // summary: The method overrides dijit._TemplatedMixin.startup. |
---|
139 | Array.forEach(this._startupWidgets, function(w){ |
---|
140 | if(w && !w._started && w.startup){ |
---|
141 | w.startup(); |
---|
142 | } |
---|
143 | }); |
---|
144 | this.inherited(arguments); |
---|
145 | } |
---|
146 | }); |
---|
147 | }); |
---|