view noderef_view.js @ 2:25a0c2fcbcfb

Don't wrap the field in a div, it may cause problems. Instead, we just go back in the DOM 'till we find the div holding the real data. It's not really all that good, but, it seems to do the job.
author Franck Deroche <franck@defr.org>
date Fri, 23 Jan 2009 18:57:45 +0100
parents a3c1e224e807
children b4c1e3d5d5ce
line wrap: on
line source
// vim: set ts=2 sw=2 expandtab syntax=php:
/**
 * 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();
      }

      // 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() {
        selection = noderef_view_get_selection();
        for(nid in selection) {
          // Autocomplete field
          $('input.form-autocomplete:not([value])', wrapper)
            .filter(':first')
            .val(selection[nid]);
          // Select field
          $('select', wrapper).val(nid);
          // Radio field
          $('input:radio[value='+ nid + '], input:checkbox[value='+ nid +']', wrapper).attr('checked', 'checked');
        }
      });
    });
  });
}

/**
 * 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;
}