[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7