jquery.anubis 10.3 KB
/*
 * Created by PyramIDE.
 * User: Jeremy
 * Date: 21/08/2012
 * Time: 16:52
 *
 */

transmit xlib/web/making_a_web_site.anubis
read xlib/web/load_content.anubis
read tools/printable_tree.anubis
read tools/basis.anubis

/* jQuery Actioner type */
public type JQuery_Actioner_type:
  jqform  ( String          form, WEB_Action_Name name, List((String,String)) extra_ops),
  jqformload  ( String form, String target, WEB_Action_Name name, List((String,String)) extra_ops),
  jqlink  ( WEB_Action_Name name, List((String,String)) extra_ops),
  jqflink ( String          link),
  jqscript( String          script),
  jqload  ( String          target, WEB_Action_Name name, List((String,String)) extra_ops)
.

public define JQuery_Actioner_type jqlink(WEB_Action_Name name) = jqlink(name, []).
public define JQuery_Actioner_type jqlink(String name)          = jqlink(action_name(name), []).
public define JQuery_Actioner_type jqform(String form)          = jqform(form, no_action, []).

/* jQuery Actioner */
public type JQuery_Actioner:
  jQuery_actioner(Actioner_Target target, JQuery_Actioner_type type). //, String url_or_form_id_or_script

/* jQuery Selector - http://api.jquery.com/category/selectors/ */
public type JQuerySelector:
  element(HTML_Id id),
  element_class(String name),
  childs(HTML_Id id).

define String
    jquery_button_get_onclick_action_window_options
    (
      List(Other_Window_Option) options,
      String                    formated_options
    )
    =
    //if there is already formated_options add a comma + space between the options
    with comma = if formated_options="" then "" else ", ",

      if options is
      {
        [ ] then formated_options,
        [ c . t ] then
          if c is
          {
             resizable  then jquery_button_get_onclick_action_window_options(
                                t, formated_options+ comma + "resizable=yes"),

             scrollbars then jquery_button_get_onclick_action_window_options(
                                t, formated_options+ comma + "scrollbars=yes"),

             width(w)   then jquery_button_get_onclick_action_window_options(
                                t, formated_options+ comma + "width="+w),

             height(h)  then jquery_button_get_onclick_action_window_options(
                                t, formated_options+ comma + "height="+h)
          }
      }
    .

