webmcp

diff framework/env/ui/field/boolean.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/ui/field/boolean.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +--[[--
     1.5 +ui.field.boolean{
     1.6 +  ...                        -- generic ui.field.* arguments, as described for ui.autofield{...}
     1.7 +  style       = style,       -- "radio" or "checkbox",
     1.8 +  nil_allowed = nil_allowed  -- set to true, if nil is allowed as third value
     1.9 +}
    1.10 +
    1.11 +This function inserts a field for boolean values in the active slot. For description of the generic field helper arguments, see help for ui.autofield{...}.
    1.12 +
    1.13 +--]]--
    1.14 +
    1.15 +function ui.field.boolean(args)
    1.16 +  local style = args.style
    1.17 +  if not style then
    1.18 +    if args.nil_allowed then
    1.19 +      style = "radio"
    1.20 +    else
    1.21 +      style = "checkbox"
    1.22 +    end
    1.23 +  end
    1.24 +  local extra_args = { fetch_value = true }
    1.25 +  if not args.readonly and args.style == "radio" then
    1.26 +    extra_args.disable_label_for_id = true
    1.27 +  end
    1.28 +  ui.form_element(args, extra_args, function(args)
    1.29 +    local value = args.value
    1.30 +    if value ~= true and value ~= false and value ~= nil then
    1.31 +      error("Boolean value must be true, false or nil.")
    1.32 +    end
    1.33 +    if value == nil then
    1.34 +      if args.nil_allowed then
    1.35 +        value = args.default
    1.36 +      else
    1.37 +        value = args.default or false
    1.38 +      end
    1.39 +    end
    1.40 +    if args.readonly then
    1.41 +      ui.tag{
    1.42 +        tag     = args.tag,
    1.43 +        attr    = args.attr,
    1.44 +        content = format.boolean(value, args.format_options)
    1.45 +      }
    1.46 +    elseif style == "radio" then
    1.47 +      local attr = table.new(args.attr)
    1.48 +      attr.type  = "radio"
    1.49 +      attr.name  = args.html_name
    1.50 +      attr.id    = ui.create_unique_id()
    1.51 +      attr.value = "1"
    1.52 +      if value == true then
    1.53 +        attr.checked = "checked"
    1.54 +      else
    1.55 +        attr.checked = nil
    1.56 +      end
    1.57 +      ui.container{
    1.58 +        attr          = { class = "ui_radio_div" },
    1.59 +        label         = args.true_as or "Yes",  -- TODO: localize
    1.60 +        label_for     = attr.id,
    1.61 +        label_attr    = { class = "ui_radio_label" },
    1.62 +        content_first = true,
    1.63 +        content       = function()
    1.64 +          ui.tag{ tag  = "input", attr = attr }
    1.65 +        end
    1.66 +      }
    1.67 +      attr.id    = ui.create_unique_id()
    1.68 +      attr.value = "0"
    1.69 +      if value == false then
    1.70 +        attr.checked = "1"
    1.71 +      else
    1.72 +        attr.checked = nil
    1.73 +      end
    1.74 +      ui.container{
    1.75 +        attr          = { class = "ui_radio_div" },
    1.76 +        label         = args.false_as or "No",  -- TODO: localize
    1.77 +        label_for     = attr.id,
    1.78 +        label_attr    = { class = "ui_radio_label" },
    1.79 +        content_first = true,
    1.80 +        content       = function()
    1.81 +          ui.tag{ tag  = "input", attr = attr }
    1.82 +        end
    1.83 +      }
    1.84 +      if args.nil_allowed then
    1.85 +        attr.id    = ui.create_unique_id()
    1.86 +        attr.value = ""
    1.87 +        if value == nil then
    1.88 +          attr.checked = "1"
    1.89 +        else
    1.90 +          attr.checked = nil
    1.91 +        end
    1.92 +        ui.container{
    1.93 +          attr          = { class = "ui_radio_div" },
    1.94 +          label         = args.nil_as or "N/A",  -- TODO: localize
    1.95 +          label_for     = attr.id,
    1.96 +          label_attr    = { class = "ui_radio_label" },
    1.97 +          content_first = true,
    1.98 +          content       = function()
    1.99 +            ui.tag{ tag  = "input", attr = attr }
   1.100 +          end
   1.101 +        }
   1.102 +      end
   1.103 +      ui.hidden_field{
   1.104 +        name = args.html_name .. "__format", value = "boolean"
   1.105 +      }
   1.106 +    elseif style == "checkbox" then
   1.107 +      if args.nil_allowed then
   1.108 +        error("Checkboxes do not support nil values.")
   1.109 +      end
   1.110 +      local attr = table.new(args.attr)
   1.111 +      attr.type  = "checkbox"
   1.112 +      attr.name  = args.html_name
   1.113 +      attr.value = "1"
   1.114 +      if value then
   1.115 +        attr.checked = "checked"
   1.116 +      else
   1.117 +        attr.checked = nil
   1.118 +      end
   1.119 +      ui.tag{ tag = "input", attr = attr }
   1.120 +      ui.hidden_field{
   1.121 +        name = args.html_name .. "__format",
   1.122 +        value = encode.format_info(
   1.123 +          "boolean",
   1.124 +          { true_as = "1", false_as = "" }
   1.125 +        )
   1.126 +      }
   1.127 +    else
   1.128 +      error("'style' attribute for ui.field.boolean{...} must be set to \"radio\", \"checkbox\" or nil.")
   1.129 +    end
   1.130 +  end)
   1.131 +end

Impressum / About Us