webmcp

view framework/env/ui/filters.lua @ 569:5b19007574de

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

Impressum / About Us