rev |
line source |
bsw@295
|
1 --[[--
|
bsw@295
|
2 ui.filters{
|
bsw@295
|
3 selector = selector, -- selector to be modified
|
bsw@295
|
4 label = label, -- text to be displayed when filters are collapsed
|
bsw@295
|
5 {
|
bsw@295
|
6 name = name1, -- name of first filter (used as GET param)
|
bsw@295
|
7 label = label1, -- label of first filter
|
bsw@295
|
8 {
|
bsw@295
|
9 name = name1a, -- name of first option of first filter
|
bsw@295
|
10 label = label1a, -- label of first option of first filter
|
bsw@295
|
11 selector_modifier = function(selector)
|
bsw@295
|
12 ...
|
bsw@295
|
13 end
|
bsw@295
|
14 },
|
bsw@295
|
15 {
|
bsw@295
|
16 name = name1b, -- name of second option of first filter
|
bsw@295
|
17 label = label1b, -- label of second option of first filter
|
bsw@295
|
18 selector_modifier = function(selector)
|
bsw@295
|
19 ...
|
bsw@295
|
20 end
|
bsw@295
|
21 },
|
bsw@295
|
22 ...
|
bsw@295
|
23 },
|
bsw@295
|
24 {
|
bsw@295
|
25 name = name2, -- name of second filter (used as GET param)
|
bsw@295
|
26 label = label2, -- label of second filter
|
bsw@295
|
27 {
|
bsw@295
|
28 ...
|
bsw@295
|
29 }, {
|
bsw@295
|
30 ...
|
bsw@295
|
31 },
|
bsw@295
|
32 ...
|
bsw@295
|
33 },
|
bsw@295
|
34 ...
|
bsw@295
|
35 content = function()
|
bsw@295
|
36 ... -- inner code where filter is to be applied
|
bsw@295
|
37 end
|
bsw@295
|
38 }
|
bsw@295
|
39
|
bsw@295
|
40 --]]--
|
bsw@295
|
41
|
bsw@295
|
42 function ui.filters(args)
|
bsw@295
|
43 local el_id = ui.create_unique_id()
|
bsw@295
|
44 ui.container{
|
bsw@295
|
45 attr = { class = "ui_filter" },
|
bsw@295
|
46 content = function()
|
bsw@295
|
47 ui.container{
|
bsw@295
|
48 attr = {
|
bsw@295
|
49 class = "ui_filter_closed_head"
|
bsw@295
|
50 },
|
bsw@295
|
51 content = function()
|
bsw@295
|
52 if args.folded then
|
bsw@295
|
53 ui.tag{
|
bsw@295
|
54 tag = "span",
|
bsw@295
|
55 content = function()
|
bsw@295
|
56 local current_options = {}
|
bsw@295
|
57 for idx, filter in ipairs(args) do
|
bsw@295
|
58 local filter_name = filter.name or "filter"
|
bsw@295
|
59 local current_option = atom.string:load(cgi.params[filter_name])
|
bsw@295
|
60 if not current_option then
|
bsw@295
|
61 current_option = param.get(filter_name)
|
bsw@295
|
62 end
|
bsw@295
|
63 if not current_option or #current_option == 0 then
|
bsw@295
|
64 current_option = filter[1].name
|
bsw@295
|
65 end
|
bsw@295
|
66 for idx, option in ipairs(filter) do
|
bsw@295
|
67 if current_option == option.name then
|
bsw@295
|
68 current_options[#current_options+1] = encode.html(filter.label) .. ": " .. encode.html(option.label)
|
bsw@295
|
69 end
|
bsw@295
|
70 end
|
bsw@295
|
71 end
|
bsw@295
|
72 slot.put(table.concat(current_options, "; "))
|
bsw@295
|
73 end
|
bsw@295
|
74 }
|
bsw@295
|
75 slot.put(" (")
|
bsw@295
|
76 ui.link{
|
bsw@295
|
77 attr = {
|
bsw@295
|
78 onclick = "this.parentNode.style.display='none'; document.getElementById('" .. el_id .. "_head').style.display='block'; return(false);"
|
bsw@295
|
79 },
|
bsw@295
|
80 text = args.label,
|
bsw@295
|
81 external = "#"
|
bsw@295
|
82 }
|
bsw@295
|
83 slot.put(")")
|
bsw@295
|
84 end
|
bsw@295
|
85 end
|
bsw@295
|
86 }
|
bsw@295
|
87 ui.container{
|
bsw@295
|
88 attr = {
|
bsw@295
|
89 id = el_id .. "_head",
|
bsw@295
|
90 style = args.folded and "display: none;" or nil
|
bsw@295
|
91 },
|
bsw@295
|
92 content = function()
|
bsw@295
|
93 for idx, filter in ipairs(args) do
|
bsw@295
|
94 local filter_name = filter.name or "filter"
|
bsw@295
|
95 local current_option = atom.string:load(cgi.params[filter_name])
|
bsw@295
|
96 if not current_option then
|
bsw@295
|
97 current_option = param.get(filter_name)
|
bsw@295
|
98 end
|
bsw@295
|
99 if not current_option or #current_option == 0 then
|
bsw@295
|
100 current_option = filter[1].name
|
bsw@295
|
101 end
|
bsw@295
|
102 local id = param.get_id_cgi()
|
bsw@295
|
103 local params = param.get_all_cgi()
|
bsw@295
|
104 ui.container{
|
bsw@295
|
105 attr = { class = "ui_filter_head" },
|
bsw@295
|
106 content = function()
|
bsw@295
|
107 slot.put(filter.label)
|
bsw@295
|
108 for idx, option in ipairs(filter) do
|
bsw@295
|
109 params[filter_name] = option.name
|
bsw@295
|
110 local attr = {}
|
bsw@295
|
111 if current_option == option.name then
|
bsw@295
|
112 attr.class = "active"
|
bsw@295
|
113 option.selector_modifier(args.selector)
|
bsw@295
|
114 end
|
bsw@295
|
115 ui.link{
|
bsw@295
|
116 attr = attr,
|
bsw@295
|
117 module = request.get_module(),
|
bsw@295
|
118 view = request.get_view(),
|
bsw@295
|
119 id = id,
|
bsw@295
|
120 params = params,
|
bsw@295
|
121 text = option.label,
|
bsw@295
|
122 partial = {
|
bsw@295
|
123 params = {
|
bsw@295
|
124 [filter_name] = option.name
|
bsw@295
|
125 }
|
bsw@295
|
126 }
|
bsw@295
|
127 }
|
bsw@295
|
128 end
|
bsw@295
|
129 end
|
bsw@295
|
130 }
|
bsw@295
|
131 end
|
bsw@295
|
132 end
|
bsw@295
|
133 }
|
bsw@295
|
134 end
|
bsw@295
|
135 }
|
bsw@295
|
136 ui.container{
|
bsw@295
|
137 attr = { class = "ui_filter_content" },
|
bsw@295
|
138 content = function()
|
bsw@295
|
139 args.content()
|
bsw@295
|
140 end
|
bsw@295
|
141 }
|
bsw@295
|
142 end
|