From 586c53ea0594a327b346c6fed38528a1f508c9e1 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sat, 5 Nov 2011 13:59:43 +0200 Subject: [PATCH] fixed notes loading/paging --- app/assets/javascripts/note.js | 24 ++++++++++++++++++++++- app/controllers/application_controller.rb | 12 ++++++++++++ app/controllers/commits_controller.rb | 5 +---- app/controllers/issues_controller.rb | 5 +---- app/controllers/projects_controller.rb | 5 +---- app/views/notes/_load.js.haml | 11 +++++++++-- 6 files changed, 47 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/note.js b/app/assets/javascripts/note.js index 71279fc4..7acc81e5 100644 --- a/app/assets/javascripts/note.js +++ b/app/assets/javascripts/note.js @@ -31,6 +31,15 @@ append: this.initLoadMore(); }, +replace: + function(fid, lid, html) { + this.first_id = fid; + this.last_id = lid; + $("#notes-list").html(html); + this.initLoadMore(); + }, + + prepend: function(id, html) { this.last_id = id; @@ -47,10 +56,23 @@ getNew: dataType: "script"}); }, +refresh: + function() { + // refersh notes list + $.ajax({ + type: "GET", + url: location.href, + data: "first_id=" + this.first_id + "&last_id=" + this.last_id, + dataType: "script"}); + }, + + + initRefresh: function() { // init timer - var int = setInterval("NoteList.getNew()", 20000); + var intNew = setInterval("NoteList.getNew()", 15000); + var intRefresh = setInterval("NoteList.refresh()", 90000); }, initLoadMore: diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 070def96..5c25d7d6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -83,4 +83,16 @@ class ApplicationController < ActionController::Base cookies[:view_style] = "" end end + + def respond_with_notes + if params[:last_id] && params[:first_id] + @notes = @notes.where("id >= ?", params[:first_id]) + elsif params[:last_id] + @notes = @notes.where("id > ?", params[:last_id]) + elsif params[:first_id] + @notes = @notes.where("id < ?", params[:first_id]) + else + nil + end + end end diff --git a/app/controllers/commits_controller.rb b/app/controllers/commits_controller.rb index f6af56f8..fa0d0b57 100644 --- a/app/controllers/commits_controller.rb +++ b/app/controllers/commits_controller.rb @@ -33,10 +33,7 @@ class CommitsController < ApplicationController respond_to do |format| format.html - format.js do - @notes = @notes.where("id > ?", params[:last_id]) if params[:last_id] - @notes = @notes.where("id < ?", params[:first_id]) if params[:first_id] - end + format.js { respond_with_notes } end end end diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index b6fd85b8..7ff1ccb3 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -40,10 +40,7 @@ class IssuesController < ApplicationController respond_to do |format| format.html - format.js do - @notes = @notes.where("id > ?", params[:last_id]) if params[:last_id] - @notes = @notes.where("id < ?", params[:first_id]) if params[:first_id] - end + format.js { respond_with_notes } end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 067245c4..ef7eb921 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -90,10 +90,7 @@ class ProjectsController < ApplicationController respond_to do |format| format.html - format.js do - @notes = @notes.where("id > ?", params[:last_id]) if params[:last_id] - @notes = @notes.where("id < ?", params[:first_id]) if params[:first_id] - end + format.js { respond_with_notes } end end diff --git a/app/views/notes/_load.js.haml b/app/views/notes/_load.js.haml index f560f0bd..c7ec9ed3 100644 --- a/app/views/notes/_load.js.haml +++ b/app/views/notes/_load.js.haml @@ -1,10 +1,17 @@ - unless @notes.blank? - - if params[:last_id] + - if params[:last_id] && params[:first_id] + :plain + NoteList.replace(#{@notes.last.id}, #{@notes.first.id}, "#{escape_javascript(render(:partial => 'notes/notes_list'))}"); + + + - elsif params[:last_id] :plain NoteList.prepend(#{@notes.first.id}, "#{escape_javascript(render(:partial => 'notes/notes_list'))}"); - - if params[:first_id] + - elsif params[:first_id] :plain NoteList.append(#{@notes.last.id}, "#{escape_javascript(render(:partial => 'notes/notes_list'))}"); + - else + :plain