CXM_jquery_button.anubis 11.2 KB
/*
 * Created by PyramIDE.
 * User: Jeremy
 * Date: 07/09/2012
 * Time: 15:10
 *
 */

transmit calexium_lib/web/CXM_making_a_web_site.anubis
transmit calexium_lib/web/CXM_jquery.anubis
transmit calexium_lib/web/jQuery/CXM_jquery_icons.anubis
transmit calexium_lib/web/widgets/button.anubis
transmit calexium_lib/web/js_tools.anubis
transmit tools/basis.anubis
transmit system/string.anubis
transmit tools/random.anubis


public type JQuery_button_type:
  button,
  reset,
  submit.    //button with confirmation dialog

public define String
  to_String
  (
    JQuery_button_type bt_type
  )=
  if bt_type is
  {
    button  then  "button",
    reset   then  "reset",
    submit  then  "submit"
  }.

public type JQuery_button:
  jQuery_button( JQuery_button_type bt_type,
    String button_id, String button_name, String button_label, JQuery_Actioner actioner, JQuery_icon primary, JQuery_icon secondary   // primary icon is for left icon, secondary icon is for right icon and button_id must set with an unique ID in order to have icons working
  )
.

public define JQuery_button
  jQuery_button
  (
    JQuery_button_type  bt_type,
    String              button_id,
    String              button_name,
    String              button_label,
    JQuery_Actioner     actioner
  ) =
  jQuery_button(bt_type, button_id, button_name, button_label, actioner, jq_icon_none, jq_icon_none).

define HTML_Head_Tag
  jquery_button_get_onclick_action
  (
    String          button_id,
    JQuery_Actioner actioner
  )
  =
    with js_action = jquery_button_get_onclick_action(actioner),
    js_inline(jquery_ready("$('#"+button_id+"').click(function(e){" + js_action + "});"))
  .

define HTML_Partial_Content
  _jquery_buttons
  (
    List(JQuery_button) buttons,
    List(HTML_Head_Tag) html_tags,
    List(HTML_Off_Form) html_buttons
  )
  =
    if buttons is
    {
      [ ]       then
        partial_content(
          reverse(html_tags),
          sequence(reverse(html_buttons))
        ),
      [ h . t ] then
        if h is jQuery_button(bt_type, id, name, label, actioner, primary, secondary) then
          with onclick_action=jquery_button_get_onclick_action(id, actioner),
          if id = "" then     // if the ID is not set => we can't use icons in the buttonset.
            _jquery_buttons(
              t,
              [ onclick_action . html_tags ],
              [
                  literal("<button type=\""+to_String(bt_type)+"\" name=\""+name+"\">"+label+"</button>")

                .
                html_buttons
              ]
            )
          else
            if jquery_icon_to_string(primary) = "" then
              if jquery_icon_to_string(secondary) = "" then
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+id+"').button();")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    literal("<button type=\""+to_String(bt_type)+"\" id=\""+id+"\" name=\""+name+"\">"+label+"</button>")
                    .
                    html_buttons
                  ]
                )
              else
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+id+"').button({icons:{secondary:'"+jquery_icon_to_string(secondary)+"'}});")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    literal("<button id=\""+id+"\">"+label+"</button>")
                    .
                    html_buttons
                  ]
                )
            else
              if jquery_icon_to_string(secondary) = "" then
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+id+"').button({icons:{primary:'"+jquery_icon_to_string(primary)+"'}});")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    literal("<button id=\""+id+"\">"+label+"</button>")
                    .
                    html_buttons
                  ]
                )
              else
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+id+"').button({icons:{primary:'"+jquery_icon_to_string(primary)+"',secondary:'"+jquery_icon_to_string(secondary)+"'}});")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    literal("<button id=\""+id+"\">"+label+"</button>")
                    .
                    html_buttons
                  ]
                )
    }
  .

public define HTML_Partial_Content
  jquery_buttons
  (
    String              buttons_container_id,
    List(JQuery_button) buttons
  )
  =
      partial_content(
        [ js_inline(jquery_ready("$('#"+buttons_container_id+"').buttonset();")) ],
        sequence([
          literal("<div id=\""+buttons_container_id+"\">"),
          partial(_jquery_buttons(buttons, [], [])),
          literal("</div>")
        ])
      )
  .

public define HTML_Partial_Content
  jquery_button
  (
    JQuery_button button
  )
  =
    if button is jQuery_button(bt_type, id, name, label, actioner, primary, secondary) then
      with onclick_action=jquery_button_get_onclick_action(id, actioner),
      if jquery_icon_to_string(primary) = "" then
        if jquery_icon_to_string(secondary) = "" then
          partial_content(
            [
              js_inline(jquery_ready("$('#"+id+"').button();")),
              onclick_action
            ],
            literal("<button type=\""+to_String(bt_type)+"\" id=\""+id+"\" name=\""+name+"\">"+label+"</button>")
          )
        else
          partial_content(
            [
              js_inline(jquery_ready("$('#"+id+"').button({icons:{secondary:'"+jquery_icon_to_string(secondary)+"'}});")),
              onclick_action
            ],
            literal("<button type=\""+to_String(bt_type)+"\" id=\""+id+"\">"+label+"</button>")
          )
      else
        if jquery_icon_to_string(secondary) = "" then
          partial_content(
            [
              js_inline(jquery_ready("$('#"+id+"').button({icons:{primary:'"+jquery_icon_to_string(primary)+"'}});")),
              onclick_action
            ],
            literal("<button type=\""+to_String(bt_type)+"\" id=\""+id+"\">"+label+"</button>")
          )
        else
          partial_content(
            [
              js_inline(jquery_ready("$('#"+id+"').button({icons:{primary:'"+jquery_icon_to_string(primary)+"',secondary:'"+jquery_icon_to_string(secondary)+"'}});")),
              onclick_action
            ],
            literal("<button type=\""+to_String(bt_type)+"\" id=\""+id+"\">"+label+"</button>")
          )
  .

