webmcp
diff framework/env/ui/multiselect.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children | 81dfcfd960ed |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/ui/multiselect.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +--[[-- 1.5 +ui.multiselect{ 1.6 + name = name, -- HTML name ('html_name' is NOT a valid argument for this function) 1.7 + container_attr = container_attr, -- extra HTML attributes for the container (div) enclosing field and label 1.8 + attr = attr, -- extra HTML attributes for the field 1.9 + label = label, -- text to be used as label for the input field 1.10 + label_attr = label_attr, -- extra HTML attributes for the label 1.11 + readonly = readonly_flag -- set to true, to force read-only mode 1.12 + foreign_records = foreign_records, -- list of records to be chosen from, or function returning such a list 1.13 + foreign_id = foreign_id, -- name of id field in foreign records 1.14 + foreign_name = foreign_name, -- name of field to be used as name in foreign records 1.15 + selected_ids = selected_ids, -- list of ids of currently selected foreign records 1.16 + connecting_records = connecting_records, -- list of connection entries, determining which foreign records are currently selected 1.17 + own_id = own_id, -- TODO documentation needed 1.18 + own_reference = own_reference, -- name of foreign key field in connecting records, which references the main record 1.19 + foreign_reference = foreign_reference, -- name of foreign key field in connecting records, which references foreign records 1.20 + format_options = format_options -- format options for format.string 1.21 +} 1.22 + 1.23 +This function inserts a select field with possibility of multiple selections in the active slot. This function does not reside within ui.field.*, because multiple selections are not stored within a field of a record, but within a different SQL table. Note that 'html_name' is NOT a valid argument to this function. For description of the generic field helper arguments, see help for ui.autofield{...}. 1.24 + 1.25 +--]]-- 1.26 + 1.27 +function ui.multiselect(args) 1.28 + local style = args.style or "checkbox" 1.29 + local extra_args = { fetch_record = true } 1.30 + if not args.readonly and args.style == "checkbox" then 1.31 + extra_args.disable_label_for_id = true 1.32 + end 1.33 + ui.form_element(args, extra_args, function(args) 1.34 + local foreign_records = args.foreign_records 1.35 + if type(foreign_records) == "function" then 1.36 + foreign_records = foreign_records(args.record) 1.37 + end 1.38 + local connecting_records = args.connecting_records 1.39 + if type(connecting_records) == "function" then 1.40 + connecting_records = connecting_records(args.record) 1.41 + end 1.42 + local select_hash = {} 1.43 + if args.selected_ids then 1.44 + for idx, selected_id in ipairs(args.selected_ids) do 1.45 + select_hash[selected_id] = true 1.46 + end 1.47 + elseif args.own_reference then 1.48 + for idx, connecting_record in ipairs(args.connecting_records) do 1.49 + if connecting_record[args.own_reference] == args.record[args.own_id] then 1.50 + select_hash[connecting_record[args.foreign_reference]] = true 1.51 + end 1.52 + end 1.53 + else 1.54 + for idx, connecting_record in ipairs(args.connecting_records) do 1.55 + select_hash[connecting_record[args.foreign_reference]] = true 1.56 + end 1.57 + end 1.58 + local attr = table.new(args.attr) 1.59 + if not attr.class then 1.60 + attr.class = "ui_multi_selection" 1.61 + end 1.62 + if args.readonly then 1.63 + ui.tag{ 1.64 + tag = "ul", 1.65 + attr = attr, 1.66 + content = function() 1.67 + for idx, record in ipairs(foreign_records) do 1.68 + if select_hash[record[args.foreign_id]] then 1.69 + ui.tag{ 1.70 + tag = "li", 1.71 + content = format.string( 1.72 + record[args.foreign_name], 1.73 + args.format_options 1.74 + ) 1.75 + } 1.76 + end 1.77 + end 1.78 + end 1.79 + } 1.80 + elseif style == "select" then 1.81 + attr.name = args.name 1.82 + attr.multiple = "multiple" 1.83 + ui.tag{ 1.84 + tag = "select", 1.85 + attr = attr, 1.86 + content = function() 1.87 + if args.nil_as then 1.88 + ui.tag{ 1.89 + tag = "option", 1.90 + attr = { value = "" }, 1.91 + content = format.string( 1.92 + args.nil_as, 1.93 + args.format_options 1.94 + ) 1.95 + } 1.96 + end 1.97 + for idx, record in ipairs(foreign_records) do 1.98 + local key = record[args.foreign_id] 1.99 + local selected = select_hash[key] 1.100 + ui.tag{ 1.101 + tag = "option", 1.102 + attr = { 1.103 + value = key, 1.104 + selected = (selected and "selected" or nil) 1.105 + }, 1.106 + content = format.string( 1.107 + record[args.foreign_name], 1.108 + args.format_options 1.109 + ) 1.110 + } 1.111 + end 1.112 + end 1.113 + } 1.114 + elseif style == "checkbox" then 1.115 + attr.type = "checkbox" 1.116 + attr.name = args.name 1.117 + for idx, record in ipairs(foreign_records) do 1.118 + local key = record[args.foreign_id] 1.119 + local selected = select_hash[key] 1.120 + attr.id = ui.create_unique_id() 1.121 + attr.value = key 1.122 + attr.checked = selected and "checked" or nil 1.123 + ui.container{ 1.124 + label = format.string( 1.125 + record[args.foreign_name], 1.126 + args.format_options 1.127 + ), 1.128 + attr = { class = "ui_checkbox_div" }, 1.129 + label_for = attr.id, 1.130 + label_attr = { class = "ui_checkbox_label" }, 1.131 + content_first = true, 1.132 + content = function() 1.133 + ui.tag{ tag = "input", attr = attr } 1.134 + end 1.135 + } 1.136 + end 1.137 + else 1.138 + error("'style' attribute for ui.multiselect{...} must be set to \"select\", \"checkbox\" or nil.") 1.139 + end 1.140 + end) 1.141 +end