jbe/bsw@14: --[[-- jbe/bsw@14: ui.filters{ jbe/bsw@14: selector = selector, -- selector to be modified jbe/bsw@14: label = label, -- text to be displayed when filters are collapsed jbe/bsw@14: { jbe/bsw@14: name = name1, -- name of first filter (used as GET param) jbe/bsw@14: label = label1, -- label of first filter jbe/bsw@14: { jbe/bsw@14: name = name1a, -- name of first option of first filter jbe/bsw@14: label = label1a, -- label of first option of first filter jbe/bsw@14: selector_modifier = function(selector) jbe/bsw@14: ... jbe/bsw@14: end jbe/bsw@14: }, jbe/bsw@14: { jbe/bsw@14: name = name1b, -- name of second option of first filter jbe/bsw@14: label = label1b, -- label of second option of first filter jbe/bsw@14: selector_modifier = function(selector) jbe/bsw@14: ... jbe/bsw@14: end jbe/bsw@14: }, jbe/bsw@14: ... jbe/bsw@14: }, jbe/bsw@14: { jbe/bsw@14: name = name2, -- name of second filter (used as GET param) jbe/bsw@14: label = label2, -- label of second filter jbe/bsw@14: { jbe/bsw@14: ... jbe/bsw@14: }, { jbe/bsw@14: ... jbe/bsw@14: }, jbe/bsw@14: ... jbe/bsw@14: }, jbe/bsw@14: ... jbe/bsw@14: content = function() jbe/bsw@14: ... -- inner code where filter is to be applied jbe/bsw@14: end jbe/bsw@14: } jbe/bsw@14: jbe/bsw@14: --]]-- jbe/bsw@14: jbe/bsw@14: function ui.filters(args) jbe/bsw@14: local el_id = ui.create_unique_id() jbe/bsw@14: ui.container{ jbe/bsw@14: attr = { class = "ui_filter" }, jbe/bsw@14: content = function() jbe/bsw@14: ui.container{ jbe/bsw@14: attr = { jbe/bsw@14: class = "ui_filter_closed_head" jbe/bsw@14: }, jbe/bsw@14: content = function() jbe/bsw@14: ui.tag{ jbe/bsw@14: tag = "span", jbe/bsw@14: content = function() jbe/bsw@14: local current_options = {} jbe/bsw@14: for idx, filter in ipairs(args) do jbe/bsw@14: local filter_name = filter.name or "filter" jbe@223: local current_option = atom.string:load(request.get_param{name=filter_name}) jbe/bsw@14: if not current_option then jbe/bsw@14: current_option = param.get(filter_name) jbe/bsw@14: end jbe/bsw@14: if not current_option or #current_option == 0 then jbe/bsw@14: current_option = filter[1].name jbe/bsw@14: end jbe/bsw@14: for idx, option in ipairs(filter) do jbe/bsw@14: if current_option == option.name then jbe/bsw@14: current_options[#current_options+1] = encode.html(filter.label) .. ": " .. encode.html(option.label) jbe/bsw@14: end jbe/bsw@14: end jbe/bsw@14: end jbe/bsw@14: slot.put(table.concat(current_options, "; ")) jbe/bsw@14: end jbe/bsw@14: } jbe/bsw@14: slot.put(" (") jbe/bsw@14: ui.link{ jbe/bsw@14: attr = { jbe/bsw@14: onclick = "this.parentNode.style.display='none'; document.getElementById('" .. el_id .. "_head').style.display='block'; return(false);" jbe/bsw@14: }, jbe/bsw@14: text = args.label, jbe/bsw@14: external = "#" jbe/bsw@14: } jbe/bsw@14: slot.put(")") jbe/bsw@14: end jbe/bsw@14: } jbe/bsw@14: ui.container{ jbe/bsw@14: attr = { jbe/bsw@14: id = el_id .. "_head", jbe/bsw@14: style = "display: none;" jbe/bsw@14: }, jbe/bsw@14: content = function() jbe/bsw@14: for idx, filter in ipairs(args) do jbe/bsw@14: local filter_name = filter.name or "filter" jbe@223: local current_option = atom.string:load(request.get_param{name=filter_name}) jbe/bsw@14: if not current_option then jbe/bsw@14: current_option = param.get(filter_name) jbe/bsw@14: end jbe/bsw@14: if not current_option or #current_option == 0 then jbe/bsw@14: current_option = filter[1].name jbe/bsw@14: end jbe@92: local id = request.get_id_string() jbe@92: local params = request.get_param_strings() jbe/bsw@14: ui.container{ jbe/bsw@14: attr = { class = "ui_filter_head" }, jbe/bsw@14: content = function() jbe/bsw@14: slot.put(filter.label or "Filter", ": ") jbe/bsw@14: for idx, option in ipairs(filter) do jbe/bsw@14: params[filter_name] = option.name jbe/bsw@14: local attr = {} jbe/bsw@14: if current_option == option.name then jbe/bsw@14: attr.class = "active" jbe/bsw@14: option.selector_modifier(args.selector) jbe/bsw@14: end jbe/bsw@14: ui.link{ jbe/bsw@14: attr = attr, jbe/bsw@14: module = request.get_module(), jbe/bsw@14: view = request.get_view(), jbe/bsw@14: id = id, jbe/bsw@14: params = params, jbe@223: text = option.label jbe/bsw@14: } jbe/bsw@14: end jbe/bsw@14: end jbe/bsw@14: } jbe/bsw@14: end jbe/bsw@14: end jbe/bsw@14: } jbe/bsw@14: end jbe/bsw@14: } jbe/bsw@14: ui.container{ jbe/bsw@14: attr = { class = "ui_filter_content" }, jbe/bsw@14: content = function() jbe/bsw@14: args.content() jbe/bsw@14: end jbe/bsw@14: } jbe/bsw@14: end