cubicweb/web/data/cubicweb.ajax.box.js
changeset 11057 0b59724cb3f2
parent 10279 d7479a5ac553
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 /**
       
     2  * Functions for ajax boxes.
       
     3  *
       
     4  *  :organization: Logilab
       
     5  *  :copyright: 2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     6  *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     7  *
       
     8  */
       
     9 
       
    10 function ajaxBoxValidateSelectorInput(boxid, eid, separator, fname, msg) {
       
    11     var holderid = cw.utils.domid(boxid) + eid + 'Holder';
       
    12     var value = $('#' + holderid + 'Input').val();
       
    13     if (separator) {
       
    14         value = $.map(value.split(separator), jQuery.trim);
       
    15     }
       
    16     var d = loadRemote(AJAX_BASE_URL, ajaxFuncArgs(fname, null, eid, value));
       
    17     d.addCallback(function() {
       
    18             $('#' + holderid).empty();
       
    19             var formparams = ajaxFuncArgs('render', null, 'ctxcomponents', boxid, eid);
       
    20             $('#' + cw.utils.domid(boxid) + eid).loadxhtml(AJAX_BASE_URL, formparams, null, 'swap');
       
    21             if (msg) {
       
    22                 document.location.hash = '#header';
       
    23                 updateMessage(msg);
       
    24             }
       
    25         });
       
    26 }
       
    27 
       
    28 function ajaxBoxRemoveLinkedEntity(boxid, eid, relatedeid, delfname, msg) {
       
    29     var d = loadRemote(AJAX_BASE_URL, ajaxFuncArgs(delfname, null, eid, relatedeid));
       
    30     d.addCallback(function() {
       
    31             var formparams = ajaxFuncArgs('render', null, 'ctxcomponents', boxid, eid);
       
    32             $('#' + cw.utils.domid(boxid) + eid).loadxhtml(AJAX_BASE_URL, formparams, null, 'swap');
       
    33             if (msg) {
       
    34                 document.location.hash = '#header';
       
    35                 updateMessage(msg);
       
    36             }
       
    37     });
       
    38 }
       
    39 
       
    40 /**
       
    41  * .. function:: ajaxBoxShowSelector(boxid, eid, unrelfname,
       
    42  *                                  addfname, msg,
       
    43  *                                  oklabel, cancellabel,
       
    44  *                                  separator=None)
       
    45  *
       
    46  * Display an ajax selector within a box of regid `boxid`, for entity with eid
       
    47  * `eid`.
       
    48  *
       
    49  * Other parameters are:
       
    50  *
       
    51  * * `addfname`, name of the json controller method to call to add a relation
       
    52  *
       
    53  * * `msg`, message to display to the user when a relation has been added
       
    54  *
       
    55  * * `oklabel`/`cancellabel`, OK/cancel buttons label
       
    56  *
       
    57  * * `separator`, items separator if the field is multi-valued (will be
       
    58  *   considered mono-valued when not specified)
       
    59  */
       
    60 function ajaxBoxShowSelector(boxid, eid,
       
    61                              unrelfname,
       
    62                              addfname, msg,
       
    63                              oklabel, cancellabel,
       
    64                              separator) {
       
    65     var holderid = cw.utils.domid(boxid) + eid + 'Holder';
       
    66     var holder = $('#' + holderid);
       
    67     if (holder.children().length) {
       
    68         holder.empty();
       
    69     }
       
    70     else {
       
    71         var inputid = holderid + 'Input';
       
    72         var deferred = loadRemote(AJAX_BASE_URL, ajaxFuncArgs(unrelfname, null, eid));
       
    73         deferred.addCallback(function (unrelated) {
       
    74             var input = INPUT({'type': 'text', 'id': inputid, 'size': 20});
       
    75             holder.append(input).show();
       
    76             var $input = $(input);
       
    77             $input.keypress(function (evt) {
       
    78                 if (evt.keyCode == $.ui.keyCode.ENTER) {
       
    79                     ajaxBoxValidateSelectorInput(boxid, eid, separator, addfname, msg);
       
    80                 }
       
    81             });
       
    82             $input.cwautocomplete(unrelated, {multiple: Boolean(separator)});
       
    83             var buttons = DIV({'class' : "sgformbuttons"},
       
    84                               A({href : "javascript: $.noop();",
       
    85                                  onclick : cw.utils.strFuncCall('ajaxBoxValidateSelectorInput',
       
    86                                                                 boxid, eid, separator, addfname, msg)},
       
    87                                 oklabel),
       
    88                               ' / ',
       
    89                               A({'href' : "javascript: $.noop();",
       
    90                                  'onclick' : '$("#' + holderid + '").empty()'},
       
    91                                   cancellabel));
       
    92             holder.append(buttons);
       
    93             $input.focus();
       
    94         });
       
    95     }
       
    96 }