webmcp
view framework/env/ui/container.lua @ 485:9b7a391fd461
Updated documentation on passing moonbridge_io.poll to main handlers
| author | jbe | 
|---|---|
| date | Sun Jun 11 22:31:17 2017 +0200 (2017-06-11) | 
| parents | a2ff42a66212 | 
| children | efc700d98c17 | 
 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, value_container_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     value_container_attr = auto_args.value_container_attr
    30     if label and auto_args.attr and auto_args.attr.id then
    31       label_attr = table.new(label_attr)
    32       label_attr["for"] = auto_args.attr.id
    33     end
    34   else
    35     attr        = args.attr
    36     label       = args.label
    37     label_attr  = args.label_attr or {}
    38     legend      = args.legend
    39     legend_attr = args.legend_attr
    40     content     = content
    41     if args.label_for then
    42       label_attr["for"] = args.label_for
    43     end
    44   end
    45   local content = args.content
    46   if label and not legend then
    47     return ui.tag {
    48       tag     = "div",
    49       attr    = attr,
    50       content = function()
    51         if not args.content_first then
    52           ui.tag{ tag = "label", attr = label_attr, content = label }
    53           slot.put(" ")
    54         end
    55         if type(content) == "function" then
    56           if value_container_attr then
    57             ui.container{
    58               attr = value_container_attr,
    59               content = content
    60             }
    61           else
    62             content()
    63           end
    64         elseif content then
    65           slot.put(encode.html(content))
    66         end
    67         if args.content_first then
    68           slot.put(" ")
    69           ui.tag{ tag = "label", attr = label_attr, content = label }
    70         end
    71       end
    72     }
    73   elseif legend and not label then
    74     return ui.tag {
    75       tag     = "fieldset",
    76       attr    = attr,
    77       content = function()
    78         if not args.content_first then
    79           ui.tag{ tag = "legend", attr = legend_attr, content = legend }
    80           slot.put(" ")
    81         end
    82         if type(content) == "function" then
    83           content()
    84         elseif content then
    85           slot.put(encode.html(content))
    86         end
    87         if args.content_first then
    88           slot.put(" ")
    89           ui.tag{ tag = "legend", attr = legend_attr, content = legend }
    90         end
    91       end
    92     }
    93   elseif legend and label then
    94     error("ui.container{...} may either get a label or a legend.")
    95   else
    96     return ui.tag{ tag = "div", attr = attr, content = content }
    97   end
    98 end
