Commit 830f280b authored by Sam152's avatar Sam152

Issue #1335792, Issue #1316194 - AJAX compatability, manual load button.

parent d9fe1f99
// $Id:
(function ($) {
var views_infinite_scroll_was_initialised = false;
Drupal.behaviors.views_infinite_scroll = {
attach:function() {
// Make sure that autopager plugin is loaded
if($.autopager) {
if(!views_infinite_scroll_was_initialised) {
views_infinite_scroll_was_initialised = true;
// There should not be multiple Infinite Scroll Views on the same page
if(Drupal.settings.views_infinite_scroll.length == 1) {
var settings = Drupal.settings.views_infinite_scroll[0];
var use_ajax = false;
// Make sure that views ajax is disabled
if(Drupal.settings.views && Drupal.settings.views.ajaxViews) {
$.each(Drupal.settings.views.ajaxViews, function(key, value) {
if((value.view_name == settings.view_name) && (value.view_display_id == settings.display)) {
use_ajax = true;
}
});
}
if(!use_ajax) {
var view_selector = 'div.view-id-' + settings.view_name + '.view-display-id-' + settings.display;
var content_selector = view_selector + ' > ' + settings.content_selector;
var items_selector = content_selector + ' ' + settings.items_selector;
var pager_selector = view_selector + ' > div.item-list ' + settings.pager_selector;
var next_selector = view_selector + ' ' + settings.next_selector;
var img_location = view_selector + ' > div.view-content';
var img_path = settings.img_path;
var img = '<div id="views_infinite_scroll-ajax-loader"><img src="' + img_path + '" alt="loading..."/></div>';
$(pager_selector).hide();
var handle = $.autopager({
appendTo: content_selector,
content: items_selector,
link: next_selector,
page: 0,
start: function() {
$(img_location).after(img);
},
load: function() {
$('div#views_infinite_scroll-ajax-loader').remove();
Drupal.attachBehaviors(this);
}
});
// Trigger autoload if content height is less than doc height already
var prev_content_height = $(content_selector).height();
do {
var last = $(items_selector).filter(':last');
if(last.offset().top + last.height() < $(document).scrollTop() + $(window).height()) {
last = $(items_selector).filter(':last');
handle.autopager('load');
}
else {
break;
}
}
while ($(content_selector).height() > prev_content_height);
/**
* Attach infinite scroll to the relevant views.
*/
Drupal.behaviors.views_infinite_scroll = {
attach: function (context, settings) {
}
else {
alert(Drupal.t('Views infinite scroll pager is not compatible with Ajax Views. Please disable "Use Ajax" option.'));
// Make sure that autopager plugin is loaded.
if (!$.autopager) {
alert(Drupal.t('Autopager jquery plugin in not loaded.'));
return;
}
var settings = settings.views_infinite_scroll[0];
// Ensure we are refreshing the view component.
var view_selector = 'div.view-id-' + settings.view_name + '.view-display-id-' + settings.display;
var $view = $(context).is(view_selector) ? $(context) : $(context).find(view_selector);
if ($view.length == 0) {
return;
}
// Destroy an existing instance of autopager.
$.autopager('destroy');
var content_selector = view_selector + ' > ' + settings.content_selector;
var $items = $view.find(settings.items_selector);
var $pager = $view.find('> div.item-list ' + settings.pager_selector);
var next_selector = view_selector + ' ' + settings.next_selector;
var $next = $(next_selector);
var $img_location = $view.find('div.view-content');
var img_path = settings.img_path;
var img = '<div id="views_infinite_scroll-ajax-loader"><img src="' + img_path + '" alt="loading..."/></div>';
$($pager).hide();
$.autopager({
appendTo: content_selector,
content: content_selector + ' ' + settings.items_selector,
link: next_selector,
page: 0,
autoLoad: !settings.manual_load,
start: function () {
$img_location.after(img);
},
load: function (current, next) {
$('div#views_infinite_scroll-ajax-loader').remove();
Drupal.attachBehaviors(this);
// Use >= because of views page numbers begin at 0.
if (settings.manual_load && next.page >= settings.pager_max) {
$next.hide();
}
}
else if(Drupal.settings.views_infinite_scroll.length > 1) {
alert(Drupal.t('Views Infinite Scroll module can\'t handle more than one infinite view in the same page.'));
});
if (settings.manual_load) {
// The pager is hidden. Pop "next" link out so it will be visible.
$next.text(settings.text);
$next.click(function (e) {
e.preventDefault();
// do load
$.autopager('load');
});
$($pager).before($next);
// Wrap the button to allow for easier styling in the theme
$next.wrap('<div id="views_infinite_scroll_button" />');
}
else {
$($pager).hide();
// Trigger autoload if content height is less than doc height already
var prev_content_height = $(content_selector).height();
do {
var last = $($items).filter(':last');
if (last.offset().top + last.height() < $(document).scrollTop() + $(window).height()) {
last = $($items).filter(':last');
$.autopager('load');
}
else {
break;
}
}
while ($(content_selector).height() > prev_content_height);
}
}
else {
alert(Drupal.t('Autopager jquery plugin in not loaded.'));
}
}
}
};
})(jQuery);
......@@ -12,6 +12,8 @@ function theme_views_infinite_scroll_pager($variables) {
$content_selector = isset($variables['content_selector']) ? $variables['content_selector'] : 'div.view-content';
$items_selector = isset($variables['items_selector']) ? $variables['items_selector'] : 'div.view-content .views-row';
$img_path = $variables['img_path'];
$manual_load = isset($variables['manual_load']) ? $variables['manual_load'] : FALSE;
$text = isset($variables['text']) ? $variables['text'] : array('default' => 'Show More', 'translatable' => TRUE);
$parameters = $variables['parameters'];
$quantity = $variables['quantity'];
......@@ -80,6 +82,9 @@ function theme_views_infinite_scroll_pager($variables) {
'content_selector' => $content_selector,
'items_selector' => $items_selector,
'img_path' => $img_path,
'manual_load' => $manual_load,
'text' => t($text),
'pager_max' => $pager_max,
),
),
);
......
......@@ -10,6 +10,40 @@ class views_plugin_pager_infinite_scroll extends views_plugin_pager_full {
parent::init($view, $display, $options);
}
function option_definition() {
$options = parent::option_definition();
$options['manual_load'] = array('default' => 0);
$options['text'] = array('default' => 'Show More', 'translatable' => TRUE);
return $options;
}
/**
* Provide the default form for setting options.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['manual_load'] = array(
'#type' => 'checkbox',
'#title' => t('Load subsequent pages manually instead of automatically'),
'#description' => t('When checked, use a link to trigger loading of subsequent pages instead of window scroll.'),
'#default_value' => $this->options['manual_load'],
'#weight' => -10,
);
$form['text'] = array(
'#type' => 'textfield',
'#title' => t('Manual load label'),
'#required' => FALSE,
'#description' => t('Label to use on the manual link.'),
'#default_value' => $this->options['text'],
'#process' => array('ctools_dependent_process'),
'#dependency' => array(
'edit-pager-options-manual-load' => array(1),
),
'#weight' => -10,
);
}
function summary_title() {
return "Infinite Scroll";
if (!empty($this->options['offset'])) {
......@@ -48,6 +82,17 @@ class views_plugin_pager_infinite_scroll extends views_plugin_pager_full {
break;
}
$pager_theme = views_theme_functions('views_infinite_scroll_pager', $this->view, $this->display);
return theme($pager_theme, array('tags' => $input, 'quantity' => $this->options['items_per_page'], 'view_name' => $this->view->name, 'current_display' => $this->view->current_display, 'content_selector' => $content_selector, 'items_selector' => $items_selector, 'img_path' => $img_path, 'element' => $this->options['id']));
return theme($pager_theme, array(
'tags' => $input,
'quantity' => $this->options['items_per_page'],
'view_name' => $this->view->name,
'current_display' => $this->view->current_display,
'content_selector' => $content_selector,
'items_selector' => $items_selector,
'img_path' => $img_path,
'element' => $this->options['id'],
'manual_load' => $this->options['manual_load'],
'text' => $this->options['text'],)
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment