Commit 07eec9c66a910b5a808852f498e1dc9c88b701d2

Authored by Riyad Preukschas
1 parent 7563abbe

Update Notes JS for reversed notes

app/assets/javascripts/notes.js
@@ -4,14 +4,17 @@ var NoteList = { @@ -4,14 +4,17 @@ var NoteList = {
4 target_params: null, 4 target_params: null,
5 target_id: 0, 5 target_id: 0,
6 target_type: null, 6 target_type: null,
  7 + top_id: 0,
7 bottom_id: 0, 8 bottom_id: 0,
8 loading_more_disabled: false, 9 loading_more_disabled: false,
  10 + reversed: false,
9 11
10 init: 12 init:
11 function(tid, tt, path) { 13 function(tid, tt, path) {
12 this.notes_path = path + ".js"; 14 this.notes_path = path + ".js";
13 this.target_id = tid; 15 this.target_id = tid;
14 this.target_type = tt; 16 this.target_type = tt;
  17 + this.reversed = $("#notes-list").hasClass("reversed");
15 this.target_params = "&target_type=" + this.target_type + "&target_id=" + this.target_id; 18 this.target_params = "&target_type=" + this.target_type + "&target_id=" + this.target_id;
16 19
17 // get initial set of notes 20 // get initial set of notes
@@ -69,12 +72,18 @@ var NoteList = { @@ -69,12 +72,18 @@ var NoteList = {
69 * Replaces the content of #notes-list with the given html. 72 * Replaces the content of #notes-list with the given html.
70 */ 73 */
71 setContent: 74 setContent:
72 - function(last_id, html) { 75 + function(first_id, last_id, html) {
  76 + this.top_id = first_id;
73 this.bottom_id = last_id; 77 this.bottom_id = last_id;
74 $("#notes-list").html(html); 78 $("#notes-list").html(html);
75 79
76 - // Init infinite scrolling 80 + // init infinite scrolling
77 this.initLoadMore(); 81 this.initLoadMore();
  82 +
  83 + // init getting new notes
  84 + if (this.reversed) {
  85 + this.initRefreshNew();
  86 + }
78 }, 87 },
79 88
80 89
@@ -114,7 +123,7 @@ var NoteList = { @@ -114,7 +123,7 @@ var NoteList = {
114 $.ajax({ 123 $.ajax({
115 type: "GET", 124 type: "GET",
116 url: this.notes_path, 125 url: this.notes_path,
117 - data: "loading_more=1&after_id=" + this.bottom_id + this.target_params, 126 + data: "loading_more=1&" + (this.reversed ? "before_id" : "after_id") + "=" + this.bottom_id + this.target_params,
118 complete: function(){ $('.notes-status').removeClass("loading")}, 127 complete: function(){ $('.notes-status').removeClass("loading")},
119 beforeSend: function() { $('.notes-status').addClass("loading") }, 128 beforeSend: function() { $('.notes-status').addClass("loading") },
120 dataType: "script"}); 129 dataType: "script"});
@@ -142,7 +151,9 @@ var NoteList = { @@ -142,7 +151,9 @@ var NoteList = {
142 this.loading_more_disabled = true; 151 this.loading_more_disabled = true;
143 152
144 // from now on only get new notes 153 // from now on only get new notes
145 - this.initRefreshNew(); 154 + if (!this.reversed) {
  155 + this.initRefreshNew();
  156 + }
146 }, 157 },
147 158
148 159
@@ -164,14 +175,14 @@ var NoteList = { @@ -164,14 +175,14 @@ var NoteList = {
164 }, 175 },
165 176
166 /** 177 /**
167 - * Gets the new set of notes (i.e. all notes after ). 178 + * Gets the new set of notes.
168 */ 179 */
169 getNew: 180 getNew:
170 function() { 181 function() {
171 $.ajax({ 182 $.ajax({
172 type: "GET", 183 type: "GET",
173 url: this.notes_path, 184 url: this.notes_path,
174 - data: "loading_new=1&after_id=" + this.bottom_id + this.target_params, 185 + data: "loading_new=1&after_id=" + (this.reversed ? this.top_id : this.bottom_id) + this.target_params,
175 dataType: "script"}); 186 dataType: "script"});
176 }, 187 },
177 188
@@ -189,7 +200,9 @@ var NoteList = { @@ -189,7 +200,9 @@ var NoteList = {
189 */ 200 */
190 appendNewNote: 201 appendNewNote:
191 function(id, html) { 202 function(id, html) {
192 - if(id != this.bottom_id) { 203 + if (this.reversed) {
  204 + $("#new-notes-list").prepend(html);
  205 + } else {
193 $("#new-notes-list").append(html); 206 $("#new-notes-list").append(html);
194 } 207 }
195 } 208 }
app/contexts/notes/load_context.rb
@@ -4,6 +4,7 @@ module Notes @@ -4,6 +4,7 @@ module Notes
4 target_type = params[:target_type] 4 target_type = params[:target_type]
5 target_id = params[:target_id] 5 target_id = params[:target_id]
6 after_id = params[:after_id] 6 after_id = params[:after_id]
  7 + before_id = params[:before_id]
7 8
8 9
9 @notes = case target_type 10 @notes = case target_type
@@ -17,14 +18,16 @@ module Notes @@ -17,14 +18,16 @@ module Notes
17 project.snippets.find(target_id).notes.fresh 18 project.snippets.find(target_id).notes.fresh
18 when "wall" 19 when "wall"
19 # this is the only case, where the order is DESC 20 # this is the only case, where the order is DESC
20 - project.common_notes.order("created_at DESC").limit(50) 21 + project.common_notes.order("created_at DESC, id DESC").limit(50)
21 when "wiki" 22 when "wiki"
22 project.wikis.reverse.map {|w| w.notes.fresh }.flatten[0..20] 23 project.wikis.reverse.map {|w| w.notes.fresh }.flatten[0..20]
23 end 24 end
24 25
25 @notes = if after_id 26 @notes = if after_id
26 @notes.where("id > ?", after_id) 27 @notes.where("id > ?", after_id)
27 - else 28 + elsif before_id
  29 + @notes.where("id < ?", before_id)
  30 + else
28 @notes 31 @notes
29 end 32 end
30 end 33 end
app/models/note.rb
@@ -36,7 +36,7 @@ class Note &lt; ActiveRecord::Base @@ -36,7 +36,7 @@ class Note &lt; ActiveRecord::Base
36 scope :today, where("created_at >= :date", date: Date.today) 36 scope :today, where("created_at >= :date", date: Date.today)
37 scope :last_week, where("created_at >= :date", date: (Date.today - 7.days)) 37 scope :last_week, where("created_at >= :date", date: (Date.today - 7.days))
38 scope :since, lambda { |day| where("created_at >= :date", date: (day)) } 38 scope :since, lambda { |day| where("created_at >= :date", date: (day)) }
39 - scope :fresh, order("created_at ASC") 39 + scope :fresh, order("created_at ASC, id ASC")
40 scope :inc_author_project, includes(:project, :author) 40 scope :inc_author_project, includes(:project, :author)
41 scope :inc_author, includes(:author) 41 scope :inc_author, includes(:author)
42 42
app/views/notes/index.js.haml
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 9
10 - else 10 - else
11 :plain 11 :plain
12 - NoteList.setContent(#{@notes.last.id}, "#{escape_javascript(render 'notes/notes')}"); 12 + NoteList.setContent(#{@notes.first.id}, #{@notes.last.id}, "#{escape_javascript(render 'notes/notes')}");
13 13
14 - else 14 - else
15 - if loading_more_notes? 15 - if loading_more_notes?