diff -r ef903fff826d -r 7b9553a9db65 web/data/cubicweb.widgets.js --- a/web/data/cubicweb.widgets.js Thu Jun 03 10:17:44 2010 +0200 +++ b/web/data/cubicweb.widgets.js Thu Jun 03 14:51:42 2010 +0200 @@ -1,6 +1,8 @@ -/* +/** + * Functions dedicated to widgets. + * * :organization: Logilab - * :copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. + * :copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. * :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr * * @@ -9,144 +11,175 @@ // widget namespace Widgets = {}; - -/* this function takes a DOM node defining a widget and +/** + * .. function:: buildWidget(wdgnode) + * + * this function takes a DOM node defining a widget and * instantiates / builds the appropriate widget class */ function buildWidget(wdgnode) { var wdgclass = Widgets[wdgnode.getAttribute('cubicweb:wdgtype')]; if (wdgclass) { - var wdg = new wdgclass(wdgnode); + var wdg = new wdgclass(wdgnode); } } -/* This function is called on load and is in charge to build +/** + * .. function:: buildWidgets(root) + * + * This function is called on load and is in charge to build * JS widgets according to DOM nodes found in the page */ function buildWidgets(root) { root = root || document; jQuery(root).find('.widget').each(function() { - if (this.getAttribute('cubicweb:loadtype') == 'auto') { - buildWidget(this); - } + if (this.getAttribute('cubicweb:loadtype') == 'auto') { + buildWidget(this); + } }); } - // we need to differenciate cases where initFacetBoxEvents is called // with one argument or without any argument. If we use `initFacetBoxEvents` // as the direct callback on the jQuery.ready event, jQuery will pass some argument // of his, so we use this small anonymous function instead. -jQuery(document).ready(function() {buildWidgets();}); +jQuery(document).ready(function() { + buildWidgets(); +}); +function postJSON(url, data, callback) { + return jQuery.post(url, data, callback, 'json'); +} + +function getJSON(url, data, callback) { + return jQuery.get(url, data, callback, 'json'); +} Widgets.SuggestField = defclass('SuggestField', null, { __init__: function(node, options) { - var multi = node.getAttribute('cubicweb:multi') || "no"; - options = options || {}; - options.multiple = (multi == "yes") ? true : false; - var dataurl = node.getAttribute('cubicweb:dataurl'); + var multi = node.getAttribute('cubicweb:multi') || "no"; + options = options || {}; + options.multiple = (multi == "yes") ? true: false; + var dataurl = node.getAttribute('cubicweb:dataurl'); var method = postJSON; - if (options.method == 'get'){ - method = function(url, data, callback) { - // We can't rely on jQuery.getJSON because the server - // might set the Content-Type's response header to 'text/plain' - jQuery.get(url, data, function(response) { - callback(evalJSON(response)); - }); - }; - } - var self = this; // closure - method(dataurl, null, function(data) { - // in case we received a list of couple, we assume that the first - // element is the real value to be sent, and the second one is the - // value to be displayed - if (data.length && data[0].length == 2) { - options.formatItem = function(row) { return row[1]; }; - self.hideRealValue(node); - self.setCurrentValue(node, data); - } - jQuery(node).autocomplete(data, options); - }); + if (options.method == 'get') { + method = function(url, data, callback) { + // We can't rely on jQuery.getJSON because the server + // might set the Content-Type's response header to 'text/plain' + jQuery.get(url, data, function(response) { + callback(cw.evalJSON(response)); + }); + }; + } + var self = this; // closure + method(dataurl, null, function(data) { + // in case we received a list of couple, we assume that the first + // element is the real value to be sent, and the second one is the + // value to be displayed + if (data.length && data[0].length == 2) { + options.formatItem = function(row) { + return row[1]; + }; + self.hideRealValue(node); + self.setCurrentValue(node, data); + } + jQuery(node).autocomplete(data, options); + }); }, hideRealValue: function(node) { - var hidden = INPUT({'type': "hidden", 'name': node.name, 'value': node.value}); - node.parentNode.appendChild(hidden); - // remove 'name' attribute from visible input so that it is not submitted - // and set correct value in the corresponding hidden field - jQuery(node).removeAttr('name').bind('result', function(_, row, _) { - hidden.value = row[0]; - }); + var hidden = INPUT({ + 'type': "hidden", + 'name': node.name, + 'value': node.value + }); + node.parentNode.appendChild(hidden); + // remove 'name' attribute from visible input so that it is not submitted + // and set correct value in the corresponding hidden field + jQuery(node).removeAttr('name').bind('result', function(_, row, _) { + hidden.value = row[0]; + }); }, setCurrentValue: function(node, data) { - // called when the data is loaded to reset the correct displayed - // value in the visible input field (typically replacing an eid - // by a displayable value) - var curvalue = node.value; - if (!node.value) { - return; - } - for (var i=0,length=data.length; i