/* * Created by PyramIDE. * User: フランスのトトロ * Date: 07/12/2014 * Time: 16:49 * © Calexium */ // ************************************************************************************* // *** Generic form // ************************************************************************************* read tools/basis.anubis read system/string.anubis read calexium_lib/web/CXM_common.anubis read calexium_lib/web/CXM_web_arg_utils.anubis read calexium_lib/web/CXM_making_a_web_site.anubis read calexium_lib/web/CXM_html_tooltip.anubis read calexium_lib/web/CXM_widgets.anubis public type Mandatory: // used to mark fields as mandatory. mandatory, non_mandatory. public type Width: small, narrow, wide, custom(Int). public type FormFieldWidth: auto, custom(Int). public type CXM_Form_Field: no_field, // empty field //--- title field --------------------------------------------------------------------- title( String text ), //--- message field ------------------------------------------------------------------- message( String text ), //--- text input field ---------------------------------------------------------------- input( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, InitialValue init_value, Width width, Mandatory mandatory ), input_readonly( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, InitialValue init_value, Width width ), //--- text area field ----------------------------------------------------------------- text_area( List(TextAreaOption) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, InitialValue init_value, Width width, Int height, Mandatory mandatory ), //--- password input field ------------------------------------------------------------ password_input( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, InitialValue init_value, Width width, Mandatory mandatory), //--- hidden field ------------------------------------------------------------ hidden( HTML_Id id, WebArgName web_arg_name, WebArgValue init_value ), //--- explanation field --------------------------------------------------------------- explain( String text ), //--- selector field ------------------------------------------------------------------ selector( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, Int size, List(String) items, Maybe(InitialValue) selected, Mandatory mandatory ), selector_c( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, Int size, List((List(CoreAttrs), WebArgValue, String)) items, Maybe(InitialValue) selected, Mandatory mandatory ), selector_hide_show (List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, Int size, List((List(CoreAttrs), WebArgValue, String, HTML_Partial_Content)) items, Maybe(InitialValue) selected, Mandatory mandatory), //--- checkbox field ------------------------------------------------------------------ checkbox (List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, WebArgValue web_arg_value, Bool checked, Mandatory mandatory), // the same one, but with the tag on the right of the checkbox checkboxr( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, WebArgValue web_arg_value, Bool checked, Mandatory mandatory ), //checkbox_hide_show checkbox_hide_show( List(CoreAttrs) options, HTML_Label label, WebArgName web_arg_name, WebArgValue web_arg_value, Bool checked, List(CXM_Form_Field) fields ), //--- radio-button field -------------------------------------------------------------- radio_button( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, WebArgValue web_arg_value, Bool checked, Mandatory mandatory ), //radio_button_hide_show radio_button_hide_show( List(CoreAttrs) options, HTML_Label label, WebArgName web_arg_name, WebArgValue web_arg_value, Bool checked, List(CXM_Form_Field) fields ), // the same one, but with the tag on the right of the radio_button radio_buttonr (List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, WebArgValue web_arg_value, Bool checked, Mandatory mandatory), radio_button_list( List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName web_arg_name, List((WebArgValue, String)) web_arg_value, Maybe(InitialValue) selected, Mandatory mandatory, Bool is_wide), // //--- fields table -------------------------------------------------------------------- // fields_table (String tag, // List(FormField) fields), // // //--- fields line --------------------------------------------------------------------- // fields_line (String tag, // List(FormField) fields), one_line_fields (List(CXM_Form_Field) fields), // // //--- preview field ------------------------------------------------------------------- // preview (String html_text), //--- upload field ------------------------------------------------------------------- upload (List(CoreAttrs) options, HTML_Label label, HTML_Id id, WebArgName name, Width width, Mandatory mandatory), raw_in_form (HTML_In_Form html), label_for ( String label, HTML_Id id, HTML_In_Form input), div (List(CoreAttrs), HTML_In_Form content), partial (HTML_Partial_Content p_content), br, // input_with_help (CXM_Form_Field field, // Maybe(String) help_title, // String help_text, // Int width), fieldset (List(CoreAttrs) options, String legend, List(CXM_Form_Field) fields), blank_space, blank_space (HtmlClass class), //--- submit button ------------------------------------------------------------------- submit (List(CoreAttrs) options, HtmlClass class, String action_name, Maybe(String) label, String button_text, List((String,String)) extra_operands), empty. // ////////////////////////////////////////////// // Alternate constructors: public define CXM_Form_Field fieldset ( HTML_Id fieldSetId, List(CXM_Form_Field) fields ) = fieldset([id(fieldSetId.id)], "", fields). public define CXM_Form_Field fieldset ( List(CXM_Form_Field) fields ) = fieldset([], "", fields). public define CXM_Form_Field fieldset ( String legend, List(CXM_Form_Field) fields ) = fieldset([], legend, fields). public define CXM_Form_Field input(WebArgName n, InitialValue init_value, Width width) = input([], no_label, html_Id(n.name), n, init_value, width, non_mandatory). public define CXM_Form_Field input(HTML_Label label, WebArgName n, InitialValue init_value, Width width) = input([], label, html_Id(n.name), n, init_value, width, non_mandatory). public define CXM_Form_Field input(HTML_Label label, WebArgName n, InitialValue init_value, Width width, Mandatory mand) = input([], label, html_Id(n.name), n, init_value, width, mand). public define CXM_Form_Field input_readonly(HTML_Label label, WebArgName n, InitialValue init_value, Width width) = input_readonly([], label, html_Id(n.name), n, init_value, width). public define CXM_Form_Field password_input(HTML_Label label, WebArgName n, InitialValue init_value, Width width, Mandatory mand) = password_input([], label, html_Id(n.name), n, init_value, width, mand). public define CXM_Form_Field checkbox(HTML_Label label, WebArgName n, WebArgValue value, Bool checked, Mandatory mand) = checkbox([], label, html_Id(n.name), n, value, checked, mand). public define CXM_Form_Field checkboxr(HTML_Label label, WebArgName n, WebArgValue value, Bool checked, Mandatory mand) = checkboxr([], label, html_Id(n.name), n, value, checked, mand). public define CXM_Form_Field radio_button(HTML_Label label, WebArgName n, WebArgValue value, Bool checked, Mandatory mand) = radio_button([], label, html_Id(n.name), n, value, checked, mand). public define CXM_Form_Field radio_buttonr(HTML_Label label, WebArgName n, WebArgValue value, Bool checked, Mandatory mand) = radio_buttonr([], label, html_Id(n.name), n, value, checked, mand). public define CXM_Form_Field submit(String action_name, HtmlClass class, Maybe(String) label, String button_text, List((String,String)) extra_operands) = submit([], class, action_name, label, button_text, extra_operands). public define CXM_Form_Field submit(String action_name, Maybe(String) label, String button_text, List((String,String)) extra_operands) = submit([], htmlClass(""), action_name, label, button_text, extra_operands). public define CXM_Form_Field upload(HTML_Label label, HTML_Id id, WebArgName name, Width width) = upload([], label, id, name, width, non_mandatory). public define CXM_Form_Field upload(HTML_Label label, HTML_Id id, WebArgName name, Width width, Mandatory mand) = upload([], label, id, name, width, mand). public define CXM_Form_Field input_with_help (CXM_Form_Field field, String help_text) = input_with_help (field, failure, help_text, 250). public define CXM_Form_Field input_with_help (CXM_Form_Field field, String help_text, Int width) = input_with_help (field, failure, help_text, width). public define CXM_Form_Field ip_input ( String label, HTML_Id id, WebArgName wan_prefix, String ip )= label_for( label, id, ip_input(id, wan_prefix, ip) ). public define CXM_Form_Field partial_ip_input ( String label, HTML_Id id, WebArgName wan_prefix, List(String) ip_fixed_part, String ip )= label_for( label, id, partial_ip_input(id, wan_prefix, ip_fixed_part, ip) ). public define CXM_Form_Field hidden ( WebArgName web_arg_name, WebArgValue value )= hidden(html_Id(""), web_arg_name, value). //--- explanation field --------------------- // ////////////////////////////////////////////////////////// define (List((List(CoreAttrs), WebArgValue, String)), List(HTML_Partial_Content)) construct_selector_hide_show ( String div_id, List((List(CoreAttrs), WebArgValue, String, HTML_Partial_Content)) items, List((List(CoreAttrs), WebArgValue, String)) so_far_1, List(HTML_Partial_Content) so_far_2, Int n ) = if items is { [] then (so_far_1, so_far_2), [h . t] then since h is (cores, wav, label, content), with linked_element = div_id + n, construct_selector_hide_show(div_id, t, [([data("linked_element", linked_element) . cores], wav, label) . so_far_1], [ partial_content([], div([id(linked_element), style("display: none")], content)) . so_far_2 ], n + 1) }. define (List((List(CoreAttrs), WebArgValue, String)), List(HTML_Partial_Content)) construct_selector_hide_show ( String div_id, List((List(CoreAttrs), WebArgValue, String, HTML_Partial_Content)) items ) = construct_selector_hide_show(div_id, items, [], [], 0). public define HTML_In_Form format_CXM_Form_Field ( CXM_Form_Field ff, List(Web_arg) lwa ) = with star = (HTML_Label l, Mandatory m) |-> (HTML_Label) if m is { mandatory then if l is { no_label then l, label(str, help) then label(str + "*", help), } non_mandatory then l }, pixels = (Width width) |-> if width is { small then 10, narrow then 50, wide then 70, custom(n) then n }, if ff is { no_field then empty, title(t) then literal("

