jq_tabs.anubis 3 KB
/*
 * Created by PyramIDE.
 * User: Jeremy
 * Date: 04/09/2012
 * Time: 16:19
 * 
 *
 */
 
read xlib/web/making_a_web_site.anubis
read xlib/web/jquery.anubis
read xlib/web/common.anubis
read tools/basis.anubis

public type JQuery_tab:
 jQuery_tab(String title, HTML content)
.

  // Helper to be compatible with the first version of JQuery_tab which
  // take HTML as second argument instead of HTML.
  
public define JQuery_tab
  jQuery_tab
  (
    String        title,
    HTML content
  )=
  jQuery_tab(title, partial_content([], content)).
  
define HTML
  _jquery_tabs
  (
    String              tabs_container_id,
    List(JQuery_tab)    tabs,
    String              tabs_titles,
    List(HTML) tabs_contents,
    List(HTML_Head_Tag) tabs_heads,
    Int                 count
  )
  =
   if tabs is
   {
      [ ]         then
        partial_content(
          tabs_heads,
          sequence([
            ul(text(tabs_titles)),
            sequence(reverse(tabs_contents))
          ])
        ),
      [ h . t ]   then
        if h is jQuery_tab(title, p_content) then
        if p_content is partial_content(tags, content) then
          _jquery_tabs(
            tabs_container_id,
            t,
            tabs_titles + "<li><a href=\"#"+tabs_container_id+"-tab-"+count+"\">"+title+"</a></li>",
            [
              div([id(tabs_container_id+"-tab-"+count)], content)
              .
              tabs_contents
            ],
            tabs_heads + tags ,
            count+1
          )
    }
  .
  
public define HTML
  jquery_tabs
  (
    String              tabs_container_id,
    List(JQuery_tab)    tabs,
    List(Web_arg)       lwa
  )
  =
    with index_str = if web_arg_value(lwa, "jq_tabs_" + tabs_container_id) is 
      {
        not_found  then "0",
        found(ps)  then ps
      },
    if _jquery_tabs(tabs_container_id, tabs, "", [], [], 0) is partial_content(tags, html) then
      partial_content(
        [
          js_inline(jquery_ready("
            $('#"+tabs_container_id+"').tabs({
                active: " + index_str + ",
                activate: function( event, ui ) {
                    ui.newPanel.find('input[name=jq_tabs_" + tabs_container_id + "]').each(function () {
                        $(this).attr('value', ui.newTab.index());
                      });
                      
                    if (typeof Xlib !== 'undefined' && typeof Xlib.jq_tab_activate !== 'undefined') {
                      Xlib.jq_tab_activate(event, ui);
                    }
                  },
                create: function( event, ui ) {
                  $('#"+tabs_container_id+"').find('form').prepend('<input type=\"hidden\" name=\"jq_tabs_" + tabs_container_id + "\" value=\"'+ui.tab.index()+'\">');
                }
              });
          "))
          .
          tags
        ],
        
        div([id(tabs_container_id)], html)
      ).

public define HTML
  jquery_tabs
  (
    String              tabs_container_id,
    List(JQuery_tab)    tabs
  )
  =
  jquery_tabs(tabs_container_id, tabs, []).