public define String
    jquery_button_get_onclick_action
    (
      JQuery_Actioner actioner
    )
    =
      if actioner is jQuery_actioner(target, act_type) then
        if target is
        {
          same then
            if act_type is
            {
              jqform(form, control_action, extra_ops)       then 
                
                if control_action is
                {
                  no_action                       then "",  //will use the default action defined in form
                  controller_action(ctrl, action) then 
                    "$('#"+form+"').get(0).setAttribute('action','/?aws_controller="+ctrl+"&aws_action="+action+format_extra_operands(extra_ops)+"');\n"
                  action_name(action)             then 
                    "$('#"+form+"').get(0).setAttribute('action','/?aws_action="+action+"');",
                  url(url)                        then 
                    "$('#"+form+"').get(0).setAttribute('action','"+url+"');",
                }+
                "$('#"+form+"').submit();\n",
              jqformload(form_id, target_id, web_action, extra_args)       then 
                "CalexiumToolBox.jq_form_submit_and_load('"+form_id+"', '"+format_web_action_name(web_action, extra_args)+"', '"+target_id+"');\n"
              jqlink(control_action, extra_ops)  then 
                if control_action is
                {
                  no_action                       then "",
                  controller_action(ctrl, action) then "document.location='/?aws_controller="+ctrl+"&aws_action="+action+format_extra_operands(extra_ops)+"';",
                  action_name(action)             then "document.location='/?aws_action="+action+"';",
                  url(url)                        then "document.location='"+url+"';",
                }
              jqflink(link)                       then "document.location='"+link+"';",
              jqscript(script)                    then script
              jqload(target_id, web_action, extra_args) then load_content(target_id, web_action, extra_args, false)
            },
          same(label) then
            if act_type is
            {
              jqform(form, control_action, extra_ops)       then 
                if control_action is
                {
                  no_action                       then "",  //will use the default action defined in form
                  controller_action(ctrl, action) then 
                    "$('#"+form+"').get(0).setAttribute('action','/?aws_c="+ctrl+"&aws_a="+action+format_extra_operands(extra_ops)+"');\n"
                  action_name(action)             then 
                    "$('#"+form+"').get(0).setAttribute('action','/?aws_action="+action+"');",
                  url(url)                        then 
                    "$('#"+form+"').get(0).setAttribute('action','"+url+"');",
                }+
                "$('#"+form+"').submit();\n",       
              jqformload(form_id, target_id, web_action, extra_args)       then 
                "CalexiumToolBox.jq_form_submit_and_load('"+form_id+"', '"+format_web_action_name(web_action, extra_args)+"', '"+target_id+"');\n"
              jqlink(control_action, extra_ops)  then 
                if control_action is
                {
                  no_action                       then  "",
                  controller_action(ctrl, action) then  "document.location='/?aws_controller="+ctrl+"&aws_action="+action+format_extra_operands(extra_ops)+"';",
                  action_name(action)             then  "document.location='/?aws_action="+action+format_extra_operands(extra_ops)+"';",
                  url(url)                        then  "document.location='"+url+"';",
                }
              jqflink(link)     then "document.location='"+link+"';",
              jqscript(script)  then script
              jqload(target_id, web_action, extra_args) then load_content(target_id, web_action, extra_args, false)
            },
          other(window_name, window_options) then
            if act_type is
            {
              jqform(form, control_action, extra_ops)       then //il s'agit d'une soumision d'un formulaire => cela se fait uniquement dans la même fenêtre !
                if control_action is
                {
                  no_action                       then "",  //will use the default action defined in form
                  controller_action(ctrl, action) then 
                    "$('#"+form+"').get(0).setAttribute('action','/?aws_controller="+ctrl+"&aws_action="+action+format_extra_operands(extra_ops)+"');\n"
                  action_name(action)             then 
                    "$('#"+form+"').get(0).setAttribute('action','/?aws_action="+action+"');",
                  url(url)                        then 
                    "$('#"+form+"').get(0).setAttribute('action','"+url+"');",
                }+
                "$('#"+form+"').submit();\n",
              jqformload(form_id, target_id, web_action, extra_args)       then 
                "CalexiumToolBox.jq_form_submit_and_load('"+form_id+"', '"+format_web_action_name(web_action, extra_args)+"', '"+target_id+"');\n"
              jqlink(control_action, extra_ops)  then 
                if control_action is
                {
                  no_action                       then "",
                  controller_action(ctrl, action) then
                    "window.open('aws_controller="+ctrl+"&aws_action="+action+format_extra_operands(extra_ops)+"', '"+window_name+"', '"+jquery_button_get_onclick_action_window_options(window_options, "")+"');",
                    //"document.location='/?aws_controller="+ctrl+"&aws_action="+action+"';",
                  action_name(action)             then 
                    "window.open('"+action+format_extra_operands(extra_ops)+"', '"+window_name+"', '"+jquery_button_get_onclick_action_window_options(window_options, "")+"');",
                  url(url)                        then "document.location='"+url+"';",
                }
              jqflink(link)     then "document.location='"+link+"';",
              jqscript(script)  then script
              jqload(target_id, web_action, extra_args) then load_content(target_id, web_action, extra_args, false)
            }
        }.

public define String
  format_jquery_selector
  (
    JQuerySelector selector
  ) =
  if selector is
  {
    element(html_id) then
      if html_id is html_Id(id) then "'#" + id + "'",

    element_class(name) then
      "'." + name + "'",

    childs(html_id) then
      if html_id is html_Id(id) then  "'#" + id + " > *'"
  }.

/* */
public define Script
  jquery_ready
  (
    String  code  //javascript code to execute when the document is ready
  )=
  script("text/javascript","$(document).ready(function(){"+code+"});").

public define String
  jquery_ready
  (
    String  code  //javascript code to execute when the document is ready
  )=
  "$(document).ready(function(){"+code+"});".

/* jQuery default js files */
define List(HTML_Head_Tag)
   jquery_defaults
   =
   [
     js(js_file("/js/jquery/jquery-1.12.4.min.js")),
     js(js_file("/js/jquery/jquery-ui-1.11.4/jquery-ui.min.js")),
   ].

/* Initialize jQuery in compatiblity mode with jQueryUI */
public define List(HTML_Head_Tag)
  jquery_init
  (
   String web_dir,
   String theme_name
  )
  =
    if file_exists(web_dir+"/public/js/jquery/jquery-ui-1.11.4/theme/"+theme_name+"/jquery-ui.min.css") then
      [ css(css_file("/js/jquery/jquery-ui-1.11.4/theme/"+theme_name+"/jquery-ui.min.css")),
        css(css_file("/js/jquery/jquery-ui-1.11.4/theme/"+theme_name+"/jquery-ui.structure.min.css")),
        css(css_file("/js/jquery/jquery-ui-1.11.4/theme/"+theme_name+"/jquery-ui.theme.min.css")) . jquery_defaults ]
    else
      println("Jquery Theme not found "+web_dir+"/public/js/jquery/jquery-ui-1.11.4/theme/"+theme_name+"/jquery-ui.min.css");
      //TODO change to real default jquery ui css
      [ css(css_file("/js/jquery/jquery-ui-1.11.4/theme/"+theme_name+"/jquery-ui.min.css")) . jquery_defaults ]
  .