Debian Codegraph @

dojox/dtl/tag/loop.js

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