pager.anubis 2.26 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 03/02/2019
 * Time: 19:32
 * © David RENÉ
 */

read xlib/web/making_a_web_site.anubis
read xlib/web/web_session.anubis

public define HTML_Partial_Content
  control_panel_pager
  (
    String  pager_id,
    Int     current_page,
    Int     total_page,
    Int     total_items,
    String  previous,
    String  next
  )
  =
  partial_content([js(js_file("xlib/js/pager.js"))],
  div(id(pager_id), [
    text(class("o_pager_current_page"), current_page),
    text("/"),
    text(class("o_pager_total_page"), total_page),
    text(" "),
    text(class("o_pager_total_items"), total_items),
    span(class("btn-group btn-group-sm"), [
        button([class("fa fa-chevron-left btn btn-icon o_pager_previous"), attr("type","button"), bool_attr(current_page = 1, "disabled"),          accesskey('p'), event(onclick, previous)]),
        button([class("fa fa-chevron-right btn btn-icon o_pager_next"),    attr("type","button"), bool_attr(current_page = total_page, "disabled"), accesskey('n'), event(onclick, next)])
    ])
  ]))
.

public define HTML_Partial_Content
  control_panel_pager
  (
    String  pager_id,
    String  previous,
    String  next
  )
  =
  control_panel_pager(pager_id, 0, 0, 0, previous, next)
.

public define HTML_Head_Tag
  update_pager_script
  (
    String  pager_id,
    Int     current_page,
    Int     total_pages,
    Int     total_items
  )=
  js_script("update_pager('"+pager_id+"', "+current_page+", "+total_pages+", "+total_items+");")
.

public define One
  get_current_page
  (
    WEB_Session _session
  )=
  with       lwa = *_session.web_request.lwa,
     //check if we clicked on next or previous pager
     next_page = get_Bool(lwa, "next_page", false),
     previous_page = get_Bool(lwa, "previous_page", false),
     
    //get the current page stored in session
    _page = get_Int(_session.fields, "pager_current_page", 1),
    
    //if clicked on pager change page number else set to 1 (means change on query or tag. In that case reset the page)
    current_page = if next_page then _page+1 else if previous_page then _page-1 else 1,
    
    //store current page in web session for the next call
    replace_Int(_session.fields, "pager_current_page", current_page)
.