menu.anubis 7.05 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 18/02/2017
 * Time: 01:52
 * © David RENÉ
 */

transmit tools/basis.anubis
read system/string.anubis

read xlib/web/making_a_web_site.anubis
transmit xlib/web/widgets/icon.anubis

public type Menu_Item:
  menu_item(        //MENU entry
    String          text,           //text to show
    Icon            icon,
    List(String)    ids,            //it's also use as icon prefix
    List(String)    classes,        //additional classes if need (for disabling entry, change background color, etc.)
    WEB_Action_Name action,         //action (url to apply when click
    List((String, String))  extra,    //extra web arguments to send when click
    Actioner_Target         target,    //target if need
    List(Menu_Item) sub_menu
    //Bool            selected
  ),
//  menu_title(     //TITLE entry hence no selectable
//    String        text,       //text to show  
//    List(String)  ids,        //it's also use as icon prefix
//    List(String)  classes     //additional classes if need (change background color, font size, etc.)
//  ),
  menu_item_content(
    HTML_Partial_Content content
  ),
  menu_separator
.

public type Menu:
  no_menu,
  menu(
    List(Menu_Item)  items
    //String            selected
  )
.

public define Menu_Item
  menu_item
  (
    String                  text,
    WEB_Action_Name         action,
    List((String, String))  extra_op
  )=
  menu_item(text, no_icon, [], [], action, extra_op, same, [])
.

public define Menu_Item
  menu_item
  (
    String                  text,
    Icon                    icon,
    WEB_Action_Name         action,
    List((String, String))  extra_op
  )=
  menu_item(text, icon, [], [], action, extra_op, same, [])
.

public define Menu_Item
  menu_item
  (
    String                  text,
    WEB_Action_Name         action
  )=
  menu_item(text, no_icon, [], [], action, [], same, [])
.

public define Menu_Item
  menu_item
  (
    String                  text,
    Icon                    icon,    
    WEB_Action_Name         action
  )=
  menu_item(text, icon, [], [], action, [], same, [])
.

public define Menu_Item
  menu_item
  (
    String            text,
    WEB_Action_Name   action,
    List(Menu_Item)   sub_menu
  )=
  menu_item(text, no_icon, [], [], action, [], same, sub_menu)
.

public define Menu_Item
  menu_item
  (
    String            text,
    Icon              icon,     
    WEB_Action_Name   action,
    List(Menu_Item)   sub_menu
  )=
  menu_item(text, icon, [], [], action, [], same, sub_menu)
.

//public define Nav_Menu_Entry
//  nav_menu_entry
//  (
//    String          entry_id,       //it's also use as icon prefix
//    WEB_Action_Name action,         //action (url to apply when click
//    String          text,           //text to show
//    List((String, String)) extra  //extra web arguments to send when click
//  )=
//  left_menu_entry(entry_id, [], action, text, extra)
//  .
  
//public define Menu_Item
//  menu_title
//  (
//    String  text
//  )=
//  menu_title(text, [], []). 
//  
//public define Menu_Item
//  menu_title
//  ( 
//    String        text,
//    List(String)  title_ids,
//  )=
//  menu_title(text, title_ids, []).
  
define List(HTML_Off_Form)
  generate_menu 
  (
    (String) -> String    _T,         //translator function 
    List(Menu_Item)       items,
    List(HTML_Off_Form)   so_far,
    Int                   level
  )=
  if items is 
  {
    []          then reverse(so_far),  //reverse to be in the right direction
    [_item . t] then
      if _item is
      {
        //menu_item then generate_hk_menu(_T, t, so_far, level),
        //li
        menu_item(_text, _icon, _ids, _classes, _action, _extra, _target, _sub_menu)  then
          with classes = join(" ",[(if level = 0 then "maintab" else ""),
                                   (if is_empty(_sub_menu) then "" else "has_submenu") .
                                   _classes]),
          with current_li = (HTML_Off_Form)li( [class(classes)],
            //if _icon = "" then [] else [class(_icon+"_icon")], 
              sequence([actioner(same, _target, 
                          html(if level = 0 then [class("title")] else [],
                            sequence([
                              //format the icon if need
                              to_HTML(_icon),
                              //format the text if need
                              if _text = "" then empty else span(text(_T(_text))), /*link(help_string(_text, _T))*/
                              //span(1),
                            ])),

                          _action,_extra
                        ),
                              
                        if is_empty(_sub_menu) then
                          empty
                        else
                          (HTML_Off_Form)ul([class("submenu")], generate_menu(_T, _sub_menu, [], level+1))
                       ])),
          generate_menu(_T, t, [current_li . so_far], level),

//        menu_title(text, _ids, _classes)        then
        menu_item_content(_content)             then
//          with current_li = (HTML_Off_Form)li([class("item "+_class+"_icon")],actioner(same,same, link(_T(_text)),_action,_extra)),
          with current_li = (HTML_Off_Form)li([],partial(_content)),
          generate_menu(_T, t, [current_li . so_far], level),
        menu_separator                          then
          with current_li = (HTML_Off_Form)li([class("cxm_menu_sep")],text("--- SEPARATOR ---")),
          generate_menu(_T, t, [current_li . so_far], level)
          
      }
  }
.
//        //ul
//        
//        hk_menu_list(_class, _action, _text, _extra, _entries)  then
//          with current_li = 
//              (HTML_Off_Form)li([class(_class+"_icon")],
//                              sequence([
////                                actioner(same,same, link(_T(_text)),_action,_extra),
//                                actioner(same,same, link(help_string(_text, _T)),_action,_extra),
//                                text([class(if level = 0 then "dropBottom" else "dropRight")], ""),
//                                (HTML_Off_Form)ul([], sequence(generate_hk_menu(_T, _entries, [], level+1)))])),             
//          generate_hk_menu(_T, t, [current_li . so_far], level)
//
//        }
//  }.

define HTML_Off_Form
  make_menu
  (
    (String) -> String  _T,         //translator function 
    List(Menu_Item)     menu_items,
    String              direction
  )=
    nav([id("nav-"+direction), attr("role","navigation")], [ 
      ul([class("menu")], generate_menu(_T, menu_items, [], 0)),
      span(class("menu-collapse"), to_HTML(awesome("step-backward")))
    ])
.

public define HTML_Partial_Content
  make_menu
  (
    (String) -> String  _T,         //translator function 
    Menu                _menu,
    String              _direction
  )=
  if _menu is 
  {
    no_menu     then partial_content(empty),
    menu(items) then
      partial_content([css(css_file("xlib/css/menu.css"))],
      //partial_content([css(css_file("/css/admin_theme.css"))],
        make_menu(_T, items, _direction))
  }
.