]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/dtl/tag/loop.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / dtl / tag / loop.js
1 if(!dojo._hasResource["dojox.dtl.tag.loop"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.dtl.tag.loop"] = true;
3 dojo.provide("dojox.dtl.tag.loop");
4
5 dojo.require("dojox.dtl._base");
6 dojo.require("dojox.string.tokenize");
7
8 (function(){
9         var dd = dojox.dtl;
10         var ddtl = dd.tag.loop;
11
12         ddtl.CycleNode = dojo.extend(function(cyclevars, name, TextNode, shared){
13                 this.cyclevars = cyclevars;
14                 this.name = name;
15                 this.TextNode = TextNode;
16                 this.shared = shared || {counter: -1, map: {}};
17         },
18         {
19                 render: function(context, buffer){
20                         if(context.forloop && !context.forloop.counter0){
21                                 this.shared.counter = -1;
22                         }
23
24                         ++this.shared.counter;
25                         var value = this.cyclevars[this.shared.counter % this.cyclevars.length];
26
27                         var map = this.shared.map;
28                         if(!map[value]){
29                                 map[value] = new dd._Filter(value);
30                         }
31                         value = map[value].resolve(context, buffer);
32
33                         if(this.name){
34                                 context[this.name] = value;
35                         }
36                         if(!this.contents){
37                                 this.contents = new this.TextNode("");
38                         }
39                         this.contents.set(value);
40                         return this.contents.render(context, buffer);
41                 },
42                 unrender: function(context, buffer){
43                         return this.contents.unrender(context, buffer);
44                 },
45                 clone: function(){
46                         return new this.constructor(this.cyclevars, this.name, this.TextNode, this.shared);
47                 }
48         });
49
50         ddtl.IfChangedNode = dojo.extend(function(nodes, vars, shared){
51                 this.nodes = nodes;
52                 this._vars = vars;
53                 this.shared = shared || {last: null};
54                 this.vars = dojo.map(vars, function(item){
55                         return new dojox.dtl._Filter(item);
56                 });
57         }, {
58                 render: function(context, buffer){
59                         if(context.forloop && context.forloop.first){
60                                 this.shared.last = null;
61                         }
62
63                         var change;
64                         if(this.vars.length){
65                                 change = dojo.toJson(dojo.map(this.vars, function(item){
66                                         return item.resolve(context);
67                                 }));
68                         }else{
69                                 change = this.nodes.dummyRender(context, buffer);
70                         }
71
72                         if(change != this.shared.last){
73                                 var firstloop = (this.shared.last === null);
74                                 this.shared.last = change;
75                                 context.push();
76                                 context.ifchanged = {firstloop: firstloop}
77                                 buffer = this.nodes.render(context, buffer);
78                                 context.pop();
79                         }
80                         return buffer;
81                 },
82                 unrender: function(context, buffer){
83                         this.nodes.unrender(context, buffer);
84                 },
85                 clone: function(buffer){
86                         return new this.constructor(this.nodes.clone(buffer), this._vars, this.shared);
87                 }
88         });
89
90         ddtl.RegroupNode = dojo.extend(function(expression, key, alias){
91                 this._expression = expression;
92                 this.expression = new dd._Filter(expression);
93                 this.key = key;
94                 this.alias = alias;
95         },
96         {
97                 _push: function(container, grouper, stack){
98                         if(stack.length){
99                                 container.push({ grouper: grouper, list: stack })
100                         }
101                 },
102                 render: function(context, buffer){
103                         context[this.alias] = [];
104                         var list = this.expression.resolve(context);
105                         if(list){
106                                 var last = null;
107                                 var stack = [];
108                                 for(var i = 0; i < list.length; i++){
109                                         var id = list[i][this.key];
110                                         if(last !== id){
111                                                 this._push(context[this.alias], last, stack);
112                                                 last = id;
113                                                 stack = [list[i]];
114                                         }else{
115                                                 stack.push(list[i]);
116                                         }
117                                 }
118                                 this._push(context[this.alias], last, stack);
119                         }
120                         return buffer;
121                 },
122                 unrender: function(context, buffer){
123                         return buffer;
124                 },
125                 clone: function(context, buffer){
126                         return this;
127                 }
128         });
129
130         dojo.mixin(ddtl, {
131                 cycle: function(parser, text){
132                         // summary: Cycle among the given strings each time this tag is encountered
133                         var args = text.split(" ");
134
135                         if(args.length < 2){
136                                 throw new Error("'cycle' tag requires at least two arguments");
137                         }
138
139                         if(args[1].indexOf(",") != -1){
140                                 var vars = args[1].split(",");
141                                 args = [args[0]];
142                                 for(var i = 0; i < vars.length; i++){
143                                         args.push('"' + vars[i] + '"');
144                                 }
145                         }
146
147                         if(args.length == 2){
148                                 var name = args[args.length - 1];
149
150                                 if(!parser._namedCycleNodes){
151                                         throw new Error("No named cycles in template: '" + name + "' is not defined");
152                                 }
153                                 if(!parser._namedCycleNodes[name]){
154                                         throw new Error("Named cycle '" + name + "' does not exist");
155                                 }
156
157                         return parser._namedCycleNodes[name];
158                         }
159
160                         if(args.length > 4 && args[args.length - 2] == "as"){
161                                 var name = args[args.length - 1];
162
163                                 var node = new ddtl.CycleNode(args.slice(1, args.length - 2), name, parser.getTextNodeConstructor());
164
165                                 if(!parser._namedCycleNodes){
166                                         parser._namedCycleNodes = {};
167                                 }
168                                 parser._namedCycleNodes[name] = node;
169                         }else{
170                                 node = new ddtl.CycleNode(args.slice(1), null, parser.getTextNodeConstructor());
171                         }
172
173                         return node;
174                 },
175                 ifchanged: function(parser, text){
176                         var parts = dojox.dtl.text.pySplit(text);
177                         var nodes = parser.parse(["endifchanged"]);
178                         parser.next();
179                         return new ddtl.IfChangedNode(nodes, parts.slice(1));
180                 },
181                 regroup: function(parser, text){
182                         var tokens = dojox.string.tokenize(dojo.trim(text), /(\s+)/g, function(spaces){
183                                 return spaces;
184                         });
185                         if(tokens.length < 11 || tokens[tokens.length - 3] != "as" || tokens[tokens.length - 7] != "by"){
186                                 throw new Error("Expected the format: regroup list by key as newList");
187                         }
188                         var expression = tokens.slice(2, -8).join("");
189                         var key = tokens[tokens.length - 5];
190                         var alias = tokens[tokens.length - 1];
191                         return new ddtl.RegroupNode(expression, key, alias);
192                 }
193         });
194 })();
195
196 }