web/data/cubicweb.widgets.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 15 Oct 2010 19:52:06 +0200
branchstable
changeset 6521 8c8529db5f83
parent 6372 4c3e2a92e340
child 6448 8590d82e9b1b
permissions -rw-r--r--
merge
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
// we need to differenciate cases where initFacetBoxEvents is called
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
// with one argument or without any argument. If we use `initFacetBoxEvents`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
// as the direct callback on the jQuery.ready event, jQuery will pass some argument
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
// of his, so we use this small anonymous function instead.
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    47
jQuery(document).ready(function() {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    48
    buildWidgets();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    49
});
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    51
function postJSON(url, data, callback) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    52
    return jQuery.post(url, data, callback, 'json');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    53
}
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    54
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    55
function getJSON(url, data, callback) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    56
    return jQuery.get(url, data, callback, 'json');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    57
}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
Widgets.SuggestField = defclass('SuggestField', null, {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    __init__: function(node, options) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    61
        var multi = node.getAttribute('cubicweb:multi') || "no";
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    62
        options = options || {};
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    63
        options.multiple = (multi == "yes") ? true: false;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    64
        var dataurl = node.getAttribute('cubicweb:dataurl');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        var method = postJSON;
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    66
        if (options.method == 'get') {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    67
            method = function(url, data, callback) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    68
                // We can't rely on jQuery.getJSON because the server
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    69
                // might set the Content-Type's response header to 'text/plain'
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    70
                jQuery.get(url, data, function(response) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    71
                    callback(cw.evalJSON(response));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    72
                });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    73
            };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    74
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    75
        var self = this; // closure
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    76
        method(dataurl, null, function(data) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    77
            // in case we received a list of couple, we assume that the first
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    78
            // element is the real value to be sent, and the second one is the
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    79
            // value to be displayed
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    80
            if (data.length && data[0].length == 2) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    81
                options.formatItem = function(row) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    82
                    return row[1];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    83
                };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    84
                self.hideRealValue(node);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    85
                self.setCurrentValue(node, data);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    86
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    87
            jQuery(node).autocomplete(data, options);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    88
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    hideRealValue: function(node) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    92
        var hidden = INPUT({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    93
            'type': "hidden",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    94
            'name': node.name,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    95
            'value': node.value
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    96
        });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    97
        node.parentNode.appendChild(hidden);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    98
        // remove 'name' attribute from visible input so that it is not submitted
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
    99
        // and set correct value in the corresponding hidden field
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   100
        jQuery(node).removeAttr('name').bind('result', function(_, row, _) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   101
            hidden.value = row[0];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   102
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    setCurrentValue: function(node, data) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   106
        // called when the data is loaded to reset the correct displayed
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   107
        // value in the visible input field (typically replacing an eid
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   108
        // by a displayable value)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   109
        var curvalue = node.value;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   110
        if (!node.value) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   111
            return;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   112
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   113
        for (var i = 0, length = data.length; i < length; i++) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   114
            var row = data[i];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   115
            if (row[0] == curvalue) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   116
                node.value = row[1];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   117
                return;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   118
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   119
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
Widgets.StaticFileSuggestField = defclass('StaticSuggestField', [Widgets.SuggestField], {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   125
    __init__: function(node) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   126
        Widgets.SuggestField.__init__(this, node, {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   127
            method: 'get'
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   128
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
Widgets.RestrictedSuggestField = defclass('RestrictedSuggestField', [Widgets.SuggestField], {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   135
    __init__: function(node) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   136
        Widgets.SuggestField.__init__(this, node, {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   137
            mustMatch: true
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   138
        });
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
});
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   142
//remote version of RestrictedSuggestField
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   143
Widgets.LazySuggestField = defclass('LazySuggestField', [Widgets.SuggestField], {
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   144
    __init__: function(node, options) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   145
        var self = this;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   146
        var multi = "no";
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   147
        options = options || {};
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   148
        options.max = 50;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   149
        options.delay = 50;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   150
        options.cacheLength = 0;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   151
        options.mustMatch = true;
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   152
        // 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
   153
        // initial values / display values)
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   154
        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
   155
        if (!initialvalue) {
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   156
            initialvalue = node.value;
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   157
        }
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   158
        options = jQuery.extend({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   159
            dataType: 'json',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   160
            multiple: (multi == "yes") ? true: false,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   161
            parse: this.parseResult
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   162
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   163
        options);
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   164
        var dataurl = node.getAttribute('cubicweb:dataurl');
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   165
        // remove 'name' from original input and add the hidden one that will
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   166
        // store the actual value
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   167
        var hidden = INPUT({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   168
            'type': "hidden",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   169
            'name': node.name,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   170
            'value': initialvalue
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   171
        });
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   172
        node.parentNode.appendChild(hidden);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   173
        jQuery(node).bind('result', {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   174
            hinput: hidden,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   175
            input: node
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   176
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   177
        self.hideRealValue).removeAttr('name').autocomplete(dataurl, options);
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   178
    },
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   180
    hideRealValue: function(evt, data, value) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   181
        if (!value) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   182
            value = "";
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   183
        }
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   184
        evt.data.hinput.value = value;
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   185
    },
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   186
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   187
    /*
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   188
     * @param data: a list of couple (value, label) to fill the suggestion list,
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   189
     *              (returned by CW through AJAX)
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   190
     */
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   191
    parseResult: function(data) {
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   192
        var parsed = [];
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   193
        for (var i = 0; i < data.length; i++) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   194
            var value = '' + data[i][0]; // a string is required later by jquery.autocomplete.js
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   195
            var label = data[i][1];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   196
            parsed[parsed.length] = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   197
                data: [label],
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   198
                value: value,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   199
                result: label
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   200
            };
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   201
        };
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   202
        return parsed;
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   203
    }
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   204
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   205
});
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4738
diff changeset
   206
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   207
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   208
 * .. class:: Widgets.SuggestForm
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   209
 *
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
 * suggestform displays a suggest field and associated validate / cancel buttons
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
 * constructor's argumemts are the same that BaseSuggestField widget
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
Widgets.SuggestForm = defclass("SuggestForm", null, {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   215
    __init__: function(inputid, initfunc, varargs, validatefunc, options) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   216
        this.validatefunc = validatefunc || noop;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   217
        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
   218
        this.oklabel = options.oklabel || 'ok';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   219
        this.cancellabel = options.cancellabel || 'cancel';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   220
        bindMethods(this);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   221
        connect(this.sgfield, 'validate', this, this.entryValidated);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   224
    show: function(parentnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   225
        var sgnode = this.sgfield.builddom();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   226
        var buttons = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   227
            'class': "sgformbuttons"
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   228
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   229
        [A({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   230
            'href': "javascript: noop();",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   231
            'onclick': this.onValidateClicked
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   232
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   233
        this.oklabel), ' / ', A({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   234
            'href': "javascript: noop();",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   235
            'onclick': this.destroy
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   236
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   237
        escapeHTML(this.cancellabel))]);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   238
        var formnode = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   239
            'class': "sgform"
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   240
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   241
        [sgnode, buttons]);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   242
        appendChildNodes(parentnode, formnode);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   243
        this.sgfield.textinput.focus();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   244
        this.formnode = formnode;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   245
        return formnode;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   248
    destroy: function() {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   249
        signal(this, 'destroy');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   250
        this.sgfield.destroy();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   251
        removeElement(this.formnode);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   254
    onValidateClicked: function() {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   255
        this.validatefunc(this, this.sgfield.taglist());
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
    /* 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
   258
    entryValidated: function(sgfield, taglist) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   259
        this.validatefunc(this, taglist);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   263
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   264
 * .. function:: toggleTree(event)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   265
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   266
 * called when the use clicks on a tree node
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
 *  - 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
   268
 *    by the url's content.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
 *  - else, there's nothing to do, let the jquery plugin handle it.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   271
function toggleTree(event) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
    var linode = jQuery(this);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
    var url = linode.attr('cubicweb:loadurl');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   274
    if (url) {
4738
6cca4f602486 fix open/close/open subnode bug #730709
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3079
diff changeset
   275
        linode.find('ul.placeholder').remove();
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   276
        linode.loadxhtml(url, {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   277
            callback: function(domnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   278
                linode.removeAttr('cubicweb:loadurl');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   279
                jQuery(domnode).treeview({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   280
                    toggle: toggleTree,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   281
                    prerendered: true
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   282
                });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   283
                return null;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   284
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   285
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   286
        'post', 'append');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   290
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   291
 * .. class:: Widgets.TimelineWidget
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   292
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   293
 * widget based on SIMILE's timeline widget
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
 * http://code.google.com/p/simile-widgets/
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
 * Beware not to mess with SIMILE's Timeline JS namepsace !
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
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
   300
    __init__: function(wdgnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   301
        var tldiv = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   302
            id: "tl",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   303
            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
   304
        });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   305
        wdgnode.appendChild(tldiv);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   306
        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
   307
        var eventSource = new Timeline.DefaultEventSource();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   308
        var bandData = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   309
            eventPainter: Timeline.CubicWebEventPainter,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   310
            eventSource: eventSource,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   311
            width: "100%",
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   312
            intervalUnit: Timeline.DateTime[tlunit.toUpperCase()],
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   313
            intervalPixels: 100
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   314
        };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   315
        var bandInfos = [Timeline.createBandInfo(bandData)];
6372
4c3e2a92e340 timeline view minor enhancements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   316
        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
   317
        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
   318
        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
   319
            eventSource.loadJSON(json, url);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   320
        });
0
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
Widgets.TemplateTextField = defclass("TemplateTextField", null, {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   327
    __init__: function(wdgnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   328
        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
   329
        this.options = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   330
            name: wdgnode.getAttribute('cubicweb:inputid'),
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   331
            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
   332
            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
   333
        };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   334
        // this.variableRegexp = /%\((\w+)\)s/;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   335
        this.errorField = DIV({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   336
            'class': "errorMessage"
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   337
        });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   338
        this.textField = TEXTAREA(this.options);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   339
        jQuery(this.textField).bind('keyup', {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   340
            'self': this
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   341
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   342
        this.highlightInvalidVariables);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   343
        jQuery('#substitutions').prepend(this.errorField);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   344
        jQuery('#substitutions .errorMessage').hide();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   345
        wdgnode.appendChild(this.textField);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
    },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   348
    /* signal callbacks */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   350
    highlightInvalidVariables: function(event) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   351
        var self = event.data.self;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   352
        var text = self.textField.value;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   353
        var unknownVariables = [];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   354
        var it = 0;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   355
        var group = null;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   356
        var variableRegexp = /%\((\w+)\)s/g;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   357
        // emulates rgx.findAll()
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   358
        while (group = variableRegexp.exec(text)) {
5909
9af8ac182f53 [javascript] fix some javascript errors and remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5774
diff changeset
   359
            if (!$.inArray(group[1], self.variables)) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   360
                unknownVariables.push(group[1]);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   361
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   362
            it++;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   363
            if (it > 5) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   364
                break;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   365
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   366
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   367
        var errText = '';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   368
        if (unknownVariables.length) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   369
            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
   370
            jQuery('#substitutions .errorMessage').show();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   371
        } else {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   372
            jQuery('#substitutions .errorMessage').hide();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   373
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   374
        self.errorField.innerHTML = errText;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   379
cw.widgets = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   380
    /**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   381
     * .. function:: insertText(text, areaId)
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
     * 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
   384
     * 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
   385
     */
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   386
    insertText: function (text, areaId) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   387
        var textarea = jQuery('#' + areaId);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   388
        if (document.selection) { // IE
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   389
            var selLength;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   390
            textarea.focus();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   391
            var sel = document.selection.createRange();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   392
            selLength = sel.text.length;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   393
            sel.text = text;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   394
            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
   395
            sel.select();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   396
        } 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
   397
            var startPos = textarea.selectionStart;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   398
            var endPos = textarea.selectionEnd;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   399
            // 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
   400
            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
   401
            // 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
   402
            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
   403
            textarea.focus();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   404
        } 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
   405
            textarea.value += text;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   406
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5453
diff changeset
   407
    }
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
   408
};