web/data/cubicweb.ajax.box.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 25 Apr 2013 16:10:56 +0200
changeset 8928 f5b40b66d36e
parent 8260 5a81fa526b30
child 9381 6d97e80b2e97
permissions -rw-r--r--
[notification] introduce an official `notify_on_commit` function It provides a proper notification api instead of replacing it by yet another clumsy one. Closes #2837251
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
/**
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
 * Functions for ajax boxes.
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
 *
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
 *  :organization: Logilab
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
 *  :copyright: 2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
 *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
 *
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
 */
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
function ajaxBoxValidateSelectorInput(boxid, eid, separator, fname, msg) {
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
    var holderid = cw.utils.domid(boxid) + eid + 'Holder';
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
    var value = $('#' + holderid + 'Input').val();
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
    if (separator) {
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    14
        value = $.map(value.split(separator), jQuery.trim);
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
    }
8260
5a81fa526b30 [ajax] use AJAX_BASE_URL instead of JSON_BASE_URL
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6987
diff changeset
    16
    var d = loadRemote(AJAX_BASE_URL, ajaxFuncArgs(fname, null, eid, value));
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
    d.addCallback(function() {
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    18
            $('#' + holderid).empty();
6947
3d72028a6cd4 [js] using the actual registry fixes spurious warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6448
diff changeset
    19
            var formparams = ajaxFuncArgs('render', null, 'ctxcomponents', boxid, eid);
8260
5a81fa526b30 [ajax] use AJAX_BASE_URL instead of JSON_BASE_URL
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6987
diff changeset
    20
            $('#' + cw.utils.domid(boxid) + eid).loadxhtml(AJAX_BASE_URL, formparams);
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    21
            if (msg) {
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    22
                document.location.hash = '#header';
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    23
                updateMessage(msg);
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    24
            }
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    25
        });
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
}
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
function ajaxBoxRemoveLinkedEntity(boxid, eid, relatedeid, delfname, msg) {
8260
5a81fa526b30 [ajax] use AJAX_BASE_URL instead of JSON_BASE_URL
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6987
diff changeset
    29
    var d = loadRemote(AJAX_BASE_URL, ajaxFuncArgs(delfname, null, eid, relatedeid));
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
    d.addCallback(function() {
6947
3d72028a6cd4 [js] using the actual registry fixes spurious warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6448
diff changeset
    31
            var formparams = ajaxFuncArgs('render', null, 'ctxcomponents', boxid, eid);
8260
5a81fa526b30 [ajax] use AJAX_BASE_URL instead of JSON_BASE_URL
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6987
diff changeset
    32
            $('#' + cw.utils.domid(boxid) + eid).loadxhtml(AJAX_BASE_URL, formparams);
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    33
            if (msg) {
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    34
                document.location.hash = '#header';
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    35
                updateMessage(msg);
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    36
            }
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
    });
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
}
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
6987
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    40
/**
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    41
 * .. function:: ajaxBoxShowSelector(boxid, eid, unrelfname,
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    42
 *                                  addfname, msg,
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    43
 *                                  oklabel, cancellabel,
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    44
 *                                  separator=None)
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    45
 *
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    46
 * Display an ajax selector within a box of regid `boxid`, for entity with eid
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    47
 * `eid`.
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    48
 *
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    49
 * Other parameters are:
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    50
 *
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    51
 * * `addfname`, name of the json controller method to call to add a relation
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    52
 *
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    53
 * * `msg`, message to display to the user when a relation has been added
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    54
 *
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    55
 * * `oklabel`/`cancellabel`, OK/cancel buttons label
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    56
 *
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    57
 * * `separator`, items separator if the field is multi-valued (will be
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    58
 *   considered mono-valued when not specified)
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    59
 */
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
function ajaxBoxShowSelector(boxid, eid,
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    61
                             unrelfname,
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    62
                             addfname, msg,
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    63
                             oklabel, cancellabel,
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    64
                             separator) {
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
    var holderid = cw.utils.domid(boxid) + eid + 'Holder';
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
    var holder = $('#' + holderid);
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
    if (holder.children().length) {
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    68
        holder.empty();
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
    }
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
    else {
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    71
        var inputid = holderid + 'Input';
8260
5a81fa526b30 [ajax] use AJAX_BASE_URL instead of JSON_BASE_URL
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6987
diff changeset
    72
        var deferred = loadRemote(AJAX_BASE_URL, ajaxFuncArgs(unrelfname, null, eid));
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    73
        deferred.addCallback(function (unrelated) {
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    74
            var input = INPUT({'type': 'text', 'id': inputid, 'size': 20});
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    75
            holder.append(input).show();
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6215
diff changeset
    76
            var $input = $(input);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6215
diff changeset
    77
            $input.keypress(function (evt) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6215
diff changeset
    78
                if (evt.keyCode == $.ui.keyCode.ENTER) {
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    79
                    ajaxBoxValidateSelectorInput(boxid, eid, separator, addfname, msg);
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    80
                }
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    81
            });
6987
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    82
            $input.cwautocomplete(unrelated, {multiple: Boolean(separator)});
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    83
            var buttons = DIV({'class' : "sgformbuttons"},
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6215
diff changeset
    84
                              A({href : "javascript: noop();",
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6215
diff changeset
    85
                                 onclick : cw.utils.strFuncCall('ajaxBoxValidateSelectorInput',
6987
d62d4ba5ef3e [ajax box] fix ajaxBoxShowSelector w/ mono-valued fields + minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6947
diff changeset
    86
                                                                boxid, eid, separator, addfname, msg)},
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6215
diff changeset
    87
                                oklabel),
6215
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    88
                              ' / ',
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    89
                              A({'href' : "javascript: noop();",
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    90
                                 'onclick' : '$("#' + holderid + '").empty()'},
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    91
                                  cancellabel));
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    92
            holder.append(buttons);
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    93
            $input.focus();
759cf097f5aa [javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5949
diff changeset
    94
        });
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
    }
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
}