web/data/cubicweb.ajax.box.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 26 Aug 2010 11:35:02 +0200
branchstable
changeset 6160 12038ca95f0f
parent 5949 2a273c896a38
child 6215 759cf097f5aa
permissions -rw-r--r--
Added tag cubicweb-version-3.9.5 for changeset 0a1fce8ddc67

/**
 * Functions for ajax boxes.
 *
 *  :organization: Logilab
 *  :copyright: 2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 *
 */

function ajaxBoxValidateSelectorInput(boxid, eid, separator, fname, msg) {
    var holderid = cw.utils.domid(boxid) + eid + 'Holder';
    var value = $('#' + holderid + 'Input').val();
    if (separator) {
	value = $.map(value.split(separator), jQuery.trim);
    }
    var d = loadRemote('json', ajaxFuncArgs(fname, null, eid, value));
    d.addCallback(function() {
	    $('#' + holderid).empty();
	    var formparams = ajaxFuncArgs('render', null, 'boxes', boxid, eid);
	    $('#' + cw.utils.domid(boxid) + eid).loadxhtml('json', formparams);
	    if (msg) {
		document.location.hash = '#header';
		updateMessage(msg);
	    }
	});
}

function ajaxBoxRemoveLinkedEntity(boxid, eid, relatedeid, delfname, msg) {
    var d = loadRemote('json', ajaxFuncArgs(delfname, null, eid, relatedeid));
    d.addCallback(function() {
	    var formparams = ajaxFuncArgs('render', null, 'boxes', boxid, eid);
	    $('#' + cw.utils.domid(boxid) + eid).loadxhtml('json', formparams);
	    if (msg) {
		document.location.hash = '#header';
		updateMessage(msg);
	    }
    });
}

function ajaxBoxShowSelector(boxid, eid,
			     unrelfname,
			     addfname, msg,
			     oklabel, cancellabel,
			     separator) {
    var holderid = cw.utils.domid(boxid) + eid + 'Holder';
    var holder = $('#' + holderid);
    if (holder.children().length) {
	holder.empty();
    }
    else {
	var inputid = holderid + 'Input';
	var deferred = loadRemote('json', ajaxFuncArgs(unrelfname, null, eid));
	deferred.addCallback(function (unrelated) {
	    var input = INPUT({'type': 'text', 'id': inputid, 'size': 20});
	    holder.append(input).show();
	    $input = $(input);
	    $input.keypress(function (event) {
		if (event.keyCode == KEYS.KEY_ENTER) {
		    // XXX not very user friendly: we should test that the suggestions
		    //     aren't visible anymore
		    ajaxBoxValidateSelectorInput(boxid, eid, separator, addfname, msg);
		}
	    });
	    var buttons = DIV({'class' : "sgformbuttons"},
			      A({'href' : "javascript: noop();",
				 'onclick' : cw.utils.strFuncCall('ajaxBoxValidateSelectorInput',
								  boxid, eid, separator, addfname, msg)},
				  oklabel),
			      ' / ',
			      A({'href' : "javascript: noop();",
				 'onclick' : '$("#' + holderid + '").empty()'},
				  cancellabel));
	    holder.append(buttons);
	    $input.autocomplete(unrelated, {
		multiple: separator,
		max: 15
	    });
	    $input.focus();
	});
    }
}