Index: views.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views/views.module,v retrieving revision 1.192 diff -u -r1.192 views.module --- views.module 22 Jan 2007 02:24:13 -0000 1.192 +++ views.module 4 Feb 2007 22:50:05 -0000 @@ -283,20 +283,50 @@ $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'); + } + } return $output; } /** + * Return a not found message in either HTML or JSON format. + */ +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 +1390,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,111 @@ +// $Id: $ + +Drupal.activeviewsAttach = function() { + $('form#views-filters') + .append('') + .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. + // Insert progressbar and stretch to take the same space. + 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.cached = $(this.target).html(); + $(this.target) + .html('') + .append(el) + .fadeIn('slow'); +} + +/** + * Handler for the form redirection completion. + */ +Drupal.activeviewsFilter.prototype.oncomplete = function (data) { + $(this.target) + .hide() + .html(data) + .fadeIn('slow'); + Drupal.activeviewsFilterPager(); this.progress = null; +} + +/** + * Handler for the form redirection error. + */ +Drupal.activeviewsFilter.prototype.onerror = function (error) { + var target = this.target; + var cached = this.cached; + alert('An error occurred.'); + $(this.progress.element).fadeOut('slow', function() { + $(this).remove(); + $(target) + .hide() + .html(cached) + .fadeIn('slow'); + }); + this.progress = null; + +} + +Drupal.activeviewsFilterPager = function () { + $('div.pager a').click(function () { + $.ajax({ + type: 'GET', + url: $(this).attr('href'), + success: function (response) { + // 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); + } + response = Drupal.parseJson(response); + $(target) + .hide() + .html(response.data) + .fadeIn('slow'); + Drupal.activeviewsFilterPager(); + } + }); + return false; + }); +} + +if (Drupal.jsEnabled) { + $(document).ready(Drupal.activeviewsAttach); +}