html.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. if (!dojo._hasResource["dojox.dtl.render.html"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.dtl.render.html"] = true;
  6. dojo.provide("dojox.dtl.render.html");
  7. dojox.dtl.render.html.sensitivity = {
  8. // summary:
  9. // Set conditions under which to buffer changes
  10. // description:
  11. // Necessary if you make a lot of changes to your template.
  12. // What happens is that the entire node, from the attached DOM Node
  13. // down gets swapped with a clone, and until the entire rendering
  14. // is complete, we don't replace the clone again. In this way, renders
  15. // are
  16. // "batched".
  17. //
  18. // But, if we're only changing a small number of nodes, we might no want
  19. // to buffer at all.
  20. // The higher numbers mean that even small changes will result in
  21. // buffering.
  22. // Each higher level includes the lower levels.
  23. NODE : 1, // If a node changes, implement buffering
  24. ATTRIBUTE : 2, // If an attribute or node changes, implement buffering
  25. TEXT : 3
  26. // If any text at all changes, implement buffering
  27. }
  28. dojox.dtl.render.html.Render = function(/* DOMNode? */attachPoint, /* dojox.dtl.HtmlTemplate? */
  29. tpl) {
  30. this._tpl = tpl;
  31. this._node = attachPoint;
  32. this._swap = dojo.hitch(this, function() {
  33. // summary: Swaps the node out the first time the DOM is
  34. // changed
  35. // description: Gets swapped back it at end of render
  36. if (this._node === this._tpl.getRootNode()) {
  37. var frag = this._node;
  38. this._node = this._node.cloneNode(true);
  39. frag.parentNode.replaceChild(this._node, frag);
  40. }
  41. });
  42. }
  43. dojo.extend(dojox.dtl.render.html.Render, {
  44. sensitivity : dojox.dtl.render.html.sensitivity,
  45. setAttachPoint : function(/* Node */node) {
  46. this._node = node;
  47. },
  48. render : function(/* dojox.dtl.HtmlTemplate */tpl, /* Object */context, /* dojox.dtl.HtmlBuffer? */
  49. buffer) {
  50. if (!this._node) {
  51. throw new Error("You cannot use the Render object without specifying where you want to render it");
  52. }
  53. buffer = buffer || tpl.getBuffer();
  54. if (context.getThis()
  55. && context.getThis().buffer == this.sensitivity.NODE) {
  56. var onAddNode = dojo
  57. .connect(buffer, "onAddNode", this, "_swap");
  58. var onRemoveNode = dojo.connect(buffer, "onRemoveNode", this,
  59. "_swap");
  60. }
  61. if (this._tpl && this._tpl !== tpl) {
  62. this._tpl.unrender(context, buffer);
  63. }
  64. this._tpl = tpl;
  65. var frag = tpl.render(context, buffer).getParent();
  66. dojo.disconnect(onAddNode);
  67. dojo.disconnect(onRemoveNode);
  68. if (this._node !== frag) {
  69. this._node.parentNode.replaceChild(frag, this._node);
  70. dojo._destroyElement(this._node);
  71. this._node = frag;
  72. }
  73. }
  74. });
  75. }