_Templated.js 3.9 KB

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