" + t + "

"), message(t) then literal(t), input(options, _label, id, wan, default, w, mand) then text_input([class("in") . options], star(_label, mand), id, wan, init(get_String(lwa, wan.name, default.value)), pixels(w)), input_readonly(options, _label, id, name, init, w) then text_input_ro([class("in") . options], _label, id, name, init, pixels(w)), text_area(options, _label, id, wan, default, w, height, mand) then text_area([input_attrs([class("in")]) . options], star(_label, mand), id, wan, init(get_String(lwa, wan.name, default.value)), pixels(w), height), password_input(options, _label, id, name, init, w, mand) then password_input([class("in") . options], star(_label, mand), id, name, init, pixels(w)), hidden(id, name, init) then hidden(id, name, init), explain(t) then div([class("sub")], literal(t)), selector(options, _label, id, wan, size, items, selected, mand) then if selected is { failure then selector([class("in") . options], star(_label, mand), id, wan, size, items), success(default) then selector([class("in") . options], star(_label, mand), id, wan, size, items, init(get_String(lwa, wan.name, default.value))), }, selector_c(options, _label, id, wan, size, items, selected, mand) then if selected is { failure then selector_c([class("in") . options], star(_label, mand), id, wan, size, items), success(default) then selector_c([class("in") . options], star(_label, mand), id, wan, size, items, init(get_String(lwa, wan.name, default.value))), }, selector_hide_show(options, _label, id, wan, size, items_with_partial, selected, mand) then with div_id = generate_random_string(15), since construct_selector_hide_show(div_id, items_with_partial) is (items, partial_content_list), sequence([ selector_c([class("in"), class("cxm_selector_hide_show"), event(onchange, "select_hide_show('" + div_id + "', this);")], star(_label, mand), id, wan, size, reverse(items), if selected is { failure then init(""), success(default) then init(get_String(lwa, wan.name, default.value)) } ), partial(partial_content(partial_content_list)) ]), checkbox(options, _label, id, wan, val, checked, mand) then check_box([class("in") . options], star(_label, mand), id, wan, val, get_Bool(lwa, wan.name, checked)), checkboxr(options, _label, id, wan, val, checked, mand) then check_box_r([class("in") . options], star(_label, mand), id, wan, val, get_Bool(lwa, wan.name, checked)), checkbox_hide_show(options, _label, wan, val, checked, fields) then with div_id = generate_random_string(15), sequence([ check_box([data("show_id", div_id), class("in"), class("cxm_checkbox_hide_show"), event(onclick, "checked_hide_show_element('"+div_id+"', this);")], _label, html_Id("checkbox_"+div_id), wan, val, get_Bool(lwa, wan.name, checked)), div([id(div_id), style("display:"+if checked then "block" else "none")], sequence(map((CXM_Form_Field ff2) |-> div([class("form_field")], format_CXM_Form_Field(ff2, lwa)),fields))) ]), radio_button(options, _label, id, wan, val, checked, mand) then radio_button([class("in"), event(onclick, "radio_hide_show_group('"+wan.name+"', this);") . options], star(_label, mand), id, wan, val, get_Bool(lwa, wan.name, checked)), radio_button_hide_show(options, _label, wan, val, checked, fields) then with div_id = generate_random_string(15), sequence([ radio_button([data("show_id", div_id), class("in"), class("cxm_radio_hide_show"), event(onclick, "radio_hide_show_group('"+wan.name+"', this);")], _label, html_Id("radio_button_hide_show_"+div_id), wan, val, get_Bool(lwa, wan.name, checked)), div([id(div_id), style("display:"+if checked then "block" else "none")], sequence(map((CXM_Form_Field ff2) |-> div([class("form_field")], format_CXM_Form_Field(ff2, lwa)),fields))) ]), radio_buttonr(options, _label, id, wan, val, checked, mand) then radio_button_r([class("in") . options], star(_label, mand), id, wan, val, get_Bool(lwa, wan.name, checked)), radio_button_list (options, _label, id, wan, items, selected, mand, wide) then with new_selected = if get_String(lwa, wan.name) is { failure then selected, success(value) then success(init(value)) }, sequence([ //literal(""), div( [class("radiolist")], sequence( with i = var((Int)-1), itemclass = if wide then "radiolistitem_wide" else "radiolistitem", map( ((WebArgValue, String) item) |-> i <- *i + 1; if item is (wav, item_label) then with _id = if id is html_Id(the_id) then html_Id(the_id + "_radio_" + *i), div([class(itemclass)], radio_button_r(options, label(item_label), _id, wan, wav, if new_selected is success(v) then wav.value = v.value else false)), items)) ), ]), one_line_fields(fields) then table([nude],[row([], map((CXM_Form_Field ff2) |-> cell([],format_CXM_Form_Field(ff2, lwa)),fields))]) , upload(options, _label, id, name, w, mand) then file_upload([class("in") . options], star(_label, mand), id, name, pixels(w)), raw_in_form(html) then html, // div([class("in")], html), label_for(label, id, input) then sequence([ if id is html_Id(_id) then literal(""), input ]), div(options, e) then div(options, e), partial(p_content) then partial(p_content), br then br, // input_with_help(input, help_title, help_text, width) then // sequence([ // format_CXM_Form_Field(input, lwa, html_tooltip(force_String(help_title), help_text, width)), // ]), fieldset(options, legend, fields) then sequence([ literal(""), literal(if length(legend) > 0 then "" + legend + "" else ""), sequence( map((CXM_Form_Field ff) |-> div([class("form_field")], format_CXM_Form_Field(ff, lwa)), fields) ), literal("") ]), blank_space then div([class("blank_space")], literal(" ")), blank_space(htmlClass) then div([class(htmlClass.class)], literal(" ")), submit(attrs, htmlClass, action_name, Maybe(String) mb_label, button_text, extra_operands) then actioner( same, // TODO change this to allow external URL if mb_label is { failure then same, success(n) then same(n) }, submit(attrs + [class(htmlClass.class)], button_text), action_name, extra_operands ), empty then literal("") }. public define HTML_In_Form format_CXM_Form_Field ( List(CXM_Form_Field) fields, List(Web_arg) lwa )= sequence( map((CXM_Form_Field ff) |-> div([class("form_field aligned")], format_CXM_Form_Field(ff, lwa)), fields) ). public define HTML_In_Form fieldset ( List(Web_arg) lwa, List(CXM_Form_Field) fields, )= sequence( map((CXM_Form_Field ff) |-> div([class("form_field aligned")], format_CXM_Form_Field(ff, lwa)), fields) ). public define HTML_In_Form fieldset ( List(Web_arg) lwa, CXM_Form_Field field, )= div([class("form_field aligned")], format_CXM_Form_Field(field, lwa)). public define HTML_In_Form fieldset ( List(CXM_Form_Field) fields )= sequence( map((CXM_Form_Field ff) |-> div([class("form_field aligned")], format_CXM_Form_Field(ff, [])), fields) ). public define HTML_Off_Form cxm_form ( HTML_Id form_Id, List(CoreAttrs) options, String action_name, List((String,String)) extra_ops, List(Web_arg) lwa, List(CXM_Form_Field) fields ) = form( form_Id, [class("cxm_form") . options], action_name, extra_ops, format_CXM_Form_Field(fields, lwa) ). public define HTML_Off_Form cxm_form ( String form_name, List(CoreAttrs) options, String action_name, List((String,String)) extra_ops, List(Web_arg) lwa, List(CXM_Form_Field) form_fields )= cxm_form(html_Id(form_name), options, action_name, extra_ops, lwa, form_fields). public define HTML_In_Form cxm_fieldset ( HTML_Id fieldSetId, List(Web_arg) lwa, List(CXM_Form_Field) fields ) = cxm_fieldset([id(fieldSetId.id), class("aligned")], "", lwa, fields). public define HTML_Off_Form cxm_form ( String form_id, List(CoreAttrs) options, List(HTML_In_Form) fields ) = cxm_form(html_Id(form_id), options, "", [], [], map((HTML_In_Form field) |-> raw_in_form(field), fields) ). public define HTML_Off_Form cxm_form ( String form_id, List(CoreAttrs) options, List(Web_arg) lwa, List(CXM_Form_Field) fields ) = cxm_form(html_Id(form_id), options, "", [], lwa, fields). public define HTML_Off_Form cxm_form ( String form_id, List(HTML_In_Form) fields ) = cxm_form(html_Id(form_id), [], "", [], [], map((HTML_In_Form field) |-> raw_in_form(field), fields) ). public define HTML_Off_Form cxm_form ( String form_id, List(Web_arg) lwa, List(CXM_Form_Field) fields ) = cxm_form(form_id, [], lwa, fields). public define HTML_Off_Form cxm_form ( String form_id, List(CXM_Form_Field) fields ) = cxm_form(html_Id(form_id), [], "", [], [], fields).