web/data/cubicweb.widgets.js
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 22 Oct 2014 14:39:51 +0200
changeset 10098 e8889211f256
parent 10027 292c81246347
child 10232 cda1bdc3652e
permissions -rw-r--r--
[web] Stop patching jquery.treeview.js This logically reverts part of e9b7cd2e9012 "allow treeview to work correctly in a tab #345293". The treeview and ajax code have suffered a number of changes since then, this change doesn't seem to be necessary anymore. It is most likely unneeded since f65208c9dbdc "[javascript] use jQuery.one('ajax-loaded') instead of jQuery.bind() in add_onload to avoid multiple callback executions".
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
     1
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
     2
 * Functions dedicated to widgets.
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
     3
 *
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
 *  :organization: Logilab
5767
1d811df051c2 [packaging] MANIFEST.in cleanups, update js/css copyright, remove no more used files
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4901
diff changeset
     5
 *  :copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
 *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
// widget namespace
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
Widgets = {};
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    14
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    15
 * .. function:: buildWidget(wdgnode)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    16
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    17
 * this function takes a DOM node defining a widget and
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
 * instantiates / builds the appropriate widget class
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
function buildWidget(wdgnode) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
    var wdgclass = Widgets[wdgnode.getAttribute('cubicweb:wdgtype')];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
    if (wdgclass) {
6372
4c3e2a92e340 timeline view minor enhancements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
    23
        return new wdgclass(wdgnode);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    }
