616502bfbaa9a755b36cd6733af9301be253f6fb.svn-base 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. if (!dojo._hasResource["dojox.dtl.tag.logic"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.dtl.tag.logic"] = true;
  6. dojo.provide("dojox.dtl.tag.logic");
  7. dojo.require("dojox.dtl._base");
  8. dojox.dtl.tag.logic.IfNode = function(bools, trues, falses, type) {
  9. this.bools = bools;
  10. this.trues = trues;
  11. this.falses = falses;
  12. this.type = type;
  13. }
  14. dojo.extend(dojox.dtl.tag.logic.IfNode, {
  15. render : function(context, buffer) {
  16. if (this.type == "or") {
  17. for (var i = 0, bool; bool = this.bools[i]; i++) {
  18. var ifnot = bool[0];
  19. var filter = bool[1];
  20. var value = filter.resolve(context);
  21. if ((value && !ifnot) || (ifnot && !value)) {
  22. if (this.falses) {
  23. buffer = this.falses.unrender(context,
  24. buffer);
  25. }
  26. return this.trues.render(context, buffer, this);
  27. }
  28. buffer = this.trues.unrender(context, buffer);
  29. if (this.falses)
  30. return this.falses
  31. .render(context, buffer, this);
  32. }
  33. } else {
  34. for (var i = 0, bool; bool = this.bools[i]; i++) {
  35. var ifnot = bool[0];
  36. var filter = bool[1];
  37. var value = filter.resolve(context);
  38. if (!((value && !ifnot) || (ifnot && !value))) {
  39. if (this.trues) {
  40. buffer = this.trues.unrender(context,
  41. buffer);
  42. }
  43. return this.falses
  44. .render(context, buffer, this);
  45. }
  46. buffer = this.falses.unrender(context, buffer);
  47. if (this.falses)
  48. return this.trues.render(context, buffer, this);
  49. }
  50. }
  51. return buffer;
  52. },
  53. unrender : function(context, buffer) {
  54. if (this.trues)
  55. buffer = this.trues.unrender(context, buffer);
  56. if (this.falses)
  57. buffer = this.falses.unrender(context, buffer);
  58. return buffer;
  59. },
  60. clone : function(buffer) {
  61. var trues = this.trues;
  62. var falses = this.falses;
  63. if (trues) {
  64. trues = trues.clone(buffer);
  65. }
  66. if (falses) {
  67. falses = falses.clone(buffer);
  68. }
  69. return new this.constructor(this.bools, trues, falses,
  70. this.type);
  71. },
  72. toString : function() {
  73. return "dojox.dtl.tag.logic.IfNode";
  74. }
  75. });
  76. dojox.dtl.tag.logic.ForNode = function(assign, loop, reversed, nodelist) {
  77. this.assign = assign;
  78. this.loop = loop;
  79. this.reversed = reversed;
  80. this.nodelist = nodelist;
  81. this.pool = [];
  82. }
  83. dojo.extend(dojox.dtl.tag.logic.ForNode, {
  84. render : function(context, buffer) {
  85. var parentloop = {};
  86. if (context.forloop) {
  87. parentloop = context.forloop;
  88. }
  89. var items = dojox.dtl.resolveVariable(this.loop, context);
  90. context.push();
  91. for (var i = items.length; i < this.pool.length; i++) {
  92. this.pool[i].unrender(context, buffer);
  93. }
  94. if (this.reversed) {
  95. items = items.reversed();
  96. }
  97. var j = 0;
  98. for (var i in items) {
  99. var item = items[i];
  100. context.forloop = {
  101. key : i,
  102. counter0 : j,
  103. counter : j + 1,
  104. revcounter0 : items.length - j - 1,
  105. revcounter : items.length - j,
  106. first : j == 0,
  107. parentloop : parentloop
  108. };
  109. context[this.assign] = item;
  110. if (j + 1 > this.pool.length) {
  111. this.pool.push(this.nodelist.clone(buffer));
  112. }
  113. buffer = this.pool[j].render(context, buffer, this);
  114. ++j;
  115. }
  116. context.pop();
  117. return buffer;
  118. },
  119. unrender : function(context, buffer) {
  120. for (var i = 0, pool; pool = this.pool[i]; i++) {
  121. buffer = pool.unrender(context, buffer);
  122. }
  123. return buffer;
  124. },
  125. clone : function(buffer) {
  126. return new this.constructor(this.assign, this.loop,
  127. this.reversed, this.nodelist.clone(buffer));
  128. },
  129. toString : function() {
  130. return "dojox.dtl.tag.logic.ForNode";
  131. }
  132. });
  133. dojox.dtl.tag.logic.if_ = function(parser, text) {
  134. var parts = text.split(/\s+/g);
  135. var type;
  136. var bools = [];
  137. parts.shift();
  138. text = parts.join(" ");
  139. parts = text.split(" and ");
  140. if (parts.length == 1) {
  141. type = "or";
  142. parts = text.split(" or ");
  143. } else {
  144. type = "and";
  145. for (var i = 0; i < parts.length; i++) {
  146. if (parts[i] == "or") {
  147. throw new Error("'if' tags can't mix 'and' and 'or'");
  148. }
  149. }
  150. }
  151. for (var i = 0, part; part = parts[i]; i++) {
  152. var not = false;
  153. if (part.indexOf("not ") == 0) {
  154. part = part.substring(4);
  155. not = true;
  156. }
  157. bools.push([not, new dojox.dtl.Filter(part)]);
  158. }
  159. var trues = parser.parse(["else", "endif"]);
  160. var falses = false;
  161. var token = parser.next();
  162. if (token.text == "else") {
  163. var falses = parser.parse(["endif"]);
  164. parser.next();
  165. }
  166. return new dojox.dtl.tag.logic.IfNode(bools, trues, falses, type);
  167. }
  168. dojox.dtl.tag.logic.for_ = function(parser, text) {
  169. var parts = text.split(/\s+/g);
  170. var reversed = parts.length == 5;
  171. var nodelist = parser.parse(["endfor"]);
  172. parser.next();
  173. return new dojox.dtl.tag.logic.ForNode(parts[1], parts[3], reversed,
  174. nodelist);
  175. }
  176. }