*Project* The Anubis Project *Title* *Copyright* Copyright (c) Alain Prouté 2005. *Author* Alain Prouté In this file we rationalize the construction of forms. read web/CXM_making_a_web_site.anubis public type Mandatory: // used to mark fields as mandatory. mandatory, non_mandatory. public type Width: small, narrow, wide, custom(Int32). public type FormFieldWidth: auto, custom(Int32). Sorts of fields that you can put in a form: public type FormField: //--- title field --------------------------------------------------------------------- title (String text), title (Int32 text_size, String text), title_f (List(Text_Option) -> HTML_In_Form), //--- message field ------------------------------------------------------------------- message (Result(String,String) msg), message_f (Result(List(Text_Option) -> HTML_In_Form,List(Text_Option) -> HTML_In_Form)), //--- text input field ---------------------------------------------------------------- input (String web_arg_name, String tag, Width width, String init_value, Mandatory mandatory), input (String web_arg_name, Width width, String init_value), input_f (String web_arg_name, List(Text_Option) -> HTML_In_Form tag, Width width, String init_value, Mandatory mandatory), //--- password input field ------------------------------------------------------------ password_input (String web_arg_name, String tag, Mandatory mandatory), password_input_f (String web_arg_name, List(Text_Option) -> HTML_In_Form tag, Mandatory mandatory), //--- explanation field --------------------------------------------------------------- explain (String text), explain (String text, FormFieldWidth width), explain_f (List(Text_Option) -> HTML_In_Form), //--- selector field ------------------------------------------------------------------ selector (String web_arg_name, String tag, List(String) items, Maybe(String) selected, Mandatory mandatory), selector_f (String web_arg_name, List(Text_Option) -> HTML_In_Form, List(String) items, Maybe(String) selected, Mandatory mandatory), selector_c (String web_arg_name, String tag, List((String,String)) items, Maybe(String) selected, Mandatory mandatory), //--- checkbox field ------------------------------------------------------------------ checkbox (String web_arg_name, String tag, Bool checked, Mandatory mandatory), // the same one, but with the tag on the right of the checkbox checkboxr (String web_arg_name, String tag, Bool checked, Mandatory mandatory), checkbox_f (String web_arg_name, List(Text_Option) -> HTML_In_Form, Bool checked, Mandatory mandatory), //--- radio-button field -------------------------------------------------------------- radio_button (String web_arg_name, String web_arg_value, String tag, Bool checked, Mandatory mandatory), // the same one, but with the tag on the right of the radio_button radio_buttonr (String web_arg_name, String web_arg_value, String tag, Bool checked, Mandatory mandatory), //--- text area field ----------------------------------------------------------------- text_area (String web_arg_name, String initial_text), text_area (String web_arg_name, String tag, String initial_text), text_area (String web_arg_name, String tag, String initial_text, Int32 width, Int32 height), //--- fields table -------------------------------------------------------------------- fields_table (String tag, List(FormField) fields), //--- fields line --------------------------------------------------------------------- fields_line (String tag, List(FormField) fields), fields_line (List(FormField) fields), //--- preview field ------------------------------------------------------------------- preview (String html_text), //--- submit button ------------------------------------------------------------------- submit (String action_name, Maybe(String) label, String button_text, List((String,String)) extra_operands). Convenience functions: public define FormField title(List(Text_Option) -> HTML_In_Form f) = title_f(f). public define FormField message(Result(List(Text_Option) -> HTML_In_Form,List(Text_Option) -> HTML_In_Form) f) = message_f(f). public define FormField input(String web_arg_name, List(Text_Option) -> HTML_In_Form tag, Width width, String init_value, Mandatory mandatory) = input_f(web_arg_name,tag,width,init_value,mandatory). public define FormField password_input(String web_arg_name, List(Text_Option) -> HTML_In_Form tag, Mandatory mandatory) = password_input_f(web_arg_name,tag,mandatory). public define FormField explain(List(Text_Option) -> HTML_In_Form f) = explain_f(f). public define FormField selector(String web_arg_name, List(Text_Option) -> HTML_In_Form f, List(String) items, Maybe(String) selected, Mandatory mandatory) = selector_f(web_arg_name,f,items,selected,mandatory). public define FormField checkbox(String web_arg_name, List(Text_Option) -> HTML_In_Form f, Bool checked, Mandatory mandatory) = checkbox_f(web_arg_name,f,checked,mandatory). Make the form itself with: public define HTML_Off_Form generic_form ( String form_name, RGB background_color, Int32 width, List(FormField) fields ). --- That's all for the public part ! -------------------------------------------------- define HTML_Row(HTML_In_Form) format_form_field ( FormField ff ) = with star = (Mandatory m) |-> (HTML_In_Form)text([color(rgb(255,0,0)),size(12)], if m is { mandatory then "*", non_mandatory then "" }), row( if ff is { title(t) then (List(HTML_Cell(HTML_In_Form))) [ cell([columns(3),h_center],text([size(16),bold,color(rgb(0,0,0))],t)) ], title(s,t) then (List(HTML_Cell(HTML_In_Form))) [ cell([columns(3),h_center],text([size(s),bold,color(rgb(0,0,0))],t)) ], title_f(t) then (List(HTML_Cell(HTML_In_Form))) [ cell([columns(3),h_center],t([size(16),bold,color(rgb(0,0,0))])) ], message(r) then (List(HTML_Cell(HTML_In_Form))) if r is { error(msg) then [cell([columns(3),h_center], text([size(10),color(rgb(240,0,0))],msg))] ok(msg) then [cell([columns(3),h_center], text([size(10),color(rgb(0,150,0))],msg))] }, message_f(r) then (List(HTML_Cell(HTML_In_Form))) if r is { error(msg) then [cell([columns(3),h_center], msg([size(10),color(rgb(240,0,0))]))] ok(msg) then [cell([columns(3),h_center], msg([size(10),color(rgb(0,150,0))]))] }, input(wan,tag,w,init,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], text([size(10)],tag)), cell([width(7) ], star(mand)), cell([left ], text_input("","",wan,init,if w is { small then 10, narrow then 30, wide then 70, custom(n) then n })) ], input(wan,w,init) then (List(HTML_Cell(HTML_In_Form))) [ cell([left,columns(3) ], text_input("","",wan,init,if w is { small then 10, narrow then 30, wide then 70, custom(n) then n })) ], input_f(wan,tag,w,init,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], tag([size(10)])), cell([width(7) ], star(mand)), cell([left ], text_input("","",wan,init,if w is { small then 15, narrow then 30, wide then 70, custom(n) then n })) ], password_input(wan,tag,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], text([size(10)],tag)), cell([width(7) ], star(mand)), cell([left ], password_input("","",wan,30)) ], password_input_f(wan,tag,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], tag([size(10)])), cell([width(7) ], star(mand)), cell([left ], password_input("","",wan,30)) ], explain(t) then (List(HTML_Cell(HTML_In_Form))) [ cell([columns(3),h_center], table([nude],[row(cell([width(500)], paragraph([/*justified,*/size(10),color(rgb(0,100,0))],t)))])) ], explain(t,w) then (List(HTML_Cell(HTML_In_Form))) [ cell([columns(3),h_center], table([nude],[row(cell( if w is { auto then [], custom(i) then [width(i)] }, paragraph([/*justified,*/size(10),color(rgb(0,100,0))],t)))])) ], explain_f(t) then (List(HTML_Cell(HTML_In_Form))) [ cell([columns(3),h_center], table([nude],[row(cell([width(500)], t([/*justified,*/size(10),color(rgb(0,100,0))])))])) ], selector(wan,tag,items,selected,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], text([size(10)],tag)), cell([width(7)], star(mand)), cell([left ], if selected is { failure then selector(wan,1,items) success(sel) then selector(wan,1,items,sel) }) ], selector_f(wan,tag,items,selected,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], tag([size(10)])), cell([width(7)], star(mand)), cell([left ], if selected is { failure then selector(wan,1,items) success(sel) then selector(wan,1,items,sel) }) ], selector_c(wan,tag,items,selected,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], text([size(10)],tag)), cell([width(7)], star(mand)), cell([left ], if selected is { failure then selector_c(wan,1,items) success(sel) then selector_c(wan,1,items,sel) }) ], checkbox(wan,tag,checked,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], text([size(10)],tag)), cell([width(7)], star(mand)), cell([left ], check_box("","",wan,checked)) ], checkboxr(wan,tag,checked,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], check_box("","",wan,checked)), cell([width(7)], star(mand)), cell([left ], text([size(10)],tag)) ], checkbox_f(wan,tag,checked,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], tag([size(10)])), cell([width(7)], star(mand)), cell([left ], check_box("","",wan,checked)) ], radio_button(wan,wav,tag,checked,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], text([size(10)],tag)), cell([width(7)], star(mand)), cell([left ], radio_button("","",wan,wav,checked)) ], radio_buttonr(wan,wav,tag,checked,mand) then (List(HTML_Cell(HTML_In_Form))) [ cell([right ], radio_button("","",wan,wav,checked)), cell([width(7)], star(mand)), cell([left ], text([size(10)],tag)) ], text_area(wan,tx) then (List(HTML_Cell(HTML_In_Form))) [ cell([h_center,columns(3)],text_area([wrap_lines],wan,tx,75,10)) ], text_area(wan,tag,tx) then (List(HTML_Cell(HTML_In_Form))) [ cell([right,top], text([size(10)],tag)), cell([width(7)], text([],"")), cell([h_center],text_area([wrap_lines],wan,tx,75,10)) ], text_area(wan,tag,tx,w,h) then (List(HTML_Cell(HTML_In_Form))) [ cell([right,top], text([size(10)],tag)), cell([width(7)], text([],"")), cell([h_center],text_area([wrap_lines],wan,tx,w,h)) ], fields_table(tag,fields) then (List(HTML_Cell(HTML_In_Form))) [ cell([right,top], text([size(10)],tag)), cell([width(7)], text([],"")), cell([left,top], table([],map((FormField ff) |-> format_form_field(ff),fields))) ], fields_line(tag,fields) then (List(HTML_Cell(HTML_In_Form))) [ cell([right,top], text([size(10)],tag)), cell([width(7)], text([],"")), cell([left,top], table([nude],[row([], //border(0,0,3,rgb(0,0,0)) map((FormField ff) |-> cell([top],table([nude],[format_form_field(ff)])),fields))])) ], fields_line(fields) then (List(HTML_Cell(HTML_In_Form))) [ cell([left,top,columns(3)], table([nude],[row([], map((FormField ff) |-> cell([top],table([nude],[format_form_field(ff)])),fields))])) ], preview(html_text) then (List(HTML_Cell(HTML_In_Form))) [ cell([top,left,columns(3),background_color(rgb(255,255,255))], table([border(0,8,0,rgb(0,0,0))], [row(cell([left,top,height(200)],literal(html_text)))])) ], submit(action_name,mb_label,button_text,extra_operands) then (List(HTML_Cell(HTML_In_Form))) [ cell([columns(3),right],actioner(same, if mb_label is { failure then same, success(n) then same(n) }, link(button_text), action_name, extra_operands)) ] }). public define HTML_Off_Form generic_form ( String form_name, RGB bg_color, Int32 w, List(FormField) fields ) = table([border(0,0,5,bg_color),percentage_width(100), background_color(bg_color)],[row(cell([h_center], form(form_name,[],table([border(0,2,0,bg_color)], map(format_form_field,fields)))))]).