XL_web_stepper.anubis 7.63 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_controllers/language/language_management.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
  )=
  partial_content(div(class("steppers_buttons"), _step.buttons(stp_session)))
.

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

public define HTML_Partial_Content
  web_stepper_message
  (
    WEB_Step_Session  stp_session,
    (String)->String  _T
  )=
  partial_content(div(class("stepper_message"), show_page_message(get_Page_Message(stp_session.session.fields, _T))))
.

public define HTML_Partial_Content
  web_stepper_name
  (
    WEB_Step_Session  stp_session
  )=
  partial_content(empty)
.

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

public define Bool
  is_active
  (
    WEB_Step          step,
    WEB_Step_Session  stp_session
  )=
  true
.

public define HTML_Partial_Content
  web_stepper_crumble
  (
    WEB_Stepper       stepper,
    WEB_Step_Session  stp_session
  )=
  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, stp_session)  then " active"  else "",
                current = if is_current(step, stp_session) then " current" else "",
            
            li(class("multi-step-item"+active+current),
              div(class("item-wrap"),
                p(class("item-title"),
                  text(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_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, stp_session)),
        partial(web_stepper_name(stp_session)),
        partial(web_stepper_message(stp_session, _T)),
        partial(web_stepper_main_view(_current_step, stp_session)),   //OK
        partial(web_stepper_button(_current_step, stp_session))
      ])
    ])
  ])

.

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 = "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)
          }
     }
    
  }
.