_Templated.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.dtl._Templated"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.dtl._Templated"] = true;
  8. dojo.provide("dojox.dtl._Templated");
  9. dojo.require("dijit._Templated");
  10. dojo.require("dojox.dtl._base");
  11. dojo.declare("dojox.dtl._Templated", dijit._Templated, {
  12. _dijitTemplateCompat: false,
  13. buildRendering: function(){
  14. var node;
  15. if(this.domNode && !this._template){
  16. return;
  17. }
  18. if(!this._template){
  19. var t = this.getCachedTemplate(
  20. this.templatePath,
  21. this.templateString,
  22. this._skipNodeCache
  23. );
  24. if(t instanceof dojox.dtl.Template) {
  25. this._template = t;
  26. }else{
  27. node = t;
  28. }
  29. }
  30. if(!node){
  31. var context = new dojox.dtl._Context(this);
  32. if(!this._created){
  33. delete context._getter;
  34. }
  35. var nodes = dojo._toDom(
  36. this._template.render(context)
  37. );
  38. // TODO: is it really necessary to look for the first node?
  39. if(nodes.nodeType !== 1 && nodes.nodeType !== 3){
  40. // nodes.nodeType === 11
  41. // the node is a document fragment
  42. for(var i = 0, l = nodes.childNodes.length; i < l; ++i){
  43. node = nodes.childNodes[i];
  44. if(node.nodeType == 1){
  45. break;
  46. }
  47. }
  48. }else{
  49. // the node is an element or a text
  50. node = nodes;
  51. }
  52. }
  53. this._attachTemplateNodes(node);
  54. if(this.widgetsInTemplate){
  55. //Make sure dojoType is used for parsing widgets in template.
  56. //The dojo.parser.query could be changed from multiversion support.
  57. var parser = dojo.parser, qry, attr;
  58. if(parser._query != "[dojoType]"){
  59. qry = parser._query;
  60. attr = parser._attrName;
  61. parser._query = "[dojoType]";
  62. parser._attrName = "dojoType";
  63. }
  64. //Store widgets that we need to start at a later point in time
  65. var cw = (this._startupWidgets = dojo.parser.parse(node, {
  66. noStart: !this._earlyTemplatedStartup,
  67. inherited: {dir: this.dir, lang: this.lang}
  68. }));
  69. //Restore the query.
  70. if(qry){
  71. parser._query = qry;
  72. parser._attrName = attr;
  73. }
  74. this._supportingWidgets = dijit.findWidgets(node);
  75. this._attachTemplateNodes(cw, function(n,p){
  76. return n[p];
  77. });
  78. }
  79. if(this.domNode){
  80. dojo.place(node, this.domNode, "before");
  81. this.destroyDescendants();
  82. dojo.destroy(this.domNode);
  83. }
  84. this.domNode = node;
  85. this._fillContent(this.srcNodeRef);
  86. },
  87. _templateCache: {},
  88. getCachedTemplate: function(templatePath, templateString, alwaysUseString){
  89. // summary:
  90. // Layer for dijit._Templated.getCachedTemplate
  91. var tmplts = this._templateCache;
  92. var key = templateString || templatePath;
  93. if(tmplts[key]){
  94. return tmplts[key];
  95. }
  96. templateString = dojo.string.trim(templateString || dojo.cache(templatePath, {sanitize: true}));
  97. if( this._dijitTemplateCompat &&
  98. (alwaysUseString || templateString.match(/\$\{([^\}]+)\}/g))
  99. ){
  100. templateString = this._stringRepl(templateString);
  101. }
  102. // If we always use a string, or find no variables, just store it as a node
  103. if(alwaysUseString || !templateString.match(/\{[{%]([^\}]+)[%}]\}/g)){
  104. return tmplts[key] = dojo._toDom(templateString);
  105. }else{
  106. return tmplts[key] = new dojox.dtl.Template(templateString);
  107. }
  108. },
  109. render: function(){
  110. this.buildRendering();
  111. },
  112. startup: function(){
  113. dojo.forEach(this._startupWidgets, function(w){
  114. if(w && !w._started && w.startup){
  115. w.startup();
  116. }
  117. });
  118. this.inherited(arguments);
  119. }
  120. });
  121. }