webmcp
view framework/env/ui/container.lua @ 343:34bf5f7abe0d
Code cleanup in extos.c: use 1.0e9 instead of 0.000000001 to avoid floating-point inaccuracies, added "const" qualifiers to avoid two compiler warnings
| author | jbe | 
|---|---|
| date | Thu Mar 26 01:56:10 2015 +0100 (2015-03-26) | 
| parents | a29c8ffb3f82 | 
| children | a2ff42a66212 | 
 line source
     1 --[[--
     2 ui.container{
     3   auto_args = auto_args,
     4   attr          = attr,           -- HTML attributes for the surrounding div or fieldset
     5   label         = label,          -- text to be used as label
     6   label_for     = label_for,      -- DOM id of element to which the label should refer
     7   label_attr    = label_attr,     -- extra HTML attributes for a label tag
     8   legend        = legend,         -- text to be used as legend
     9   legend_attr   = legend_attr,    -- HTML attributes for a legend tag
    10   content_first = content_first,  -- set to true to place label or legend after the content
    11   content = function()
    12     ...
    13   end
    14 }
    16 This function encloses content in a div element (or a fieldset element, if 'legend' is given). An additional 'label' or 'legend' can be placed before the content or after the content. The argument 'auto_args' is set by other ui helper functions when calling ui.container automatically.
    18 --]]--
    20 function ui.container(args)
    21   local attr, label, label_attr, legend, legend_attr, content
    22   local auto_args = args.auto_args
    23   if auto_args then
    24     attr        = auto_args.container_attr
    25     label       = auto_args.label
    26     label_attr  = auto_args.label_attr
    27     legend      = auto_args.legend
    28     legend_attr = auto_args.legend_attr
    29     if label and auto_args.attr and auto_args.attr.id then
    30       label_attr = table.new(label_attr)
    31       label_attr["for"] = auto_args.attr.id
    32     end
    33   else
    34     attr        = args.attr
    35     label       = args.label
    36     label_attr  = args.label_attr or {}
    37     legend      = args.legend
    38     legend_attr = args.legend_attr
    39     content     = content
    40     if args.label_for then
    41       label_attr["for"] = args.label_for
    42     end
    43   end
    44   local content = args.content
    45   if label and not legend then
    46     return ui.tag {
    47       tag     = "div",
    48       attr    = attr,
    49       content = function()
    50         if not args.content_first then
    51           ui.tag{ tag = "label", attr = label_attr, content = label }
    52           slot.put(" ")
    53         end
    54         if type(content) == "function" then
    55           content()
    56         elseif content then
    57           slot.put(encode.html(content))
    58         end
    59         if args.content_first then
    60           slot.put(" ")
    61           ui.tag{ tag = "label", attr = label_attr, content = label }
    62         end
    63       end
    64     }
    65   elseif legend and not label then
    66     return ui.tag {
    67       tag     = "fieldset",
    68       attr    = attr,
    69       content = function()
    70         if not args.content_first then
    71           ui.tag{ tag = "legend", attr = legend_attr, content = legend }
    72           slot.put(" ")
    73         end
    74         if type(content) == "function" then
    75           content()
    76         elseif content then
    77           slot.put(encode.html(content))
    78         end
    79         if args.content_first then
    80           slot.put(" ")
    81           ui.tag{ tag = "legend", attr = legend_attr, content = legend }
    82         end
    83       end
    84     }
    85   elseif fieldset and label then
    86     error("ui.container{...} may either get a label or a legend.")
    87   else
    88     return ui.tag{ tag = "div", attr = attr, content = content }
    89   end
    90 end
