# HG changeset patch # User bsw # Date 1345155768 -7200 # Node ID 376e3ed9b2456aea56e41f9afe10b157120142f2 # Parent 82f13fbbc05aa7c03e14da06a6bbfeac32b3ebb3 Removed not needed code from ui.filters, handle invalid filter options better diff -r 82f13fbbc05a -r 376e3ed9b245 env/ui/filters.lua --- a/env/ui/filters.lua Fri Aug 17 00:03:23 2012 +0200 +++ b/env/ui/filters.lua Fri Aug 17 00:22:48 2012 +0200 @@ -1,139 +1,56 @@ ---[[-- -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(")") + 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 + local current_option_valid = false + for idx, option in ipairs(filter) do + if current_option == option.name then + current_option_valid = true 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 + if not current_option or #current_option == 0 or not current_option_valid 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 + if idx > 1 then + slot.put(" ") + 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 - 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 - if idx > 1 then - slot.put(" ") - 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 end } ui.container{