view noderef_view.js @ 9:b942365b70eb

Change the Action label to be more descriptive and simplify the warning. The code also pass the node title through theme('placeholder') to both ensure that it's safe for display /and/ pretty print it, by default by wrapping it in an <em />
author Franck Deroche <franck@defr.org>
date Tue, 27 Jan 2009 12:28:41 +0100
parents 1f9c278126f9
children
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-wrapper div[id$="-wrapper"]:first-child').addClass('noderef-view-items');
  $('.noderef_view_link', context).each(function(i, obj) {
    $(obj).addClass('noderef_view_link_processed').click(function() {
      // Get the wrapper
      var wrapper = $(this)
        .parent()
        .find('.noderef-view-items')
        .filter(':first');
      var element = $(this);

      // Hide existing "No space left" message
      $('div.noderef_outofspace', element.parent()).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;
          // Let's compute the potential value for autocomplete fields
          acvalue = selection[nid] +' [nid:'+ nid +']';
          // Autocomplete field
          count += $('input.form-autocomplete[value="'+ acvalue +'"], input.form-autocomplete:not([value])', wrapper)
            .filter(':first')
            .val(acvalue)
            .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(Drupal.theme('placeholder', selection[nid]));
          }
        }
        if (left.length) {
          message = Drupal.t('No space left for: !titles',
            { '!titles': left.join(', ')});
          text = $('<br /><div>'+ message + '<div>')
            .css('display', 'inline')
            .addClass('warning')
            .addClass('noderef_outofspace');
          element.after(text);
        }
      });
    });
  });
}

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