Debian Codegraph @

dojox/dtl/filter/lists.js

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