aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/dtl/filter/lists.js
blob: b09524203374099cb199b37b7457a680ba94b8cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
if(!dojo._hasResource["dojox.dtl.filter.lists"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.dtl.filter.lists"] = true;
dojo.provide("dojox.dtl.filter.lists")

dojo.require("dojox.dtl._base");

dojo.mixin(dojox.dtl.filter.lists, {
	_dictsort: function(a, b){
		if(a[0] == b[0]) return 0;
		return (a[0] < b[0]) ? -1 : 1;
	},
	dictsort: function(value, arg){
		// summary: Takes a list of dicts, returns that list sorted by the property given in the argument.
		if(!arg) return value;

		var i, item, items = [];
		if(!dojo.isArray(value)){
			var obj = value, value = [];
			for(var key in obj){
				value.push(obj[k]);
			}
		}
		for(i = 0; i < value.length; i++){
			items.push([new dojox.dtl._Filter('var.' + arg).resolve(new dojox.dtl._Context({ 'var' : value[i]})), value[i]]);
		}
		items.sort(dojox.dtl.filter.lists._dictsort);
		var output = [];
		for(i = 0; item = items[i]; i++){
			output.push(item[1]);
		}
		return output;
	},
	dictsortreversed: function(value, arg){
		// summary: Takes a list of dicts, returns that list sorted in reverse order by the property given in the argument.
		if(!arg) return value;

		var dictsort = dojox.dtl.filter.lists.dictsort(value, arg);
		return dictsort.reverse();
	},
	first: function(value){
		// summary: Returns the first item in a list
		return (value.length) ? value[0] : "";
	},
	join: function(value, arg){
		// summary: Joins a list with a string, like Python's ``str.join(list)``
		// description:
		//		Django throws a compile error, but JS can't do arg checks
		//		so we're left with run time errors, which aren't wise for something
		//		as trivial here as an empty arg.
		return value.join(arg || ",");
	},
	length: function(value){
		// summary: Returns the length of the value - useful for lists
		return (isNaN(value.length)) ? (value + "").length : value.length;
	},
	length_is: function(value, arg){
		// summary: Returns a boolean of whether the value's length is the argument
		return value.length == parseInt(arg);
	},
	random: function(value){
		// summary: Returns a random item from the list
		return value[Math.floor(Math.random() * value.length)];
	},
	slice: function(value, arg){
		// summary: Returns a slice of the list.
		// description:
		//		Uses the same syntax as Python's list slicing; see
		//		http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
		//		for an introduction.
		//		Also uses the optional third value to denote every X item.
		arg = arg || "";
		var parts = arg.split(":");
		var bits = [];
		for(var i = 0; i < parts.length; i++){
			if(!parts[i].length){
				bits.push(null);
			}else{
				bits.push(parseInt(parts[i]));
			}
		}

		if(bits[0] === null){
			bits[0] = 0;
		}
		if(bits[0] < 0){
			bits[0] = value.length + bits[0];
		}
		if(bits.length < 2 || bits[1] === null){
			bits[1] = value.length;
		}
		if(bits[1] < 0){
			bits[1] = value.length + bits[1];
		}
		
		return value.slice(bits[0], bits[1]);
	},
	_unordered_list: function(value, tabs){
		var ddl = dojox.dtl.filter.lists;
		var i, indent = "";
		for(i = 0; i < tabs; i++){
			indent += "\t";
		}
		if(value[1] && value[1].length){
			var recurse = [];
			for(i = 0; i < value[1].length; i++){
				recurse.push(ddl._unordered_list(value[1][i], tabs + 1))
			}
			return indent + "<li>" + value[0] + "\n" + indent + "<ul>\n" + recurse.join("\n") + "\n" + indent + "</ul>\n" + indent + "</li>";
		}else{
			return indent + "<li>" + value[0] + "</li>";
		}
	},
	unordered_list: function(value){
		// summary:
		//		Recursively takes a self-nested list and returns an HTML unordered list --
		//		WITHOUT opening and closing <ul> tags.
		//	description:
		//		The list is assumed to be in the proper format. For example, if ``var`` contains
		//		``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
		//		then ``{{ var|unordered_list }}`` would return::
		//
		//		|	<li>States
		//		|	<ul>
		//		|		<li>Kansas
		//		|		<ul>
		//		|			<li>Lawrence</li>
		//		|			<li>Topeka</li>
		//		|		</ul>
		//		|		</li>
		//		|		<li>Illinois</li>
		//		|	</ul>
		//		|	</li>
		return dojox.dtl.filter.lists._unordered_list(value, 1);
	}
});

}