XL_web_stepper.anubis 9.59 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 24/04/2020
 * Time: 15:12
 * © David RENÉ
 */

transmit xlib/web/CXM_jquery.anubis
transmit xlib/web/types/XL_web_stepper.anubis
transmit xlib/web/controllers_web_site.anubis
read xlib/web/CXM_page_message.anubis
read xlib/web/jQuery/CXM_jquery_button.anubis
read xlib/web_controllers/language/language_management.anubis
read xlib/web/widgets/icons_set.anubis

public define Maybe(WEB_Stepper)
  get_stepper_by_uid
  (
    String            _uid,
    List(WEB_Stepper) steppers
  )=
  if steppers is 
  {
    []      then  
      println("get_stepper_by_uid: stepper uid ["+_uid+"] not found");
      failure,
    [h . t] then  
      if h.uid = _uid then 
        println("get_stepper_by_uid: stepper uid ["+_uid+"] FOUND");
        success(h) 
      else 
        get_stepper_by_uid(_uid, t)
  }
.

public define Maybe(WEB_Stepper)
  get_stepper
  (
    List(Web_arg)     _lwa,
    List(WEB_Stepper) steppers
  )=
  if get_String(_lwa, "stepper_uid") is
  {
    failure              then 
      println("get_stepper: stepper_uid not found");
      failure
    success(stepper_uid) then get_stepper_by_uid(stepper_uid, steppers)
  }
.

public define Maybe(WEB_Step)
  get_current_step
  (
    List(WEB_Step)  steps,
    String          current_step
  )=
  if steps is 
  {
    []      then 
      println("get_current_step ["+current_step+"] not found");
      failure,
    [h . t] then 
      if h.name = current_step then
        success(h)
      else
        if get_current_step(h.childs, current_step) is 
        {
          failure       then get_current_step(t, current_step),
          success(step) then success(step)
        }
  }
.

public define Maybe(WEB_Step)
  get_step
  (
    List(Web_arg)   _lwa,
    List(WEB_Step)  steps,
  )=
  if get_String(_lwa, "step") is
  {
    failure       then failure
    success(step) then get_current_step(steps, step)
  }
.

public define HTML_Partial_Content
  web_stepper_button
  (
    WEB_Step          _step,
    WEB_Step_Session  stp_session,
    (String)->String  _T
  )=
  partial_content(
    div(class("stepper_buttons"), 
      _step.buttons(stp_session, _T)))
.

public define HTML_Partial_Content
  web_stepper_main_view
  (
    WEB_Step          _step,
    WEB_Step_Session  stp_session,
    (String)->String  _T
  )=
  partial_content( 
    div(class("stepper_main_view"),
      _step.view(stp_session, _T)))
.

public define HTML_Partial_Content
  web_stepper_message
  (
    WEB_Step_Session  stp_session,
    (String)->String  _T
  )=
  with page_msg = get_Page_Message(stp_session.session.fields, _T),
  //if there is no message return empty to avoid to have an empty div with padding
  if page_msg = no_message then
    partial_empty
  else
    partial_content(
      div(class("stepper_message"), 
        show_page_message(page_msg)))
.

public define HTML_Partial_Content
  web_stepper_full_description
  (
    WEB_Step          current_step,
    (String)->String  _T
  )=
  if current_step.full_description = "" then
    partial_content(empty)
  else
    partial_content(
      div(class("stepper_full_description"), 
        text(_T(current_step.full_description))))
.

public define Bool
  is_current
  (
    WEB_Step  step,
    WEB_Step  current_step
  )=
  current_step.name = step.name
.

public define Bool
  is_active
  (
    WEB_Step  step,
    WEB_Step  current_step
  )=
  step.index < current_step.index
.

public define HTML_Partial_Content
  web_stepper_crumble
  (
    WEB_Stepper _stepper,
    WEB_Step    _current_step,
    (String) -> String _T
  )=
  partial_content(
    div(class("stepper_crumble"),
      div(class("multi-step"),
        ul(class("multi-step-list"),
        
          map((WEB_Step step) |->
            with active = if is_active(step, _current_step)  then " active"  else "",
                current = if is_current(step, _current_step) then " current" else "",
                with go = if is_active(step, _current_step) then
                            with url = format_web_action_name_to_js(_stepper.web_controller, [("stepper_uid",_stepper.uid), ("step",step.name), ("step_a", "go")]),
                            event(onclick, "stepper_go("+url+");")
                          else 
                            empty,
                            
            li([class("multi-step-item"+active+current), 
                go],
              div(class("item-wrap"),
                p(class("item-title"),
                  text(_T(to_upper(step.name)))
                )
              )
            )
          ,
          _stepper.steps
          )
          
        )
      )
    )
  )
.

