Skip to content
This repository
Browse code

References #41 - Dynamic/reflected inserting seems to be working 90% …

…or so. So long as you don't sort things too quickly, it works very reliably.

- Further refactored javascript used to slide things in and out.
- The new thought box uses the `slideThoughtIn`, so I made it accessible on the window object.
- Verified that dynamic sliding works for moving objects both forward and backward in the list, from any position to any other position, including the very front and end of the list.
- Rebuilt assets for deploy.
- Re-worked the way that the thoughts are ordered in the `ThoughtWalls#show` action, so the reflected shifts in position get replayed in the same order in which they occured. This isn't perfect because if multiple people reorder things simultaneously, the order in which they get applied is ambiguous, and might not be correct for everyone. Long term I'll have to fix this via a periodic "refresh all positions" lookup that goes through the entire list, and moves things such that all clients are guaranteed to be in a consistent state.
- Changed when the `safe-to-alter-layout` value is changed, so to address an issue the was causing updates on a client browser to permanently stall.
  • Loading branch information...
commit 4e28a9890ff3f1924582b3767e0812c926a02ad3 1 parent db231b5
Jeff Lunt authored June 08, 2012
10  app/assets/javascripts/effects.js.coffee.erb
@@ -5,12 +5,10 @@ $ ->
5 5
     toIndex   = normalizeIndex(toIndex, numElements)
6 6
     
7 7
     unless fromIndex is toIndex
8  
-      
9 8
       idFrom = getNearestIdToIndex(fromIndex, numElements)
10  
-      idTo   = getNearestIdToIndex(toIndex,   numElements) if fromIndex > toIndex
11  
-      idTo   = getNearestIdToIndex(toIndex+1, numElements) if fromIndex < toIndex
  9
+      idTo   = getNearestIdToIndex(toIndex,   numElements)
12 10
       
13  
-      shouldInsertAfter = toIndex is numElements
  11
+      shouldInsertAfter = if (toIndex > fromIndex) then true else false
14 12
       
15 13
       movingElement = $('#'+idFrom)
16 14
       
@@ -32,10 +30,10 @@ $ ->
32 30
   
33 31
   normalizeIndex = (index, numElements) ->
34 32
     return 0 if index <= 0
35  
-    return numElements if index > numElements
  33
+    return numElements if index >= numElements
36 34
     return index
37 35
   
38  
-  slideThoughtIn = (element, callback) ->
  36
+  window.slideThoughtIn = (element, callback) ->
39 37
     element.animate({
40 38
       width: 215}, 
41 39
       500, 
7  app/assets/javascripts/thoughts.js.coffee.erb
@@ -21,6 +21,8 @@ $ ->
21 21
     
22 22
   $(document).ready ->
23 23
     $('#thought-list').sortable({
  24
+      start: (event, ui) ->
  25
+        $('#safe-to-alter-layout').html('false')
24 26
       stop: (event, ui) -> 
25 27
         thoughtBoxIdParts = ui.item[0].getAttribute("id").split('-')
26 28
         backendID = thoughtBoxIdParts[thoughtBoxIdParts.length-1]
@@ -30,7 +32,4 @@ $ ->
30 32
           data: { _method:'PUT', thought: { ui_position: ui.item.index() } }
31 33
         })
32 34
         $('#safe-to-alter-layout').html('true')
33  
-    })
34  
-    
35  
-    $('#thought-list').on "mousedown", (event) ->
36  
-      $('#safe-to-alter-layout').html('false')
  35
+    })
2  app/controllers/thought_walls_controller.rb
@@ -6,7 +6,7 @@ def show
6 6
     if params[:since]
7 7
       @last_render_at = Time.at(params[:since].to_i).utc
8 8
       @thought_wall = ThoughtWall.find_by_code(params[:id])
9  
-      @thoughts = Thought.where(["thought_wall_id = ? AND updated_at >= ?", @thought_wall.id, @last_render_at]).order("id DESC").includes(:thought_histories)
  9
+      @thoughts = Thought.where(["thought_wall_id = ? AND updated_at >= ?", @thought_wall.id, @last_render_at]).order("position_updated_at ASC").includes(:thought_histories)
10 10
       
11 11
       @update_client_title = @thought_wall.updated_at > @last_render_at
12 12
       if @update_client_title || @thoughts.count > 0
4  app/models/thought.rb
@@ -42,6 +42,10 @@ def reorder_siblings_relative_to(new_manual_order)
42 42
     end
43 43
   end
44 44
   
  45
+  def intended_ui_position
  46
+    siblings.count - manual_order - 1
  47
+  end
  48
+  
45 49
   def siblings
46 50
     thought_wall.thoughts
47 51
   end
6  app/views/thought_walls/show.js.erb
@@ -16,8 +16,10 @@ if ($('#safe-to-alter-layout').html() === "true") {
16 16
     } else {
17 17
       $(thought_box_id_selector).replaceWith("<%= escape_javascript(render(:partial => 'thoughts/thought_box', :locals => {:thought => thought})) %>");
18 18
       <% if thought.position_updated_at >= @last_render_at %>
19  
-        slideThoughtOut($(thought_box_id_selector), function() {$('#thought-list').prepend($(thought_box_id_selector)) })
20  
-        slideThoughtIn($(thought_box_id_selector), function() {})
  19
+        currentIndex = $(thought_box_id_selector).index()
  20
+        intendedIndex = <%= thought.intended_ui_position %>
  21
+        
  22
+        shiftThought(currentIndex, intendedIndex)
21 23
       <% end %>
22 24
     }
23 25
   <% end %>
BIN  public/assets/application-11ed613adb6d14be5aa37a2baed1ce06.css.gz
Binary file not shown
4  ...s/application-0e007e5a47a78d9386ce69629618c342.js → ...s/application-2610858565ffd95a5e985ff0da2f00d9.js
2 additions, 2 deletions not shown
BIN  ...pplication-0e007e5a47a78d9386ce69629618c342.js.gz → ...pplication-2610858565ffd95a5e985ff0da2f00d9.js.gz
Binary file not shown
BIN  public/assets/application.css.gz
Binary file not shown
4  public/assets/application.js
2 additions, 2 deletions not shown
BIN  public/assets/application.js.gz
Binary file not shown
2  public/assets/manifest.yml
@@ -2,6 +2,6 @@
2 2
 brain-dark.jpg: brain-dark-034dd1edd61771fce859d556be61a491.jpg
3 3
 brain-light.jpg: brain-light-561b24be085e75f3378e031c64251277.jpg
4 4
 spinner.gif: spinner-6aa1008d858cbed1cd320422f59a4789.gif
5  
-application.js: application-0e007e5a47a78d9386ce69629618c342.js
  5
+application.js: application-2610858565ffd95a5e985ff0da2f00d9.js
6 6
 PTM55FT.ttf: PTM55FT-7515d9db13e596569dc717ef3606ce98.ttf
7 7
 application.css: application-11ed613adb6d14be5aa37a2baed1ce06.css

0 notes on commit 4e28a98

Please sign in to comment.
Something went wrong with that request. Please try again.