Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 64b8c513f899783fbabf648d00ce0dfc748f1570 0 parents
@Ouziel authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 README
@@ -0,0 +1,13 @@
+InfinitePage
+==============
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the infinite_page plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the infinite_page plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'InfinitePage'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
7 init.rb
@@ -0,0 +1,7 @@
+require 'infinite_page'
+
+if defined?(Rails) and defined?(ActionController)
+ return if ActionView::Base.instance_methods.include? 'infinite_page'
+ ActionView::Base.send :include, InfinitePage
+end
+
3  install.rb
@@ -0,0 +1,3 @@
+require 'fileutils'
+
+FileUtils.cp File.dirname(__FILE__) + '/public/javascripts/infinite_page.js', File.dirname(__FILE__) + '/../../../public/javascripts/infinite_page.js'
34 lib/infinite_page.rb
@@ -0,0 +1,34 @@
+include WillPaginate
+include ActionView::Helpers::PrototypeHelper
+include ActionView::Helpers::JavaScriptHelper
+include ActionView::Helpers::TagHelper
+
+module InfinitePage
+ def infinite_page(collection = nil, options = {})
+ options[:renderer] = 'InfinitePage::ScrollObserverRenderer'
+ pagination = will_paginate(collection, options)
+ options[:renderer] = 'WillPaginate::LinkRenderer'
+ pagination += "<noscript>"+will_paginate(collection, options)+"</noscript>"
+ end
+
+ class ScrollObserverRenderer < WillPaginate::LinkRenderer
+ def to_html
+ if @collection.current_page<@collection.total_pages
+ @options[:url] = @collection.next_page
+ @options[:method] = :get if @options[:method].nil?
+ js_string = "InfinitePage.start_scroll_observing('"+escape_javascript(remote_function @options)+"');"
+ if @collection.current_page==1
+ js_string += @options[:first] unless @options[:first].nil?
+ end
+ js_string = javascript_tag js_string
+ else
+ javascript_tag @options[:last] unless @options[:last].nil?
+ end
+ end
+
+ def protect_against_forgery?
+ config = Rails::Configuration.new
+ config.action_controller.allow_forgery_protection
+ end
+ end
+end
44 public/javascripts/infinite_page.js
@@ -0,0 +1,44 @@
+var InfinitePage = {
+ distance_from_bottom_to_activate: 100,
+ initialize: function() {
+ Event.observe(window,"load", function() {
+ window.scrollTo(0,0);
+ InfinitePage.initialized = true;
+ })
+ },
+ load_next_page: function() {
+ eval(InfinitePage.remote_function);
+ },
+ scroll_diff: function() {
+ return $(document.body).getHeight() - document.viewport.getHeight();
+ },
+ near_to_bottom: function() {
+ scrollOffsets = document.viewport.getScrollOffsets();
+ return (InfinitePage.scroll_diff()-scrollOffsets.top)<InfinitePage.distance_from_bottom_to_activate;
+ },
+ scroll_observer: function() {
+ if (InfinitePage.near_to_bottom())
+ {
+ Event.stopObserving(window, "scroll", InfinitePage.scroll_observer);
+ InfinitePage.load_next_page();
+ }
+ },
+ scroll_observing: function() {
+ if (InfinitePage.on_load_scroll_to_top) {
+ window.scrollTo(0,0);
+ InfinitePage.on_load_scroll_to_top = false;
+ }
+ if (InfinitePage.scroll_diff()>InfinitePage.distance_from_bottom_to_activate) {
+ Event.observe(window, "scroll", InfinitePage.scroll_observer);
+ } else {
+ InfinitePage.on_load_scroll_to_top = true
+ InfinitePage.load_next_page();
+ }
+ },
+ start_scroll_observing: function(remote_function) {
+ InfinitePage.remote_function = remote_function;
+ if (InfinitePage.initialized) InfinitePage.scroll_observing();
+ else Event.observe(window,"load",InfinitePage.scroll_observing);
+ }
+};
+InfinitePage.initialize();
4 tasks/infinite_page_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :infinite_page do
+# # Task goes here
+# end
8 test/infinite_page_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class InfinitePageTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
3  test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
3  uninstall.rb
@@ -0,0 +1,3 @@
+require 'fileutils'
+
+FileUtils.rm %{infinite_page.js}.collect { |f| File.dirname(__FILE__) + '/../../../public/javascripts/' + f }
Please sign in to comment.
Something went wrong with that request. Please try again.