web/data/cubicweb.ajax.box.js
changeset 5949 2a273c896a38
child 6215 759cf097f5aa
equal deleted inserted replaced
5948:4154bdc85fe4 5949:2a273c896a38
       
     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('json', ajaxFuncArgs(fname, null, eid, value));
       
    17     d.addCallback(function() {
       
    18 	    $('#' + holderid).empty();
       
    19 	    var formparams = ajaxFuncArgs('render', null, 'boxes', boxid, eid);
       
    20 	    $('#' + cw.utils.domid(boxid) + eid).loadxhtml('json', formparams);
       
    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('json', ajaxFuncArgs(delfname, null, eid, relatedeid));
       
    30     d.addCallback(function() {
       
    31 	    var formparams = ajaxFuncArgs('render', null, 'boxes', boxid, eid);
       
    32 	    $('#' + cw.utils.domid(boxid) + eid).loadxhtml('json', formparams);
       
    33 	    if (msg) {
       
    34 		document.location.hash = '#header';
       
    35 		updateMessage(msg);
       
    36 	    }
       
    37     });
       
    38 }
       
    39 
       
    40 function ajaxBoxShowSelector(boxid, eid,
       
    41 			     unrelfname,
       
    42 			     addfname, msg,
       
    43 			     oklabel, cancellabel,
       
    44 			     separator) {
       
    45     var holderid = cw.utils.domid(boxid) + eid + 'Holder';
       
    46     var holder = $('#' + holderid);
       
    47     if (holder.children().length) {
       
    48 	holder.empty();
       
    49     }
       
    50     else {
       
    51 	var inputid = holderid + 'Input';
       
    52 	var deferred = loadRemote('json', ajaxFuncArgs(unrelfname, null, eid));
       
    53 	deferred.addCallback(function (unrelated) {
       
    54 	    var input = INPUT({'type': 'text', 'id': inputid, 'size': 20});
       
    55 	    holder.append(input).show();
       
    56 	    $input = $(input);
       
    57 	    $input.keypress(function (event) {
       
    58 		if (event.keyCode == KEYS.KEY_ENTER) {
       
    59 		    // XXX not very user friendly: we should test that the suggestions
       
    60 		    //     aren't visible anymore
       
    61 		    ajaxBoxValidateSelectorInput(boxid, eid, separator, addfname, msg);
       
    62 		}
       
    63 	    });
       
    64 	    var buttons = DIV({'class' : "sgformbuttons"},
       
    65 			      A({'href' : "javascript: noop();",
       
    66 				 'onclick' : cw.utils.strFuncCall('ajaxBoxValidateSelectorInput',
       
    67 								  boxid, eid, separator, addfname, msg)},
       
    68 				  oklabel),
       
    69 			      ' / ',
       
    70 			      A({'href' : "javascript: noop();",
       
    71 				 'onclick' : '$("#' + holderid + '").empty()'},
       
    72 				  cancellabel));
       
    73 	    holder.append(buttons);
       
    74 	    $input.autocomplete(unrelated, {
       
    75 		multiple: separator,
       
    76 		max: 15
       
    77 	    });
       
    78 	    $input.focus();
       
    79 	});
       
    80     }
       
    81 }