Permalink
Browse files

add views_infinite_scroll

  • Loading branch information...
1 parent fb4e31e commit 10c408c98a294f4a92c9e553d48cdc971dfef5e8 @mikecrittenden committed May 31, 2012

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,3 @@
+div#views_infinite_scroll-ajax-loader {
+ text-align: center;
+}
@@ -0,0 +1,84 @@
+<?php
+
+/**
+ * @file
+ * drush integration for views_infinite_scroll.
+ */
+
+/**
+ * Implements hook_drush_command().
+ *
+ * In this hook, you specify which commands your
+ * drush module makes available, what it does and
+ * description.
+ *
+ * Notice how this structure closely resembles how
+ * you define menu hooks.
+ *
+ * @See drush_parse_command() for a list of recognized keys.
+ *
+ * @return
+ * An associative array describing your command(s).
+ */
+function views_infinite_scroll_drush_command() {
+ $items = array();
+
+ $items['dl-autopager'] = array(
+ 'callback' => 'views_infinite_scroll_drush_autopager_download',
+ 'description' => dt('Downloads the required autopager jquery plugin.'),
+ );
+ return $items;
+}
+
+/**
+ * Implements hook_drush_help().
+ *
+ * This function is called whenever a drush user calls
+ * 'drush help <name-of-your-command>'
+ *
+ * @param
+ * A string with the help section (prepend with 'drush:')
+ *
+ * @return
+ * A string with the help text for your command.
+ */
+function views_infinite_scroll_drush_help($section) {
+ switch ($section) {
+ case 'drush:dl-autopager':
+ return dt("Downloads the required autopager jquery plugin.");
+ }
+}
+
+/**
+ * Example drush command callback.
+ *
+ * This is where the action takes place.
+ *
+ * In this function, all of Drupals API is (usually) available, including
+ * any functions you have added in your own modules/themes.
+ *
+ * To print something to the terminal window, use drush_print().
+ *
+ */
+function views_infinite_scroll_drush_autopager_download() {
+ if (module_exists('libraries')) {
+ $path = 'sites/all/libraries/autopager';
+
+ // Create the path if it does not exist.
+ if (!is_dir($path)) {
+ drush_op('mkdir', $path);
+ drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
+ }
+ }
+ else {
+ $path = drupal_get_path('module', 'views_infinite_scroll') . '/js';
+ }
+ drush_op('chdir', $path);
+ // Download the plugin.
+ if (drush_shell_exec('wget http://jquery-autopager.googlecode.com/files/jquery.autopager-1.0.0.js')) {
+ drush_log(dt('jquery.autopager-1.0.0.js has been downloaded to @path', array('@path' => $path)), 'success');
+ }
+ else {
+ drush_log(dt('Drush was unable to download jquery.autopager-1.0.0.js to @path', array('@path' => $path)), 'error');
+ }
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,171 @@
+/*
+ * jQuery.autopager v1.0.0
+ *
+ * Copyright (c) lagos
+ * Dual licensed under the MIT and GPL licenses.
+ */
+(function($) {
+ var window = this, options = {},
+ content, currentUrl, nextUrl,
+ active = false,
+ defaults = {
+ autoLoad: true,
+ page: 1,
+ content: '.content',
+ link: 'a[rel=next]',
+ insertBefore: null,
+ appendTo: null,
+ start: function() {},
+ load: function() {},
+ disabled: false
+ };
+
+ $.autopager = function(_options) {
+ var autopager = this.autopager;
+
+ if (typeof _options === 'string' && $.isFunction(autopager[_options])) {
+ var args = Array.prototype.slice.call(arguments, 1),
+ value = autopager[_options].apply(autopager, args);
+
+ return value === autopager || value === undefined ? this : value;
+ }
+
+ _options = $.extend({}, defaults, _options);
+ autopager.option(_options);
+
+ content = $(_options.content).filter(':last');
+ if (content.length) {
+ if (!_options.insertBefore && !_options.appendTo) {
+ var insertBefore = content.next();
+ if (insertBefore.length) {
+ set('insertBefore', insertBefore);
+ } else {
+ set('appendTo', content.parent());
+ }
+ }
+ }
+
+ setUrl();
+
+ return this;
+ };
+
+ $.extend($.autopager, {
+ option: function(key, value) {
+ var _options = key;
+
+ if (typeof key === "string") {
+ if (value === undefined) {
+ return options[key];
+ }
+ _options = {};
+ _options[key] = value;
+ }
+
+ $.each(_options, function(key, value) {
+ set(key, value);
+ });
+ return this;
+ },
+
+ enable: function() {
+ set('disabled', false);
+ return this;
+ },
+
+ disable: function() {
+ set('disabled', true);
+ return this;
+ },
+
+ destroy: function() {
+ this.autoLoad(false);
+ options = {};
+ content = currentUrl = nextUrl = undefined;
+ return this;
+ },
+
+ autoLoad: function(value) {
+ return this.option('autoLoad', value);
+ },
+
+ load: function() {
+ if (active || !nextUrl || options.disabled) {
+ return;
+ }
+
+ active = true;
+ options.start(currentHash(), nextHash());
+ $.get(nextUrl, insertContent);
+ return this;
+ }
+
+ });
+
+ function set(key, value) {
+ switch (key) {
+ case 'autoLoad':
+ if (value && !options.autoLoad) {
+ $(window).scroll(loadOnScroll);
+ } else if (!value && options.autoLoad) {
+ $(window).unbind('scroll', loadOnScroll);
+ }
+ break;
+ case 'insertBefore':
+ if (value) {
+ options.appendTo = null;
+ }
+ break
+ case 'appendTo':
+ if (value) {
+ options.insertBefore = null;
+ }
+ break
+ }
+ options[key] = value;
+ }
+
+ function setUrl(context) {
+ currentUrl = nextUrl || window.location.href;
+ nextUrl = $(options.link, context).attr('href');
+ }
+
+ function loadOnScroll() {
+ if (content.offset().top + content.height() < $(document).scrollTop() + $(window).height()) {
+ $.autopager.load();
+ }
+ }
+
+ function insertContent(res) {
+ var _options = options,
+ nextPage = $('<div/>').append(res.replace(/<script(.|\s)*?\/script>/g, "")),
+ nextContent = nextPage.find(_options.content);
+
+ set('page', _options.page + 1);
+ setUrl(nextPage);
+ if (nextContent.length) {
+ if (_options.insertBefore) {
+ nextContent.insertBefore(_options.insertBefore);
+ } else {
+ nextContent.appendTo(_options.appendTo);
+ }
+ _options.load.call(nextContent.get(), currentHash(), nextHash());
+ content = nextContent.filter(':last');
+ }
+ active = false;
+ }
+
+ function currentHash() {
+ return {
+ page: options.page,
+ url: currentUrl
+ };
+ }
+
+ function nextHash() {
+ return {
+ page: options.page + 1,
+ url: nextUrl
+ };
+ }
+})(jQuery);
@@ -0,0 +1,77 @@
+// $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);
+
+ }
+ else {
+ alert(Drupal.t('Views infinite scroll pager is not compatible with Ajax Views. Please disable "Use Ajax" option.'));
+ }
+ }
+ 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.'));
+ }
+ }
+ }
+ else {
+ alert(Drupal.t('Autopager jquery plugin in not loaded.'));
+ }
+ }
+}
+
+})(jQuery);
Oops, something went wrong.

0 comments on commit 10c408c

Please sign in to comment.