public define HTML_Partial_Content
  web_stepper_view
  (
    WEB_Stepper       _stepper,
    WEB_Step          _current_step,
    WEB_Step_Session  stp_session,
    (String)->String  _T
  )=
  println("web_stepper_view _current_step.name ["+_current_step.name+"]");

  partial_content([
    css(css_file("xlib/css/stepper.css")),
    js(js_file("xlib/js/stepper.js")), 
    js_inline(jquery_ready("$('#stepper_form').on('submit', function(e) { e.stopPropagation(); return false; });"))
    ], [
    div([id("stepper"), class("stepper")], [
      form([id("stepper_form")],[
        hidden("step", _current_step.name),   //add current step name as hidden argument
        hidden("stepper_uid",_stepper.uid),    //add stepper_uid as hidden argument
        //construct bread crumb line
        partial(web_stepper_crumble(_stepper, _current_step, _T)),
        partial(web_stepper_full_description(_current_step, _T)),
        partial(web_stepper_message(stp_session, _T)),
        partial(web_stepper_main_view(_current_step, stp_session, _T)),   //OK
        partial(web_stepper_button(_current_step, stp_session, _T))
      ])
    ])
  ])

.

public define HTML_Partial_Content
  web_stepper_view
  (
    WEB_Stepper       stepper,
    WEB_Step_Session  stp_session,
    (String)->String  _T
  )=
  println("web_stepper_view without step. stp_session.current_step ["+stp_session.current_step+"]");
  if get_current_step(stepper.steps, stp_session.current_step) is
  {
    failure                 then  partial_content(empty)
    success(_current_step)  then  web_stepper_view(stepper, _current_step, stp_session, _T)
  }
.

public define WEB_Controller_Result
  manage_stepper_action
  (
    List(Web_arg) _lwa,
    WEB_Session   _web_session,
    WEB_Stepper   _stepper,
    WEB_Step      _step
  )=
  with stepper_session = web_step_session(_step.name, get_Session(_web_session.fields, _stepper.uid, session(_stepper.uid, empty_fields_list))),
       _T = make_translate_function(_web_session),
  println("manage_stepper_action step_session\n"+dump_Session_Field_list(*stepper_session.session.fields,""));
  
  with action = get_String(_lwa, "step_a", "N/A"),
  println("manage_stepper_action ["+action+"]");
  if      action = "submit" then
    println("manage_stepper_action submit step["+_step.name+"]");
    with new_session = _step.submit(stepper_session, _web_session),
    //TODO must store new session in WEB session
    println("store new_session\n"+dump_Session(new_session.session,""));
    replace_Session(_web_session.fields, _stepper.uid, new_session.session);
    ajax(_web_session, web_stepper_view(_stepper, new_session, _T))

//    ajax(_session, no_content)
  else if action = "go"   then
    set_Page_Message(stepper_session.session.fields, no_message);    
    replace_Session(_web_session.fields, _stepper.uid, stepper_session.session);
    ajax(_web_session, web_stepper_view(_stepper, stepper_session, _T))
  else if action = "init" then
    ajax(_web_session, no_content)
  else
    ajax(_web_session, no_content)
.

public define WEB_Controller_Result
  manage_stepper
  (
    WEB_Session       _web_session,
    List(WEB_Stepper) _steppers
  )=
  with _lwa = *_web_session.web_request.lwa,
  println("manage_stepper");
  if get_stepper(_lwa, _steppers) is
  {
    failure           then
      println("stepper not found");
      ajax(_web_session, no_content),
    success(stepper)  then
    //get session of the stepper
    if get_String(_lwa, "step") is
    {
      failure             then
        println("manage_stepper step not found ");
        ajax(_web_session, no_content)
      success(step_name)  then 
        println("manage_stepper step_name = "+step_name);
        if step_name = "start" then
          with _T = make_translate_function(_web_session),
          with initial_step_session = stepper.start(_web_session),
          println("store initial_step_session\n"+dump_Session(initial_step_session.session,""));
          replace_Session(_web_session.fields, stepper.uid, initial_step_session.session);
          ajax(_web_session, web_stepper_view(stepper, initial_step_session, _T))
        else

          if get_current_step(stepper.steps, step_name) is
          {
            failure         then  ajax(_web_session, no_content),
            success(step)   then
              manage_stepper_action(_lwa, _web_session, stepper, step)
          }
     }
    
  }
.

public define HTML_Partial_Content
  stepper_submit_button
  (
    String                  label,
    WEB_Action_Name         wan,
    String                  submit_action,
    List((String, String))  extra_args
  )=
  with url = format_web_action_name_to_js(wan,[("step_a", "submit"), ("submit_a",submit_action) . extra_args]),
  jquery_button(jquery_img_button(icn16_check, label, "", jQuery_actioner(same, jqscript("stepper_submit("+url+");")), left))
.

public define HTML_Partial_Content
  stepper_submit_button
  (
    String                  label,
    WEB_Action_Name         wan,
    String                  submit_action,
  )=
  stepper_submit_button(label, wan, submit_action, [])
.