6372
4c3e2a92e340 timeline view minor enhancements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
    25
    return null;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    28
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    29
 * .. function:: buildWidgets(root)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    30
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    31
 * This function is called on load and is in charge to build
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
 * JS widgets according to DOM nodes found in the page
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
function buildWidgets(root) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    root = root || document;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    jQuery(root).find('.widget').each(function() {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    37
        if (this.getAttribute('cubicweb:loadtype') == 'auto') {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    38
            buildWidget(this);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    39
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    43
jQuery(document).ready(function() {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    44
    buildWidgets();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    45
});
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    47
function postJSON(url, data, callback) {
8260
5a81fa526b30 [ajax] use AJAX_BASE_URL instead of JSON_BASE_URL
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7943
diff changeset
    48
    return jQuery.post(url, data, callback, AJAX_BASE_URL);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    49
}
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    50
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    51
function getJSON(url, data, callback) {
8260
5a81fa526b30 [ajax] use AJAX_BASE_URL instead of JSON_BASE_URL
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7943
diff changeset
    52
    return jQuery.get(url, data, callback, AJAX_BASE_URL);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    53
}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    55
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    56
(function ($) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    57
    var defaultSettings = {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    58
        initialvalue: '',
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    59
        multiple: false,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    60
        mustMatch: false,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    61
        delay: 50,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    62
        limit: 50
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    63
    };
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    64
    function split(val) { return val.split( /\s*,\s*/ ); }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    65
    function extractLast(term) { return split(term).pop(); }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    66
    function allButLast(val) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    67
        var terms = split(val);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    68
        terms.pop();
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    69
        return terms;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    70
    }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    71
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    72
    var methods = {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    73
        __init__: function(suggestions, options) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    74
            return this.each(function() {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    75
                // here, `this` refers to the DOM element (e.g. input) being wrapped
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    76
                // by cwautomplete plugin
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    77
                var instanceData = $(this).data('cwautocomplete');
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    78
                if (instanceData) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    79
                    // already initialized
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    80
                    return;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    81
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    82
                var settings = $.extend({}, defaultSettings, options);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    83
                instanceData =  {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    84
                    initialvalue: settings.initialvalue,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    85
                    userInput: this,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    86
                    hiddenInput: null
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    87
                };
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    88
                var hiHandlers = methods.hiddenInputHandlers;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    89
                $(this).data('cwautocomplete', instanceData);
6739
08cfa7019b27 [js] fix initial value for autocomplete
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6450
diff changeset
    90
                // in case of an existing value, the hidden input must be initialized even if
08cfa7019b27 [js] fix initial value for autocomplete
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6450
diff changeset
    91
                // the value is not changed
08cfa7019b27 [js] fix initial value for autocomplete
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6450
diff changeset
    92
                if (($(instanceData.userInput).attr('cubicweb:initialvalue') !== undefined) && !instanceData.hiddenInput){
08cfa7019b27 [js] fix initial value for autocomplete
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6450
diff changeset
    93
                    hiHandlers.initializeHiddenInput(instanceData);
08cfa7019b27 [js] fix initial value for autocomplete
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6450
diff changeset
    94
                }
9388
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
    95
                $.ui.autocomplete.prototype._value = methods._value;
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
    96
                $.data(this, 'settings', settings);
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    97
                if (settings.multiple) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    98
                    $.ui.autocomplete.filter = methods.multiple.makeFilter(this);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
    99
                    $(this).bind({
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   100
                        autocompleteselect: methods.multiple.select,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   101
                        autocompletefocus: methods.multiple.focus,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   102
                        keydown: methods.multiple.keydown
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   103
                        });
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   104
                }
6450
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   105
                // XXX katia we dont need it if minLength == 0, but by setting minLength = 0
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   106
                // we probably break the backward compatibility
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   107
                $(this).bind('blur', methods.blur);
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   108
                if ($.isArray(suggestions)) { // precomputed list of suggestions
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   109
                    settings.source = hiHandlers.checkSuggestionsDataFormat(instanceData, suggestions);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   110
                } else { // url to call each time something is typed
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   111
                    settings.source = function(request, response) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   112
                        var d = loadRemote(suggestions, {q: request.term, limit: settings.limit}, 'POST');
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   113
                        d.addCallback(function (suggestions) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   114
                            suggestions = hiHandlers.checkSuggestionsDataFormat(instanceData, suggestions);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   115
                            response(suggestions);
6450
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   116
                            if((suggestions.length) == 0){
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   117
                                methods.resetValues(instanceData);
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   118
                            }
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   119
                        });
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   120
                    };
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   121
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   122
                $(this).autocomplete(settings);
6450
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   123
                if (settings.mustMatch) {
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   124
                    $(this).keypress(methods.ensureExactMatch);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   125
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   126
            });
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   127
        },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   128
9388
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   129
        _value: function() {
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   130
            /* We extend the widget with the ability to lookup and
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   131
               handle several terms at once ('multiple' option). E.g.:
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   132
               toto, titi, tu....  The autocompletion must be
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   133
               performed only on the last of such a list of terms.
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   134
              */
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   135
            var settings = $(this.element).data('settings');
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   136
            var value = this.valueMethod.apply( this.element, arguments );
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   137
            if (settings.multiple & arguments.length === 0) {
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   138
                return extractLast(value);
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   139
            }
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   140
            return value
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   141
        },
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   142
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   143
        multiple: {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   144
            focus: function() {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   145
                // prevent value inserted on focus
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   146
                return false;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   147
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   148
            select: function(event, ui) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   149
                var terms = allButLast(this.value);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   150
                // add the selected item
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   151
                terms.push(ui.item.value);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   152
                // add placeholder to get the comma-and-space at the end
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   153
                terms.push("");
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   154
                this.value = terms.join( ", " );
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   155
                return false;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   156
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   157
            keydown: function(evt) {
9388
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   158
                if (evt.keyCode == $.ui.keyCode.TAB) {
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   159
                    evt.preventDefault();
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   160
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   161
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   162
            makeFilter: function(userInput) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   163
                return function(array, term) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   164
                    // remove already entered terms from suggestion list
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   165
                    array = cw.utils.difference(array, allButLast(userInput.value));
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   166
                    var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   167
                    return $.grep( array, function(value) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   168
                        return matcher.test( value.label || value.value || value );
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   169
                    });
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   170
                };
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   171
            }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   172
        },
6450
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   173
        blur: function(evt){
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   174
            var instanceData = $(this).data('cwautocomplete');
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   175
            if($(instanceData.userInput).val().strip().length==0){
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   176
                methods.resetValues(instanceData);
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   177
            }
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   178
        },
9388
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   179
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   180
        ensureExactMatch: function(evt) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   181
            var instanceData = $(this).data('cwautocomplete');
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   182
            if (evt.keyCode == $.ui.keyCode.ENTER || evt.keyCode == $.ui.keyCode.TAB) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   183
                var validChoices = $.map($('ul.ui-autocomplete li'),
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   184
                                         function(li) {return $(li).text();});
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   185
                if ($.inArray($(instanceData.userInput).val(), validChoices) == -1) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   186
                    $(instanceData.userInput).val('');
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   187
                    $(instanceData.hiddenInput).val(instanceData.initialvalue || '');
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   188
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   189
            }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   190
        },
