CXM_jquery_button.anubis 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/*
 * 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 calexium_lib/web/CXM_json.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, html_id, name, label, actioner, primary, secondary) then
          with onclick_action=jquery_button_get_onclick_action(html_id, actioner),
          if html_id = "" then     // if the ID is not set => we can't use icons in the buttonset.
            _jquery_buttons(
              t,
              [ onclick_action . html_tags ],
              [
                  button([attr("type",to_String(bt_type)), attr("name",name)], text(label))
                .
                html_buttons
              ]
            )
          else
            if jquery_icon_to_string(primary) = "" then
              if jquery_icon_to_string(secondary) = "" then
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+html_id+"').button();")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    button([attr("type",to_String(bt_type)), id(html_id), attr("name",name)], text(label))
                    .
                    html_buttons
                  ]
                )
              else
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+html_id+"').button({icons:{secondary:'"+jquery_icon_to_string(secondary)+"'}});")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    literal("<button id=\""+html_id+"\">"+label+"</button>")
                    .
                    html_buttons
                  ]
                )
            else
              if jquery_icon_to_string(secondary) = "" then
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+html_id+"').button({icons:{primary:'"+jquery_icon_to_string(primary)+"'}});")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    literal("<button id=\""+html_id+"\">"+label+"</button>")
                    .
                    html_buttons
                  ]
                )
              else
                _jquery_buttons(
                  t,
                  [
                    js_inline(jquery_ready("$('#"+html_id+"').button({icons:{primary:'"+jquery_icon_to_string(primary)+"',secondary:'"+jquery_icon_to_string(secondary)+"'}});")),
                    onclick_action
                  ]
                  +html_tags,
                  [
                    literal("<button id=\""+html_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();")) ],
          div(id(buttons_container_id), _jquery_buttons(buttons, [], []))
//        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),
//           btn_seq = (HTML_Off_Form)sequence([literal("<button type=\""+to_String(bt_type)+"\" id=\""+id+"\" name=\""+name+"\">"), table([attr("cellspacing", "5")], [row([cell(text(label))])]), literal("</button>")]),
           btn_seq = (HTML_Off_Form)sequence([literal("<button class=\"cxm_button\" type=\""+to_String(bt_type)+"\" id=\""+id+"\" name=\""+name+"\">"+label+"</button>")]),
      if jquery_icon_to_string(primary) = "" then
        if jquery_icon_to_string(secondary) = "" then
          partial_content(
            [
              js_inline(jquery_ready("$('#"+id+"').button();")),
              onclick_action
            ],
            btn_seq
            //sequence([literal("<button type=\""+to_String(bt_type)+"\" id=\""+id+"\" name=\""+name+"\">"), table([attr("cellspacing", "5")], [row([cell(text(label))])]), literal("</button>")])
            //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
            ],
            btn_seq
            //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
            ],
            btn_seq
            //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
            ],
            btn_seq
            //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 jq_img_button,
    String            button_id
  ) =
  since jq_img_button is jquery_img_button(img_url, label_text, _, actioner, orientation),
  button([id(button_id), class("jq_img_btn")],
                        if orientation is
                        {
                          top     then table([class("jq_img_btn_table")],[ row( cell(img(img_url))), row( cell(text(label_text))) ]),
                          bottom  then table([class("jq_img_btn_table")],[ row( cell(text(label_text))), row( cell(img(img_url))) ]),
                          left    then table([class("jq_img_btn_table")],[ row( [cell(img(img_url)), cell(text(label_text)) ]) ]),
                          right   then table([class("jq_img_btn_table")],[ row( [cell(text(label_text)), cell(img(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['" + 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")),
      css(css_file("css/cxm/cxm_button.css")),
      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['" + 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          img,      //path to image
    String          label,    //translated label of the button
    String          form_id,  //form id to submit
    WEB_Action_Name action,   //
    List((String,String)) extra_ops    
  )=
  jquery_button(jquery_img_button(img, label, "", jQuery_actioner(same, jqform(form_id, action, extra_ops)), left)).

public define HTML_Partial_Content
  img_submit_button
  (
    String          img,      //path to image
    String          label,    //translated label of the button
    String          form_id,  //form id to submit
    WEB_Action_Name action    
  )=
  jquery_button(jquery_img_button(img, label, "", jQuery_actioner(same, jqform(form_id, action, [])), left)).

public define HTML_Partial_Content
  img_submit_button
  (
    String          label,    //translated label of the button
    String          form_id,  //form id to submit
    WEB_Action_Name action    
  )=
  jquery_button(jquery_img_button(icn16_check, label, "", jQuery_actioner(same, jqform(form_id, action, [])), left)).
  
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, no_action, [])), left))
.

public define HTML_Partial_Content
  img_button
  (
    String                icon,       //URL of 
    String                label,      //translated label of the button
    WEB_Action_Name       action,     
    List((String,String)) extra_ops
  )=
  jquery_button(jquery_img_button(icon, label, "", jQuery_actioner(same, jqlink(action, extra_ops)), left))
.


public define HTML_Partial_Content
  img_button
  (
    String                icon,       //URL of 
    String                label,      //translated label of the button
    WEB_Action_Name       action,     
    List((String,String)) extra_ops,
    String                target
  )=
  jquery_button(jquery_img_button(icon, label, "", jQuery_actioner(same, jqload(target, action, extra_ops)), left))
.

public define HTML_Partial_Content
  img_button
  (
    String          icon,
    String          label,    //translated label of the button
    WEB_Action_Name action
  )=
  jquery_button(jquery_img_button(icon, label, "", jQuery_actioner(same, jqlink(action)), left))
.