Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Apr 25, 2009
0 parents commit 64b8c51
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 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 changes: 13 additions & 0 deletions 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 changes: 23 additions & 0 deletions 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 changes: 7 additions & 0 deletions 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 changes: 3 additions & 0 deletions 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 changes: 34 additions & 0 deletions 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 changes: 44 additions & 0 deletions 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 changes: 4 additions & 0 deletions tasks/infinite_page_tasks.rake
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :infinite_page do
# # Task goes here
# end
8 changes: 8 additions & 0 deletions 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 changes: 3 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,3 @@
require 'rubygems'
require 'active_support'
require 'active_support/test_case'
3 changes: 3 additions & 0 deletions uninstall.rb
@@ -0,0 +1,3 @@
require 'fileutils'

FileUtils.rm %{infinite_page.js}.collect { |f| File.dirname(__FILE__) + '/../../../public/javascripts/' + f }

0 comments on commit 64b8c51

Please sign in to comment.