webmcp

diff framework/env/ui/filters.lua @ 14:a29c8ffb3f82

New function ui.filters{...}

Also changed version information to "1.0.8"
author jbe/bsw
date Sat Feb 20 21:00:58 2010 +0100 (2010-02-20)
parents
children 2f8d8edd1836
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/ui/filters.lua	Sat Feb 20 21:00:58 2010 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +--[[--
     1.5 +ui.filters{
     1.6 +  selector = selector,  -- selector to be modified
     1.7 +  label    = label,     -- text to be displayed when filters are collapsed
     1.8 +  {
     1.9 +    name  = name1,      -- name of first filter (used as GET param)
    1.10 +    label = label1,     -- label of first filter
    1.11 +    {
    1.12 +      name  = name1a,   -- name of first option of first filter
    1.13 +      label = label1a,  -- label of first option of first filter
    1.14 +      selector_modifier = function(selector)
    1.15 +        ...
    1.16 +      end
    1.17 +    },
    1.18 +    {
    1.19 +      name  = name1b,   -- name of second option of first filter
    1.20 +      label = label1b,  -- label of second option of first filter
    1.21 +      selector_modifier = function(selector)
    1.22 +        ...
    1.23 +      end
    1.24 +    },
    1.25 +    ...
    1.26 +  },
    1.27 +  {
    1.28 +    name  = name2,      -- name of second filter (used as GET param)
    1.29 +    label = label2,     -- label of second filter
    1.30 +    {
    1.31 +      ...
    1.32 +    }, {
    1.33 +      ...
    1.34 +    },
    1.35 +    ...
    1.36 +  },
    1.37 +  ...
    1.38 +  content = function()
    1.39 +    ...                 -- inner code where filter is to be applied
    1.40 +  end
    1.41 +}
    1.42 +
    1.43 +--]]--
    1.44 +
    1.45 +function ui.filters(args)
    1.46 +  local el_id = ui.create_unique_id()
    1.47 +  ui.container{
    1.48 +    attr = { class = "ui_filter" },
    1.49 +    content = function()
    1.50 +      ui.container{
    1.51 +        attr = {
    1.52 +          class = "ui_filter_closed_head"
    1.53 +        },
    1.54 +        content = function()
    1.55 +          ui.tag{
    1.56 +            tag = "span",
    1.57 +            content = function()
    1.58 +              local current_options = {}
    1.59 +              for idx, filter in ipairs(args) do
    1.60 +                local filter_name = filter.name or "filter"
    1.61 +                local current_option = atom.string:load(cgi.params[filter_name])
    1.62 +                if not current_option then
    1.63 +                  current_option = param.get(filter_name)
    1.64 +                end
    1.65 +                if not current_option or #current_option == 0 then
    1.66 +                  current_option = filter[1].name
    1.67 +                end
    1.68 +                for idx, option in ipairs(filter) do
    1.69 +                  if current_option == option.name then
    1.70 +                    current_options[#current_options+1] = encode.html(filter.label) .. ": " .. encode.html(option.label)
    1.71 +                  end
    1.72 +                end
    1.73 +              end
    1.74 +              slot.put(table.concat(current_options, "; "))
    1.75 +            end
    1.76 +          }
    1.77 +          slot.put(" (")
    1.78 +          ui.link{
    1.79 +            attr = {
    1.80 +              onclick = "this.parentNode.style.display='none'; document.getElementById('" .. el_id .. "_head').style.display='block'; return(false);"
    1.81 +            },
    1.82 +            text = args.label,
    1.83 +            external = "#"
    1.84 +          }
    1.85 +          slot.put(")")
    1.86 +        end
    1.87 +      }
    1.88 +      ui.container{
    1.89 +        attr = {
    1.90 +          id = el_id .. "_head",
    1.91 +          style = "display: none;"
    1.92 +        },
    1.93 +        content = function()
    1.94 +          for idx, filter in ipairs(args) do
    1.95 +            local filter_name = filter.name or "filter"
    1.96 +            local current_option = atom.string:load(cgi.params[filter_name])
    1.97 +            if not current_option then
    1.98 +              current_option = param.get(filter_name)
    1.99 +            end
   1.100 +            if not current_option or #current_option == 0 then
   1.101 +              current_option = filter[1].name
   1.102 +            end
   1.103 +            local id     = param.get_id_cgi()
   1.104 +            local params = param.get_all_cgi()
   1.105 +            ui.container{
   1.106 +              attr = { class = "ui_filter_head" },
   1.107 +              content = function()
   1.108 +                slot.put(filter.label or "Filter", ": ")
   1.109 +                for idx, option in ipairs(filter) do
   1.110 +                  params[filter_name] = option.name
   1.111 +                  local attr = {}
   1.112 +                  if current_option == option.name then
   1.113 +                    attr.class = "active"
   1.114 +                    option.selector_modifier(args.selector)
   1.115 +                  end
   1.116 +                  ui.link{
   1.117 +                    attr    = attr,
   1.118 +                    module  = request.get_module(),
   1.119 +                    view    = request.get_view(),
   1.120 +                    id      = id,
   1.121 +                    params  = params,
   1.122 +                    text    = option.label,
   1.123 +                    partial = {
   1.124 +                      params = {
   1.125 +                        [filter_name] = option.name
   1.126 +                      }
   1.127 +                    }
   1.128 +                  }
   1.129 +                end
   1.130 +              end
   1.131 +            }
   1.132 +          end
   1.133 +        end
   1.134 +      }
   1.135 +    end
   1.136 +  }
   1.137 +  ui.container{
   1.138 +    attr = { class = "ui_filter_content" },
   1.139 +    content = function()
   1.140 +      args.content()
   1.141 +    end
   1.142 +  }
   1.143 +end

Impressum / About Us