Index: views.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/views.module,v
retrieving revision 1.166.2.6
diff -u -r1.166.2.6 views.module
--- views.module	21 Jan 2007 21:03:37 -0000	1.166.2.6
+++ views.module	8 Feb 2007 16:06:13 -0000
@@ -1,6 +1,6 @@
 <?php
 // $Id: views.module,v 1.166.2.6 2007/01/21 21:03:37 merlinofchaos Exp $
-// $Name:  $
+// $Name: DRUPAL-5 $
 
 // ---------------------------------------------------------------------------
 // Drupal Hooks
@@ -283,20 +283,51 @@
   $view = views_get_view($view_name);
 
   if (!$view) {
-    drupal_not_found();
-    exit;
+    _views_not_found();
   }
 
   $output = views_build_view('page', $view, $args, $view->use_pager, $view->nodes_per_page);
   if ($output === FALSE) {
-    drupal_not_found();
-    exit;
+    _views_not_found();
   }
 
+  // AJAX functionality depends on jstools module.
+  if (module_exists('jstools')) {
+    // If loading by AJAX, return in JS format.
+    if (_views_is_ajax()) {
+      print drupal_to_js(array('status' => TRUE, 'data' => $output));
+      exit;
+    }
+    // Otherwise, add the js files if needed.
+    elseif (!empty($view->exposed_filter)) {
+      jstools_add_js(drupal_get_path('module', 'views') .'/activeviews.js');
+      drupal_add_js('misc/progress.js');
+    }
+  }
   return $output;
 }
 
 /**
+ * Determine if a view is being loaded through AJAX.
+ */
+function _views_is_ajax() {
+  return isset($_GET['activeviews']);
+}
+
+/**
+ * Return a not found message in either HTML or JSON format.
+ */
+function _views_not_found() {
+  if (_views_is_ajax()) {
+    print drupal_to_js(array('status' => FALSE, 'data' => t('Not found')));
+  }
+  else {
+    drupal_not_found();
+  }
+  exit;
+}
+
+/**
  * This views a view by block. Can be used as a callback or programmatically.
  */
 function views_view_block($vid) {
@@ -1360,12 +1391,16 @@
     views_set_breadcrumb($view);
   }
 
-  if ($num_nodes) {
-    $output .= views_get_textarea($view, $type, 'header');
-  }
+  // If loading by AJAX, skip header and exposed filter, as they will
+  // already be on page.
+  if (!_views_is_ajax()) {
+    if ($num_nodes) {
+      $output .= views_get_textarea($view, $type, 'header');
+    }
 
-  if ($type != 'block' && $view->exposed_filter) {
-    $output .= views_theme('views_display_filters', $view);
+    if ($type != 'block' && $view->exposed_filter) {
+      $output .= views_theme('views_display_filters', $view);
+    }
   }
 
   $plugins = _views_get_style_plugins();
--- modules/views/activeviews.js
+++ modules/views/activeviews.js
@@ -0,0 +1,114 @@
+// $Id:  $
+
+Drupal.activeviewsAttach = function() {
+  $('form#views-filters')
+    .append('<input type="hidden" name="activeviews" value="1" />')
+    .each(function () {
+      new Drupal.activeviewsFilter(this);
+      Drupal.activeviewsFilterPager();
+    });
+}
+
+/**
+ * activeviews object.
+ */
+Drupal.activeviewsFilter = function(form) {
+  this.form = form;
+  // Use an existing view-content div if there is one.
+  if ($('div.view-content').length) {
+    this.target = $('div.view-content').get(0);
+  }
+  // Otherwise, create and use a new one.
+  else {
+    this.target = document.createElement('div');
+    $(this.target).addClass('view-content');
+    $(form).append(this.target);
+  }
+  Drupal.redirectFormSubmit($(form).attr('action'), form, this);
+}
+
+/**
+ * Handler for the form redirection submission.
+ */
+Drupal.activeviewsFilter.prototype.onsubmit = function () {
+  // Insert progressbar.
+  this.progress = new Drupal.progressBar('activeviewsprogress');
+  this.progress.setProgress(-1, 'Fetching results');
+  var el = this.progress.element;
+  $(el).css({
+    width: '250px',
+    height: '15px',
+    paddingTop: '10px'
+  });
+  $(this.target)
+    .hide()
+    .before(el);
+}
+
+/**
+ * Handler for the form redirection completion.
+ */
+Drupal.activeviewsFilter.prototype.oncomplete = function (data) {
+  $(this.progress.element).remove();
+  this.progress = null;
+  $(this.target)
+    .html(data)
+    .slideDown('slow');
+  Drupal.activeviewsFilterPager();
+}
+
+/**
+ * Handler for the form redirection error.
+ */
+Drupal.activeviewsFilter.prototype.onerror = function (error) {
+  alert('An error occurred.');
+  $(this.progress.element).remove();
+  this.progress = null;
+  $(this.target).slideDown('slow');
+}
+
+Drupal.activeviewsFilterPager = function () {
+  $('div.pager a').click(function () {
+    Drupal.scrollTo(this.target);
+    // Use an existing view-content div if there is one.
+    if ($('div.view-content').length) {
+      var target = $('div.view-content').get(0);
+    }
+    // Otherwise, create and use a new one.
+    else {
+      var target = document.createElement('div');
+      $(this.target).addClass('view-content');
+      $('form#views-filters').append(this.target);
+    }
+    // Insert progressbar.
+    var progress = new Drupal.progressBar('activeviewsprogress');
+    progress.setProgress(-1, 'Fetching results');
+    var el = progress.element;
+    $(el).css({
+      width: '250px',
+      height: '15px',
+      paddingTop: '10px'
+    });
+    $(target)
+      .hide()
+      .before(el);
+    $.ajax({
+      type: 'GET',
+      url: $(this).attr('href'),
+      success: function (response) {
+        response = Drupal.parseJson(response);
+        $(target)
+          .html(response.data)
+          .fadeIn('slow');
+        $(el).remove();
+        progress = null;
+        Drupal.activeviewsFilterPager();
+      }
+    });
+    return false;
+  });
+}
+
+if (Drupal.jsEnabled) {
+  $(document).ready(Drupal.activeviewsAttach);
+}

