webmcp

diff framework/env/ui/field/date.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/date.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,84 @@
     1.4 +--[[--
     1.5 +ui.field.date{
     1.6 +  ...           -- generic ui.field.* arguments, as described for ui.autofield{...}
     1.7 +}
     1.8 +
     1.9 +This function inserts a field for dates in the active slot. If the JavaScript library "gregor.js" has been loaded, a rich input field is used. For description of the generic field helper arguments, see help for ui.autofield{...}.
    1.10 +
    1.11 +--]]--
    1.12 +
    1.13 +function ui.field.date(args)
    1.14 +  ui.form_element(args, {fetch_value = true}, function(args)
    1.15 +    local value_string = format.date(args.value, args.format_options)
    1.16 +    if args.readonly then
    1.17 +      ui.tag{ tag = args.tag, attr = args.attr, content = value_string }
    1.18 +    else
    1.19 +      local fallback_data = slot.use_temporary(function()
    1.20 +        local attr = table.new(args.attr)
    1.21 +        attr.type  = "text"
    1.22 +        attr.name  = args.html_name
    1.23 +        attr.value = value_string
    1.24 +        attr.class = attr.class or "ui_field_date"
    1.25 +        ui.tag{ tag  = "input", attr = attr }
    1.26 +        ui.hidden_field{
    1.27 +          name  = args.html_name .. "__format",
    1.28 +          value = encode.format_info("date", args.format_options)
    1.29 +        }
    1.30 +      end)
    1.31 +      local user_field_id, hidden_field_id
    1.32 +      local helper_data = slot.use_temporary(function()
    1.33 +        local attr = table.new(args.attr)
    1.34 +        user_field_id = attr.id or ui.create_unique_id()
    1.35 +        hidden_field_id = ui.create_unique_id()
    1.36 +        attr.id    = user_field_id
    1.37 +        attr.type  = "text"
    1.38 +        attr.class = attr.class or "ui_field_date"
    1.39 +        ui.tag{ tag = "input", attr = attr }
    1.40 +        local attr = table.new(args.attr)
    1.41 +        attr.id    = hidden_field_id
    1.42 +        attr.type  = "hidden"
    1.43 +        attr.name  = args.html_name
    1.44 +        attr.value = atom.dump(args.value)  -- extra safety for JS failure
    1.45 +        ui.tag{
    1.46 +          tag = "input",
    1.47 +          attr = {
    1.48 +            id   = hidden_field_id,
    1.49 +            type = "hidden",
    1.50 +            name = args.html_name
    1.51 +          }
    1.52 +        }
    1.53 +      end)
    1.54 +      -- TODO: localization
    1.55 +      ui.script{
    1.56 +        noscript = fallback_data,
    1.57 +        type     = "text/javascript",
    1.58 +        content  = function()
    1.59 +          slot.put(
    1.60 +            "if (gregor_addGui == null) document.write(",
    1.61 +            encode.json(fallback_data),
    1.62 +            "); else { document.write(",
    1.63 +            encode.json(helper_data),
    1.64 +            "); gregor_addGui({element_id: ",
    1.65 +            encode.json(user_field_id),
    1.66 +            ", month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], weekday_names: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'], week_numbers: 'left', format: 'DD.MM.YYYY', selected: "
    1.67 +          )
    1.68 +          if (args.value) then
    1.69 +            slot.put(
    1.70 +              "{year: ", tostring(args.value.year),
    1.71 +              ", month: ", tostring(args.value.month),
    1.72 +              ", day: ", tostring(args.value.day),
    1.73 +              "}"
    1.74 +            )
    1.75 +          else
    1.76 +            slot.put("null")
    1.77 +          end
    1.78 +          slot.put(
    1.79 +           ", select_callback: function(date) { document.getElementById(",
    1.80 +           encode.json(hidden_field_id),
    1.81 +           ").value = (date == null) ? '' : date.iso_string; } } ) }"
    1.82 +          )
    1.83 +        end
    1.84 +      }
    1.85 +    end
    1.86 +  end)
    1.87 +end

Impressum / About Us