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")
5 dojo.require("dojox.dtl._base");
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;
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;
16 var i, item, items = [];
17 if(!dojo.isArray(value)){
18 var obj = value, value = [];
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]]);
26 items.sort(dojox.dtl.filter.lists._dictsort);
28 for(i = 0; item = items[i]; i++){
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;
37 var dictsort = dojox.dtl.filter.lists.dictsort(value, arg);
38 return dictsort.reverse();
40 first: function(value){
41 // summary: Returns the first item in a list
42 return (value.length) ? value[0] : "";
44 join: function(value, arg){
45 // summary: Joins a list with a string, like Python's ``str.join(list)``
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 || ",");
52 length: function(value){
53 // summary: Returns the length of the value - useful for lists
54 return (isNaN(value.length)) ? (value + "").length : value.length;
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);
60 random: function(value){
61 // summary: Returns a random item from the list
62 return value[Math.floor(Math.random() * value.length)];
64 slice: function(value, arg){
65 // summary: Returns a slice of the list.
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.
72 var parts = arg.split(":");
74 for(var i = 0; i < parts.length; i++){
78 bits.push(parseInt(parts[i]));
86 bits[0] = value.length + bits[0];
88 if(bits.length < 2 || bits[1] === null){
89 bits[1] = value.length;
92 bits[1] = value.length + bits[1];
95 return value.slice(bits[0], bits[1]);
97 _unordered_list: function(value, tabs){
98 var ddl = dojox.dtl.filter.lists;
100 for(i = 0; i < tabs; i++){
103 if(value[1] && value[1].length){
105 for(i = 0; i < value[1].length; i++){
106 recurse.push(ddl._unordered_list(value[1][i], tabs + 1))
108 return indent + "<li>" + value[0] + "\n" + indent + "<ul>\n" + recurse.join("\n") + "\n" + indent + "</ul>\n" + indent + "</li>";
110 return indent + "<li>" + value[0] + "</li>";
113 unordered_list: function(value){
115 // Recursively takes a self-nested list and returns an HTML unordered list --
116 // WITHOUT opening and closing <ul> tags.
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::
126 // | <li>Lawrence</li>
130 // | <li>Illinois</li>
133 return dojox.dtl.filter.lists._unordered_list(value, 1);