2150 |
mathias |
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 |
|
|
|
7 |
dojox.dtl.tag.loop.CycleNode = function(cyclevars, name, VarNode){
|
|
|
8 |
this._cyclevars = cyclevars;
|
|
|
9 |
this._counter = -1
|
|
|
10 |
this._name = name;
|
|
|
11 |
this._map = {};
|
|
|
12 |
this._VarNode = VarNode;
|
|
|
13 |
}
|
|
|
14 |
dojo.extend(dojox.dtl.tag.loop.CycleNode, {
|
|
|
15 |
render: function(context, buffer){
|
|
|
16 |
if(context.forloop && !context.forloop.counter0){
|
|
|
17 |
this._counter = -1;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
++this._counter;
|
|
|
21 |
var value = this._cyclevars[this._counter % this._cyclevars.length];
|
|
|
22 |
if(this._name){
|
|
|
23 |
context[this._name] = value;
|
|
|
24 |
}
|
|
|
25 |
if(!this._map[value]){
|
|
|
26 |
this._map[value] = {};
|
|
|
27 |
}
|
|
|
28 |
var node = this._map[value][this._counter] = new this._VarNode(value);
|
|
|
29 |
|
|
|
30 |
return node.render(context, buffer, this);
|
|
|
31 |
},
|
|
|
32 |
unrender: function(context, buffer){
|
|
|
33 |
return buffer;
|
|
|
34 |
},
|
|
|
35 |
clone: function(){
|
|
|
36 |
return new this.constructor(this._cyclevars, this._name);
|
|
|
37 |
},
|
|
|
38 |
_onEnd: function(){
|
|
|
39 |
this._counter = -1;
|
|
|
40 |
},
|
|
|
41 |
toString: function(){ return "dojox.dtl.tag.loop.CycleNode"; }
|
|
|
42 |
});
|
|
|
43 |
|
|
|
44 |
dojox.dtl.tag.loop.cycle = function(parser, text){
|
|
|
45 |
// summary: Cycle among the given strings each time this tag is encountered
|
|
|
46 |
var args = text.split(" ");
|
|
|
47 |
|
|
|
48 |
if(args.length < 2){
|
|
|
49 |
throw new Error("'cycle' tag requires at least two arguments");
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
if(args[1].indexOf(",") != -1){
|
|
|
53 |
var vars = args[1].split(",");
|
|
|
54 |
args = [args[0]];
|
|
|
55 |
for(var i = 0; i < vars.length; i++){
|
|
|
56 |
args.push('"' + vars[i] + '"');
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if(args.length == 2){
|
|
|
61 |
var name = args[args.length - 1];
|
|
|
62 |
|
|
|
63 |
if(!parser._namedCycleNodes){
|
|
|
64 |
throw new Error("No named cycles in template: '" + name + "' is not defined");
|
|
|
65 |
}
|
|
|
66 |
if(!parser._namedCycleNodes[name]){
|
|
|
67 |
throw new Error("Named cycle '" + name + "' does not exist");
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return parser._namedCycleNodes[name];
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if(args.length > 4 && args[args.length - 2] == "as"){
|
|
|
74 |
var name = args[args.length - 1];
|
|
|
75 |
|
|
|
76 |
var node = new dojox.dtl.tag.loop.CycleNode(args.slice(1, args.length - 2), name, parser.getVarNode());
|
|
|
77 |
|
|
|
78 |
if(!parser._namedCycleNodes){
|
|
|
79 |
parser._namedCycleNodes = {};
|
|
|
80 |
}
|
|
|
81 |
parser._namedCycleNodes[name] = node;
|
|
|
82 |
}else{
|
|
|
83 |
node = new dojox.dtl.tag.loop.CycleNode(args.slice(1), null, parser.getVarNode());
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
return node;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
}
|