Commit 707b055c4bb3533163533a4277a0ad126685b625
1 parent
32ebb9f0
Exists in
master
and in
11 other branches
proposals_discussion: load proposals on scroll
Showing
5 changed files
with
70 additions
and
7 deletions
Show diff stats
controllers/public/proposals_discussion_plugin_public_controller.rb
0 → 100644
... | ... | @@ -0,0 +1,35 @@ |
1 | +class ProposalsDiscussionPluginPublicController < ApplicationController | |
2 | + | |
3 | + needs_profile | |
4 | + | |
5 | + before_filter :set_rand_cookie | |
6 | + | |
7 | + def load_proposals | |
8 | + holder = profile.articles.find(params[:holder_id]) | |
9 | + page = (params[:page] || 1).to_i | |
10 | + | |
11 | + set_seed | |
12 | + proposals = holder.proposals.includes(:parent, :profile).reorder('random()') | |
13 | + proposals = proposals.page(page).per_page(5) | |
14 | + | |
15 | + unless proposals.empty? | |
16 | + render :partial => 'content_viewer/proposals_list_content', :locals => {:proposals => proposals, :holder => holder, :next_page => page+1} | |
17 | + else | |
18 | + render :text => '' | |
19 | + end | |
20 | + end | |
21 | + | |
22 | + private | |
23 | + | |
24 | + def set_seed | |
25 | + #XXX postgresql specific | |
26 | + seed_val = profile.connection.quote(cookies[:_noosfero_proposals_discussion_rand_seed]) | |
27 | + profile.connection.execute("select setseed(#{seed_val})") | |
28 | + end | |
29 | + | |
30 | + def set_rand_cookie | |
31 | + return if cookies[:_noosfero_proposals_discussion_rand_seed].present? | |
32 | + cookies[:_noosfero_proposals_discussion_rand_seed] = {value: rand, expires: Time.now + 900} | |
33 | + end | |
34 | + | |
35 | +end | ... | ... |
... | ... | @@ -0,0 +1,15 @@ |
1 | +/*! | |
2 | + * jScroll - jQuery Plugin for Infinite Scrolling / Auto-Paging - v2.2.4 | |
3 | + * http://jscroll.com/ | |
4 | + * | |
5 | + * Copyright 2011-2013, Philip Klauzinski | |
6 | + * http://klauzinski.com/ | |
7 | + * Dual licensed under the MIT and GPL Version 2 licenses. | |
8 | + * http://jscroll.com/#license | |
9 | + * http://www.opensource.org/licenses/mit-license.php | |
10 | + * http://www.gnu.org/licenses/gpl-2.0.html | |
11 | + * | |
12 | + * @author Philip Klauzinski | |
13 | + * @requires jQuery v1.4.3+ | |
14 | + */ | |
15 | +(function(b){b.jscroll={defaults:{debug:false,autoTrigger:true,autoTriggerUntil:false,loadingHtml:"<small>Loading...</small>",padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",callback:false}};var a=function(e,g){var o=e.data("jscroll"),n=(typeof g==="function")?{callback:g}:g,p=b.extend({},b.jscroll.defaults,n,o||{}),c=(e.css("overflow-y")==="visible"),l=e.find(p.nextSelector).first(),v=b(window),h=b("body"),q=c?v:e,m=b.trim(l.attr("href")+" "+p.contentSelector);e.data("jscroll",b.extend({},o,{initialized:true,waiting:false,nextHref:m}));r();k();t();function k(){var x=b(p.loadingHtml).filter("img").attr("src");if(x){var w=new Image();w.src=x}}function r(){if(!e.find(".jscroll-inner").length){e.contents().wrapAll('<div class="jscroll-inner" />')}}function d(w){if(p.pagingSelector){var x=w.closest(p.pagingSelector).hide()}else{var x=w.parent().not(".jscroll-inner,.jscroll-added").addClass("jscroll-next-parent").hide();if(!x.length){w.wrap('<div class="jscroll-next-parent" />').parent().hide()}}}function j(){return q.unbind(".jscroll").removeData("jscroll").find(".jscroll-inner").children().unwrap().filter(".jscroll-added").children().unwrap()}function i(){r();var D=e.find("div.jscroll-inner").first(),B=e.data("jscroll"),C=parseInt(e.css("borderTopWidth")),y=isNaN(C)?0:C,x=parseInt(e.css("paddingTop"))+y,A=c?q.scrollTop():e.offset().top,z=D.length?D.offset().top:0,w=Math.ceil(A-z+q.height()+x);if(!B.waiting&&w+p.padding>=D.outerHeight()){f("info","jScroll:",D.outerHeight()-w,"from bottom. Loading next request...");return u()}}function s(w){w=w||e.data("jscroll");if(!w||!w.nextHref){f("warn","jScroll: nextSelector not found - destroying");j();return false}else{t();return true}}function t(){var w=e.find(p.nextSelector).first();if(p.autoTrigger&&(p.autoTriggerUntil===false||p.autoTriggerUntil>0)){d(w);if(h.height()<=v.height()){i()}q.unbind(".jscroll").bind("scroll.jscroll",function(){return i()});if(p.autoTriggerUntil>0){p.autoTriggerUntil--}}else{q.unbind(".jscroll");w.bind("click.jscroll",function(){d(w);u();return false})}}function u(){var x=e.find("div.jscroll-inner").first(),w=e.data("jscroll");w.waiting=true;x.append('<div class="jscroll-added" />').children(".jscroll-added").last().html('<div class="jscroll-loading">'+p.loadingHtml+"</div>");return e.animate({scrollTop:x.outerHeight()},0,function(){x.find("div.jscroll-added").last().load(w.nextHref,function(A,z,B){if(z==="error"){return j()}var y=b(this).find(p.nextSelector).first();w.waiting=false;w.nextHref=y.attr("href")?b.trim(y.attr("href")+" "+p.contentSelector):false;b(".jscroll-next-parent",e).remove();s();if(p.callback){p.callback.call(this)}f("dir",w)})})}function f(w){if(p.debug&&typeof console==="object"&&(typeof w==="object"||typeof console[w]==="function")){if(typeof w==="object"){var y=[];for(var x in w){if(typeof console[x]==="function"){y=(w[x].length)?w[x]:[w[x]];console[x].apply(console,y)}else{console.log.apply(console,y)}}}else{console[w].apply(console,Array.prototype.slice.call(arguments,1))}}}b.extend(e.jscroll,{destroy:j});return e};b.fn.jscroll=function(c){return this.each(function(){var f=b(this),e=f.data("jscroll");if(e&&e.initialized){return}var d=new a(f,c)})}})(jQuery); | |
0 | 16 | \ No newline at end of file | ... | ... |
views/content_viewer/_proposals_list.html.erb
1 | +<script src="/javascripts/plugins/proposals_discussion/jquery.jscroll.min.js" type="text/javascript"></script> | |
2 | +<script> | |
3 | + jQuery(document).ready(function($) { | |
4 | + $('.proposals').jscroll({ | |
5 | + loadingHtml: '<img src="/images/loading.gif" alt="Loading" /> <%= _("Loading...") %>', | |
6 | + nextSelector: 'div.more a' | |
7 | + }); | |
8 | + $('.proposals').trigger('scroll.jscroll'); | |
9 | + }); | |
10 | +</script> | |
11 | + | |
1 | 12 | <div class="proposals"> |
2 | - <%= render :partial => 'content_viewer/proposal_card', :collection => holder.proposals.includes(:parent, :profile) %> | |
3 | - <div class="clear"></div> | |
13 | + <div class="more"> | |
14 | + <img src="/images/loading.gif" alt="Loading" /><%= _("Loading...") %> | |
15 | + <%= link_to '', url_for({:controller => 'proposals_discussion_plugin_public', :action => 'load_proposals', :holder_id => holder.id, :profile => profile.identifier }) %> | |
16 | + </div> | |
4 | 17 | </div> | ... | ... |
... | ... | @@ -0,0 +1,5 @@ |
1 | +<%= render :partial => 'content_viewer/proposal_card', :collection => proposals %> | |
2 | + | |
3 | +<div class="more"> | |
4 | + <%= link_to _('More'), url_for({:controller => 'proposals_discussion_plugin_public', :action => 'load_proposals', :holder_id => holder.id, :profile => profile.identifier, :page => next_page }) %> | |
5 | +</div> | ... | ... |
views/content_viewer/topic.html.erb
... | ... | @@ -25,8 +25,3 @@ |
25 | 25 | </div> |
26 | 26 | |
27 | 27 | <%= render :partial => 'content_viewer/proposals_list', :locals => {:holder => @page} %> |
28 | - | |
29 | -<div class="proposals"> | |
30 | - <%= render :partial => 'content_viewer/proposal_card', :collection => @page.proposals.includes(:parent, :profile) %> | |
31 | - <div class="clear"></div> | |
32 | -</div> | ... | ... |