From 64b8c513f899783fbabf648d00ce0dfc748f1570 Mon Sep 17 00:00:00 2001 From: Ouziel Date: Sat, 25 Apr 2009 02:19:21 +0000 Subject: [PATCH] first commit --- MIT-LICENSE | 20 +++++++++++++ README | 13 +++++++++ Rakefile | 23 +++++++++++++++ init.rb | 7 +++++ install.rb | 3 ++ lib/infinite_page.rb | 34 ++++++++++++++++++++++ public/javascripts/infinite_page.js | 44 +++++++++++++++++++++++++++++ tasks/infinite_page_tasks.rake | 4 +++ test/infinite_page_test.rb | 8 ++++++ test/test_helper.rb | 3 ++ uninstall.rb | 3 ++ 11 files changed, 162 insertions(+) create mode 100644 MIT-LICENSE create mode 100644 README create mode 100644 Rakefile create mode 100644 init.rb create mode 100644 install.rb create mode 100644 lib/infinite_page.rb create mode 100644 public/javascripts/infinite_page.js create mode 100644 tasks/infinite_page_tasks.rake create mode 100644 test/infinite_page_test.rb create mode 100644 test/test_helper.rb create mode 100644 uninstall.rb diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..9376605 --- /dev/null +++ b/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. diff --git a/README b/README new file mode 100644 index 0000000..dd6e037 --- /dev/null +++ b/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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..9573a2e --- /dev/null +++ b/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 \ No newline at end of file diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..b12a4ce --- /dev/null +++ b/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 + diff --git a/install.rb b/install.rb new file mode 100644 index 0000000..eb78806 --- /dev/null +++ b/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' \ No newline at end of file diff --git a/lib/infinite_page.rb b/lib/infinite_page.rb new file mode 100644 index 0000000..acedc54 --- /dev/null +++ b/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 += "" + 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 \ No newline at end of file diff --git a/public/javascripts/infinite_page.js b/public/javascripts/infinite_page.js new file mode 100644 index 0000000..000c1a8 --- /dev/null +++ b/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) { + 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(); \ No newline at end of file diff --git a/tasks/infinite_page_tasks.rake b/tasks/infinite_page_tasks.rake new file mode 100644 index 0000000..975fd29 --- /dev/null +++ b/tasks/infinite_page_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :infinite_page do +# # Task goes here +# end diff --git a/test/infinite_page_test.rb b/test/infinite_page_test.rb new file mode 100644 index 0000000..b2977f8 --- /dev/null +++ b/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 diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..cf148b8 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,3 @@ +require 'rubygems' +require 'active_support' +require 'active_support/test_case' \ No newline at end of file diff --git a/uninstall.rb b/uninstall.rb new file mode 100644 index 0000000..c41ef67 --- /dev/null +++ b/uninstall.rb @@ -0,0 +1,3 @@ +require 'fileutils' + +FileUtils.rm %{infinite_page.js}.collect { |f| File.dirname(__FILE__) + '/../../../public/javascripts/' + f } \ No newline at end of file