public type Icon_orientation:
  top,
  bottom,
  left,
  right.

public type JQuery_img_button:
  jquery_img_button(String img_url, String label_text, String id, JQuery_Actioner jq_action, Icon_orientation orientation).

public define HTML_Off_Form
  jquery_button_html
  (
    JQuery_img_button button,
    String            button_id
  ) =
  if button is jquery_img_button(img_url, label_text, _, actioner, orientation) then
  div([id(button_id), class("jq_img_btn")],
                        if orientation is
                        {
                          top     then table([class("jq_img_btn_table")],[ row( cell(image(img_url))), row( cell(text(label_text))) ]),
                          bottom  then table([class("jq_img_btn_table")],[ row( cell(text(label_text))), row( cell(image(img_url))) ]),
                          left    then table([class("jq_img_btn_table")],[ row( [cell(image(img_url)), cell(text(label_text)) ]) ]),
                          right   then table([class("jq_img_btn_table")],[ row( [cell(text(label_text)), cell(image(img_url)) ]) ]),
                        }).



public define HTML_Partial_Content
  jquery_button
  (
    JQuery_img_button button
  )=
  if button is jquery_img_button(_, _, button_id, actioner, _) then
  with button_id_str = if length(button_id) = 0 then
                          generate_random_string(10)
                       else
                          button_id,
  partial_content(
    [
      jquery_button_get_onclick_action(button_id_str, actioner),
      css(css_file("css/cxm/cxm_button.css")),
      js_inline(jquery_ready("$('.jq_img_btn').button();"))
    ],
    jquery_button_html(button, button_id_str))
  .

public define HTML_Partial_Content
  jq_button_confirm
  (
    String                  button_label,
    String                  dlg_title,
    String                  dlg_text,
    String                  dlg_ok,
    String                  dlg_cancel,
    //String                  url,
    JQuery_Actioner         actioner
  )=
  with uid = generate_random_string(10),

       js_confirm_dialog_data_obj  = "CalexiumToolBox.data.confirm_dialog[" + to_js_string(uid) + "]",
       js_data = js_confirm_dialog_data_obj + " = {
                    title:  " + to_js_string(dlg_title)  + ",
                    text:   " + to_js_string(dlg_text)   + ",
                    ok :    " + to_js_string(dlg_ok)     + ",
                    cancel: " + to_js_string(dlg_cancel) + "};",

       button_name  = uid,
       actioner     = jQuery_actioner(same, jqscript, "CalexiumToolBox.make_confirm_dialog(this, " /*+ to_js_string("/?a=" + url + format_extra_operands(extra_ops))+*/ + to_js_string(jquery_button_get_onclick_action(actioner)) + ");"),

  partial_content([js(js_file("js/cxm/cxm.js")), js_inline(jquery_ready(js_data))],sequence([
            partial(jquery_button(jQuery_button(button, uid, button_name, button_label, actioner, jq_icon_none, jq_icon_none)))
        ])).

public define HTML_Partial_Content
  jq_button_confirm
  (
    JQuery_img_button       button,
    String                  dlg_title,
    String                  dlg_text,
    String                  dlg_ok,
    String                  dlg_cancel
    //JQuery_Actioner         actioner
    //String                  url,
    //List((String, String))  extra_ops
  )=
  since button is jquery_img_button(img_url, button_label, button_name, actioner, orientation),
  with uid = generate_random_string(10),

       js_confirm_dialog_data_obj  = "CalexiumToolBox.data.confirm_dialog[" + to_js_string(uid) + "]",
       js_data = js_confirm_dialog_data_obj + " = {
                    title:  " + to_js_string(dlg_title)  + ",
                    text:   " + to_js_string(dlg_text)   + ",
                    ok :    " + to_js_string(dlg_ok)     + ",
                    cancel: " + to_js_string(dlg_cancel) + "};",

       button_name  = uid,
       actioner     = jQuery_actioner(same, jqscript, "CalexiumToolBox.make_confirm_dialog(this, " + to_js_string(jquery_button_get_onclick_action(actioner)) +");"),

  partial_content([js(js_file("js/cxm/cxm.js")), js_inline(jquery_ready(js_data))],sequence([
              partial(jquery_button(jquery_img_button(img_url, button_label, button_name, actioner, orientation)))
          ])).

public define HTML_Partial_Content
  img_submit_button
  (
    String  label,    //translated label of the button
    String  form_id   //
  )=
  jquery_button(jquery_img_button(icn16_check, label, "", jQuery_actioner(same, jqform, form_id), left)).