Mercurial > defr > drupal > noderef_view
view noderef_view.js @ 5:4fc2f22efcdf
Better name for the attribute to prevent collision, add to #after_build
#link was probably overly too broad, so I've renamed the attribute to include
the module name.
The code previously also fully took over #after_build, which killed other
modules registrations. Now the module's a good citizen and only add its
handler leaving the others as is.
author | Franck Deroche <franck@defr.org> |
---|---|
date | Mon, 26 Jan 2009 14:59:57 +0100 |
parents | b4c1e3d5d5ce |
children | e3f85c9247b7 |
line wrap: on
line source
// vim: set ts=2 sw=2 expandtab: /** * Attach the noderef_view behavior. */ Drupal.behaviors.noderef_view = function(context) { $('.noderef_view_link', context).each(function(i, obj) { $(obj).addClass('noderef_view_link_processed').click(function() { // Get the wrapper var wrapper = $(this).prev(); while (!wrapper.is('div')) { wrapper = wrapper.prev(); } // Hide existing "No space left" message $('div.noderef_nospace', wrapper).fadeOut(); // Suppress behavior if it was previsously attached $(document).unbind('popups_form_success.noderef_view'); // And now bind it. This code will be executed after successful // completion of the popup select form $(document).bind('popups_form_success.noderef_view', function() { var selection = noderef_view_get_selection(), left = []; for(nid in selection) { count = 0; // Autocomplete field count += $('input.form-autocomplete:not([value])', wrapper) .filter(':first') .val(selection[nid]) .length; // Select field (exclude the weight of autocomplete fields) count += $('select:not([id$="--weight"])', wrapper).val(nid).length; // Radio field count += $('input:radio[value='+ nid + '], input:checkbox[value='+ nid +']', wrapper) .attr('checked', 'checked') .length; if (count == 0) { left.push(selection[nid]); } } if (left.length) { message = Drupal.t('No space left for value(s): @titles', { '@titles': left.join(', ')}); wrapper.append('<div class="warning noderef_nospace">'+ message +'</div>'); } }); }); }); } /** * Helper function to extract the references values */ function noderef_view_get_selection() { var cookies = document.cookie.split(';'), selection = {}; for(var i = 0; i < cookies.length; i++) { var cookie = $.trim(cookies[i]).split('='); if (cookie[0].indexOf('noderef[') == 0) { var nid = cookie[0].replace(/noderef\[(\d+)\]/, "$1"); var title = decodeURIComponent(cookie[1]); selection[nid] = title; } } return selection; }