9388
4c34a63bd113 update jquery to 1.10 (closes #2786674)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9377
diff changeset
   191
6450
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   192
        resetValues: function(instanceData){
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   193
            $(instanceData.userInput).val('');
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   194
            $(instanceData.hiddenInput).val('');
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   195
        },
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   196
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   197
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   198
        hiddenInputHandlers: {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   199
            /**
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   200
             * `hiddenInputHandlers` defines all methods specific to handle the
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   201
             * hidden input created along the standard text input.
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   202
             * An hiddenInput is necessary when displayed suggestions are
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   203
             * different from actual values to submit.
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   204
             * Imagine an autocompletion widget to choose among a list of CWusers.
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   205
             * Suggestions would be the list of logins, but actual values would
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   206
             * be the corresponding eids.
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   207
             * To handle such cases, suggestions list should be a list of JS objects
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   208
             * with two `label` and `value` properties.
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   209
             **/
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   210
            suggestionSelected: function(evt, ui) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   211
                var instanceData = $(this).data('cwautocomplete');
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   212
                instanceData.hiddenInput.value = ui.item.value;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   213
                instanceData.value = ui.item.label;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   214
                return false; // stop propagation
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   215
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   216
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   217
            suggestionFocusChanged: function(evt, ui) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   218
                var instanceData = $(this).data('cwautocomplete');
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   219
                instanceData.userInput.value = ui.item.label;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   220
                return false; // stop propagation
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   221
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   222
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   223
            needsHiddenInput: function(suggestions) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   224
                return suggestions[0].label !== undefined;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   225
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   226
            initializeHiddenInput: function(instanceData) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   227
                var userInput = instanceData.userInput;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   228
                var hiddenInput = INPUT({
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   229
                    type: "hidden",
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   230
                    name: userInput.name,
6450
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   231
                    // XXX katia : this must be handeled in .SuggestField widget, but
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   232
                    // it seems not to be used anymore
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   233
                    value: $(userInput).attr('cubicweb:initialvalue') || userInput.value
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   234
                });
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   235
                $(userInput).removeAttr('name').after(hiddenInput);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   236
                instanceData.hiddenInput = hiddenInput;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   237
                $(userInput).bind({
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   238
                    autocompleteselect: methods.hiddenInputHandlers.suggestionSelected,
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   239
                    autocompletefocus: methods.hiddenInputHandlers.suggestionFocusChanged
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   240
                });
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   241
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   242
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   243
            /*
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   244
             * internal convenience function: old jquery plugin accepted to be fed
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   245
             * with a list of couples (value, label). The new (jquery-ui) autocomplete
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   246
             * plugin expects a list of objects with "value" and "label" properties.
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   247
             *
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   248
             * This function converts the old format to the new one.
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   249
             */
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   250
            checkSuggestionsDataFormat: function(instanceData, suggestions) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   251
                // check for old (value, label) format
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   252
                if ($.isArray(suggestions) && suggestions.length &&
6450
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   253
                    $.isArray(suggestions[0])){
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   254
                    if (suggestions[0].length == 2) {
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   255
                        cw.log('[3.10] autocomplete init func should return {label,value} dicts instead of lists');
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   256
                        suggestions = $.map(suggestions, function(sugg) {
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   257
                                            return {value: sugg[0], label: sugg[1]};
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   258
                                            });
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   259
                    } else {
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   260
                        if(suggestions[0].length == 1){
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   261
                            suggestions = $.map(suggestions, function(sugg) {
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   262
                                return {value: sugg[0], label: sugg[0]};
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   263
                            });
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   264
                        }
c23639f26ec6 small typo on mustMach, handle initialvalue, handle empty userInput
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6448
diff changeset
   265
                    }
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   266
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   267
                var hiHandlers = methods.hiddenInputHandlers;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   268
                if (suggestions.length && hiHandlers.needsHiddenInput(suggestions)
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   269
                    && !instanceData.hiddenInput) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   270
                    hiHandlers.initializeHiddenInput(instanceData);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   271
                    hiHandlers.fixUserInputInitialValue(instanceData, suggestions);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   272
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   273
                // otherwise, assume data shape is correct
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   274
                return suggestions;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   275
            },
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   276
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   277
            fixUserInputInitialValue: function(instanceData, suggestions) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   278
                // called when the data is loaded to reset the correct displayed
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   279
                // value in the visible input field (typically replacing an eid
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   280
                // by a displayable value)
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   281
                var curvalue = instanceData.userInput.value;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   282
                if (!curvalue) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   283
                    return;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   284
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   285
                for (var i=0, length=suggestions.length; i < length; i++) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   286
                    var sugg = suggestions[i];
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   287
                    if (sugg.value == curvalue) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   288
                        instanceData.userInput.value = sugg.label;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   289
                        return;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   290
                    }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   291
                }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   292
            }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   293
        }
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   294
    };
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   295
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   296
    $.fn.cwautocomplete = function(data, options) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   297
        return methods.__init__.apply(this, [data, options]);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   298
    };
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   299
})(jQuery);
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   300
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   301
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
Widgets.SuggestField = defclass('SuggestField', null, {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
    __init__: function(node, options) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   304
        options = options || {};
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   305
        var multi = node.getAttribute('cubicweb:multi');
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   306
        options.multiple = (multi == "yes") ? true: false;
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   307
        var d = loadRemote(node.getAttribute('cubicweb:dataurl'));
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   308
        d.addCallback(function(data) {
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   309
            $(node).cwautocomplete(data, options);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   310
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
Widgets.StaticFileSuggestField = defclass('StaticSuggestField', [Widgets.SuggestField], {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   316
    __init__: function(node) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   317
        Widgets.SuggestField.__init__(this, node, {
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   318
            method: 'get' // XXX
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   319
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
Widgets.RestrictedSuggestField = defclass('RestrictedSuggestField', [Widgets.SuggestField], {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   325
    __init__: function(node) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   326
        Widgets.SuggestField.__init__(this, node, {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   327
            mustMatch: true
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   328
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
    }
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   330
});
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   332
//remote version of RestrictedSuggestField
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   333
Widgets.LazySuggestField = defclass('LazySuggestField', [Widgets.SuggestField], {
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   334
    __init__: function(node, options) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   335
        var self = this;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   336
        options = options || {};
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   337
        options.delay = 50;
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   338
        // multiple selection not supported yet (still need to formalize correctly
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   339
        // initial values / display values)
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   340
        var initialvalue = cw.evalJSON(node.getAttribute('cubicweb:initialvalue') || 'null');
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   341
        if (!initialvalue) {
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   342
            initialvalue = node.value;
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   343
        }
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   344
        options.initialvalue = initialvalue;
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6372
diff changeset
   345
        Widgets.SuggestField.__init__(this, node, options);
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   346
    }
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   347
});
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   348
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   349
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   350
 * .. class:: Widgets.SuggestForm
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   351
 *
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
 * suggestform displays a suggest field and associated validate / cancel buttons
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
 * constructor's argumemts are the same that BaseSuggestField widget
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   355
Widgets.SuggestForm = defclass("SuggestForm", null, {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   357
    __init__: function(inputid, initfunc, varargs, validatefunc, options) {
10027
292c81246347 [web] There is no global noop(), use jQuery's instead (closes #4487832)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9529
diff changeset
   358
        this.validatefunc = validatefunc || $.noop;
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   359
        this.sgfield = new Widgets.BaseSuggestField(inputid, initfunc, varargs, options);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   360
        this.oklabel = options.oklabel || 'ok';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   361
        this.cancellabel = options.cancellabel || 'cancel';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   362
        bindMethods(this);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   363
        connect(this.sgfield, 'validate', this, this.entryValidated);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   366
    show: function(parentnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   367
        var sgnode = this.sgfield.builddom();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   368
        var buttons = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   369
            'class': "sgformbuttons"
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   370
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   371
        [A({
10027
292c81246347 [web] There is no global noop(), use jQuery's instead (closes #4487832)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9529
diff changeset
   372
            'href': "javascript: $.noop();",
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   373
            'onclick': this.onValidateClicked
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   374
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   375
        this.oklabel), ' / ', A({
10027
292c81246347 [web] There is no global noop(), use jQuery's instead (closes #4487832)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9529
diff changeset
   376
            'href': "javascript: $.noop();",
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   377
            'onclick': this.destroy
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   378
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   379
        escapeHTML(this.cancellabel))]);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   380
        var formnode = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   381
            'class': "sgform"
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   382
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   383
        [sgnode, buttons]);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   384
        appendChildNodes(parentnode, formnode);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   385
        this.sgfield.textinput.focus();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   386
        this.formnode = formnode;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   387
        return formnode;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   390
    destroy: function() {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   391
        signal(this, 'destroy');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   392
        this.sgfield.destroy();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   393
        removeElement(this.formnode);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   396
    onValidateClicked: function() {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   397
        this.validatefunc(this, this.sgfield.taglist());
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
    /* just an indirection to pass the form instead of the sgfield as first parameter */
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   400
    entryValidated: function(sgfield, taglist) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   401
        this.validatefunc(this, taglist);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   403
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   405
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   406
 * .. function:: toggleTree(event)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   407
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   408
 * called when the use clicks on a tree node
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
 *  - if the node has a `cubicweb:loadurl` attribute, replace the content of the node
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   410
 *    by the url's content.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   411
 *  - else, there's nothing to do, let the jquery plugin handle it.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   412
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   413
function toggleTree(event) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   414
    var linode = jQuery(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   415
    var url = linode.attr('cubicweb:loadurl');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
    if (url) {
4738
6cca4f602486 fix open/close/open subnode bug #730709
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3079
diff changeset
   417
        linode.find('ul.placeholder').remove();
9529
39b46b0b01e4 [web/data] fix treeview regression (closes #3526466)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9388
diff changeset
   418
        var d = linode.loadxhtml(url, null, 'post', 'append');
39b46b0b01e4 [web/data] fix treeview regression (closes #3526466)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9388
diff changeset
   419
        d.addCallback(function(domnode) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   420
                linode.removeAttr('cubicweb:loadurl');
9529
39b46b0b01e4 [web/data] fix treeview regression (closes #3526466)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9388
diff changeset
   421
                linode.find('> ul.treeview').treeview({
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   422
                    toggle: toggleTree,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   423
                    prerendered: true
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   424
                });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   425
                return null;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   426
            }
9529
39b46b0b01e4 [web/data] fix treeview regression (closes #3526466)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9388
diff changeset
   427
        );
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   431
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   432
 * .. class:: Widgets.TimelineWidget
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   433
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   434
 * widget based on SIMILE's timeline widget
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
 * http://code.google.com/p/simile-widgets/
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   437
 * Beware not to mess with SIMILE's Timeline JS namepsace !
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   439
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   440
Widgets.TimelineWidget = defclass("TimelineWidget", null, {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   441
    __init__: function(wdgnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   442
        var tldiv = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   443
            id: "tl",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   444
            style: 'height: 200px; border: 1px solid #ccc;'
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   445
        });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   446
        wdgnode.appendChild(tldiv);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   447
        var tlunit = wdgnode.getAttribute('cubicweb:tlunit') || 'YEAR';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   448
        var eventSource = new Timeline.DefaultEventSource();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   449
        var bandData = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   450
            eventPainter: Timeline.CubicWebEventPainter,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   451
            eventSource: eventSource,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   452
            width: "100%",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   453
            intervalUnit: Timeline.DateTime[tlunit.toUpperCase()],
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   454
            intervalPixels: 100
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   455
        };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   456
        var bandInfos = [Timeline.createBandInfo(bandData)];
6372
4c3e2a92e340 timeline view minor enhancements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   457
        this.tl = Timeline.create(tldiv, bandInfos);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   458
        var loadurl = wdgnode.getAttribute('cubicweb:loadurl');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   459
        Timeline.loadJSON(loadurl, function(json, url) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   460
            eventSource.loadJSON(json, url);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   461
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   465
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
Widgets.TemplateTextField = defclass("TemplateTextField", null, {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   468
    __init__: function(wdgnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   469
        this.variables = jQuery(wdgnode).attr('cubicweb:variables').split(',');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   470
        this.options = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   471
            name: wdgnode.getAttribute('cubicweb:inputid'),
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   472
            rows: wdgnode.getAttribute('cubicweb:rows') || 40,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   473
            cols: wdgnode.getAttribute('cubicweb:cols') || 80
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   474
        };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   475
        // this.variableRegexp = /%\((\w+)\)s/;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   476
        this.errorField = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   477
            'class': "errorMessage"
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   478
        });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   479
        this.textField = TEXTAREA(this.options);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   480
        jQuery(this.textField).bind('keyup', {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   481
            'self': this
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   482
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   483
        this.highlightInvalidVariables);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   484
        jQuery('#substitutions').prepend(this.errorField);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   485
        jQuery('#substitutions .errorMessage').hide();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   486
        wdgnode.appendChild(this.textField);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
    /* signal callbacks */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   491
    highlightInvalidVariables: function(event) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   492
        var self = event.data.self;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   493
        var text = self.textField.value;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   494
        var unknownVariables = [];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   495
        var it = 0;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   496
        var group = null;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   497
        var variableRegexp = /%\((\w+)\)s/g;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   498
        // emulates rgx.findAll()
7258
2e7f0d6fa2d6 [js] fix inArray usage in reorderTabIndex() : inArray returns -1 if element is not found, not 'false'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6890
diff changeset
   499
        while ( (group = variableRegexp.exec(text)) ) {
2e7f0d6fa2d6 [js] fix inArray usage in reorderTabIndex() : inArray returns -1 if element is not found, not 'false'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6890
diff changeset
   500
            if ($.inArray(group[1], self.variables) == -1) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   501
                unknownVariables.push(group[1]);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   502
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   503
            it++;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   504
            if (it > 5) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   505
                break;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   506
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   507
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   508
        var errText = '';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   509
        if (unknownVariables.length) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   510
            errText = "Detected invalid variables : " + unknownVariables.join(', ');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   511
            jQuery('#substitutions .errorMessage').show();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   512
        } else {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   513
            jQuery('#substitutions .errorMessage').hide();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   514
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   515
        self.errorField.innerHTML = errText;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   518
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   519
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   520
cw.widgets = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   521
    /**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   522
     * .. function:: insertText(text, areaId)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   523
     *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   524
     * inspects textarea with id `areaId` and replaces the current selected text
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   525
     * with `text`. Cursor is then set at the end of the inserted text.
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   526
     */
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   527
    insertText: function (text, areaId) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   528
        var textarea = jQuery('#' + areaId);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   529
        if (document.selection) { // IE
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   530
            var selLength;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   531
            textarea.focus();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   532
            var sel = document.selection.createRange();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   533
            selLength = sel.text.length;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   534
            sel.text = text;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   535
            sel.moveStart('character', selLength - text.length);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   536
            sel.select();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   537
        } else if (textarea.selectionStart || textarea.selectionStart == '0') { // mozilla
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   538
            var startPos = textarea.selectionStart;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   539
            var endPos = textarea.selectionEnd;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   540
            // insert text so that it replaces the [startPos, endPos] part
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   541
            textarea.value = textarea.value.substring(0, startPos) + text + textarea.value.substring(endPos, textarea.value.length);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   542
            // set cursor pos at the end of the inserted text
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   543
            textarea.selectionStart = textarea.selectionEnd = startPos + text.length;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   544
            textarea.focus();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   545
        } else { // safety belt for other browsers
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   546
            textarea.value += text;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   547
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   548
    }
5699
f4f6ee3af50b [javascript] move CubicWeb object declaration to cubicweb.js, remove CubicWeb.provide/require, use cw instead of CubicWeb
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
   549
};
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   550
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   551
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   552
// InOutWidget  This contains specific InOutnWidget javascript
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   553
// IE things can not handle hide/show options on select, this cloned list solition (should propably have 2 widgets)
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   554
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   555
(function ($) {
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   556
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   557
    var methods = {
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   558
        __init__: function(fromSelect, toSelect) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   559
            // closed over state
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   560
            var state = {'$fromNode' : $(cw.escape('#' + fromSelect)),
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   561
                         '$toNode'   : $(cw.escape('#' + toSelect)),
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   562
                         'name'      : this.attr('id')};
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   563
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   564
            function sortoptions($optionlist) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   565
                var $sorted = $optionlist.find('option').sort(function(opt1, opt2) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   566
                    return $(opt1).text() > $(opt2).text() ? 1 : -1;
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   567
                });
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   568
                // this somehow translates to an inplace sort
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   569
                $optionlist.append($sorted);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   570
            };
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   571
            sortoptions(state.$fromNode);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   572
            sortoptions(state.$toNode);
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   573
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   574
            // will move selected options from one list to the other
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   575
            // and call an option handler on each option
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   576
            function moveoptions ($fromlist, $tolist, opthandler) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   577
                $fromlist.find('option:selected').each(function(index, option) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   578
                    var $option = $(option);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   579
                    // add a new option to the target list
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   580
                    $tolist.append(OPTION({'value' : $option.val()},
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   581
	 			          $option.text()));
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   582
                    // process callback on the option
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   583
                    opthandler.call(null, $option);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   584
                    // remove option from the source list
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   585
                    $option.remove();
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   586
                });
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   587
                // re-sort both lists
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   588
                sortoptions($fromlist);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   589
                sortoptions($tolist);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   590
            };
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   591
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   592
            function addvalues () {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   593
                moveoptions(state.$fromNode, state.$toNode, function ($option) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   594
                    // add an hidden input for the edit controller
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   595
                    var hiddenInput = INPUT({
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   596
                        type: 'hidden', name: state.name,
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   597
                        value : $option.val()
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   598
                    });
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   599
                    state.$toNode.parent().append(hiddenInput);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   600
                });
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   601
            };
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   602
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   603
            function removevalues () {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   604
                moveoptions(state.$toNode, state.$fromNode, function($option) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   605
                    // remove hidden inputs for the edit controller
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   606
                    var selector = 'input[name=' + cw.escape(state.name) + ']'
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   607
                    state.$toNode.parent().find(selector).each(function(index, input) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   608
                        if ($(input).val() == $option.val()) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   609
                            $(input).remove();
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   610
                        }
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   611
                    });
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   612
                });
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   613
            };
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   614
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   615
            var $this = $(this);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   616
            $this.find('.cwinoutadd').bind( // 'add >>>' symbol
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   617
                'click', {'state' : state}, addvalues);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   618
            $this.find('.cwinoutremove').bind( // 'remove <<<' symbol
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   619
                'click', {'state' : state}, removevalues);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   620
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   621
            state.$fromNode.bind('dblclick', {'state': state}, addvalues);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   622
            state.$toNode.bind('dblclick', {'state': state}, removevalues);
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   623
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   624
        }
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   625
    };
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   626
    $.fn.cwinoutwidget = function(fromSelect, toSelect) {
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   627
        return methods.__init__.apply(this, [fromSelect, toSelect]);
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6739
diff changeset
   628
    };
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8260
diff changeset
   629
})(jQuery);