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

transmit calexium_lib/web/CXM_making_a_web_site.anubis
read tools/printable_tree.anubis
read tools/basis.anubis

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

public define JQuery_Actioner_type jqlink(String name) = jqlink(action_name(name)).

/* 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)       then "$('#"+form+"').submit();",
              jqlink(control_action)  then 
                if control_action is
                {
                  no_action                       then "",
                  controller_action(ctrl, action) then "document.location='/?aws_controller="+ctrl+"&aws_action="+action+"';",
                  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
            },
          same(label) then
            if act_type is
            {
              jqform(form)       then "$('#"+form+"').submit();",
              jqlink(control_action)  then 
                if control_action is
                {
                  no_action                       then  "",
                  controller_action(ctrl, action) then  "document.location='/?aws_controller="+ctrl+"&aws_action="+action+"';",
                  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
            },
          other(window_name, window_options) then
            if act_type is
            {
              jqform(form)       then "$('#"+form+"').submit();",//il s'agit d'une soumision d'un formulaire => cela se fait uniquement dans la même fenêtre !
              jqlink(control_action)  then 
                if control_action is
                {
                  no_action                       then "",
                  controller_action(ctrl, action) then
                    "window.open('aws_controller="+ctrl+"&aws_action="+action+"', '"+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+"', '"+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

            }
        }.

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 ]
  .