[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/js/ -> ajax_view.js (source)

   1  
   2  /**
   3   * @file ajaxView.js
   4   *
   5   * Handles AJAX fetching of views, including filter submission and response.
   6   */
   7  
   8  Drupal.Views.Ajax = Drupal.Views.Ajax || {};
   9  
  10  /**
  11   * An ajax responder that accepts a packet of JSON data and acts appropriately.
  12   *
  13   * The following fields control behavior.
  14   * - 'display': Display the associated data in the view area.
  15   */
  16  Drupal.Views.Ajax.ajaxViewResponse = function(target, response) {
  17  
  18    if (response.debug) {
  19      alert(response.debug);
  20    }
  21  
  22    var $view = $(target);
  23  
  24    // Check the 'display' for data.
  25    if (response.status && response.display) {
  26      var $newView = $(response.display);
  27      $view.replaceWith($newView);
  28      $view = $newView;
  29      Drupal.attachBehaviors($view.parent());
  30    }
  31  
  32    if (response.messages) {
  33      // Show any messages (but first remove old ones, if there are any).
  34      $view.find('.views-messages').remove().end().prepend(response.messages);
  35    }
  36  };
  37  
  38  /**
  39   * Ajax behavior for views.
  40   */
  41  Drupal.behaviors.ViewsAjaxView = function() {
  42    if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
  43      var ajax_path = Drupal.settings.views.ajax_path;
  44      // If there are multiple views this might've ended up showing up multiple times.
  45      if (ajax_path.constructor.toString().indexOf("Array") != -1) {
  46        ajax_path = ajax_path[0];
  47      }
  48      $.each(Drupal.settings.views.ajaxViews, function(i, settings) {
  49        if (settings.view_dom_id) {
  50          var view = '.view-dom-id-' + settings.view_dom_id;
  51          if (!$(view).size()) {
  52            // Backward compatibility: if 'views-view.tpl.php' is old and doesn't
  53            // contain the 'view-dom-id-#' class, we fall back to the old way of
  54            // locating the view:
  55            view = '.view-id-' + settings.view_name + '.view-display-id-' + settings.view_display_id;
  56          }
  57        }
  58  
  59  
  60        // Process exposed filter forms.
  61        $('form#views-exposed-form-' + settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-'))
  62        .filter(':not(.views-processed)')
  63        .each(function () {
  64          // remove 'q' from the form; it's there for clean URLs
  65          // so that it submits to the right place with regular submit
  66          // but this method is submitting elsewhere.
  67          $('input[name=q]', this).remove();
  68          var form = this;
  69          // ajaxSubmit doesn't accept a data argument, so we have to
  70          // pass additional fields this way.
  71          $.each(settings, function(key, setting) {
  72            $(form).append('<input type="hidden" name="'+ key + '" value="'+ setting +'"/>');
  73          });
  74        })
  75        .addClass('views-processed')
  76        .submit(function () {
  77          $('input[type=submit], button', this).after('<span class="views-throbbing">&nbsp</span>');
  78          var object = this;
  79          $(this).ajaxSubmit({
  80            url: ajax_path,
  81            type: 'GET',
  82            success: function(response) {
  83              // Call all callbacks.
  84              if (response.__callbacks) {
  85                $.each(response.__callbacks, function(i, callback) {
  86                  eval(callback)(view, response);
  87                });
  88                $('.views-throbbing', object).remove();
  89              }
  90            },
  91            error: function(xhr) { Drupal.Views.Ajax.handleErrors(xhr, ajax_path); $('.views-throbbing', object).remove(); },
  92            dataType: 'json'
  93          });
  94  
  95          return false;
  96        });
  97  
  98        $(view).filter(':not(.views-processed)')
  99          // Don't attach to nested views. Doing so would attach multiple behaviors
 100          // to a given element.
 101          .filter(function() {
 102            // If there is at least one parent with a view class, this view
 103            // is nested (e.g., an attachment). Bail.
 104            return !$(this).parents('.view').size();
 105          })
 106          .each(function() {
 107            // Set a reference that will work in subsequent calls.
 108            var target = this;
 109            $(this)
 110              .addClass('views-processed')
 111              // Process pager, tablesort, and attachment summary links.
 112              .find('ul.pager > li > a, th.views-field a, .attachment .views-summary a')
 113              .each(function () {
 114                var viewData = { 'js': 1 };
 115                // Construct an object using the settings defaults and then overriding
 116                // with data specific to the link.
 117                $.extend(
 118                  viewData,
 119                  Drupal.Views.parseQueryString($(this).attr('href')),
 120                  // Extract argument data from the URL.
 121                  Drupal.Views.parseViewArgs($(this).attr('href'), settings.view_base_path),
 122                  // Settings must be used last to avoid sending url aliases to the server.
 123                  settings
 124                );
 125                $(this).click(function () {
 126                  $.extend(viewData, Drupal.Views.parseViewArgs($(this).attr('href'), settings.view_base_path));
 127                  $(this).addClass('views-throbbing');
 128                  $.ajax({
 129                    url: ajax_path,
 130                    type: 'GET',
 131                    data: viewData,
 132                    success: function(response) {
 133                      $(this).removeClass('views-throbbing');
 134                      // Scroll to the top of the view. This will allow users
 135                      // to browse newly loaded content after e.g. clicking a pager
 136                      // link.
 137                      var offset = $(target).offset();
 138                      // We can't guarantee that the scrollable object should be
 139                      // the body, as the view could be embedded in something
 140                      // more complex such as a modal popup. Recurse up the DOM
 141                      // and scroll the first element that has a non-zero top.
 142                      var scrollTarget = target;
 143                      while ($(scrollTarget).scrollTop() == 0 && $(scrollTarget).parent()) {
 144                        scrollTarget = $(scrollTarget).parent()
 145                      }
 146                      // Only scroll upward
 147                      if (offset.top - 10 < $(scrollTarget).scrollTop()) {
 148                        $(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
 149                      }
 150                      // Call all callbacks.
 151                      if (response.__callbacks) {
 152                        $.each(response.__callbacks, function(i, callback) {
 153                          eval(callback)(target, response);
 154                        });
 155                      }
 156                    },
 157                    error: function(xhr) { $(this).removeClass('views-throbbing'); Drupal.Views.Ajax.handleErrors(xhr, ajax_path); },
 158                    dataType: 'json'
 159                  });
 160  
 161                  return false;
 162                });
 163              }); // .each function () {
 164        }); // $view.filter().each
 165      }); // .each Drupal.settings.views.ajaxViews
 166    } // if
 167  };


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7