web/data/cubicweb.ajax.box.js
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Fri, 21 Jun 2013 12:08:06 +0200
changeset 9059 795ea498225f
parent 8260 5a81fa526b30
child 9381 6d97e80b2e97
permissions -rw-r--r--
[cwuser] make CWUser callable, returning self for dbapi compatibility In the dbapi, Connection and Session have a ``user`` method to generated a user for a request In the repo api, Connection and Session have a user attribute inherited from SessionRequestBase prototype. This ugly hack allows to not break user of the user method. To ease transition from dbapi to repoapi we need the ClientConnection to be as compatible as possible with the dbapi. Adding this method goes in this direction. It'll get deprecated in the deprecation wave that will conclude the repoapi refactoring. related to #2503918

/**
 * 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(AJAX_BASE_URL, ajaxFuncArgs(fname, null, eid, value));
    d.addCallback(function() {
            $('#' + holderid).empty();
            var formparams = ajaxFuncArgs('render', null, 'ctxcomponents', boxid, eid);
            $('#' + cw.utils.domid(boxid) + eid).loadxhtml(AJAX_BASE_URL, formparams);
            if (msg) {
                document.location.hash = '#header';
                updateMessage(msg);
            }
        });
}

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

/**
 * .. function:: ajaxBoxShowSelector(boxid, eid, unrelfname,
 *                                  addfname, msg,
 *                                  oklabel, cancellabel,
 *                                  separator=None)
 *
 * Display an ajax selector within a box of regid `boxid`, for entity with eid
 * `eid`.
 *
 * Other parameters are:
 *
 * * `addfname`, name of the json controller method to call to add a relation
 *
 * * `msg`, message to display to the user when a relation has been added
 *
 * * `oklabel`/`cancellabel`, OK/cancel buttons label
 *
 * * `separator`, items separator if the field is multi-valued (will be
 *   considered mono-valued when not specified)
 */
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(AJAX_BASE_URL, ajaxFuncArgs(unrelfname, null, eid));
        deferred.addCallback(function (unrelated) {
            var input = INPUT({'type': 'text', 'id': inputid, 'size': 20});
            holder.append(input).show();
            var $input = $(input);
            $input.keypress(function (evt) {
                if (evt.keyCode == $.ui.keyCode.ENTER) {
                    ajaxBoxValidateSelectorInput(boxid, eid, separator, addfname, msg);
                }
            });
            $input.cwautocomplete(unrelated, {multiple: Boolean(separator)});
            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.focus();
        });
    }
}