# HG changeset patch # User bsw # Date 1330201152 -3600 # Node ID fac04b72bc9a63ed016d4076e27c1f65ced1ca68 # Parent fee2fcc5d88e8e7ccee264c5af3e16498141aeed Added modified filters function diff -r fee2fcc5d88e -r fac04b72bc9a env/ui/filters.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/env/ui/filters.lua Sat Feb 25 21:19:12 2012 +0100 @@ -0,0 +1,142 @@ +--[[-- +ui.filters{ + selector = selector, -- selector to be modified + label = label, -- text to be displayed when filters are collapsed + { + name = name1, -- name of first filter (used as GET param) + label = label1, -- label of first filter + { + name = name1a, -- name of first option of first filter + label = label1a, -- label of first option of first filter + selector_modifier = function(selector) + ... + end + }, + { + name = name1b, -- name of second option of first filter + label = label1b, -- label of second option of first filter + selector_modifier = function(selector) + ... + end + }, + ... + }, + { + name = name2, -- name of second filter (used as GET param) + label = label2, -- label of second filter + { + ... + }, { + ... + }, + ... + }, + ... + content = function() + ... -- inner code where filter is to be applied + end +} + +--]]-- + +function ui.filters(args) + local el_id = ui.create_unique_id() + ui.container{ + attr = { class = "ui_filter" }, + content = function() + ui.container{ + attr = { + class = "ui_filter_closed_head" + }, + content = function() + if args.folded then + ui.tag{ + tag = "span", + content = function() + local current_options = {} + for idx, filter in ipairs(args) do + local filter_name = filter.name or "filter" + local current_option = atom.string:load(cgi.params[filter_name]) + if not current_option then + current_option = param.get(filter_name) + end + if not current_option or #current_option == 0 then + current_option = filter[1].name + end + for idx, option in ipairs(filter) do + if current_option == option.name then + current_options[#current_options+1] = encode.html(filter.label) .. ": " .. encode.html(option.label) + end + end + end + slot.put(table.concat(current_options, "; ")) + end + } + slot.put(" (") + ui.link{ + attr = { + onclick = "this.parentNode.style.display='none'; document.getElementById('" .. el_id .. "_head').style.display='block'; return(false);" + }, + text = args.label, + external = "#" + } + slot.put(")") + end + end + } + ui.container{ + attr = { + id = el_id .. "_head", + style = args.folded and "display: none;" or nil + }, + content = function() + for idx, filter in ipairs(args) do + local filter_name = filter.name or "filter" + local current_option = atom.string:load(cgi.params[filter_name]) + if not current_option then + current_option = param.get(filter_name) + end + if not current_option or #current_option == 0 then + current_option = filter[1].name + end + local id = param.get_id_cgi() + local params = param.get_all_cgi() + ui.container{ + attr = { class = "ui_filter_head" }, + content = function() + slot.put(filter.label) + for idx, option in ipairs(filter) do + params[filter_name] = option.name + local attr = {} + if current_option == option.name then + attr.class = "active" + option.selector_modifier(args.selector) + end + ui.link{ + attr = attr, + module = request.get_module(), + view = request.get_view(), + id = id, + params = params, + text = option.label, + partial = { + params = { + [filter_name] = option.name + } + } + } + end + end + } + end + end + } + end + } + ui.container{ + attr = { class = "ui_filter_content" }, + content = function() + args.content() + end + } +end