Avoid using inline javascript to update message list
authorTom Hughes <tom@compton.nu>
Wed, 30 May 2018 14:30:23 +0000 (15:30 +0100)
committerTom Hughes <tom@compton.nu>
Wed, 30 May 2018 14:30:23 +0000 (15:30 +0100)
app/assets/javascripts/messages.js [new file with mode: 0644]
app/views/messages/_sent_message_summary.html.erb
app/views/messages/destroy.js.erb [deleted file]
app/views/messages/destroy.json.jsonify [new file with mode: 0644]
app/views/messages/inbox.html.erb
app/views/messages/mark.js.erb [deleted file]
app/views/messages/mark.json.jsonify [new file with mode: 0644]
app/views/messages/outbox.html.erb
config/initializers/assets.rb

diff --git a/app/assets/javascripts/messages.js b/app/assets/javascripts/messages.js
new file mode 100644 (file)
index 0000000..0cae79a
--- /dev/null
@@ -0,0 +1,30 @@
+$(document).ready(function () {
+  $(".inbox-mark-unread").on("ajax:success", function (event, data) {
+    $("#inboxanchor").remove();
+    $(".user-button").before(data.inboxanchor);
+
+    $("#inbox-count").replaceWith(data.inbox_count);
+
+    $(this).parents(".inbox-row").removeClass("inbox-row").addClass("inbox-row-unread");
+  });
+
+  $(".inbox-mark-read").on("ajax:success", function (event, data) {
+    $("#inboxanchor").remove();
+    $(".user-button").before(data.inboxanchor);
+
+    $("#inbox-count").replaceWith(data.inbox_count);
+
+    $(this).parents(".inbox-row-unread").removeClass("inbox-row-unread").addClass("inbox-row");
+  });
+
+  $(".inbox-destroy").on("ajax:success", function (event, data) {
+    $("#inboxanchor").remove();
+    $(".user-button").before(data.inboxanchor);
+
+    $("#inbox-count").replaceWith(data.inbox_count);
+
+    $(this).parents(".inbox-row, .inbox-row-unread").fadeOut(800, "linear", function () {
+      $(this).remove();
+    });
+  });
+});
index 5a64ea8..a9346fb 100644 (file)
@@ -2,5 +2,5 @@
   <td class="inbox-sender"><%= link_to h(sent_message_summary.recipient.display_name), user_path(sent_message_summary.recipient) %></td>
   <td class="inbox-subject"><%= link_to h(sent_message_summary.title), message_path(sent_message_summary) %></td>
   <td class="inbox-sent"><%= l sent_message_summary.sent_on, :format => :friendly %></td>
-  <td class="inbox-destroy"><%= button_to t('.destroy_button'), destroy_message_path(sent_message_summary, :referer => request.fullpath) %></td>
+  <td class="inbox-destroy"><%= button_to t('.destroy_button'), destroy_message_path(sent_message_summary, :referer => request.fullpath), { :remote => true } %></td>
 </tr>
diff --git a/app/views/messages/destroy.js.erb b/app/views/messages/destroy.js.erb
deleted file mode 100644 (file)
index 86677ab..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-$("#inboxanchor").replaceWith("<%=j render :partial => "layouts/inbox" %>");
-$("#inbox-count").replaceWith("<%=j render :partial => "message_count" %>");
-$("#inbox-<%= @message.id %>").fadeOut(800, "linear", function () {
-  $(this).remove();
-});
diff --git a/app/views/messages/destroy.json.jsonify b/app/views/messages/destroy.json.jsonify
new file mode 100644 (file)
index 0000000..3403ac8
--- /dev/null
@@ -0,0 +1,2 @@
+json.inboxanchor render(:partial => "layouts/inbox")
+json.inbox_count render(:partial => "message_count")
index 56ed53c..b51815b 100644 (file)
@@ -1,3 +1,7 @@
+<% content_for :head do %>
+  <%= javascript_include_tag "messages" %>
+<% end %>
+
 <% content_for :heading do %>
   <h2><%= t '.my_inbox'%>/<%= link_to t('.outbox'), outbox_path(current_user.display_name) %></h2>
 <% end %>
diff --git a/app/views/messages/mark.js.erb b/app/views/messages/mark.js.erb
deleted file mode 100644 (file)
index d67e74a..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-$("#inboxanchor").replaceWith("<%=j render :partial => "layouts/inbox" %>");
-$("#inbox-count").replaceWith("<%=j render :partial => "message_count" %>");
-<% if @message.message_read? -%>
-$("#inbox-<%= @message.id %>").removeClass("inbox-row-unread").addClass("inbox-row");
-<% else -%>
-$("#inbox-<%= @message.id %>").removeClass("inbox-row").addClass("inbox-row-unread");
-<% end -%>
diff --git a/app/views/messages/mark.json.jsonify b/app/views/messages/mark.json.jsonify
new file mode 100644 (file)
index 0000000..3403ac8
--- /dev/null
@@ -0,0 +1,2 @@
+json.inboxanchor render(:partial => "layouts/inbox")
+json.inbox_count render(:partial => "message_count")
index 38639cd..c9688de 100644 (file)
@@ -1,3 +1,7 @@
+<% content_for :head do %>
+  <%= javascript_include_tag "messages" %>
+<% end %>
+
 <% content_for :heading do %>
   <h2><%= raw(t '.my_inbox', :inbox_link => link_to(t('.inbox'), inbox_path(current_user.display_name))) %>/<%= t'.outbox' %></h2>
 <% end %>
index 5d90804..1610975 100644 (file)
@@ -12,7 +12,7 @@ Rails.application.config.assets.paths << Rails.root.join("config")
 # Precompile additional assets.
 # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
 Rails.application.config.assets.precompile += %w[index.js browse.js welcome.js fixthemap.js]
-Rails.application.config.assets.precompile += %w[user.js login.js diary_entry.js edit/*.js]
+Rails.application.config.assets.precompile += %w[user.js login.js diary_entry.js messages.js edit/*.js]
 Rails.application.config.assets.precompile += %w[screen-ltr.css print-ltr.css]
 Rails.application.config.assets.precompile += %w[screen-rtl.css print-rtl.css]
 Rails.application.config.assets.precompile += %w[leaflet-all.css leaflet.ie.css]