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 4 target_params: null,
5 5 target_id: 0,
6 6 target_type: null,
  7 + top_id: 0,
7 8 bottom_id: 0,
8 9 loading_more_disabled: false,
  10 + reversed: false,
9 11  
10 12 init:
11 13 function(tid, tt, path) {
12 14 this.notes_path = path + ".js";
13 15 this.target_id = tid;
14 16 this.target_type = tt;
  17 + this.reversed = $("#notes-list").hasClass("reversed");
15 18 this.target_params = "&target_type=" + this.target_type + "&target_id=" + this.target_id;
16 19  
17 20 // get initial set of notes
... ... @@ -69,12 +72,18 @@ var NoteList = {
69 72 * Replaces the content of #notes-list with the given html.
70 73 */
71 74 setContent:
72   - function(last_id, html) {
  75 + function(first_id, last_id, html) {
  76 + this.top_id = first_id;
73 77 this.bottom_id = last_id;
74 78 $("#notes-list").html(html);
75 79  
76   - // Init infinite scrolling
  80 + // init infinite scrolling
77 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 123 $.ajax({
115 124 type: "GET",
116 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 127 complete: function(){ $('.notes-status').removeClass("loading")},
119 128 beforeSend: function() { $('.notes-status').addClass("loading") },
120 129 dataType: "script"});
... ... @@ -142,7 +151,9 @@ var NoteList = {
142 151 this.loading_more_disabled = true;
143 152  
144 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 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 180 getNew:
170 181 function() {
171 182 $.ajax({
172 183 type: "GET",
173 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 186 dataType: "script"});
176 187 },
177 188  
... ... @@ -189,7 +200,9 @@ var NoteList = {
189 200 */
190 201 appendNewNote:
191 202 function(id, html) {
192   - if(id != this.bottom_id) {
  203 + if (this.reversed) {
  204 + $("#new-notes-list").prepend(html);
  205 + } else {
193 206 $("#new-notes-list").append(html);
194 207 }
195 208 }
... ...
app/contexts/notes/load_context.rb
... ... @@ -4,6 +4,7 @@ module Notes
4 4 target_type = params[:target_type]
5 5 target_id = params[:target_id]
6 6 after_id = params[:after_id]
  7 + before_id = params[:before_id]
7 8  
8 9  
9 10 @notes = case target_type
... ... @@ -17,14 +18,16 @@ module Notes
17 18 project.snippets.find(target_id).notes.fresh
18 19 when "wall"
19 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 22 when "wiki"
22 23 project.wikis.reverse.map {|w| w.notes.fresh }.flatten[0..20]
23 24 end
24 25  
25 26 @notes = if after_id
26 27 @notes.where("id > ?", after_id)
27   - else
  28 + elsif before_id
  29 + @notes.where("id < ?", before_id)
  30 + else
28 31 @notes
29 32 end
30 33 end
... ...
app/models/note.rb
... ... @@ -36,7 +36,7 @@ class Note &lt; ActiveRecord::Base
36 36 scope :today, where("created_at >= :date", date: Date.today)
37 37 scope :last_week, where("created_at >= :date", date: (Date.today - 7.days))
38 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 40 scope :inc_author_project, includes(:project, :author)
41 41 scope :inc_author, includes(:author)
42 42  
... ...
app/views/notes/index.js.haml
... ... @@ -9,7 +9,7 @@
9 9  
10 10 - else
11 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 14 - else
15 15 - if loading_more_notes?
... ...