edit_view_table_form.anubis 4.83 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 09/04/2017
 * Time: 02:37
 * © Calexium 
 */

read hayamiki_lib/types/hayamiki.anubis
read tools/streams.anubis
read columns.anubis

define String
  make_lines
  (
    List(HK_Editor_Table_Line) lines,
    List(HK_Model_Column)         columns,    
    String                        table_name,
    String                        indent
  ).
  
define String
  make_cell_attribute
  (
    HK_Editor_Table_Cell_Attribute attr
  )=
  if attr is
  {
    colspan(n)  then "colspan("+n+")",
    rowspan(n)  then "rowspan("+n+")"
  }
.

define String
  make_cell_attributes
  (
    List(HK_Editor_Table_Cell_Attribute) cell_attrs
  )=
  join(", ", map((HK_Editor_Table_Cell_Attribute attr) |-> make_cell_attribute(attr), cell_attrs))
.

public define String
  make_cell
  (
    HK_Editor_Table_Cell    cell_description,   //
    List(HK_Model_Column)   columns,    
    String                  type_name,
    String                  indent
  )=
  if cell_description is
  {
    empty(attrs)             then  "hk_form_table_cell(["+make_cell_attributes(attrs)+"],\n"+indent+"empty)"
    column(attrs, col_name)  then  
      if get_column_type_from_name(columns, col_name) is 
      {
        failure           then  "hk_form_table_cell(["+make_cell_attributes(attrs)+"],\n"+indent+"label(\"column ["+col_name+"] not found\"))",
        success(col_type) then  
          if get_HK_Form_Entry(col_type, type_name) is
          {
            failure               then  "hk_form_table_cell(["+make_cell_attributes(attrs)+"],\n"+indent+"label(\" form for column ["+col_name+"] not found\"))",
            success(form_string)  then  "hk_form_table_cell(["+make_cell_attributes(attrs)+"],\n"+indent+form_string+")"
          }
      }
    column_label(attrs, col_name)  then  "hk_form_table_cell(["+make_cell_attributes(attrs)+"],\n"+indent+"label(\""+to_upper(col_name)+"\"))",
    label(attrs, label_str)        then  "hk_form_table_cell(["+make_cell_attributes(attrs)+"],\n"+indent+"label(\""+label_str+"\"))"
    table(attrs, lines)            then  
      "hk_form_table_cell_table(["+make_cell_attributes(attrs)+"], [\n"+indent+
        make_lines(lines, columns, type_name, indent+"  ")+
      "\n"+indent+"])"
  }
.

    
    get_column_type_from_name(columns, column_name)
    
public define String
  make_cells
  (
    List(HK_Editor_Table_Cell) cells,
    List(HK_Model_Column)         columns,
    String                        table_name,
    String                        indent
  ) =
  if is_empty(cells) then
    ""
  else
    "\n"+indent+ join(",\n"+indent, map((HK_Editor_Table_Cell cell) |->  make_cell(cell, columns, table_name, indent), cells))
.

  
  if columns is
  {
    []      then   indent+"  "+join(",\n"+indent+"  ", reverse(so_far)),
    [h . t] then
      if get_HK_Form_Entry(h, type_name) is
      { 
        failure         then edit_entries(t, type_name, indent, so_far)
        success(result) then edit_entries(t, type_name, indent, [result . so_far])
      }
  }
.



define String
  make_lines
  (
    List(HK_Editor_Table_Line) lines,
    List(HK_Model_Column)         columns,    
    String                        table_name,
    String                        indent
  )=
  if is_empty(lines) then
    ""
  else
    "\n"+indent+ join(",\n"+indent, map((HK_Editor_Table_Line line) |-> "hk_form_table_line(["+ make_cells(line.cells, columns, table_name, indent+"  ") +"\n"+indent+"])", lines))
.


public define Maybe(One)
/* Generate 

*/
  generate_edit_view_table_form
  (
    Stream              stream,     //stream file where to write
    HK_Table            table,      //
    String              view_name,
    HK_Editor_Table  form_in_table   //edit view to generate
  )=
  since table   is hk_table(table_name, short_name, model, display),
  since model   is hk_model( columns, constraints, _),
  with type_name =  to_upper(table_name, 1), //make the first character to upper case and be an Anubis Type.

  //generate the type of the table that contain all components 
  if write_string(stream, 
   "\n\n"+
   //SQL query
   "define HK_Form\n"+
   "  _get_editable_"+table_name+"_"+view_name+"\n"+
   "  (\n"+
   "    "+type_name+" "+table_name+",\n"+
   "    SQLite3DataBase      db\n"+
   "  )=\n"+
   "  with form_table = hk_form_table( [\n"+
   "    //Attributes of the table\n"+
   "      hidden(\"id\", to_String("+table_name+".id)),\n"+
   "    ],\n"+
   "    //All lines and cells of the table \n"+
   "    ["+

   make_lines(form_in_table.lines, columns, table_name, "      ")+"\n"+
   "    ]),\n\n"+
   "  hk_form_in_table(\""+table_name+"\", \""+view_name+"\", form_table).\n\n"
   ) is                                                           //end of the function
  {
    failure     then  println("can't write generate_VT_view "+type_name); failure,
    success(_)  then  success(unique)
  }.