Parcourir la source

Added turbolinks to buttons and forms

Andrew Kane il y a 8 ans
Parent
commit
5b2f570991

+ 0 - 4
app/assets/javascripts/blazer/application.js

@@ -24,10 +24,6 @@ $(document).on('mouseenter', '.dropdown-toggle', function () {
   $(this).parent().addClass('open');
 });
 
-$(document).on('ajax:complete', '.refresh-link', function () {
-  Turbolinks.visit(window.location.pathname + window.location.search);
-});
-
 $(document).on("submit", "form[method=get]", function() {
   Turbolinks.visit(this.action+(this.action.indexOf('?') == -1 ? '?' : '&')+$(this).serialize());
   return false;

+ 4 - 0
app/assets/stylesheets/blazer/application.css

@@ -174,3 +174,7 @@ input.search:focus {
 #code.expanded {
   max-height: none;
 }
+
+.turbolinks-progress-bar {
+  visibility: hidden;
+}

+ 40 - 0
app/controllers/blazer/base_controller.rb

@@ -80,5 +80,45 @@ module Blazer
       send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
     end
     helper_method :blazer_user
+
+    # from turbolinks
+    # https://github.com/turbolinks/turbolinks-rails/blob/master/lib/turbolinks/redirection.rb
+    def redirect_to(url = {}, options = {})
+      turbolinks = options.delete(:turbolinks)
+
+      super.tap do
+        if turbolinks != false && request.xhr? && !request.get?
+          visit_location_with_turbolinks(location, turbolinks)
+        else
+          if request.headers["Turbolinks-Referrer"]
+            store_turbolinks_location_in_session(location)
+          end
+        end
+      end
+    end
+
+    def visit_location_with_turbolinks(location, action)
+      visit_options = {
+        action: action.to_s == "advance" ? action : "replace"
+      }
+
+      script = []
+      script << "Turbolinks.clearCache()"
+      script << "Turbolinks.visit(#{location.to_json}, #{visit_options.to_json})"
+
+      self.status = 200
+      self.response_body = script.join("\n")
+      response.content_type = "text/javascript"
+    end
+
+    def store_turbolinks_location_in_session(location)
+      session[:_turbolinks_location] = location if session
+    end
+
+    def set_turbolinks_location_header_from_session
+      if session && session[:_turbolinks_location]
+        response.headers["Turbolinks-Location"] = session.delete(:_turbolinks_location)
+      end
+    end
   end
 end

+ 1 - 1
app/controllers/blazer/dashboards_controller.rb

@@ -66,7 +66,7 @@ module Blazer
         Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement
         data_source.clear_cache(statement)
       end
-      head :ok
+      redirect_to dashboard_path(@dashboard, variable_params)
     end
 
     protected

+ 1 - 1
app/controllers/blazer/queries_controller.rb

@@ -144,7 +144,7 @@ module Blazer
       process_vars(@statement, @query.data_source)
       Blazer.transform_statement.call(data_source, @statement) if Blazer.transform_statement
       data_source.clear_cache(@statement)
-      head :ok
+      redirect_to query_path(@query, variable_params)
     end
 
     def update

+ 1 - 1
app/views/blazer/checks/_form.html.erb

@@ -63,7 +63,7 @@
   <p class="text-muted">Emails are sent when a check starts failing, and when it starts passing again.
   <p>
     <% if @check.persisted? %>
-      <%= link_to "Delete", check_path(@check), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
+      <%= link_to "Delete", check_path(@check), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger", remote: true %>
     <% end %>
     <%= f.submit "Save", class: "btn btn-success" %>
     <%= link_to "Back", :back, class: "btn btn-link" %>

+ 1 - 1
app/views/blazer/dashboards/_form.html.erb

@@ -43,7 +43,7 @@
   </div>
   <p>
     <% if @dashboard.persisted? %>
-      <%= link_to "Delete", dashboard_path(@dashboard), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
+      <%= link_to "Delete", dashboard_path(@dashboard), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger", remote: true %>
     <% end %>
     <%= f.submit "Save", class: "btn btn-success" %>
     <%= link_to "Back", :back, class: "btn btn-link" %>

+ 1 - 1
app/views/blazer/dashboards/show.html.erb

@@ -21,7 +21,7 @@
 <% if @data_sources.any? { |ds| ds.cache_mode != "off" } %>
   <p class="text-muted" style="float: right;">
     Some queries may be cached
-    <%= link_to "Refresh", refresh_dashboard_path(@dashboard, variable_params), method: :post, remote: true, class: "refresh-link" %>
+    <%= link_to "Refresh", refresh_dashboard_path(@dashboard, variable_params), method: :post, remote: true %>
   </p>
 <% end %>
 

+ 2 - 2
app/views/blazer/queries/_form.html.erb

@@ -2,7 +2,7 @@
   <div class="alert alert-danger"><%= @query.errors.full_messages.first %></div>
 <% end %>
 
-<%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params) : queries_path(variable_params)), html: {class: "the_form", autocomplete: "off"} do |f| %>
+<%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params) : queries_path(variable_params)), html: {class: "the_form", autocomplete: "off"}, remote: true do |f| %>
   <div class="row">
     <div id="statement-box" class="col-xs-8">
       <div class= "form-group">
@@ -41,7 +41,7 @@
       </div>
       <div class="text-right">
         <% if @query.persisted? %>
-          <%= link_to "Delete", query_path(@query), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
+          <%= link_to "Delete", query_path(@query), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger", remote: true %>
           <%= f.submit "Fork", class: "btn btn-info" %>
         <% end %>
         <%= f.submit @query.persisted? ? "Update" : "Create", class: "btn btn-success" %>

+ 1 - 1
app/views/blazer/queries/run.html.erb

@@ -20,7 +20,7 @@
         <% end %>
 
         <% if @query && !params[:data_source] %>
-          <%= link_to "Refresh", refresh_query_path(@query, variable_params), method: :post, remote: true, class: "refresh-link" %>
+          <%= link_to "Refresh", refresh_query_path(@query, variable_params), method: :post, remote: true %>
         <% end %>
       </p>
     <% end %>