]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/dtl/filter/lists.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / dtl / filter / lists.js
1 if(!dojo._hasResource["dojox.dtl.filter.lists"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.dtl.filter.lists"] = true;
3 dojo.provide("dojox.dtl.filter.lists")
4
5 dojo.require("dojox.dtl._base");
6
7 dojo.mixin(dojox.dtl.filter.lists, {
8         _dictsort: function(a, b){
9                 if(a[0] == b[0]) return 0;
10                 return (a[0] < b[0]) ? -1 : 1;
11         },
12         dictsort: function(value, arg){
13                 // summary: Takes a list of dicts, returns that list sorted by the property given in the argument.
14                 if(!arg) return value;
15
16                 var i, item, items = [];
17                 if(!dojo.isArray(value)){
18                         var obj = value, value = [];
19                         for(var key in obj){
20                                 value.push(obj[k]);
21                         }
22                 }
23                 for(i = 0; i < value.length; i++){
24                         items.push([new dojox.dtl._Filter('var.' + arg).resolve(new dojox.dtl._Context({ 'var' : value[i]})), value[i]]);
25                 }
26                 items.sort(dojox.dtl.filter.lists._dictsort);
27                 var output = [];
28                 for(i = 0; item = items[i]; i++){
29                         output.push(item[1]);
30                 }
31                 return output;
32         },
33         dictsortreversed: function(value, arg){
34                 // summary: Takes a list of dicts, returns that list sorted in reverse order by the property given in the argument.
35                 if(!arg) return value;
36
37                 var dictsort = dojox.dtl.filter.lists.dictsort(value, arg);
38                 return dictsort.reverse();
39         },
40         first: function(value){
41                 // summary: Returns the first item in a list
42                 return (value.length) ? value[0] : "";
43         },
44         join: function(value, arg){
45                 // summary: Joins a list with a string, like Python's ``str.join(list)``
46                 // description:
47                 //              Django throws a compile error, but JS can't do arg checks
48                 //              so we're left with run time errors, which aren't wise for something
49                 //              as trivial here as an empty arg.
50                 return value.join(arg || ",");
51         },
52         length: function(value){
53                 // summary: Returns the length of the value - useful for lists
54                 return (isNaN(value.length)) ? (value + "").length : value.length;
55         },
56         length_is: function(value, arg){
57                 // summary: Returns a boolean of whether the value's length is the argument
58                 return value.length == parseInt(arg);
59         },
60         random: function(value){
61                 // summary: Returns a random item from the list
62                 return value[Math.floor(Math.random() * value.length)];
63         },
64         slice: function(value, arg){
65                 // summary: Returns a slice of the list.
66                 // description:
67                 //              Uses the same syntax as Python's list slicing; see
68                 //              http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
69                 //              for an introduction.
70                 //              Also uses the optional third value to denote every X item.
71                 arg = arg || "";
72                 var parts = arg.split(":");
73                 var bits = [];
74                 for(var i = 0; i < parts.length; i++){
75                         if(!parts[i].length){
76                                 bits.push(null);
77                         }else{
78                                 bits.push(parseInt(parts[i]));
79                         }
80                 }
81
82                 if(bits[0] === null){
83                         bits[0] = 0;
84                 }
85                 if(bits[0] < 0){
86                         bits[0] = value.length + bits[0];
87                 }
88                 if(bits.length < 2 || bits[1] === null){
89                         bits[1] = value.length;
90                 }
91                 if(bits[1] < 0){
92                         bits[1] = value.length + bits[1];
93                 }
94                 
95                 return value.slice(bits[0], bits[1]);
96         },
97         _unordered_list: function(value, tabs){
98                 var ddl = dojox.dtl.filter.lists;
99                 var i, indent = "";
100                 for(i = 0; i < tabs; i++){
101                         indent += "\t";
102                 }
103                 if(value[1] && value[1].length){
104                         var recurse = [];
105                         for(i = 0; i < value[1].length; i++){
106                                 recurse.push(ddl._unordered_list(value[1][i], tabs + 1))
107                         }
108                         return indent + "<li>" + value[0] + "\n" + indent + "<ul>\n" + recurse.join("\n") + "\n" + indent + "</ul>\n" + indent + "</li>";
109                 }else{
110                         return indent + "<li>" + value[0] + "</li>";
111                 }
112         },
113         unordered_list: function(value){
114                 // summary:
115                 //              Recursively takes a self-nested list and returns an HTML unordered list --
116                 //              WITHOUT opening and closing <ul> tags.
117                 //      description:
118                 //              The list is assumed to be in the proper format. For example, if ``var`` contains
119                 //              ``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
120                 //              then ``{{ var|unordered_list }}`` would return::
121                 //
122                 //              |       <li>States
123                 //              |       <ul>
124                 //              |               <li>Kansas
125                 //              |               <ul>
126                 //              |                       <li>Lawrence</li>
127                 //              |                       <li>Topeka</li>
128                 //              |               </ul>
129                 //              |               </li>
130                 //              |               <li>Illinois</li>
131                 //              |       </ul>
132                 //              |       </li>
133                 return dojox.dtl.filter.lists._unordered_list(value, 1);
134         }
135 });
136
137 }