Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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